Viewing: trunk/collection/views.py

Revision: 29

View as text-only | View History
Index: views.py
===================================================================
--- views.py	(revision 29)
+++ views.py	(revision 61)
@@ -1,20 +1,19 @@
 from django.views.generic.list import ListView
 from collection.models import Movie, Genre, Watchlist, Director
 from django.shortcuts import get_object_or_404, render, redirect
-import random
 from django.views.generic.edit import CreateView, UpdateView
 from collection.forms import MovieForm, UserForm
 from django.views.generic.detail import DetailView
-import urllib
-import json
-import datetime
 from django.contrib import messages
 from django.contrib.auth.decorators import login_required
 from django.core.mail import mail_admins
-import sys
 from django.http.response import HttpResponseRedirect, Http404
 from django.contrib.auth.models import User
 from django.contrib.auth import authenticate, login
+from django.template.loader import get_template
+from django.template.context import Context
+import random, urllib, json, datetime, sys
+from django.conf import settings
 
 LENGTH_STAMP = datetime.datetime(1983, 05, 10)  # This is used to determine the length of a movie title. Trivia, what date is this from?
 
@@ -36,12 +35,18 @@
     model = Movie
     paginate_by = 100
     def get_queryset(self):
+        self.query = ''
         qs = super(BrowseCollection, self).get_queryset().select_related()
         if 'ordering' in self.request.GET:
+            self.query = '&ordering=%s' % self.request.GET.get('ordering')
             return qs.order_by(self.request.GET.get('ordering'))
         if 'startswith' in self.request.GET:
+            self.query = '&startswith=%s' % self.request.GET.get('startswith')
             return qs.filter(title__startswith=self.request.GET.get('startswith'))
         return qs
+    def get_context_data(self, **kwargs):
+        kwargs.update({'extra_query':self.query})
+        return super(BrowseCollection, self).get_context_data(**kwargs)
 
 class BrowseGenre(ListView):
     model = Movie
@@ -48,15 +53,18 @@
     paginate_by = 100
     template_name = 'collection/genre_detail.html'
     def get_queryset(self):
+        self.query = ''
         self.genre = get_object_or_404(Genre, slug=self.kwargs['slug'])
         qs = super(BrowseGenre, self).get_queryset().select_related().filter(genre=self.genre)
         if 'ordering' in self.request.GET:
+            self.query = '&ordering=%s' % self.request.GET.get('ordering')
             return qs.order_by(self.request.GET.get('ordering'))
         if 'startswith' in self.request.GET:
+            self.query = '&startswith=%s' % self.request.GET.get('startswith')
             return qs.filter(title__startswith=self.request.GET.get('startswith'))
         return qs
     def get_context_data(self, **kwargs):
-        kwargs.update({'genre': self.genre})
+        kwargs.update({'genre': self.genre, 'extra_query':self.query})
         return super(BrowseGenre, self).get_context_data(**kwargs)
 
 class GenreList(ListView):
@@ -67,20 +75,29 @@
     paginate_by = 100
     template_name = 'collection/format_detail.html'
     def get_queryset(self):
+        self.query = ''
         self.format = int(self.kwargs['format_id'])
         qs = super(BrowseFormat, self).get_queryset().select_related().filter(format=self.format)
         if 'ordering' in self.request.GET:
+            self.query = '&ordering=%s' % self.request.GET.get('ordering')
             return qs.order_by(self.request.GET.get('ordering'))
         if 'startswith' in self.request.GET:
+            self.query = '&startswith=%s' % self.request.GET.get('startswith')
             return qs.filter(title__startswith=self.request.GET.get('startswith'))
         return qs
     def get_context_data(self, **kwargs):
-        kwargs.update({'format': self.format})
+        kwargs.update({'format': self.format, 'extra_query':self.query})
         return super(BrowseFormat, self).get_context_data(**kwargs)
 
 class AddMovie(OnlyNickMixin, CreateView):
     model = Movie
     form_class = MovieForm
+    def form_valid(self, form):
+        self.object = form.save()
+        for watchlist in self.object.genre.watchlist_set.all():
+            body = get_template("collection/notifications/new_movie.txt").render(Context({'user':watchlist.owner, 'movie':self.object}))
+            watchlist.owner.email_user('Nick added a new %s movie' % self.object.genre, body)
+        return redirect(self.object)
 
 class MovieDetail(DetailView):
     model = Movie
@@ -104,9 +121,9 @@
             movie_title=movie_title.replace('Part 2', 'Part II').replace('Part 3', 'Part III').replace(', A', '')
             try:
                 if self.object.year:
-                    omdb = urllib.urlopen('http://www.omdbapi.com/?t=%s&y=%s' % (movie_title, self.object.year)).read()
+                    omdb = urllib.urlopen('http://www.omdbapi.com/?t=%s&y=%s&apikey=%s' % (movie_title, self.object.year, settings.OMDB_APIKEY)).read()
                 else:
-                    omdb = urllib.urlopen('http://www.omdbapi.com/?t=%s' % movie_title).read()
+                    omdb = urllib.urlopen('http://www.omdbapi.com/?t=%s&apikey=%s' % (movie_title, settings.OMDB_APIKEY)).read()
             except:
                 omdb = '{"Response":"False"}'
                 value = sys.exc_info()[1]
@@ -206,7 +223,7 @@
         messages.error(req, "You do not have access to update the info from IMDb.")
         return redirect(movie)
     try:
-        omdb = urllib.urlopen('http://www.omdbapi.com/?i=%s' % movie.imdb).read()
+        omdb = urllib.urlopen('http://www.omdbapi.com/?i=%s&apikey=%s' % (movie.imdb, settings.OMDB_APIKEY)).read()
     except:
         omdb = '{"Response":"False"}'
         value = sys.exc_info()[1]