Exemple #1
0
def praise():
    if 'user_id' not in session:
        return jsonify({'status': 2})
    uid = session['user_id']
    if 'comment_id' not in request.form:
        return jsonify({'status': 3})
    try:
        cmid = request.form['comment_id']
        cmt = Comment.query.filter(Comment.id == cmid).one()
        if cmt.user_id == uid:
            db.close()
            return jsonify({'status': 3})
        pr = Praise.query.filter(
            and_(Praise.comment_id == cmid, Praise.user_id == uid))
        if pr.count() > 0:
            db.close()
            return jsonify({'status': 3})
        praise = Praise(comment_id=cmid,
                        user_id=uid,
                        created_at=now_datetime())
        db.add(praise)
        db.commit()
        msg = Message(reciver_id=cmt.user_id,
                      type='praise',
                      rel_id=cmid,
                      sender_id=uid,
                      created_at=now_datetime())
        db.add(msg)
        db.commit()
    finally:
        pass
    return jsonify({'status': 1})
Exemple #2
0
def comment_feedback(cmid):
    if 'user_id' not in session:
        return redirect(url_for('user_page.login',
                                url=request.args.get('url')))
    try:
        cf = FeedBack.query.filter(
            and_(FeedBack.comment_id == cmid,
                 FeedBack.user_id == session['uid']))
        if cf.count() > 0:
            db.close()
            return jsonify({'status': 3})

        comment = Comment.query.filter(Comment.id == cmid).one()

        fb = FeedBack(comment_id=cmid,
                      user_id=session['uid'],
                      content=request.form['content'],
                      created_at=now_datetime())
        db.add(fb)
        db.commit()

        msg = Message(reciver_id=comment.user_id,
                      type='feedback',
                      rel_id=cmid,
                      sender_id=session['uid'],
                      created_at=now_datetime())
        db.add(msg)
        db.commit()
    finally:
        pass
    return redirect(request.args.get('url'))
Exemple #3
0
def favorite(mid):
    if not 'uid' in session:
        return jsonify({'status':2, 'info':'not login'})

    uid = session['uid']

    res = db.query(Movie).filter(Movie.id==mid)
    if res.count()==0:
        return jsonify({'status':3, 'info':'the movie is not exists'})

    movie = res.one()

    
    if movie.favorited():
        sql = "delete from movie_favorite where movie_id=:mid and user_id=:uid"
        conn.execute(text(sql),{
            'mid':mid,
            'uid':uid
            })
        movie.favorite_count-=1
    else:
        sql = "insert into movie_favorite set movie_id=:mid, user_id=:uid, created_at=:ca"
        conn.execute(text(sql),{
            'mid':mid,
            'uid':uid,
            'ca':now_datetime()
            })
        movie.favorite_count+=1
    db.commit()

    return jsonify({'status':1})
Exemple #4
0
def create_user(name, email, password, superuser=True):
    user_role = Role.query.filter(Role.name == 'user').one()
    roles = []
    roles.append(user_role)
    verify_token = None
    token_expire = None
    active = 1
    if superuser:
        super_user_role = Role.query.filter(Role.name == 'superuser').one()
        roles.append(super_user_role)
    else:
        active = 0
        token_expire = int(time.time()) + 3600 * 24
        verify_token = md5(email + str(token_expire) +
                           app.config['SECRET_KEY'])

    user = User(name=name,
                email=email,
                password=encrypt_password(password),
                roles=roles,
                verify_token=verify_token,
                token_expire=token_expire,
                active=active,
                status=u'正常',
                created_at=now_datetime())
    db.add(user)
    db.commit()
    return user
Exemple #5
0
 def edit_view(self):
     if request.method == 'GET':
         id = request.args['id']
     else:
         id = request.form['id']
     article = Article.query.get(id)
     form = ArticleEditForm(request.form)
     if request.method == 'POST' and form.validate():
         article.title = request.form['title']
         article.content = request.form['content']
         article.pic = request.form['pic']
         article.brief = request.form['brief']
         article.rel_movies = request.form['rel_movies']
         article.created_at = now_datetime()
         db.commit()
         rel_movies = request.form['rel_movies']
         if rel_movies:
             sql = "delete from movie_article where article_id=:aid"
             conn.execute(text(sql), {'aid': id})
             arr = rel_movies.split(',')
             if len(arr) > 0:
                 for ma in arr:
                     sql = "insert into movie_article set article_id=:aid, movie_id=:mid"
                     conn.execute(text(sql), {'aid': id, 'mid': ma})
         return redirect('/admin/article')
     return self.render('/admin/edit_article.html',
                        form=form,
                        article=article)
Exemple #6
0
    def create_view(self):
        form = ArticleCreateForm(request.form)
        if request.method == 'POST' and form.validate():

            article = Article(title=request.form['title'],
                              content=request.form['content'],
                              pic=request.form['pic'],
                              rel_movies=request.form['rel_movies'],
                              brief=request.form['brief'],
                              created_at=now_datetime())
            db.add(article)
            db.commit()
            if article.id > 0:
                rel_movies = request.form['rel_movies']
                if rel_movies:
                    arr = rel_movies.split(',')
                    if len(arr) > 0:
                        for ma in arr:
                            sql = "insert into movie_article set article_id=:aid, movie_id=:mid"
                            conn.execute(text(sql), {
                                'aid': article.id,
                                'mid': ma
                            })
                return redirect('/admin/article')
        return self.render('/admin/create_article.html', form=form)
Exemple #7
0
def index():
    db = db_session
    # movie_categories = Category.query.all()
    # carousels = db.query(Carousel).filter(Carousel.position=='home-top').all()
    articles = Article.query.order_by(Article.created_at.desc()).limit(15)
    t = time.localtime()
    year = t.tm_year
    newest = db.query(Movie).filter(
        and_(Movie.publish_date < now_datetime(),
             Movie.year == 2017)).order_by(
                 Movie.read_count.desc()).limit(20).all()

    return render_template('index.html', newest=newest, articles=articles)
Exemple #8
0
def movie_comment(mid):
    type = request.args.get('type')
    if not type:
        type = 'movie'
    if 'username' not in session:
        return redirect(url_for('login', url=request.args.get('url')))
    try:

        comment = Comment(imdbid=mid,
                          user_id=session['uid'],
                          created_at=now_datetime(),
                          content=request.form['content'],
                          type=type)
        db.add(comment)
        db.commit()
    finally:
        pass
    return redirect(request.args.get('url'))
Exemple #9
0
def show(mid):
    res =  db.query(Movie).filter(Movie.id==mid)
    if res.count()==0:
        abort(404)
    else:
        movie = res.one()
        ip = request.remote_addr
        res = db.query(MovieRead).filter(MovieRead.ip==ip, MovieRead.movie_id==mid)
        if res.count()==0:
            mr = MovieRead(
                    ip=ip,
                    movie_id=mid,
                    created_at=now_datetime()
                )
            db.add(mr)
            db.commit()
            if mr.id>0:
                movie.read_count+=1
                db.commit()

        form = LoginForm(request.form)
        cmform = CommentForm(request.form)
        fbfm = FeedbackForm(request.form)
        followers = movie.followers().all()
        sql = "select c.*,mc.celebrity_type from movie_celebrity mc LEFT JOIN celebrity c on c.id=mc.celebrity_id WHERE mc.movie_id=:movie_id"
        celebrities = conn.execute(text(sql), {'movie_id':mid})
        actors = []
        directors = []
        for cel in celebrities:
            if cel.celebrity_type==u'演员':
                actors.append(cel)
            elif cel.celebrity_type==u'导演':
                directors.append(cel)

        sql = "select u.* from movie_favorite mf left join users u on u.id=mf.user_id where mf.movie_id=:mid limit 10"
        favorites = conn.execute(text(sql),{'mid':mid})
        subjects = movie.subjects.all()
        articles = movie.articles.all()
    return render_template('movie/detail.html', movie=movie, form=form, cmform=cmform,favorites=favorites,subjects=subjects,
                               followers=followers, fbfm=fbfm, actors=actors, directors=directors, articles=articles)
Exemple #10
0
def vote(type):
    btid = request.form.get('btid')

    if not btid:
        return abort(503)

    bt = Bt.query.get(btid)
    if not bt:
        return abort(503)

    is_admin = False
    if not bt.user_id == None:
        user = User.query.get(bt.user_id)
        if not user:
            return abort(503)
    else:
        is_admin = True

    if bt.voted():
        return abort(503)

    if not 'uid' in session:
        return jsonify({'status': 2})

    if type == 'up':
        votetype = u'赞'
    elif type == 'down':
        votetype = u'踩'.encode('utf-8')
    else:
        return abort(503)
    try:
        vote = BtVote(bt_id=btid,
                      user_id=session['uid'],
                      type=votetype,
                      created_at=int(time.time()))
        db.add(vote)
        db.commit()
        if vote.id > 0:
            if type == 'up':
                bt.vote_count += 1
                if not is_admin:
                    user.reputation = user.reputation + 2
            else:
                bt.vote_count -= 1
                if not is_admin:
                    user.reputation = user.reputation - 2
            db.commit()
            #send message
            if not is_admin:
                content = votetype + u'了你提交的电影资源'
                msg = Message(reciver_id=user.id,
                              content=content,
                              type='btvote',
                              rel_id=btid,
                              sender_id=session['uid'],
                              created_at=now_datetime())
                db.add(msg)
                db.commit()
        else:
            return abort(503)
    except:
        db.rollback()
    return jsonify({'status': 1})