def add_movie(request): # Get the context from the request. context = RequestContext(request) mov_list = get_movie_list() context_dict = {} context_dict['mov_list'] = mov_list # A HTTP POST? if request.method == 'POST': form = MovieForm(request.POST) # Have we been provided with a valid form? if form.is_valid(): # Save the new movie to the database. form.save(commit=True) # Now call the index() view. # The user will be shown the homepage. return index(request) else: # The supplied form contained errors - just print them to the terminal. print form.errors else: # If the request was not a POST, display the form to enter details. form = MovieForm() # Bad form (or form details), no form supplied... # Render the form with error messages (if any). context_dict['form'] = form return render_to_response('movie/add_movie.html', context_dict, context)
def create(): if request.method == 'POST': form = MovieForm() if form.validate_on_submit(): name = form.name.data rating = form.rating.data pic = form.pic.data or "http://gearr.scannain.com/wp-content/uploads/2015/02/noposter.jpg" notes = form.notes.data or None movie = Movies(name=name, pic=pic, rating=rating, notes=notes) db.session.add(movie) db.session.commit() flash(u"Successfully Created Movie: {}".format(movie.name), "success") else: flash( u"Movie not created due to incorrect data. Please re-enter valid data.", "danger") return redirect(url_for('core.home')) else: return redirect(url_for('core.home'))
def edit(id): messages = [] if request.method == 'POST': form = MovieForm() movie = Movies.query.get(id) if form.validate_on_submit(): movie.name = form.name.data movie.rating = form.rating.data movie.pic = form.pic.data movie.notes = request.form.get('notes') or movie.notes db.session.commit() flash(u"Successfully Updated Movie: {}".format(movie.name), "success") else: flash( u"Movie details not edited due to incorrect data. Please re-edit with valid data.", "danger") return redirect(url_for('core.home')) else: return redirect(url_for('core.home'))
def movie_add(request): if request.method == "POST": post = request.POST form = MovieForm(post, request.FILES) category = Category.objects.get(pk=post["category"]) if form.is_valid(): movie = form.save() for i in post: if i.split("_")[0] == 'country': country = Country.objects.get(pk=int(i.split("_")[1])) movie.countries.add(country) if i.split("_")[0] == 'genre': genre = Genre.objects.get(pk=int(i.split("_")[1])) movie.genres.add(genre) if 'title_en' in post: print(post) movie.url = str(post['title_en']) + "_" + str(movie.pk) else: movie.url = str(movie.pk) movie.save() return redirect("/category/{}".format(category.pk)) else: print(form.errors) genres = Genre.objects.all() categories = Category.objects.all() countries = Country.objects.all() return render(request, 'movie_add.html', {"categories": categories, "genres": genres, "countries": countries})
def movierec(): form = MovieForm() if form.validate_on_submit(): import pandas as pd import numpy as np ratings = pd.read_csv('ratings.csv') movies = pd.read_csv('movies.csv') movie_data = pd.merge(ratings, movies, on='movieId') movie_data.groupby('title')['rating'].mean().sort_values( ascending=False) movie_data.groupby('title')['rating'].count().sort_values( ascending=False) rating_mean = pd.DataFrame( movie_data.groupby('title')['rating'].mean()) rating_mean['rating_count'] = pd.DataFrame( movie_data.groupby('title')['rating'].count()) user_movie_rating = movie_data.pivot_table(index='userId', columns='title', values='rating') x = form.moviename.data x = x.split() for i in range(0, len(x)): x[i] = x[i].capitalize() x = ' '.join(x) flag = -1 for i in range(0, len(movies)): if x in movies.iloc[i, 1]: x = movies.iloc[i, 1] flag = 1 if flag == -1: flash( 'There was some error. The admin has been notified. Please try another movie. Sorry for the inconvenience!!' ) else: movie = x movie_ratings = user_movie_rating[movie] movies_like_movie = user_movie_rating.corrwith(movie_ratings) corr_movie = pd.DataFrame(movies_like_movie, columns=['Correlation']) corr_movie.dropna(inplace=True) corr_movie = corr_movie.sort_values('Correlation', ascending=False) corr_movie = corr_movie.join(rating_mean['rating_count']) final = corr_movie[corr_movie['rating_count'] > 40].sort_values( 'Correlation', ascending=False) final = final.index for i in range(1, 5): flash(final[i]) return render_template('main.html', form=form)
def home(): Search = SearchForm() Create = MovieForm() Edit = MovieForm() movies = Movies.query.all() return render_template('home.html', movies=movies, Search=Search, Create=Create, Edit=Edit)
def addmovie(request): form = MovieForm() if request.method == 'POST': # If the form has been submitted... form = MovieForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass m = Movie() m.name = form.cleaned_data['name'] m.sourcefile = form.cleaned_data['url'] m.save() m.download_file() return HttpResponseRedirect('/movies/') # Redirect after POST< return render(request, 'movie/addmovie.html', {'form': form})
def addmovie(request): try: f = MovieForm(request.POST) if not f.is_valid(): raise Exception m = Movie.objects.create(title=f['title'].value(), director=f['director'].value(), cast=f['cast'].value(), imdb=f['imdb'].value()) m.save() return success({'id': m.id, 'message': 'Movie added'}, 'success') except: return error('Invalid form data')
def updmovie(request): try: f = MovieForm(request.POST) if not f.is_valid(): raise Exception m = Movie.objects.get(id=request.POST['id']) m.title = f['title'].value() m.director = f['director'].value() m.cast = f['cast'].value() m.imdb = f['imdb'].value() m.save() return success('Movie updated', 'message') except: return error('Invalid form data')
def update(request, updid): '''Display update movie page''' # form data loaded form forms.py classMovieForm # but initialized try: m = Movie.objects.get(id=int(updid)) # form with prefill data in a dictionary mf = MovieForm({ 'title': m.title, 'imdb': m.imdb, 'director': m.director, 'cast': m.cast }) # send form to template, this time prefilled c = { 'form': mf, 'id': m.id, 'action': 'updatemovie', 'button': 'Update' } except: return redirect('/?msg=Movie does not exist') return render(request, "add.html", c)
def updmovie(request): try: f = MovieForm(request.POST) if not f.is_valid(): raise Exception m = Movie.objects.get(id=request.POST['id']) m.title = f['title'].value() m.director = f['director'].value() m.cast = f['cast'].value() m.imdb = f['imdb'].value() m.save() return HttpResponse( getobject(request, {'message': 'Movie updated'}, 'success', ['message']), 'text/xml') except: return HttpResponse(geterror(request, 'Invalid form data'), 'text/xml')
def addmovie(request): try: f = MovieForm(request.POST) if not f.is_valid(): raise Exception m = Movie.objects.create(title=f['title'].value(), director=f['director'].value(), cast=f['cast'].value(), imdb=f['imdb'].value()) m.save() return HttpResponse( getobject(request, { 'id': m.id, 'message': 'Movie added' }, 'success', ['id', 'message']), 'text/xml') except: return HttpResponse(geterror(request, 'Invalid form data'), 'text/xml')
def search(): if request.method == 'POST': Search = SearchForm() Create = MovieForm() Edit = MovieForm() search = Search.search.data movies = Movies.query.filter(Movies.name.contains(search)).all() Search.search.data = "" return render_template('home.html', movies=movies, Search=Search, Create=Create, Edit=Edit) else: return redirect(url_for('core.home'))
def movie_update(request, pk): if request.method == "POST": movie = Movie.objects.get(pk=pk) movie.genres.clear() movie.countries.clear() post = request.POST form = MovieForm(post, request.FILES, instance=movie) if form.is_valid(): form.save() for i in post: if i.split("_")[0] == 'country': country = Country.objects.get(pk=int(i.split("_")[1])) movie.countries.add(country) if i.split("_")[0] == 'genre': genre = Genre.objects.get(pk=int(i.split("_")[1])) movie.genres.add(genre) if 'title_en' in post: movie.url = str(post['title_en']) + "_" + str(movie.pk) else: movie.url = str(movie.pk) movie.save() return redirect(movie.get_absolute_url()) else: print(form.errors)
def add(request): '''Display add movie page''' # form data loaded form forms.py classMovieForm c = {'form': MovieForm(), 'action': 'addmovie', 'button': 'Add'} return render(request, "add.html", c)