Esempio n. 1
0
def movie(movie_id):
    user = ctx.request.user
    movie = Movie.get(movie_id)
    movie = _get_movie_details(movie)
    if movie is None:
        raise notfound()
    movie.html_summary = markdown2.markdown(movie.summary)
    if user:
        history = History.find_first('where user_id=? and movie_id=?', user.id,
                                     movie_id)
        if not history:
            history = History(user_id=user.id, movie_id=movie_id)
            history.insert()
        else:
            history.created_at = time.time()
            history.update()
    reviews = Review.find_by(
        'where movie_id=? order by created_at desc limit 1000', movie_id)
    user_review = ''
    if user:
        user_review = Review.find_first('where user_id=? and movie_id=?',
                                        user.id, movie_id)
    return dict(movie=movie,
                reviews=reviews,
                user=user,
                user_review=user_review)
Esempio n. 2
0
def api_create_movie_review(movie_id):
    user = ctx.request.user
    if user is None:
        raise APIPermissionError('Need signin.')
    movie = Movie.get(movie_id)
    if movie is None:
        raise APIResourceNotFoundError('Movie')
    i = ctx.request.input(score='', content='')
    score = int(i.score) * 10
    content = i.content.strip()
    if not score:
        raise APIValueError('score')
    if not content:
        content = ''
    review = Review.find_first('where user_id=? and movie_id=?', user.id,
                               movie_id)
    if review:
        movie.score = (movie.score * movie.num_review - review.score +
                       score) / movie.num_review
        movie.update()
        review.score = score
        review.content = content
        review.created_at = time.time()
        review.update()
    else:
        review = Review(movie_id=movie_id,
                        user_id=user.id,
                        user_name=user.name,
                        score=score,
                        content=content)
        review.insert()
        movie.score = (movie.score * movie.num_review +
                       score) / (movie.num_review + 1)
        movie.num_review = movie.num_review + 1
        movie.update()
    return dict(review=review)