Advertisement
Guest User

Untitled

a guest
Jul 11th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. └── my-project
  2. ├── application
  3. │ ├── __init__.py
  4. │ ├── admin
  5. │ │ ├── __init__.py
  6. │ │ ├── forms.py
  7. │ │ └── views.py
  8. │ ├── auth
  9. │ │ ├── __init__.py
  10. │ │ ├── forms.py
  11. │ │ └── views.py
  12. │ │ └── token.py
  13. │ ├── home
  14. │ │ ├── __init__.py
  15. │ │ └── views.py
  16. │ ├── models.py
  17. │ ├── static
  18. │ └── templates
  19. │ └──....
  20. ├── config.py
  21. ├── instance
  22. │ └── config.py
  23. ├── migrations
  24. │ ├── README
  25. │ ├── alembic.ini
  26. │ ├── env.py
  27. │ ├── script.py.mako
  28. │ └── versions
  29. │ └── a1a1d8b30202_.py
  30. ├── requirements.txt
  31. └── run.py
  32.  
  33. import os
  34.  
  35. from application import create_app
  36.  
  37. config_name = os.getenv('FLASK_CONFIG')
  38. app = create_app(config_name)
  39.  
  40. if __name__ == '__main__':
  41. app.run()
  42.  
  43. # third-party imports
  44. from flask import Flask, render_template
  45. from flask_sqlalchemy import SQLAlchemy
  46. from flask_login import LoginManager
  47. from flask_migrate import Migrate
  48. from flask_bootstrap import Bootstrap
  49. from flask_mail import Mail
  50. import stripe
  51.  
  52.  
  53. # local imports
  54. from config import app_config
  55.  
  56. # db variable initialization
  57. db = SQLAlchemy()
  58. login_manager = LoginManager()
  59. LoginManager.user_loader
  60.  
  61. def create_app(config_name):
  62. app = Flask(__name__, instance_relative_config=True)
  63. app.config.from_object(app_config[config_name])
  64. app.config.from_pyfile('config.py')
  65.  
  66.  
  67. Bootstrap(app)
  68. db.init_app(app)
  69. login_manager.init_app(app)
  70. mail = Mail(app)
  71. migrate = Migrate(app,db)
  72.  
  73. from application import models
  74.  
  75. from .admin import admin as admin_blueprint
  76. app.register_blueprint(admin_blueprint, url_prefix='/admin')
  77. #the rest of the blueprint import goes here
  78.  
  79.  
  80. return app
  81.  
  82. from itsdangerous import URLSafeTimedSerializer
  83.  
  84. from . import auth
  85.  
  86. def generate_confirmation_token(email):
  87. serializer = URLSafeTimedSerializer(app.config['SECRET_KEY'])
  88. return serializer.dumps(email, salt=app.config['SECURITY_PASSWORD_SALT'])
  89.  
  90. def confirm_token(token, expiration = 600):
  91. serializer = URLSafeTimedSerializer(app.config['SECRET_KEY'])
  92. try:
  93. email = serializer.loads(
  94. toke,
  95. salt=app.config['SECURITY_PASSWORD_SALT'],
  96. max_age=expiration
  97. )
  98. except:
  99. return False
  100. return email
  101.  
  102. from flask_mail import Message
  103.  
  104. from . import auth
  105.  
  106. def send_mail(to, subject, template):
  107. msg = Message(
  108. subject,
  109. recipients=[to],
  110. html=template,
  111. sender=app.config['MAIL_DEFAULT_SENDER']
  112. )
  113. mail.send(msg)
  114.  
  115. from flask import flash, redirect, render_template, url_for, request
  116. from flask_login import login_required, login_user, logout_user
  117. from werkzeug.security import check_password_hash
  118. import datetime
  119.  
  120. from . import auth
  121. from forms import LoginForm, RegistrationForm
  122. from .. import db
  123. from ..models import User
  124.  
  125. @auth.route('/register', methods=['GET', 'POST'])
  126. def register():
  127. """
  128. Handle requests to the /register route
  129. Add a user to the database through the registration form
  130. """
  131. form = RegistrationForm()
  132. form.id = 'form_signup'
  133. if form.validate_on_submit():
  134. user = User(email=form.email.data,
  135. #username=form.username.data,
  136. first_name=form.first_name.data,
  137. last_name=form.last_name.data,
  138. password=form.password.data,
  139. registered_on=datetime.datetime.now(),
  140. confirmed=False,
  141. premium=False)
  142.  
  143. # add employee to the database
  144. db.session.add(user)
  145. db.session.commit()
  146. flash("We've just sent you an email confirmation. Please activate you account to completly finish your registration", 'succes')
  147.  
  148. token = generate_confirmation_token(user.email)
  149. confirm_url = url_for('auth.confirm_email', token=token, _external=True)
  150. html = render_template('auth/activate.html', confirm_url=confirm_url)
  151. subject = "Please confirm your email"
  152. send_email(user.email, subject, html)
  153.  
  154. login_user(user)
  155.  
  156. flash('A confirmation email has been sent via email.', 'success')
  157.  
  158. # redirect to the login page
  159. #return redirect(url_for('auth.login'))
  160. return redirect(url_for('home.homepage'))
  161.  
  162. # load registration template
  163. return render_template('auth/register.html', form=form, title='Register')
  164.  
  165. @auth.route('/confirm/<token>')
  166. @login_required
  167. def confirm_email(token):
  168. try:
  169. email = confirm_token(token)
  170. except:
  171. flash('The confirmation link is invalid or has expired.', 'danger')
  172. user = User.query.filter_by(email=email).first_or_404()
  173. if user.confirmed:
  174. flash('Account already confirmed. Please login.', 'succes')
  175. else:
  176. user.confirmed =True
  177. user.confirmed_on = datetime.datetime.now()
  178. db.session.add(user)
  179. db.session.commit()
  180. flash("You've confirmed your account. Thanks!", 'succes')
  181. return redirect(url_for('auth.login'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement