예제 #1
0
파일: views.py 프로젝트: pari685/movies
def add_movie_form(request):
    """
    Module that adds the data from the page /addmovieform/
        if a movie with the same title does not exist

    :param request: request, HTML Template, movie_listing = the list of Movie objects
    :return:
    """
    if request.POST:
        form = MovieForm(request.POST)
        if form.is_valid():
            # Check if the movie already exists in the database
            check_db = Movie.objects.filter(title=request.POST['title'])
            if len(check_db) > 0:
                # If a movie with same name exists the do not enter to DB
                return render(request, 'movie_exists.html',
                              {'movie_title': request.POST['title']})
            else:
                # Save form and redirect to the success page
                form.save()
                return render_to_response(
                    'add_success.html', {'movie_title': request.POST['title']})
    else:
        form = MovieForm()
    return render(request, 'add_movie_form.html', {'form': form})
예제 #2
0
def new_movie(request):
    if request.method == "POST":
        form = MovieForm(request.POST, request.FILES)
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.save()
            return redirect(all_statistics)
    else:
        form = MovieForm()
    return render(request, 'movieform.html', {'form': form})
예제 #3
0
파일: views.py 프로젝트: nisa/my-movie-book
def add_movie(request):
    if request.method == "POST":
        form = MovieForm(request.POST)
        if form.is_valid():
            new_movie = form.save(commit=False)
            new_movie.user_id = request.user.id
            new_movie.save()
            request.user.message_set.create(message="Movie Added !!!")
            return redirect("movies_list")
    else:
        form = MovieForm()
    return direct_to_template(request, "templates/add_movie.html", {"form": form})
예제 #4
0
def dashboard_add_movie(request):
    context = RequestContext(request)
    if request.method == 'POST':
        form = MovieForm(request.POST, request.FILES)
        if form.is_valid():
            obj = form.save(commit=False)
            obj.uploaded_by = request.user
            obj.save()
            return HttpResponseRedirect('/movies/dashboard')
        return render_to_response("movie_library/dashboard/add_movie.html", {"form": form, 'dashboard_active':'active'}, context)

    elif request.method == 'GET':
        form = MovieForm()
        return render_to_response("movie_library/dashboard/add_movie.html", {"form": form, 'dashboard_active':'active'}, context)
예제 #5
0
def add(request):
    if request.method == "POST":
        form = MovieForm(request.POST)
        if form.is_valid():
            new_movie = Movie.objects.create_movie(**form.cleaned_data)
            try:
                genre_id = request.POST.getlist('genre', '')
                for id in genre_id:
                    new_category = get_object_or_404(Category, id=id)
                    new_movie.categories.add(new_category)
            except:
                pass
            new_movie.save()

            return HttpResponseRedirect(reverse('tommymovies:history'))
    else:
        form = MovieForm()

    return render(request, 'tommymovies/add.html', {'form': form})
예제 #6
0
def dashboard_add_movie(request):
    context = RequestContext(request)
    if request.method == 'POST':
        form = MovieForm(request.POST, request.FILES)
        if form.is_valid():
            obj = form.save(commit=False)
            obj.uploaded_by = request.user
            obj.save()
            return HttpResponseRedirect('/movies/dashboard')
        return render_to_response("movie_library/dashboard/add_movie.html", {
            "form": form,
            'dashboard_active': 'active'
        }, context)

    elif request.method == 'GET':
        form = MovieForm()
        return render_to_response("movie_library/dashboard/add_movie.html", {
            "form": form,
            'dashboard_active': 'active'
        }, context)
예제 #7
0
def addMovieSubmit(request):
    if request.method == "POST":
        form = MovieForm(request.POST)
        genre_id = request.POST.get('genre', '')
        category = get_object_or_404(Category, id=genre_id)
        if form.is_valid():
            new_movie = Movie.objects.create_movie(
                form.cleaned_data['name'], form.cleaned_data['year'],
                form.cleaned_data['watched'], form.cleaned_data['watch_date'],
                form.cleaned_data['p_rating'],
                form.cleaned_data['imdb_rating'],
                form.cleaned_data['rt_rating'])
            new_movie.categories.add(category)
            new_movie.save()
            return HttpResponseRedirect(reverse('tommymovies:history'))
    else:
        form = MovieForm()

    return render(request, 'tommymovies/addMovie.html', {
        'movie_form': movie_form,
        'category_form': category_form
    })
예제 #8
0
def edit_submit(request, movie_id):
    movie = get_object_or_404(Movie, id=movie_id)
    if request.method == "POST":
        form = MovieForm(request.POST)
        form.data = form.data.copy()
        category_id_list = []
        listgenres = request.POST.getlist('categories')
        for genre in movie.categories.all():
            movie.categories.remove(genre)
        for genre in listgenres:
            try:
                category = get_object_or_404(Category, genre=genre)
                category_id = str(category.id).encode("utf-8").decode("utf-8")
                category_id_list.append(category_id)
            except:
                pass
            else:
                if genre not in movie.categories.all():
                    movie.categories.add(category)
                form.data.setlist('categories', category_id_list)
        try:
            genre_id = request.POST.get('genre', '')
            new_category = get_object_or_404(Category, id=genre_id)
            movie.categories.add(new_category)
            movie.save()
        except:
            pass

        #hack to avoid '"" is not a valid value for a primary key' error
        sample_category = get_object_or_404(Category, id=1)
        sample_category_id = str(
            sample_category.id).encode("utf-8").decode("utf-8")
        category_id_list.append(sample_category_id)
        form.data.setlist('categories', category_id_list)

        if form.is_valid():
            movie.name = form.cleaned_data[
                'name']  #request.POST.get("name", "")#
            movie.year = form.cleaned_data[
                'year']  #request.POST.get("year", "")#
            movie.watched = form.cleaned_data[
                'watched']  #request.POST.get("watched", "")#
            movie.watch_date = form.cleaned_data[
                'watch_date']  #request.POST.get("watch_date", "")#
            movie.p_rating = form.cleaned_data[
                'p_rating']  #request.POST.get("p_rating", "")#
            movie.imdb_rating = form.cleaned_data[
                'imdb_rating']  #request.POST.get("imdb_rating", "")#
            movie.rt_rating = form.cleaned_data[
                'rt_rating']  #request.POST.get("rt_rating", "")#
            movie.imdb_url = form.cleaned_data[
                'imdb_url']  #request.POST.get("imdb_url", "")#
            movie.rt_url = form.cleaned_data[
                'rt_url']  #request.POST.get("rt_url", "")#
            movie.douban_url = form.cleaned_data[
                'douban_url']  #request.POST.get("douban_url", "")#
            movie.comment = form.cleaned_data[
                'comment']  #request.POST.get("comment", "")
            movie.save()
        return render(request, 'tommymovies/movie.html', {
            'movie': movie,
            'form': form,
            'listgenres': listgenres
        })
    else:
        form = MovieForm(instance=movie)
    #return render(request, 'tommymovies/movie.html', {'movie': movie })
    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))