You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.2 KiB

  1. from flask import render_template, redirect, url_for, flash
  2. from flask_login import login_required, current_user
  3. from . import main
  4. from .forms import EditProfileForm, EditProfileAdminForm
  5. from .. import db
  6. from ..models import User, Role
  7. from ..decorators import admin_required
  8. @main.route('/')
  9. def index():
  10. return render_template('index.html')
  11. @main.route('/user/<username>')
  12. def user(username):
  13. user = User.query.filter_by(username=username).first_or_404()
  14. return render_template('user.html', user=user)
  15. @main.route('/edit-profile', methods=['GET', 'POST'])
  16. @login_required
  17. def edit_profile():
  18. form = EditProfileForm()
  19. if form.validate_on_submit():
  20. current_user.name = form.name.data
  21. current_user.location = form.location.data
  22. current_user.about_me = form.about_me.data
  23. db.session.add(current_user._get_current_object())
  24. db.session.commit()
  25. flash('Your profile has been updated.')
  26. return redirect(url_for('.user', username=current_user.username))
  27. form.name.data = current_user.name
  28. form.location.data = current_user.location
  29. form.about_me.data = current_user.about_me
  30. return render_template('edit_profile.html', form=form)
  31. @main.route('/edit-profile/<int:id>', methods=['GET', 'POST'])
  32. @login_required
  33. @admin_required
  34. def edit_profile_admin(id):
  35. user = User.query.get_or_404(id)
  36. form = EditProfileAdminForm(user=user)
  37. if form.validate_on_submit():
  38. user.email = form.email.data
  39. user.username = form.username.data
  40. user.comfirmed = form.confirmed.data
  41. user.role = Role.query.get(form.role.data)
  42. user.name = form.name.data
  43. user.location = form.location.data
  44. user.about_me = form.about_me.data
  45. db.session.add(user)
  46. db.session.commit()
  47. flash('The profile has been updated.')
  48. return redirect(url_for('.user', username=user.username))
  49. form.email.data = user.email
  50. form.username.data = user.username
  51. form.confirmed.data = user.confirmed
  52. form.role.data = user.role_id
  53. form.name.data = user.name
  54. form.location.data = user.location
  55. form.about_me.data = user.about_me
  56. return render_template('edit_profile.html', form=form, user=user)