예제 #1
0
def new_comment(photo_id):
	"""
	新的评论
	:param photo_id: 图片id
	"""
	logger.info('url = ' + str(request.url))
	photo = Photo.query.get_or_404(photo_id)
	page = request.args.get("page", 1, type=int)
	form = CommentForm()
	if form.validate_on_submit():
		body = form.body.data
		author = current_user._get_current_object()
		comment = Comment(body=body, author=author, photo=photo)
		logger.info('用户:{}对图片:{}发表了评论:{}'.format(current_user.username, photo_id, body))
		# 被回复的用户
		replied_id = request.args.get("reply")
		if replied_id:
			comment.replied = Comment.query.get_or_404(replied_id)
			if comment.replied.author.receive_comment_notification:
				push_comment_notification(
					photo_id=photo.id, receiver=comment.replied.author
				)
		db.session.add(comment)
		db.session.commit()
		flash("评论成功!", "success")

		if current_user != photo.author and photo.author.receive_comment_notification:
			push_comment_notification(photo_id, receiver=photo.author, page=page)

	flash_errors(form)
	return redirect(url_for(".show_photo", photo_id=photo_id, page=page))
예제 #2
0
파일: main.py 프로젝트: 925150722/albumy
def new_comment(photo_id):
    photo = Photo.query.get_or_404(photo_id)
    page = request.args.get('page', 1, type=int)
    form = CommentForm()
    if form.validate_on_submit():
        body = form.body.data
        author = current_user._get_current_object()
        comment = Comment(body=body, author=author, photo=photo)
        replied_id = request.args.get('reply')

        if replied_id:
            comment.replied = Comment.query.get_or_404(replied_id)
            if comment.replied.author.receive_comment_notification:
                push_comment_notification(photo_id=photo.id,
                                          receiver=comment.replied.author)

            if current_user != photo.author and photo.author.receive_comment_notification:
                push_comment_notification(photo_id,
                                          receiver=photo.author,
                                          page=page)

        db.session.add(comment)
        db.session.commit()
        flash('Comment published.', 'success')

    flash_errors(form)
    return redirect(url_for('.show_photo', photo_id=photo.id))
예제 #3
0
    def setUp(self):
        app = create_app('testing')
        self.context = app.test_request_context()
        self.context.push()
        self.client = app.test_client()
        self.runner = app.test_cli_runner()

        db.create_all()
        Role.init_role()

        admin_user = User(email='*****@*****.**',
                          name='Admin',
                          username='******',
                          confirmed=True)
        admin_user.set_password('123')
        normal_user = User(email='*****@*****.**',
                           name='Normal User',
                           username='******',
                           confirmed=True)
        normal_user.set_password('123')
        unconfirmed_user = User(email='*****@*****.**',
                                name='unconfirmed',
                                username='******',
                                confirmed=False)
        unconfirmed_user.set_password('123')
        locked_user = User(email='*****@*****.**',
                           name='Locked User',
                           username='******',
                           confirmed=True,
                           locked=True)
        locked_user.set_password('123')
        locked_user.lock()
        blocked_user = User(email='*****@*****.**',
                            name='Blocked User',
                            username='******',
                            confirmed=True,
                            active=False)
        blocked_user.set_password('123')

        photo = Photo(filename='test.jpg',
                      filename_s='test_s.jpg',
                      filename_m='test_m.jpg',
                      description='Photo 1',
                      author=admin_user)
        photo2 = Photo(filename='test2.jpg',
                       filename_s='test_s2.jpg',
                       filename_m='test_m2.jpg',
                       description='Photo 2',
                       author=normal_user)

        comment = Comment(body='test comment body',
                          photo=photo,
                          author=normal_user)
        tag = Tag(name='test tag')
        photo.tags.append(tag)
        db.session.add_all([
            admin_user, normal_user, unconfirmed_user, locked_user,
            blocked_user
        ])
        db.session.commit()
예제 #4
0
def fake_comment(count=100):
    for i in range(count):
        comment = Comment(body=fake.sentence(),
                          timestamp=fake.date_time_this_year(),
                          photo_id=random.randint(1, Photo.query.count()),
                          author_id=random.randint(1, User.query.count()))
        db.session.add(comment)
    db.session.commit()
예제 #5
0
def fake_comment(count=100):
    for i in range(count):
        comment = Comment(
            author=User.query.get(random.randint(1, User.query.count())),
            body=fake.sentence(),
            timestamp=fake.date_time_this_year(),
            photo=Photo.query.get(random.randint(1, Photo.query.count())))
        db.session.add(comment)
    db.session.commit()

    # 产生200条回复
    for j in range(count * 2):
        comment = Comment.query.get(random.randint(1, Comment.query.count()))
        reply = Comment(author=User.query.get(
            random.randint(1, User.query.count())),
                        body=fake.sentence(),
                        timestamp=fake.date_time_this_year(),
                        photo=comment.photo,
                        replied=comment)
        db.session.add(reply)
    db.session.commit()
예제 #6
0
def fake_comment(count=100):
	"""
	评论
	"""
	for i in range(count):
		comment = Comment(
			# 随机用户
			author=User.query.get(random.randint(1, User.query.count())),
			# 评论内容
			body=fake.sentence(),
			# 时间
			timestamp=fake.date_time_this_year(),
			# 图片
			photo=Photo.query.get(random.randint(1, Photo.query.count())),
		)
		db.session.add(comment)
	db.session.commit()
예제 #7
0
파일: fakes.py 프로젝트: dr2us-com/Dr2us
def fake_comment(count=100):
    doctors = User.query.join(Role).filter(Role.name == 'Doctor').all()
    for i in range(count):
        comment = Comment(author=doctors[random.randint(0,
                                                        len(doctors) - 1)],
                          body=fake.sentence(),
                          timestamp=fake.date_time_this_year(),
                          photo=Photo.query.get(
                              random.randint(1, Photo.query.count())))
        # comment = Comment(
        #     author=User.query.get(random.randint(1, User.query.count())),
        #     body=fake.sentence(),
        #     timestamp=fake.date_time_this_year(),
        #     photo=Photo.query.get(random.randint(1, Photo.query.count()))
        # )
        db.session.add(comment)
    db.session.commit()
예제 #8
0
파일: base.py 프로젝트: guoxy2016/Albumy
    def setUp(self) -> None:
        app = create_app('testing')
        self.context = app.test_request_context()
        self.context.push()
        self.client = app.test_client()
        self.runner = app.test_cli_runner()

        db.create_all()
        Role.init_role()

        admin_user = User(email='*****@*****.**',
                          name='Admin',
                          username='******',
                          confirmed=True)
        admin_user.password = '******'

        normal_user = User(email='*****@*****.**',
                           name='Normal',
                           username='******',
                           confirmed=True)
        normal_user.password = '******'

        unconfirm_user = User(email='*****@*****.**',
                              name='Unconfirm',
                              username='******',
                              confirmed=False)
        unconfirm_user.password = '******'

        locked_user = User(email='*****@*****.**',
                           name='Locked',
                           username='******',
                           confirmed=True,
                           locked=True)
        locked_role = Role.query.filter_by(name='Locked').first()
        locked_user.role = locked_role
        locked_user.password = '******'

        block_user = User(email='*****@*****.**',
                          name='Block',
                          username='******',
                          confirmed=True,
                          active=False)
        block_user.password = '******'

        photo = Photo(filename='test.jpg',
                      filename_s='test_s.jpg',
                      filename_m='test_m.jpg',
                      author=admin_user,
                      description='Photo 1')
        photo2 = Photo(filename='test2.jpg',
                       filename_s='test2_s.jpg',
                       filename_m='test2_m.jpg',
                       author=normal_user,
                       description='Photo 2')

        comment = Comment(body='test comment body',
                          photo=photo,
                          author=normal_user)
        tag = Tag(name='test tag')
        photo.tags.append(tag)
        db.session.add_all([
            admin_user, normal_user, unconfirm_user, locked_user, block_user,
            photo, photo2, comment, tag
        ])
        db.session.commit()