Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the addressed issue #23

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions user/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
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,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='AddShowView'),


]
path('add_show/', AddShowView.as_view(), name='add_show_view'),
path('autocomplete/', MovieAutocomplete.as_view(), name='movie_autocomplete'),
]
98 changes: 87 additions & 11 deletions user/views.py
Original file line number Diff line number Diff line change
@@ -1,33 +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.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

API_KEY = os.getenv('API_KEY')

# Redirect root URL to the user shows page
def redirect_to_shows(request):
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)
movie = Movies.objects.create(title=title, description=description,
poster=poster, available=available)
print(movie)
movie.save

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'
Expand All @@ -41,4 +117,4 @@ def post(self, request, *args, **kwargs):
if form.is_valid():
form.save()
return redirect('user:shows')
return render(request, self.template_name, {'form': form})
return render(request, self.template_name, {'form': form})