diff --git a/project/__init__.py b/project/__init__.py index 5b1ba892afb65ec7c69465173e5284b2715bb8a6..ae31105a1ac099007c8887dfc21dbb6242a928c3 100644 --- a/project/__init__.py +++ b/project/__init__.py @@ -8,6 +8,10 @@ import sys sys.path.append('../pytmdb3/') +from tmdb3 import set_key + +set_key('542606a6ccff81a0337dc370a0cbfc37') + app = Flask('project') app.config['SECRET_KEY'] = 'random' app.config['MONGODB_SETTINGS'] = {'DB': 'my_movie_library'} diff --git a/project/controllers/library.py b/project/controllers/library.py index 904a47294abe7944063c95e626960fb263be75d1..46fe19a649ee4b326738c96dbaf7fcb493f1e722 100644 --- a/project/controllers/library.py +++ b/project/controllers/library.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- from project import app, security -from flask import render_template, request, session, redirect, url_for +from flask import render_template, request, session, redirect, url_for,jsonify from flask.ext.wtf import Form, TextField, validators from project.model.Library import Library from project.model.User import User @@ -29,7 +29,7 @@ def libraryItem(name, index,user=None): library = Library.objects(user=user,name=name,unit='Movie').first() if not library: return render_template('404.html',message='Unable to find given Library',user=user),404 - movie = library.hydrateUnit(index) + movie = library.hydrateUnit(index-1) if not movie: return render_template('404.html',message='Unable to find given Movie',user=user),404 return render_template('library/libraryItem.html',item=movie,user=user) @@ -40,9 +40,39 @@ def removelibraryItem(name, index,user=None): from project.model.Movie import Movie library = Library.objects(user=user,name=name,unit='Movie').first() if not library: - return render_template('404.html',message='Unable to find given Library',user=user),404 + return jsonify(response='error',message='Unable to find the given Library'),404 movie = library.hydrateUnit(index) if not movie: - return render_template('404.html',message='Unable to find given Movie',user=user),404 + return jsonify(response='error',message='Unable to find the given Movie in Library %s' % library.name),404 library.removeUnit(movie) + return jsonify(response='success',type='redirect',path=url_for(endpoint='library',name=name,_external=True)) + +@app.route('/libraries/<name>/add', methods=['POST']) +@security('user') +def addlibraryItem(name,user=None): + from project.model.Movie import Movie + + library = Library.objects(user=user,name=name,unit='Movie').first() + if not library: + return jsonify(response='error',message='Unable to find the given Library'),404 + + movie_id = request.form['id'] + if not movie_id: + return jsonify(response='error',message='Invalid Movie given'),404 + + from project.model.Movie import Movie + movie = Movie.objects(tmdb_id=movie_id).first() + if movie: + library.addUnit(movie) + return jsonify(response='success',type='redirect',path=url_for(endpoint='library',name=name,_external=True)) + + from tmdb3 import Movie as tmdbMovie + movie = tmdbMovie(movie_id) + if not movie: + return jsonify(response='error',message='Invalid Movie given'),404 + + from project.model.Movie import Movie + movie = Movie.convertMovie(movie) + library.addUnit(movie) + return jsonify(response='success',type='redirect',path=url_for(endpoint='library',name=name,_external=True)) \ No newline at end of file diff --git a/project/controllers/movies.py b/project/controllers/movies.py index 0c071d34c6db059f850b6bb38fcccb4748c99097..6188a3447382a038464c2e81ec92078bfbe3d28d 100644 --- a/project/controllers/movies.py +++ b/project/controllers/movies.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -from project import app -from flask import render_template, request +from project import app, security +from flask import render_template, request, jsonify from flask.ext.wtf import Form, TextField, validators @@ -11,3 +11,33 @@ def movies(): @app.route('/movies/<movieId>') def movie_item(movieId): return render_template('movies/movie.html') + +@app.route('/search-movies', methods=['POST']) +@security('user') +def searchMovie(user=None): + term = request.form['term']; + if not term: + return jsonify(response='error',message='Invalid search term'),404 + from tmdb3 import searchMovie + movies = searchMovie(term) + if len(movies) == 0: + return jsonify(response='error',message='No results given'),404 + print movies + results = [] + limit = 10 + for index in range(len(movies)): + movie = movies[index] + if limit <= 0: + break + result = {} + poster = movie.poster + if poster: + sizes = poster.sizes() + if len(sizes) > 0: + result['poster'] = poster.geturl(sizes[0]) + result['title'] = movie.title + result['id'] = movie.id + results.append(result) + limit -= 1; + + return jsonify(response='success',movies=results) \ No newline at end of file diff --git a/project/model/Library.py b/project/model/Library.py index b9ecf9b127a80c42d77ba1d33d2bbe01238d9871..4ab0ba64b0dde504a53c99948df61e8c8bcc6019 100644 --- a/project/model/Library.py +++ b/project/model/Library.py @@ -14,6 +14,7 @@ class Library(db.Document): value = unit[self.lookup_attribute] if value is not None and value not in self.collection: self.collection.append("%s" % value) + self.save() else: return self else: @@ -24,7 +25,8 @@ class Library(db.Document): if self.unit == type(unit).__name__: value = unit[self.lookup_attribute] if value is not None and value in self.collection: - self.collection.remove("%s" % value).save() + self.collection.remove("%s" % value) + self.save() else: return self else: diff --git a/project/model/Movie.py b/project/model/Movie.py index c6e0a1aff0e28fc256dce3173ef8e03992415140..4458faa1761928e2edd163645a09e45f219e65ac 100644 --- a/project/model/Movie.py +++ b/project/model/Movie.py @@ -2,27 +2,54 @@ from project import db import datetime class Movie(db.Document): - created = db.DateTimeField(default=datetime.datetime.now, required=True) - title = db.StringField(max_length=255, required=True) - summary = db.StringField(max_length=10000, required=True) - tags = db.ListField(db.StringField(max_length=50)) + created = db.DateTimeField(default=datetime.datetime.now, required=True) + title = db.StringField(max_length=255, required=True) + summary = db.StringField(max_length=10000, required=True) + tags = db.ListField(db.StringField(max_length=50)) + tmdb_id = db.IntField() + runtime = db.IntField() + poster = db.StringField() + popularity = db.FloatField() - def addTag(self,tag): - if tag not in self.tags: - self.tags.append(tag) - return self + def addTag(self,tag): + if tag not in self.tags: + self.tags.append(tag) + return self - def removeTag(self,tag): - if tag in self.tags: - self.tags.remove(tag) - return self + def removeTag(self,tag): + if tag in self.tags: + self.tags.remove(tag) + return self - def __str__(self): - return self.title + def __str__(self): + return self.title - def __repr__(self): - return str(self.toJSON()) + def __repr__(self): + return self.__str__() - def toJSON(self): - import json - return json.dumps({'created': self.created.isoformat(), 'title': self.title, 'summary': self.summary, 'tags': str(self.tags), 'id':str(self.id)}) \ No newline at end of file + def toJSON(self): + import json + return json.dumps({'created': self.created.isoformat(), 'title': self.title, 'summary': self.summary, 'tags': str(self.tags), 'id':str(self.id)}) + + @staticmethod + def convertMovie(movie): + result = Movie() + result.tmdb_id = int(movie.id) + result.title = str(movie.title) + print movie.overview + result.summary = str(movie.overview.encode('utf-8')) + if movie.poster: + sizes = movie.poster.sizes() + if len(sizes) > 0: + medium = int(len(sizes)/2) + result.poster = str(movie.poster.geturl(sizes[medium])) + result.popularity = float(movie.popularity) + result.runtime = int(movie.runtime) + tags = movie.keywords + for tag in tags: + result.addTag(str(tag)) + genres = movie.genres + for genre in genres: + result.addTag(str(genre)) + result.save() + return result diff --git a/project/static/css/style.css b/project/static/css/style.css index 177d079906c5af196cc6cad80a44b23bae915f64..c467639afa2569617ed7e638514ab019644f3da4 100644 --- a/project/static/css/style.css +++ b/project/static/css/style.css @@ -21,4 +21,8 @@ html,body { padding-left: 20px; padding-right: 20px; } +} + +.adjust-left { + padding-right: 5%; } \ No newline at end of file diff --git a/project/templates/library/addmovie_modal.html b/project/templates/library/addmovie_modal.html new file mode 100644 index 0000000000000000000000000000000000000000..5e3d49fa8a2df15635a4197bf7e2d6db013f5440 --- /dev/null +++ b/project/templates/library/addmovie_modal.html @@ -0,0 +1,81 @@ +{% extends "modal_base.html" %} +{% block title %} Add Movie {% endblock %} +{% block modal_content %} + <div class="row"> + <div class="pull-right adjust-left"> + <form id="movieSearch "class="navbar-form" role="search" method="post" action="{{ url_for('searchMovie') }}"> + <div class="form-group"> + <input type="text" name="term" class="form-control" placeholder="Title, keyword, etc."> + </div> + <button type="submit" id="search-accept" class="btn btn-default">Search</button> + </form> + </div> + </div> + <div id="results" class="row well col-md-10 col-md-offset-1"> + Search for your movie! + </div> + <div class="hidden"> + <form class="hidden" id="addMovie" method="post" action="{{ url_for('addlibraryItem',name=library.name) }}"> + <div class="form-group"> + <div class="col-xs-9 col-md-6"> + <input type="hidden" name="id"> + </div> + </div> + </form> + </div> +{% endblock %} +{% block accept_button_text %}Submit{% endblock %} +{% block ajax_url %}'{{ url_for('addlibraryItem',name=library.name) }}'{% endblock %} +{% block form_id %}addMovie{% endblock %} +{% block additional_javascripts %} + <script type="text/javascript"> + $(function(){ + var $addForm = $(document.getElementById("addMovie")); + var $acceptButton = $(document.getElementById("search-accept")); + var $form = $acceptButton.parent(); + var $results = $(document.getElementById("results")); + $acceptButton.on('click',function(event){ + event.preventDefault(); + $.ajax({ + url: "{{ url_for('searchMovie') }}", + data: $form.serialize(), + method: 'POST', + success: function(data,status,jqXHR){ + movies = data['movies']; + listMovies($results,movies); + $results.on('click','.movie-select',function(){ + var id = $(this).data('id'); + $addForm.find('input').val(id); + }); + }, + error: function(jqXHR, textStatus, errorThrown){ + $results.html('There was an error with your search please try again') + } + }); + }); + $("#search input").keyup(function(event){ + if(event.keyCode == 13){ + $acceptButton.click(); + } + }); + }); + function listMovies(list,movies){ + var html = ""; + for (var i = movies.length - 1; i >= 0; i--) { + movie = movies[i]; + html += "<div class='col-md-3 movie-select' data-id='"+movie['id']+"'>"; + html += "<img class='img-thumbnail' src='"+movie['poster']+"'/>"; + html += "<h3>"+movie['title']+"</h3>"; + html += "</div>"; + }; + list.html(html); + }; + </script> +{% endblock %} +{% block validation_rules %} +rules: { + id: "required", + errorLabelContainer: "#addMovie-error" +} +{% endblock %} + \ No newline at end of file diff --git a/project/templates/library/library.html b/project/templates/library/library.html index c77414423a0c6a7a449f9863cee7ab4f1203d1c7..cd175c2295cf3cacd5c71643afcc2ff1de93ef31 100644 --- a/project/templates/library/library.html +++ b/project/templates/library/library.html @@ -5,7 +5,7 @@ <h3>{{ library.name ~ ' ' ~ library.unit }} Library </h3> </div> <div class="btn-group pull-right"> - <button type="button" class="btn btn-default add-movie" data-id='{{ library.name }}'>Add Movie</button> + <button type="button" class="btn btn-default add-movie" data-toggle="modal" data-target="#addMovie-form">Add Movie</button> </div> <table class="table table-striped table-hover"> <thead> @@ -23,9 +23,9 @@ </tr> {% else %} {% for movie in library.hydrateList() %} - <tr> + <tr class='clickable' data-id='{{ loop.index }}'> <td> - <img src="{{ movie.image|default('') }}" style="max-width: 200px;" alt="{{ movie.title }}"> + <img src="{{ movie.poster|default('') }}" style="max-width: 200px;" alt="{{ movie.title }}"> <p>{{ movie.title }}</p> </td> <td> @@ -48,4 +48,20 @@ </tbody> </table> </div> +{% endblock %} + +{% block javascript %} + <script> + $(function(){ + var libraryPath = '{{ url_for("libraryItem", name=library.name, index="999") }}'; + $('.clickable').on('click',function(){ + var libname = $(this).data('id'); + window.document.location = libraryPath.replace('999',libname); + }) + }); + </script> +{% endblock %} + +{% block modals %} + {% include 'library/addmovie_modal.html' %} {% endblock %} \ No newline at end of file