Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- └── my-project
- ├── application
- │ ├── __init__.py
- │ ├── admin
- │ │ ├── __init__.py
- │ │ ├── forms.py
- │ │ └── views.py
- │ ├── auth
- │ │ ├── __init__.py
- │ │ ├── forms.py
- │ │ └── views.py
- │ │ └── token.py
- │ ├── home
- │ │ ├── __init__.py
- │ │ └── views.py
- │ ├── models.py
- │ ├── static
- │ └── templates
- │ └──....
- │
- ├── config.py
- ├── instance
- │ └── config.py
- ├── migrations
- │ ├── README
- │ ├── alembic.ini
- │ ├── env.py
- │ ├── script.py.mako
- │ └── versions
- │ └── a1a1d8b30202_.py
- ├── requirements.txt
- └── run.py
- import os
- from application import create_app
- config_name = os.getenv('FLASK_CONFIG')
- app = create_app(config_name)
- if __name__ == '__main__':
- app.run()
- # third-party imports
- from flask import Flask, render_template
- from flask_sqlalchemy import SQLAlchemy
- from flask_login import LoginManager
- from flask_migrate import Migrate
- from flask_bootstrap import Bootstrap
- from flask_mail import Mail
- import stripe
- # local imports
- from config import app_config
- # db variable initialization
- db = SQLAlchemy()
- login_manager = LoginManager()
- LoginManager.user_loader
- def create_app(config_name):
- app = Flask(__name__, instance_relative_config=True)
- app.config.from_object(app_config[config_name])
- app.config.from_pyfile('config.py')
- Bootstrap(app)
- db.init_app(app)
- login_manager.init_app(app)
- mail = Mail(app)
- migrate = Migrate(app,db)
- from application import models
- from .admin import admin as admin_blueprint
- app.register_blueprint(admin_blueprint, url_prefix='/admin')
- #the rest of the blueprint import goes here
- return app
- from itsdangerous import URLSafeTimedSerializer
- from . import auth
- def generate_confirmation_token(email):
- serializer = URLSafeTimedSerializer(app.config['SECRET_KEY'])
- return serializer.dumps(email, salt=app.config['SECURITY_PASSWORD_SALT'])
- def confirm_token(token, expiration = 600):
- serializer = URLSafeTimedSerializer(app.config['SECRET_KEY'])
- try:
- email = serializer.loads(
- toke,
- salt=app.config['SECURITY_PASSWORD_SALT'],
- max_age=expiration
- )
- except:
- return False
- return email
- from flask_mail import Message
- from . import auth
- def send_mail(to, subject, template):
- msg = Message(
- subject,
- recipients=[to],
- html=template,
- sender=app.config['MAIL_DEFAULT_SENDER']
- )
- mail.send(msg)
- from flask import flash, redirect, render_template, url_for, request
- from flask_login import login_required, login_user, logout_user
- from werkzeug.security import check_password_hash
- import datetime
- from . import auth
- from forms import LoginForm, RegistrationForm
- from .. import db
- from ..models import User
- @auth.route('/register', methods=['GET', 'POST'])
- def register():
- """
- Handle requests to the /register route
- Add a user to the database through the registration form
- """
- form = RegistrationForm()
- form.id = 'form_signup'
- if form.validate_on_submit():
- user = User(email=form.email.data,
- #username=form.username.data,
- first_name=form.first_name.data,
- last_name=form.last_name.data,
- password=form.password.data,
- registered_on=datetime.datetime.now(),
- confirmed=False,
- premium=False)
- # add employee to the database
- db.session.add(user)
- db.session.commit()
- flash("We've just sent you an email confirmation. Please activate you account to completly finish your registration", 'succes')
- token = generate_confirmation_token(user.email)
- confirm_url = url_for('auth.confirm_email', token=token, _external=True)
- html = render_template('auth/activate.html', confirm_url=confirm_url)
- subject = "Please confirm your email"
- send_email(user.email, subject, html)
- login_user(user)
- flash('A confirmation email has been sent via email.', 'success')
- # redirect to the login page
- #return redirect(url_for('auth.login'))
- return redirect(url_for('home.homepage'))
- # load registration template
- return render_template('auth/register.html', form=form, title='Register')
- @auth.route('/confirm/<token>')
- @login_required
- def confirm_email(token):
- try:
- email = confirm_token(token)
- except:
- flash('The confirmation link is invalid or has expired.', 'danger')
- user = User.query.filter_by(email=email).first_or_404()
- if user.confirmed:
- flash('Account already confirmed. Please login.', 'succes')
- else:
- user.confirmed =True
- user.confirmed_on = datetime.datetime.now()
- db.session.add(user)
- db.session.commit()
- flash("You've confirmed your account. Thanks!", 'succes')
- return redirect(url_for('auth.login'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement