Ejemplo n.º 1
0
def getfullpost(post_id):
	post=Post.cached_get_by_id(post_id)
	if post:
		comments=Comment.cached_get_by_id(post_id)
		return render_template('page.html',post=post,comments=comments)
	else:
		abort(404)
Ejemplo n.º 2
0
def post(post_id):
	post = Post.query.get_or_404(post_id) #return post with this id; if it doesn't, return 404
	recent_posts = Post.query.filter(Post.draft == 0, Post.id != post_id).order_by(func.random()).limit(3).all()
	comments = Comment.query.filter_by(post_id=post_id).all()

	form = CommentForm()
	if form.validate_on_submit():
		if form.username.data != "do_not_change": #spam prevention, hidden field
			return redirect(url_for("main.home"))
		if current_user.is_authenticated:
			comment = Comment(name=current_user.username, content=form.content.data, post_id=post_id, timestamp=datetime.now(get_localzone()))
		else:
			comment = Comment(name=form.name.data, content=form.content.data, post_id=post_id, timestamp=datetime.now(get_localzone()))
		db.session.add(comment)
		db.session.commit()
		return redirect(url_for("posts.post", post_id=post.id))
	return render_template("post.html", title=post.title, post=post, comments=comments, form=form, recent_posts=recent_posts)
Ejemplo n.º 3
0
def add_post_comment(post_id):
    post = Post.query.get_or_404(post_id)
    form = ResponseForm()
    if form.validate_on_submit():
        comment = Comment(content=form.message.data, author=current_user, post=post)
        db.session.add(comment)
        db.session.commit()
        flash('Odpoveď bola pridaná', 'success')
    return redirect(url_for('posts.post', post_id=post_id))
Ejemplo n.º 4
0
def init_database():
    db.drop_all()
    db.create_all()
    for i in range(0, 100):
        db.session.add(User('User' + str(i + 1), 'p' + str(i)))
        for j in range(0, 10):
            db.session.add(Image(get_url(), i + 1))
            for k in range(0, 5):
                db.session.add(
                    Comment('This is comment test' + str(k), 10 * i + j + 1,
                            i + 1))
    db.session.commit()
Ejemplo n.º 5
0
def init_database():
    db.drop_all()
    db.create_all()
    for i in xrange(100):
        db.session.add(User('User'+str(i+1), 'password'+str(i)))
        for j in xrange(10):
            db.session.add(Image(get_image_url(), i+1))
            for k in xrange(3):
                db.session.add(Comment("This is a comment"+str(k+1), 1+10*i+j, i+1))
    db.session.commit()

    image = Image.query.get(3)
    print image.user
Ejemplo n.º 6
0
    def edit_comment(edit_request, app, db):
        # if current_user.is_authenticated:
        #     return redirect(url_for('admin'))
        form = EditCommentForm()
        if edit_request.method == 'POST':
            if form.validate_on_submit():
                comment = Comment.get_by_name(form.name.data)
                if comment is None:
                    comment = Comment()

                comment.name = form.name.data
                comment.title = form.title.data
                comment.social_url = form.social_url.data
                comment.body = form.body.data

                if form.file.data is not None:
                    AdminController.remove_if_exists(comment.file_name)
                    comment.file_name = photos.save(form.file.data)
                    comment.file_url = photos.url(comment.file_name)

                db.session.add(comment)
                db.session.commit()

                return render_template('edit_or_add_comment.html',
                                       comment=comment,
                                       form=form)
            #
        else:
            comment_id = request.args.get('id')
            if comment_id is not None:
                c = Comment.get_by_id(comment_id)
                form.name.data = c.name
                form.title.data = c.title
                form.social_url.data = c.social_url

        return render_template('edit_or_add_comment.html',
                               title='Comment',
                               form=form)
Ejemplo n.º 7
0
def post(id):
    post = Post.query.get_or_404(id)
    form = CommentForm()
    if form.validate_on_submit():
        comment = Comment(body=form.body.data,
                          post=post,
                          author=current_user._get_current_object())
        db.session.add(comment)
        return redirect(url_for('main.post', id=post.id))
    comments = post.comments.order_by(Comment.timestamp.desc()).all()
    return render_template('main/post.html',
                           posts=[post],
                           comments=comments,
                           form=form)
Ejemplo n.º 8
0
def add_comment():
    image_id = int(request.values['image_id'])
    content = request.values['content']
    comment = Comment(content, image_id, current_user.id)
    database.session.add(comment)
    database.session.commit()
    map = {
        "code": 0,
        "id": comment.id,
        "content": comment.content,
        "username": comment.user.user_name,
        "user_id": comment.user_id
    }
    return json.dumps(map)
Ejemplo n.º 9
0
def create_comment(task_id):
    task = Task.query.get(task_id)
    species = Species.to_the_comment.name
    form = CommentForm()
    if request.method == 'POST':
        print("000000000", flush=True)
        new_comment = Comment(
            subject=form.subject.data,
            creator_id=current_user.id,
            task_id=task.id,
            species=species
        )
        db.session.add(new_comment)
        db.session.commit()
        return redirect(url_for('comment.show_comments', task_id=task_id))
    return render_template('comment/add_comment.html', task=task, form=form)
Ejemplo n.º 10
0
def leavecomment(post_id=0):
    if (not g.isadmin) and (not g.isguest):
        status = 0
        message = "please login first"
    elif post_id == 0:
        status = 0
        message = "comment to the wrong page"
    elif Post.all().filter('post_id', post_id).get().allowcomment == False:
        status = 0
        message = "comment is not allowded here"
    else:
        comment = Comment(post_id=post_id,
                          email=g.user.email(),
                          nickname=g.user.nickname(),
                          comment=urllib.unquote(request.data).decode('utf-8'),
                          create_date=int(time.time()),
                          ip=request.remote_addr)
        comment.comment_id = Comment.properid()
        comment.put()
        status = 1
        message = ""
    Comment.updatecache()
    return json.dumps({"status": status, "message": message})
Ejemplo n.º 11
0
def comment():
    form = CommentForm()
    if form.validate_on_submit():
        u = User.query.get(form.user_id.data)
        b = Book.query.get(form.book_id.data)
        if u and b:
            c = Comment.query.filter_by(user=u, book=b).first()
            if c:
                c.body = form.body.data
                c.score = form.score.data
                c.timestamp = arrow.utcnow()
                flash('修改成功!', 'is-success')
            else:
                c = Comment(user=u, book=b, body=form.body.data, score=form.score.data)
                db.session.add(c)
                flash('评论成功', 'is-success')
            b.set_avg()
            db.session.commit()
        else:
            flash('该用户或书籍不存在', 'is-danger')
            return redirect(url_for('main.index'))
    else:
        flash_errors(form)
    return redirect(url_for('book.index', book_id=form.book_id.data))
Ejemplo n.º 12
0
    def test_comments(self):
        # add two users
        r = Role.query.filter_by(name='User').first()
        self.assertIsNotNone(r)
        u1 = User(email='*****@*****.**', username='******',
                  password='******', confirmed=True, role=r)
        u2 = User(email='*****@*****.**', username='******',
                  password='******', confirmed=True, role=r)
        db.session.add_all([u1, u2])
        db.session.commit()

        # add a post
        post = Post(body='body of the post', author=u1)
        db.session.add(post)
        db.session.commit()

        # write a comment
        response = self.client.post(
            url_for('api.new_post_comment', id=post.id),
            headers=self.get_api_headers('*****@*****.**', 'dog'),
            data=json.dumps({'body': 'Good [post](http://example.com)!'}))
        self.assertTrue(response.status_code == 201)
        json_response = json.loads(response.data.decode('utf-8'))
        url = response.headers.get('Location')
        self.assertIsNotNone(url)
        self.assertTrue(json_response['body'] ==
                        'Good [post](http://example.com)!')
        self.assertTrue(
            re.sub('<.*?>', '', json_response['body_html']) == 'Good post!')

        # get the new comment
        response = self.client.get(
            url,
            headers=self.get_api_headers('*****@*****.**', 'cat'))
        self.assertTrue(response.status_code == 200)
        json_response = json.loads(response.data.decode('utf-8'))
        self.assertTrue(json_response['url'] == url)
        self.assertTrue(json_response['body'] ==
                        'Good [post](http://example.com)!')

        # add another comment
        comment = Comment(body='Thank you!', author=u1, post=post)
        db.session.add(comment)
        db.session.commit()

        # get the two comments from the post
        response = self.client.get(
            url_for('api.get_post_comments', id=post.id),
            headers=self.get_api_headers('*****@*****.**', 'dog'))
        self.assertTrue(response.status_code == 200)
        json_response = json.loads(response.data.decode('utf-8'))
        self.assertIsNotNone(json_response.get('comments'))
        self.assertTrue(json_response.get('count', 0) == 2)

        # get all the comments
        response = self.client.get(
            url_for('api.get_comments', id=post.id),
            headers=self.get_api_headers('*****@*****.**', 'dog'))
        self.assertTrue(response.status_code == 200)
        json_response = json.loads(response.data.decode('utf-8'))
        self.assertIsNotNone(json_response.get('comments'))
        self.assertTrue(json_response.get('count', 0) == 2)
Ejemplo n.º 13
0
def getcomment(post_id=0):
    comments = Comment.get_by_id(post_id)
    comments = [i.getjsonobj() for i in comments]
    #logging.info(comments)
    return json.dumps(comments)
Ejemplo n.º 14
0
def init_database():
    db.drop_all()
    db.create_all()
    # 添加用户
    for i in range(0, 100):
        db.session.add(
            User('User' + str(i), 'a' + str(i),
                 '.'.join(sample('0123456789asdfghjklqwertyuiopzxcvbnm', 10)),
                 get_image_url()))
        for j in range(0, 5):
            db.session.add(Image(get_image_url(), i + 1))
            for k in range(0, 3):
                db.session.add(
                    Comment('This is a comment' + str(k), 1 + 1 * i + j,
                            i + 1))
    db.session.commit()

    # 修改两种方式
    for i in range(50, 100, 2):
        user = User.query.get(i)
        user.username = '******' + user.username
    db.session.commit()

    User.query.filter_by(id=51).update({'username': '******'})
    db.session.commit()

    # 删除
    for i in range(50, 100, 2):
        comment = Comment.query.get(i + 1)
        db.session.delete(comment)
    db.session.commit()
    # 另一种直接使用delete()方法

    # 查询
    print(1)
    print(User.query.all())
    print(2)
    print(User.query.get(3))
    print(3)
    print(User.query.filter_by(id=5).first())
    print(4)
    print(User.query.order_by(User.id.desc()).offset(1).limit(2).all())
    print(5)
    print(User.query.filter(User.username.endswith('0')).limit(3).all())
    print(6)
    print(User.query.filter(or_(User.id == 88, User.id == 99)).all())
    print(7)
    print(User.query.filter(and_(User.id > 88, User.id < 99)).all())
    print(8)
    print(User.query.filter(and_(User.id > 88, User.id < 99)).first_or_404())
    print(9)
    print(
        User.query.order_by(User.id.desc()).paginate(page=1,
                                                     per_page=10).items)

    user = User.query.get(1)  # 从1开始,第1个用户
    print(10)
    print(user.images.all())
    image = Image.query.get(1)
    print(11)
    print(image.user)