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.

29 lines
1.0 KiB

  1. from flask import render_template, session, redirect, url_for, current_app
  2. from .. import db
  3. from ..models import User
  4. from ..email import send_email
  5. from . import main
  6. from .forms import NameForm
  7. @main.route('/', methods=['GET', 'POST'])
  8. def index():
  9. form = NameForm()
  10. if form.validate_on_submit():
  11. user = User.query.filter_by(username=form.name.data).first()
  12. if user is None:
  13. user = User(username=form.name.data)
  14. db.session.add(user)
  15. db.session.commit()
  16. session['known'] = False
  17. if current_app.config['FLASKY_ADMIN']:
  18. send_email(current_app.config['FLASKY_ADMIN'], 'New user',
  19. 'mail/new_user', user=user)
  20. else:
  21. session['known'] = True
  22. session['name'] = form.name.data
  23. form.name.data = ''
  24. return redirect(url_for('.index'))
  25. return render_template('index.html',
  26. form=form, name=session.get('name'),
  27. known=session.get('known', False))