mirror of
https://github.com/aykhans/series-robot-web.git
synced 2025-04-21 06:13:34 +00:00
API_clien app removed
This commit is contained in:
parent
f9930c2476
commit
36ebeae44f
@ -1 +0,0 @@
|
||||
from .get_request import APIClient
|
@ -1,33 +0,0 @@
|
||||
from requests import get
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
|
||||
class APIClient:
|
||||
def __init__(self, api_key: str) -> None:
|
||||
self.api_key = api_key
|
||||
|
||||
def __get_data(self, link: str) -> dict:
|
||||
raw_data = get(link)
|
||||
if raw_data.status_code == 200:
|
||||
data = raw_data.json()
|
||||
|
||||
if data['errorMessage'] == 'Invalid API Key': raise ValidationError('Invalid API Key')
|
||||
elif data['errorMessage']: raise ValidationError('id is not correct.')
|
||||
|
||||
return data
|
||||
raise ValidationError('Could not created. Please try again later.')
|
||||
|
||||
def key_control(self) -> None:
|
||||
self.__get_data(f"https://imdb-api.com/en/API/Title/{self.api_key}/tt7939766")
|
||||
|
||||
def get_seasons(self, series_id: str) -> list:
|
||||
data = self.__get_data(f"https://imdb-api.com/en/API/Title/{self.api_key}/{series_id}")
|
||||
|
||||
if not data['tvSeriesInfo']:
|
||||
raise ValidationError('This is not a TV series id')
|
||||
return data['tvSeriesInfo']['seasons']
|
||||
|
||||
def get_episodes_count(self, season: int, series_id: str) -> int:
|
||||
data = self.__get_data(
|
||||
f"https://imdb-api.com/en/API/SeasonEpisodes/{self.api_key}/{series_id}/{season}")
|
||||
return len(data['episodes'])
|
@ -1,21 +1,10 @@
|
||||
from django.db import models
|
||||
from django.contrib.auth.models import AbstractUser
|
||||
from API_client import APIClient
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
|
||||
class User(AbstractUser):
|
||||
imdb_api_key = models.CharField(max_length=15, blank=False, null=False)
|
||||
|
||||
def clean(self) -> None:
|
||||
client = APIClient(self.imdb_api_key)
|
||||
client.key_control()
|
||||
# raw_data = get(f"https://imdb-api.com/en/API/Title/{self.imdb_api_key}/tt0110413")
|
||||
|
||||
# if raw_data.status_code == 200: data = raw_data.json()
|
||||
# else: raise ValidationError('Account not created. Please try again later')
|
||||
# if not data['errorMessage']: raise ValidationError(data['errorMessage'])
|
||||
|
||||
class Meta:
|
||||
db_table = 'user'
|
||||
verbose_name = 'User'
|
||||
|
@ -5,6 +5,7 @@
|
||||
{% block title %} Signup {% endblock title %}
|
||||
|
||||
{% block content %}
|
||||
{% include 'components/message.html' %}
|
||||
|
||||
<form method="POST">
|
||||
{% csrf_token %}
|
||||
|
@ -2,7 +2,6 @@ from django.shortcuts import render, redirect
|
||||
from django.contrib import messages
|
||||
from account.forms import RegisterForm
|
||||
from django.contrib.auth import login, authenticate
|
||||
from django.core.exceptions import ValidationError
|
||||
from requests import get
|
||||
|
||||
|
||||
@ -10,15 +9,16 @@ def register_view(request):
|
||||
if request.method == 'POST':
|
||||
form = RegisterForm(request.POST)
|
||||
if form.is_valid():
|
||||
# raw_data = get(f"https://imdb-api.com/en/API/Title/{request.POST['imdb_api_key']}/tt0110413")
|
||||
# if raw_data.status_code == 200: data = raw_data.json()
|
||||
# else:
|
||||
# messages.info(request, 'Account not created. Please try again later')
|
||||
# return redirect('register')
|
||||
raw_data = get(f"https://imdb-api.com/en/API/Title/{request.POST['imdb_api_key']}/tt0110413")
|
||||
|
||||
# if data['errorMessage'] == 'Invalid API Key':
|
||||
# messages.info(request, 'Invalid API Key')
|
||||
# return redirect('register')
|
||||
if raw_data.status_code != 200:
|
||||
messages.info(request, 'Account not created. Please try again later')
|
||||
return redirect('register')
|
||||
data = raw_data.json()
|
||||
|
||||
if data['errorMessage'] == 'Invalid API Key':
|
||||
form.add_error('imdb_api_key', 'Invalid API Key')
|
||||
return render(request, 'register.html', context={"form": form})
|
||||
|
||||
form.save()
|
||||
username = form.cleaned_data.get('username')
|
||||
|
@ -1,6 +1,6 @@
|
||||
from django.db import models
|
||||
from django.core.exceptions import ValidationError
|
||||
from API_client import APIClient
|
||||
# from API_client import APIClient
|
||||
from django.core.validators import (MaxValueValidator,
|
||||
MinValueValidator,
|
||||
MinLengthValidator)
|
||||
@ -21,22 +21,5 @@ class SeriesModel(models.Model):
|
||||
], blank=False, null=False)
|
||||
show = models.BooleanField(default=True)
|
||||
|
||||
def clean(self) -> None:
|
||||
if len(self.imdb_id) < 9:
|
||||
raise ValidationError(f'Ensure this value has at least 9 characters (it has {len(self.imdb_id)}).')
|
||||
|
||||
if len(self.imdb_id) > 10:
|
||||
raise ValidationError(f'Make sure this value is no more than 10 characters (it has {len(self.imdb_id)}).')
|
||||
|
||||
client = APIClient(self.user.imdb_api_key)
|
||||
|
||||
seasons = client.get_seasons(self.imdb_id)
|
||||
if str(self.last_season) not in seasons:
|
||||
raise ValidationError('The season number you entered is not correct.')
|
||||
|
||||
episodes_count = client.get_episodes_count(self.last_season, self.imdb_id)
|
||||
if self.last_episode > episodes_count:
|
||||
raise ValidationError('The episode number you entered is not correct.')
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.user.username} - {self.title}"
|
22
src/series/templates/add_series.html
Normal file
22
src/series/templates/add_series.html
Normal file
@ -0,0 +1,22 @@
|
||||
{% extends 'base.html' %}
|
||||
{% load crispy_forms_tags %}
|
||||
|
||||
{% block title %} Add Series {% endblock title %}
|
||||
|
||||
{% block content %}
|
||||
{% include 'components/message.html' %}
|
||||
|
||||
<form enctype="multipart/form-data" method="POST">
|
||||
{% csrf_token %}
|
||||
{{form|crispy}}
|
||||
<style>
|
||||
.btn:hover {
|
||||
background: #fff;
|
||||
color: #218838;
|
||||
}
|
||||
</style>
|
||||
<input type="submit" value="Add" class="btn btn-primary mt-3 mb-5">
|
||||
</form>
|
||||
|
||||
{% endblock content %}
|
||||
|
@ -4,5 +4,5 @@ from series import views
|
||||
|
||||
urlpatterns = [
|
||||
path('', views.homepage_view, name='homepage'),
|
||||
# path('series/create', views.homepage_view, name='homepage'),
|
||||
path('series/add', views.AddSeriesView.as_view(), name='add-series'),
|
||||
]
|
@ -1 +1,2 @@
|
||||
from .homepage import homepage_view
|
||||
from .add_series import AddSeriesView
|
53
src/series/views/add_series.py
Normal file
53
src/series/views/add_series.py
Normal file
@ -0,0 +1,53 @@
|
||||
from django.views.generic import CreateView
|
||||
from series.models import SeriesModel
|
||||
from django.urls import reverse_lazy, reverse
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.contrib import messages
|
||||
from django.shortcuts import redirect
|
||||
from requests import get
|
||||
|
||||
|
||||
class AddSeriesView(LoginRequiredMixin, CreateView):
|
||||
login_url = reverse_lazy('login')
|
||||
template_name = 'add_series.html'
|
||||
model = SeriesModel
|
||||
fields = ('title', 'imdb_id', 'last_season', 'last_episode', 'show')
|
||||
|
||||
def get_success_url(self):
|
||||
messages.success(self.request, 'Series Added')
|
||||
return reverse('homepage')
|
||||
|
||||
def form_valid(self, form):
|
||||
series = form.save(commit=False)
|
||||
series.user = self.request.user
|
||||
|
||||
raw_data = get(f"https://imdb-api.com/en/API/Title/{series.user.imdb_api_key}/{series.imdb_id}")
|
||||
|
||||
if raw_data.status_code != 200:
|
||||
messages.info(self.request, 'TV Series no added. Please try again.')
|
||||
return redirect('add-series')
|
||||
data = raw_data.json()
|
||||
|
||||
if data['errorMessage']:
|
||||
form.add_error('imdb_id', 'ID is not correct.')
|
||||
return self.form_invalid(form)
|
||||
|
||||
if not data['tvSeriesInfo']:
|
||||
form.add_error('imdb_id', 'This is not a TV series id.')
|
||||
return self.form_invalid(form)
|
||||
seasons = data['tvSeriesInfo']['seasons']
|
||||
|
||||
if str(series.last_season) not in seasons:
|
||||
form.add_error('last_season', 'The season number is not correct.')
|
||||
return self.form_invalid(form)
|
||||
|
||||
data = get(f"https://imdb-api.com/en/API/SeasonEpisodes/{series.user.imdb_api_key}/{series.imdb_id}/{series.last_season}").json()
|
||||
episodes_count = len(data['episodes'])
|
||||
if series.last_episode > episodes_count:
|
||||
form.add_error('last_episode', 'The episode number is not correct.')
|
||||
return self.form_invalid(form)
|
||||
|
||||
series.save()
|
||||
form.save_m2m()
|
||||
|
||||
return super().form_valid(form)
|
Loading…
x
Reference in New Issue
Block a user