Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. except:
  2. abort(404)
  3.  
  4. @app.errorhandler(404)
  5. def not_found(e):
  6. return render_template('404.html')
  7.  
  8. email = ts.loads(token, salt='email-confirm-key', max_age=86400)
  9.  
  10. @app.route('/signup', methods=['GET', 'POST'])
  11. def signup():
  12. if 'email' in session:
  13. return redirect(url_for('tutorials'))
  14. form = SignupForm()
  15. if request.method == 'POST':
  16. emailalreadyexists = False
  17. emails = []
  18. wholetable = User.query.all()
  19. for row in wholetable:
  20. emails.append(row.email)
  21. if not form.validate() or form.email.data in emails:
  22. if form.email.data in emails and form.email.data != '':
  23. emailalreadyexists = True
  24. dplctemailerror = "That email is already in use."
  25. return render_template('signup.html', emailalreadyexists=emailalreadyexists,
  26. dplctemailerror=dplctemailerror, form=form)
  27. else:
  28. signupuser = User(form.first_name.data, form.last_name.data, form.role.data, form.school.data,
  29. form.email.data, form.password.data)
  30. db.session.add(signupuser)
  31. db.session.commit()
  32. token = ts.dumps(form.email.data, salt='email-confirm-key')
  33. subject = 'subject goes here'
  34. msg = Message(subject=subject, sender='name@email.com', recipients=form.email.data.split())
  35. link = url_for('confirm_email', token=token, _external=True)
  36. # msg.body = render_template("email_confirmationemail.txt", link=link, name=request.form['first_name'])
  37. msg.html = render_template("email_confirmationemail.html", link=link, name=request.form['first_name'])
  38. with app.app_context():
  39. mail.send(msg)
  40. return redirect(url_for('checkyouremail'))
  41. elif request.method == "GET":
  42. return render_template('signup.html', form=form)
  43.  
  44. @app.route('/confirmemail/<token>')
  45. def confirm_email(token):
  46. try:
  47. email = ts.loads(token, salt='email-confirm-key', max_age=86400)
  48. except:
  49. abort(404)
  50.  
  51. update_this = User.query.filter_by(email=email).first_or_404()
  52. update_this.emailconfirmed = True
  53. db.session.commit()
  54. if update_this.role == 'teacher':
  55. email_myself = 'me@email.com'
  56. subject = 'the subject'
  57. msg = Message(subject=subject, sender='name@email.com', recipients=email_myself.split())
  58. # msg.body = render_template("teachertoapprove.txt", email=email)
  59. msg.html = render_template("teachertoapprove.html", email=email)
  60. with app.app_context():
  61. mail.send(msg)
  62. # create a session with the given email and send user to the tutorials page where they will be logged in
  63. session['email'] = update_this.email
  64. return redirect(url_for('tutorials'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement