From d3acfcd1ef8a27e99e29a34a5c25ef5e350f3ca1 Mon Sep 17 00:00:00 2001 From: meemansha07 Date: Wed, 9 Oct 2024 15:23:43 +0530 Subject: [PATCH 1/3] changed landing url --- user/urls.py | 16 ++++++++-------- user/views.py | 20 ++++++++++++++------ 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/user/urls.py b/user/urls.py index 5d0a2eb..f72eabe 100644 --- a/user/urls.py +++ b/user/urls.py @@ -1,15 +1,15 @@ from django.contrib import admin from django.urls import path, include -from django.views.generic import TemplateView -from django.contrib.auth.views import LogoutView -from .views import shows,add_movie,movie_list,AddShowView +from .views import shows, add_movie, movie_list, AddShowView, redirect_to_shows + urlpatterns = [ - path('admin/', admin.site.urls), + + # Redirect root URL to /shows/ + path('', redirect_to_shows, name='redirect_to_shows'), + path('shows/', shows, name='shows'), path('add_movie/', add_movie, name='add_movie'), path('movie_list/', movie_list, name='movie_list'), - path('add_show/', AddShowView.as_view(), name='AddShowView'), - - -] \ No newline at end of file + path('add_show/', AddShowView.as_view(), name='add_show_view'), +] diff --git a/user/views.py b/user/views.py index 93f1ab6..8bc4c22 100644 --- a/user/views.py +++ b/user/views.py @@ -3,6 +3,11 @@ from django.utils import timezone from .forms import ShowForm from django.views import View +from django.urls import reverse + +# Redirect to /user/shows/ +def redirect_to_shows(request): + return redirect(reverse('user:shows')) # Use 'reverse' for named URLs def shows(request): upcoming_shows = Show.objects.all().order_by('-time') @@ -13,11 +18,15 @@ def add_movie(request): title = request.POST.get('title') description = request.POST.get('description') poster = request.FILES.get('poster') - available = request.POST.get('available', False) - movie = Movies.objects.create(title=title, description=description, -poster=poster, available=available) + available = request.POST.get('available', False) == 'on' # Correctly handle checkbox value + movie = Movies.objects.create( + title=title, + description=description, + poster=poster, + available=available + ) print(movie) - movie.save + movie.save() # Call save() to save the movie object properly return redirect('movie_list') else: @@ -27,7 +36,6 @@ def movie_list(request): movies = Movies.objects.all() return render(request, 'movie_list.html', {'movies': movies}) - class AddShowView(View): form_class = ShowForm template_name = 'add_show.html' @@ -40,5 +48,5 @@ def post(self, request, *args, **kwargs): form = self.form_class(request.POST) if form.is_valid(): form.save() - return redirect('user:shows') + return redirect(reverse('user:shows')) # Use reverse to redirect to 'shows' return render(request, self.template_name, {'form': form}) From b1abdcdf895abcb2593388862d1e8f0cb0b959bf Mon Sep 17 00:00:00 2001 From: meemansha07 Date: Thu, 10 Oct 2024 16:09:49 +0530 Subject: [PATCH 2/3] changes added --- user/urls.py | 19 +++++---- user/views.py | 106 +++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 98 insertions(+), 27 deletions(-) diff --git a/user/urls.py b/user/urls.py index f72eabe..6555ac7 100644 --- a/user/urls.py +++ b/user/urls.py @@ -1,15 +1,18 @@ from django.contrib import admin from django.urls import path, include -from .views import shows, add_movie, movie_list, AddShowView, redirect_to_shows +from django.views.generic import TemplateView +from django.contrib.auth.views import LogoutView +from .views import shows, add_movie, movie_list, AddShowView,MovieAutocomplete, redirect_to_shows urlpatterns = [ path('admin/', admin.site.urls), - + # Redirect root URL to /shows/ - path('', redirect_to_shows, name='redirect_to_shows'), - - path('shows/', shows, name='shows'), - path('add_movie/', add_movie, name='add_movie'), - path('movie_list/', movie_list, name='movie_list'), - path('add_show/', AddShowView.as_view(), name='add_show_view'), + path('', redirect_to_shows, name='redirect_to_shows'), + + path('user/shows/', shows, name='shows'), + path('user/add_movie/', add_movie, name='add_movie'), + path('user/movie_list/', movie_list, name='movie_list'), + path('user/add_show/', AddShowView.as_view(), name='add_show_view'), + path('user/autocomplete/', MovieAutocomplete.as_view(), name='movie_autocomplete'), ] diff --git a/user/views.py b/user/views.py index 8bc4c22..efa5cce 100644 --- a/user/views.py +++ b/user/views.py @@ -1,41 +1,109 @@ +import requests +import os +from django.http import JsonResponse from django.shortcuts import render, redirect from .models import Show, Movies from django.utils import timezone from .forms import ShowForm from django.views import View -from django.urls import reverse +from django.core.files import File +from django.core.files.temp import NamedTemporaryFile +from django.conf import settings +from dotenv import load_dotenv +load_dotenv() +from django.contrib.admin.views.decorators import staff_member_required +from django.utils.decorators import method_decorator -# Redirect to /user/shows/ +API_KEY = os.getenv('API_KEY') + +# Redirect root URL to the user shows page def redirect_to_shows(request): - return redirect(reverse('user:shows')) # Use 'reverse' for named URLs + return redirect('/user/shows/') def shows(request): - upcoming_shows = Show.objects.all().order_by('-time') - return render(request, 'shows.html', {'shows': upcoming_shows}) + shows = Show.objects.all().order_by('-time') + return render(request, 'shows.html', {'shows': shows}) + +class MovieAutocomplete(View): + def get(self, request): + query = request.GET.get('query', '') + print(f"Query received: {query}") + if query: + ########## + api=API_KEY + ########## + url = f"http://www.omdbapi.com/?s={query}&apikey={api}" + response = requests.get(url) + data = response.json() + print(f"API response: {data}") + + if data.get('Response') == 'True': + titles = [movie['Title'] for movie in data.get('Search', [])] + return JsonResponse(titles, safe=False) + else: + + return JsonResponse([], safe=False) + return JsonResponse([], safe=False) + +# it will fetch data from api if only title field is provided +@staff_member_required def add_movie(request): if request.method == 'POST': title = request.POST.get('title') - description = request.POST.get('description') - poster = request.FILES.get('poster') - available = request.POST.get('available', False) == 'on' # Correctly handle checkbox value - movie = Movies.objects.create( - title=title, - description=description, - poster=poster, - available=available - ) - print(movie) - movie.save() # Call save() to save the movie object properly - return redirect('movie_list') + if Movies.objects.filter(title=title).exists(): + return render(request, 'add_movie.html', {'error_message': 'Movie already exists!'}) + + # only title is provided fetch data from the API + if title : + ############ + api_key =API_KEY + ############ + url = f'http://www.omdbapi.com/?t={title}&apikey={api_key}' + response = requests.get(url) + data = response.json() + + if data['Response'] == 'True': + description = data.get('Plot', 'No description available.') + poster_url = data.get('Poster') + + # Downloading the image + img_temp = NamedTemporaryFile() + img_response = requests.get(poster_url) + img_temp.write(img_response.content) + img_temp.flush() + + # Creating the movie object + movie = Movies.objects.create( + title=title, + description=description, + available=True + ) + movie.poster.save(f"{title}_poster.jpg", File(img_temp)) + movie.save() + + return redirect('user:movie_list') + + else: + # movie not found + error_message = data.get('Error', 'Movie not found.') + return render(request, 'add_movie.html', {'error_message': error_message}) + + + else: + return redirect('user:movie_list') + else: return render(request, 'add_movie.html') + def movie_list(request): movies = Movies.objects.all() return render(request, 'movie_list.html', {'movies': movies}) + +@method_decorator(staff_member_required, name="dispatch") class AddShowView(View): form_class = ShowForm template_name = 'add_show.html' @@ -48,5 +116,5 @@ def post(self, request, *args, **kwargs): form = self.form_class(request.POST) if form.is_valid(): form.save() - return redirect(reverse('user:shows')) # Use reverse to redirect to 'shows' - return render(request, self.template_name, {'form': form}) + return redirect('user:shows') + return render(request, self.template_name, {'form': form}) \ No newline at end of file From cf8351051de150cc8170ccfc2da6f8fb3d8beab5 Mon Sep 17 00:00:00 2001 From: meemansha07 Date: Sat, 12 Oct 2024 17:46:42 +0530 Subject: [PATCH 3/3] fixed the addressed issue --- user/urls.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/user/urls.py b/user/urls.py index 6555ac7..cd94cd6 100644 --- a/user/urls.py +++ b/user/urls.py @@ -10,9 +10,9 @@ # Redirect root URL to /shows/ path('', redirect_to_shows, name='redirect_to_shows'), - path('user/shows/', shows, name='shows'), - path('user/add_movie/', add_movie, name='add_movie'), - path('user/movie_list/', movie_list, name='movie_list'), - path('user/add_show/', AddShowView.as_view(), name='add_show_view'), - path('user/autocomplete/', MovieAutocomplete.as_view(), name='movie_autocomplete'), + path('shows/', shows, name='shows'), + path('add_movie/', add_movie, name='add_movie'), + path('movie_list/', movie_list, name='movie_list'), + path('add_show/', AddShowView.as_view(), name='add_show_view'), + path('autocomplete/', MovieAutocomplete.as_view(), name='movie_autocomplete'), ]