예제 #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
def new_tag(photo_id):
	"""
	新标签
	:param photo_id: 图片id
	"""
	logger.info('url = ' + str(request.url))
	photo = Photo.query.get_or_404(photo_id)
	if current_user != photo.author and not current_user.can("MODERATE"):
		abort(403)

	form = TagForm()
	if form.validate_on_submit():
		# 添加新标签时,如果有多个标签,会以空格隔开
		for name in form.tag.data.split():
			# 查询,判断标签是否已经存在
			tag = Tag.query.filter_by(name=name).first()
			# 如果不存在,则先创建标签
			if tag is None:
				tag = Tag(name=name)
				logger.debug('用户:{}添加了新标签:{}'.format(current_user.username, name))
				db.session.add(tag)
				db.session.commit()
			# 将标签加入到photo的标签中
			if tag not in photo.tags:
				photo.tags.append(tag)
				db.session.commit()
		flash("标签添加成功!", "success")

	flash_errors(form)
	return redirect(url_for(".show_photo", photo_id=photo_id))
예제 #3
0
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)
        db.session.add(comment)
        db.session.commit()
        flash('Comment published.', '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))
예제 #4
0
파일: user.py 프로젝트: bbbbx/flask-notes
def upload_avatar():
    form = UploadAvatarForm()
    if form.validate_on_submit():
        image = form.image.data
        filename = avatars.save_avatar(image)  # 保存头像,并取得文件名
        current_user.avatar_raw = filename
        db.session.commit()
        flash('文件已上传。', 'info')
    flash_errors(form)
    return redirect(url_for('.change_avatar'))
예제 #5
0
def upload_avatar():
    form = UploadAvatarForm()
    if form.validate_on_submit():
        image = form.image.data
        filename = avatars.save_avatar(image)
        current_user.avatar_raw = filename
        db.session.commit()
        flash('Image uploaded, please crop.', 'success')
    flash_errors(form)
    return redirect(url_for('.change_avatar'))
예제 #6
0
def edit_description(photo_id):
    photo = Photo.query.get_or_404(photo_id)
    if current_user != photo.author and not current_user.can('MODERATE'):
        abort(403)
    form = DescriptionForm()
    if form.validate_on_submit():
        photo.description = form.description.data
        db.session.commit()
        flash('已更新图片描述', 'success')
    flash_errors(form)  #flash表单的错误
    return redirect(url_for('.show_photo', photo_id=photo_id))
예제 #7
0
def crop_avatar2():
    form = CropAvatarForm()
    if form.validate_on_submit():
        x = form.x.data
        y = form.y.data
        w = form.w.data
        h = form.h.data
        filenames = avatars.crop_avatar(current_user.avatar_raw2, x, y, w, h)
        db.session.commit()
        flash('Avatar updated.', 'success')
    flash_errors(form)
    return redirect(url_for('.change_avatar2'))
예제 #8
0
def edit_description(photo_id):
    photo = Photo.query.get_or_404(photo_id)
    if current_user != photo.author:
        abort(403)

    form = DescriptionForm()
    if form.validate_on_submit():
        photo.description = form.description.data
        db.session.commit()
        flash('Description updated.', 'success')
        flash_errors(form)
        return redirect(url_for('.show_post', photo_id=photo_id))
예제 #9
0
파일: main.py 프로젝트: jiangyue0013/albumy
def edit_description(photo_id):
    photo = Photo.query.get_or_404(photo_id)
    if current_user != photo.author and not current_user.can("MODERATE"):
        abort(403)

    form = DescriptionForm()
    if form.validate_on_submit():
        photo.description = form.description.data
        db.session.commit()
        flash("Description updated.", "success")

    flash_errors(form)
    return redirect(url_for("main.show_photo", photo_id=photo.id))
예제 #10
0
def crop_avatar():
    form = CropForm()
    if form.validate_on_submit():
        x = form.x.data
        y = form.x.data
        w = form.x.data
        h = form.x.data
        filenames = avatars.crop_avatar(current_user.avatar_raw, x, y, w, h)
        current_user.avatar_s = filenames[0]
        current_user.avatar_m = filenames[1]
        current_user.avatar_l = filenames[2]
        db.session.commit()
        flash('Avatar updated', 'success')
    flash_errors(form)
    return redirect(url_for('.change_avatar'))
예제 #11
0
def upload_avatar():
	"""
	更新头像
	"""
	logger.info('url = ' + str(request.url))
	form = UploadAvatarForm()
	if form.validate_on_submit():
		image = form.image.data
		filename = avatars.save_avatar(image)
		# 更新头像
		current_user.avatar_raw = filename
		db.session.commit()
		flash('文件已上传,请裁剪!', 'success')
	flash_errors(form)
	return redirect(url_for('.change_avatar'))
예제 #12
0
def new_tag(photo_id):
    photo = Photo.query.get_or_404(photo_id)
    if current_user != photo.author:
        abort(403)
    form = TagForm()
    if form.validate_on_submit():
        for name in form.tag.data.split():
            tag = Tag.query.filter_by(name=name).first()
            if tag is None:
                tag = Tag(name=name)
                db.session.add(tag)
            if tag not in photo.tags:
                photo.tags.append(tag)
        db.session.commit()
    flash_errors(form)
    return redirect(url_for('.show_photo', photo_id=photo_id))
예제 #13
0
파일: user.py 프로젝트: bbbbx/flask-notes
def crop_avatar():
    form = CropAvatarForm()
    if form.validate_on_submit():
        x = form.x.data
        y = form.y.data
        h = form.h.data
        w = form.w.data
        # 裁剪头像并保存,返回三个尺寸头像的文件名
        filename = avatars.crop_avatar(current_user.avatar_raw, x, y, w, h)
        current_user.avatar_s = filename[0]
        current_user.avatar_m = filename[1]
        current_user.avatar_l = filename[2]
        db.session.commit()
        flash('头像已更新。', 'success')
    flash_errors(form)
    return redirect(url_for('.change_avatar'))
예제 #14
0
파일: main.py 프로젝트: zgfhill/albumy-na
def new_tag(photo_id):
    photo = Photo.query.get_or_404(photo_id)
    if current_user != photo.author and not current_user.can('MODERATE'):
        abort(403)

    form = TagForm()
    if form.validate_on_submit():
        for name in form.tag.data.split():
            tag = Tag.query.filter_by(name=name).first()
            if tag is None:
                tag = Tag(name=name)
            photo.tags.append(tag)
        db.session.commit()
        flash('Tag added', 'success')

    flash_errors(form)
    return redirect(url_for('main.show_photo', photo_id=photo_id))
예제 #15
0
def edit_description(photo_id):
	"""
	编辑图片描述
	:param photo_id: 图片id
	"""
	logger.info('url = ' + str(request.url))
	photo = Photo.query.get_or_404(photo_id)
	if current_user != photo.author and not current_user.can("MODERATE"):
		abort(403)

	form = DescriptionForm()
	if form.validate_on_submit():
		photo.description = form.description.data
		logger.info('修改了图片id={}的描述={}'.format(photo_id, photo.description))
		db.session.commit()
		flash("描述更新成功!", "success")

	flash_errors(form)
	return redirect(url_for(".show_photo", photo_id=photo_id))
예제 #16
0
def new_tag(photo_id):
	photo = Photo.query.get_or_404(photo_id)
	if current_user != photo.author:
		abort(403)
	tag_form = TagForm()
	if tag_form.validate_on_submit():
		tag_names = tag_form.tag.data.split()
		for tag_name in tag_names:
			tag = Tag.query.filter_by(name=tag_name).first()
			if not tag:
				tag = Tag(name=tag_name)
				db.session.add(tag)
				# db.session.commit()
			if tag not in photo.tags:
				photo.tags.append(tag)
			db.session.commit()
		flash("Tag added.", 'success')

	flash_errors(tag_form)
	return redirect(url_for('.show_photo', photo_id=photo_id))
예제 #17
0
def crop_avatar():
	"""
	裁剪头像
	"""
	logger.info('url = ' + str(request.url))
	form = CropAvatarForm()
	if form.validate_on_submit():
		x = form.x.data
		y = form.y.data
		w = form.w.data
		h = form.h.data
		# 头像裁剪
		filenames = avatars.crop_avatar(current_user.avatar_raw, x, y, w, h)
		# 更新头像
		current_user.avatar_s = filenames[0]
		current_user.avatar_m = filenames[1]
		current_user.avatar_l = filenames[2]
		db.session.commit()
		flash('头像更新成功', 'success')
	flash_errors(form)
	return redirect(url_for('.change_avatar'))