def comment_action(comment_id, action): """ Comment actions. :param comment_id: ID of the comment to take action upon :param action: Action to take on comment """ referrer = request.referrer comment = PostComment.query.filter_by(id=comment_id).first_or_404() # Delete comment. if action == 'delete-comment': if current_user == comment.author: referrer = url_for('users.post', post_id=comment.post.id) comment.delete() NotificationHelper(comment=comment).delete_comment() comment.commit() flash('Comment was deleted.') # Like comment. if action == 'like-comment': current_user.like_comment(comment) NotificationHelper(notified=comment.author, notifier=current_user, comment=comment, post=comment.post).comment_like() current_user.commit() # Unlike comment. if action == 'unlike-comment': current_user.unlike_comment(comment) NotificationHelper(notifier=current_user, comment=comment).delete_comment_like() current_user.commit() return redirect(referrer)
def settings_profile(): form = SettingsProfileForm() if form.validate_on_submit(): # Profile picture. pp_data = form.profile_picture.data if pp_data: pp = ProfilePhoto(current_user, pp_data) pp.manage_files() current_user.profile_photo = pp.new_basename current_user.commit() current_user.first_name = form.first_name.data current_user.last_name = form.last_name.data current_user.bio = form.bio.data current_user.location = form.location.data current_user.website_url = form.website_url.data current_user.commit() flash('Your profile settings have been updated.') return redirect(url_for('users.settings_profile')) elif request.method == 'GET': form.first_name.data = current_user.first_name form.last_name.data = current_user.last_name form.bio.data = current_user.bio form.location.data = current_user.location form.website_url.data = current_user.website_url return render_template('users/settings/profile.html', form=form)
def post_action(post_id, action): """ Post actions. :param post_id: ID of the post to take action upon :param action: Action to take on post """ referrer = request.referrer post = Post.query.filter_by(id=post_id).first_or_404() # Delete post. if action == 'delete': if current_user == post.author or current_user.id == post.recipient_id: referrer = url_for('users.profile', username=current_user.username) post.delete() NotificationHelper(post=post).delete_post() post.commit() flash('Post was deleted.') # Like post. if action == 'like': current_user.like_post(post) NotificationHelper(notified=post.author, notifier=current_user, post=post).post_like() current_user.commit() # Unlike post. if action == 'unlike': current_user.unlike_post(post) NotificationHelper(notifier=current_user, post=post).delete_post_like() current_user.commit() return redirect(referrer)
def settings_password(): form = SettingsPasswordForm() if form.validate_on_submit(): current_user.set_password(form.password.data) current_user.commit() flash('Your password has been changed.') return redirect(url_for('users.settings_password')) return render_template('users/settings/password.html', form=form)
def settings_delete_account(): form = SettingsDeleteAccountForm() if form.validate_on_submit(): current_user.delete() NotificationHelper(notifier=current_user).delete_by_user() current_user.commit() flash('Your account has been removed.') return redirect(url_for('users.home')) return render_template('users/settings/delete-account.html', form=form)
def settings_miscellaneous(): form = SettingsMiscellaneousForm() if form.validate_on_submit(): current_user.posts_per_page = int(form.posts_per_page.data) current_user.commit() flash('Your settings have been updated.') return redirect(url_for('users.settings_miscellaneous')) elif request.method == 'GET': form.posts_per_page.default = str(current_user.posts_per_page) form.process() return render_template('users/settings/miscellaneous.html', form=form)
def settings_account(): form = SettingsAccountForm(current_user.username, current_user.email) if form.validate_on_submit(): current_user.username = form.username.data current_user.email = form.email.data current_user.commit() flash('Your account settings have been updated.') return redirect(url_for('users.settings_account')) elif request.method == 'GET': form.username.data = current_user.username form.email.data = current_user.email return render_template('users/settings/account.html', form=form)
def profile_edit(): form = EditProfileForm(obj=current_user) if form.validate_on_submit(): if User.if_exists_email(form.email._value()) and current_user.email!=form.email._value(): flash(gettext("An account has already been registered with that email. Try another?"), 'warning') return render_template('profile-edit.html', form=form, user=current_user) if not current_user.username == form.username._value(): flash(gettext("You little rebel! I like you!"), 'warning') return render_template('profile-edit.html', form=form, user=current_user) form.populate_obj(current_user) current_user.commit() flash(gettext('User {username} edited').format(username=current_user.username),'success') return render_template('profile-edit.html', form=form, user=current_user)
def edit_profile(): form = EditProfileForm(original_username=current_user.username, original_email=current_user.email) if form.validate_on_submit(): current_user.username = form.username.data if form.username.data != "" else current_user.username current_user.email = form.email.data if form.email.data != "" else current_user.email if form.username.data != "" or form.email.data != "": current_user.commit() flash("Your changes have been saved.") return redirect( url_for("main.user", username=current_user.username)) elif request.method == "GET": form.username.data = current_user.username form.email.data = current_user.email return render_template("edit_profile.html", title="Edit Profile", form=form)
def required_base(): errors = {} try: nickname = request.form['nickname'] native = request.form['native'] learning = request.form['learning'] if not vali_nickname(nickname): errors['nickname'] = _(u"Please enter the correct nickname, nickname must has 1-40 characters") if not langName(native): errors['native'] = _(u"Language error, system don't support this language") if not langName(learning): errors['learning'] = _(u"Language error, system don't support this language") except KeyError: errors['KeyError'] = _(u"key error") if not errors: current_user.nickname = nickname current_user.native = native current_user.learning = learning current_user.commit() return errors
def profile_edit(): form = EditProfileForm(obj=current_user) if form.validate_on_submit(): if User.if_exists_email(form.email._value( )) and current_user.email != form.email._value(): flash( gettext( "An account has already been registered with that email. Try another?" ), 'warning') return render_template('profile-edit.html', form=form, user=current_user) if not current_user.username == form.username._value(): flash(gettext("You little rebel! I like you!"), 'warning') return render_template('profile-edit.html', form=form, user=current_user) form.populate_obj(current_user) current_user.commit() flash( gettext('User {username} edited').format( username=current_user.username), 'success') return render_template('profile-edit.html', form=form, user=current_user)
def user_action(username, action): """ User actions. :param username: Username to take action upon :param action: Action to take on `username` """ user = User.query.filter_by(username=username, active=True).first_or_404() # Do not allow users to take action on themselves. no_self_action = ['follow', 'unfollow'] if action in no_self_action and user == current_user: return redirect(url_for('users.home')) # Follow user. if action == 'follow': current_user.follow(user) NotificationHelper(notified=user, notifier=current_user).follow() current_user.commit() flash('You are now following {}.'.format(user.full_name)) # Unfollow user. if action == 'unfollow': current_user.unfollow(user) NotificationHelper(notified=user).delete_follow() current_user.commit() flash('You are no longer following {}.'.format(user.full_name)) return redirect(request.referrer)
def notifications(): current_user.notification_last_read_time = datetime.utcnow() current_user.commit() notifications = current_user.get_notifications() return render_template('users/notifications.html', notifications=notifications)
def before_request(): if current_user.is_authenticated: if not current_user.active: return redirect(url_for('static_views.logout')) current_user.last_seen = datetime.utcnow() current_user.commit()