def recipe_detail(request, recipe_id):
    recipe = get_object_or_404(Recipe, pk=recipe_id)
    form = RatingForm(request.POST or None)
    if form.is_valid():
        rating = form.save(commit=False)
        rating.rating_recipe = recipe
        rating.rating_user = request.user.profile
        rating.save()
        return redirect(request.path)
    return render(request, 'recipes/recipe_detail.html',
                  {'recipe': recipe, 'form': form})
def recipe_detail(request, recipe_id):
    recipe = get_object_or_404(Recipe, pk=recipe_id)
    form = RatingForm(request.POST or None)
    if form.is_valid():
        rating = form.save(commit=False)
        rating.rating_recipe = recipe
        rating.rating_user = request.user.profile
        rating.save()
        return redirect(request.path)
    return render(request, 'recipes/recipe_detail.html', {
        'recipe': recipe,
        'form': form
    })
Exemplo n.º 3
0
def add_category(user_id, book_id):
    rating_form = RatingForm()
    comment_form = CommentForm()
    category_form = CategoryForm()
    book = Book.query.get(book_id)

    if category_form.validate_on_submit():
        ub = User_Book.query.filter_by(user_id=user_id,
                                       book_id=book_id).first()
        if ub:
            ub.category = category_form.category.data
        else:
            ub = User_Book(user_id=user_id,
                           book_id=book_id,
                           category=category_form.category.data)
            db.session.add(ub)
        db.session.commit()
        return redirect(url_for('book', book_id=book_id))

    return render_template('book.html',
                           title=book.title,
                           GENRES=GENRES,
                           book=book,
                           rating_form=rating_form,
                           comment_form=comment_form,
                           category_form=category_form)
Exemplo n.º 4
0
def book(book_id):
    book = Book.query.get(int(book_id))
    if not book:
        return redirect(url_for('index'))
    page = request.args.get('page', 1, type=int)
    comments = book.comments.order_by(Comment.datetime.desc()).paginate(
        page, app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('book', book_id=book_id, page=comments.next_num) \
        if comments.has_next else None
    prev_url = url_for('book',  book_id=book_id, page=comments.prev_num) \
        if comments.has_prev else None
    if current_user.is_anonymous:
        return render_template('book.html',
                               title=book.title,
                               book=book,
                               comments=comments.items,
                               GENRES=GENRES,
                               next_url=next_url,
                               prev_url=prev_url)
    rating_form = RatingForm()
    comment_form = CommentForm()
    category_form = CategoryForm()
    return render_template('book.html',
                           title=book.title,
                           book=book,
                           comments=comments.items,
                           rating_form=rating_form,
                           comment_form=comment_form,
                           category_form=category_form,
                           GENRES=GENRES,
                           next_url=next_url,
                           prev_url=prev_url)
Exemplo n.º 5
0
def add_rating(user_id, book_id):
    rating_form = RatingForm()
    comment_form = CommentForm()
    category_form = CategoryForm()
    book = Book.query.get(book_id)
    if rating_form.validate_on_submit():
        rating = float(rating_form.rating.data)
        ub = User_Book.query.filter_by(user_id=user_id,
                                       book_id=book_id).first()
        if ub:
            if ub.mark:
                # уже оценивал
                book.rating += (rating - ub.mark) / book.number_of_votes
            else:
                # ещё не оценивал
                if book.rating:

                    book.rating = book.number_of_votes / \
                        (book.number_of_votes+1)*(book.rating +
                                                  rating/book.number_of_votes)
                    book.number_of_votes += 1
                else:
                    book.rating = rating
                    book.number_of_votes = 1
            ub.mark = rating
            db.session.commit()
        else:
            ub = User_Book(user_id=user_id, book_id=book_id, mark=rating)
            if book.rating:
                book.rating = book.number_of_votes/(book.number_of_votes+1) *\
                    (book.rating + rating/book.number_of_votes)
                book.number_of_votes += 1
            else:
                book.rating = rating
                book.number_of_votes = 1
            db.session.add(ub)
            db.session.commit()
        return redirect(url_for('book', book_id=book_id))
    return render_template('book.html',
                           title=book.title,
                           book=book,
                           rating_form=rating_form,
                           comment_form=comment_form,
                           category_form=category_form)
Exemplo n.º 6
0
def book():
    book = ""
    rating = ""
    form = RatingForm()

    if request.method == 'POST':
        print(form.book_id.data)
        if form.validate_on_submit():
            redundant = False
            filename = os.path.join(app.static_folder, 'ratings.json')

            jsonData = {
                "user_id": current_user.user_id,
                "book_id": form.book_id.data,
                "rating": form.rating.data
            }
            with open(filename) as f:
                data = json.load(f)
            for i in range(len(data)):
                if data[i]["user_id"] == current_user.user_id and data[i][
                        "book_id"] == form.book_id.data:
                    redundant = True
            if redundant == False:
                data.append(jsonData)

                with open(filename, 'w') as f:
                    json.dump(data, f)
                    flash('Your rating has been updated!', 'success')
            else:
                flash('Rating unsuccessful, you have rated the movie',
                      'danger')
            return redirect(url_for('book'))
        else:
            flash('Rating Unsuccessful', 'danger')

    if request.method == 'GET':
        filename = os.path.join(app.static_folder, 'books.json')
        with open(filename) as blog_file:
            book = json.load(blog_file)
    return render_template('book.html', book=book, form=form)
Exemplo n.º 7
0
def update(username):
    form = RatingForm()
    if form.validate_on_submit():
        user = User.query.filter_by(username=username).first_or_404()
        print(form.movie_id.data)
        movie = Movie.query.get(form.movie_id.data)
        rating = float(form.rating.data)
        
        r = user.is_watched(movie)
        if r is not None:
            db.session.delete(r) 
            db.session.commit()

        if rating == -1:
            return redirect(url_for('user', username=username))

        r = UserMovieRating(movieid = movie.movieid, userid = user.userid, rating = float(rating))
        user.movies.append(r)
        movie.users.append(r)
        db.session.add(r)
        db.session.commit()

        print('movie {}, user {}, rating {}'.format(movie.movieid, username, form.rating.data))
        return redirect(url_for('user', username=username))
Exemplo n.º 8
0
def add_comment(user_id, book_id):
    rating_form = RatingForm()
    comment_form = CommentForm()
    category_form = CategoryForm()
    book = Book.query.get(book_id)

    if comment_form.validate_on_submit():
        comment = Comment(user_id=user_id,
                          book_id=book_id,
                          text=comment_form.body.data,
                          datetime=datetime.utcnow())
        db.session.add(comment)
        db.session.commit()
        return redirect(url_for('book', book_id=book_id))

    return render_template('book.html',
                           title=book.title,
                           GENRES=GENRES,
                           book=book,
                           rating_form=rating_form,
                           comment_form=comment_form,
                           category_form=category_form)
Exemplo n.º 9
0
def get_movies():
    result=[]
    searchform = SearchForm()
    ratingform = RatingForm()
    args = request.args.copy()

    #default page value of 1
    page = args.get('page', 1, type=int)

    #removing page from args
    if(bool(args.get('page'))):
        args.pop('page')

    #constructing querystring and saving current url
    querystring = ''
    for arg in args:
        querystring = querystring+'&'+arg+'='+args.get(arg)
    this_url = url_for('get_movies', page=page)+querystring

    #Search bar functionality
    if searchform.validate_on_submit():
        search = searchform.search.data
        search_type = searchform.search_field.data
        return redirect(url_for('get_movies')+'?'+search_type+'='+search)
    
    #Rating functionality
    if ratingform.validate_on_submit():
        movie = Movie.query.get(int(ratingform.request_id.data))
        movie.num_of_ratings = movie.num_of_ratings + 1
        movie.user_rating = (movie.user_rating*(movie.num_of_ratings-1) + int(ratingform.rating.data))/movie.num_of_ratings
        db.session.commit()
        return redirect(this_url)

    #if has no search return all
    if not bool(args):
        title = "Welcome to the Oscarpedia"
        result = query_all()

    #return query of args 
    else:
        title = "Search Results"
        result = query_args(args)

    #getting page number of last page
    lastpage = int(len(result)/app.config['MOVIES_PER_PAGE'])
    if(len(result)%app.config['MOVIES_PER_PAGE']>0):
        lastpage = lastpage+1

    #checking if the given page is more than the last page or less than 1
    if(page>lastpage):
        page = lastpage
    if(page<1):
        page = 1

    #handling urls
    json_url = url_for('get_movies_json', page=page)+querystring
    first_url = url_for('get_movies', page=1)+querystring
    last_url = url_for('get_movies', page=lastpage)+querystring
    if not page==1:
        prev_url=url_for('get_movies', page=page-1)+querystring
    else:
        prev_url=first_url
    if not page==lastpage:
        next_url=url_for('get_movies', page=page+1)+querystring
    else:
        next_url=last_url
    
    #picking out results relavent ot the page
    result = result[page*app.config['MOVIES_PER_PAGE']-app.config['MOVIES_PER_PAGE']:page*app.config['MOVIES_PER_PAGE']]

    return render_template('index.html', title=title, result=result, searchform=searchform, ratingform = ratingform, json_url=json_url,
                        page=page, prev_url=prev_url, next_url=next_url, last_url=last_url, first_url=first_url, this_url=this_url)
Exemplo n.º 10
0
def user(username):
    form = RatingForm()
    user = User.query.filter_by(username=username).first_or_404()
    ratings = user.movies 
    recommended_movies = _get_recommendations(user.userid)
    return render_template('user.html', user=user, ratings=ratings, recs=recommended_movies, form=form)