Skip to content

Instantly share code, notes, and snippets.

@maxcnunes
Created February 2, 2015 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxcnunes/719d948e4dd1f15c97a2 to your computer and use it in GitHub Desktop.
Save maxcnunes/719d948e4dd1f15c97a2 to your computer and use it in GitHub Desktop.
from flask import Flask, render_template, redirect, url_for
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)
# Basic Routes #
@app.route('/')
def index():
return redirect(url_for('users.index'))
@app.errorhandler(404)
def not_found(error):
return render_template('404.html'), 404
# BluePrints - Modules #
# Users Module
from app.users.views import mod as usersModule
app.register_blueprint(usersModule)
# Exercises Module
from app.exercises.views import mod as exercisesModule
app.register_blueprint(exercisesModule)
<html>
<head>
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
<title>{% block title %}I Sweated Yesterday{% endblock %}</title>
{% block css %}
<link rel="stylesheet" href="/static/css/reset.css" />
<link rel="stylesheet" href="/static/css/main.css" />
{% endblock %}
{% block script %}
<script src="/static/js/main.js" type="text/javascript"></script>
{% endblock %}
</head>
<body>
<div id="wrapper">
<div id="header">
{% block header %}
<h1>I Sweated Yesterday</h1>
{% endblock %}
</div>
<div id="messages">
{% for category, msg in get_flashed_messages(with_categories=true) %}
{{ msg }}
{% endfor %}
</div>
<div id="content" class="shadow">
{% block logout %}
<a class="bt-action-logout" href="{{ url_for('users.logout') }}">
Logout
</a>
{% endblock %}
{% block content %}{% endblock %}
</div>
<div id="footer">{% block footer %}{% endblock %}</div>
</div>
</body>
</html>
import os
_basedir = os.path.abspath(os.path.dirname(__file__))
DEBUG = False
ADMINS = frozenset(['youremail@yourdomain.com'])
SECRET_KEY = 'SECRET_KEY_FOR_SESSION_SIGNING'
# Define the path of our database inside the root application,
# where 'app.db' is the database's name
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(_basedir, 'app.db')
DATABASE_CONNECT_OPTION = {}
THREADS_PER_PAGE = 8
CSRF_ENABLED = True
CSRF_SESSION_KEY = 'SOMETHING_IMPOSSIBLE_TO_GUEES'
# User role
ADMIN = 0
STAFF = 1
USER = 2
ROLE = {
ADMIN: 'admin',
STAFF: 'staff',
USER: 'user',
}
# User status
INACTIVE = 0
NEW = 1
ACTIVE = 2
STATUS = {
INACTIVE: 'inactive',
NEW: 'new',
ACTIVE: 'active',
}
# Session Name
SESSION_NAME_USER_ID = 'user_id'
from functools import wraps
from flask import g, flash, redirect, url_for, request
def requires_login(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if g.user is None:
flash(u'You need to be signed in for this page.')
return redirect(url_for('users.login', next=request.path))
return f(*args, **kwargs)
return decorated_function
from flask.ext.wtf import Form, TextField, PasswordField, BooleanField
from flask.ext.wtf import Required, Email, EqualTo
class LoginForm(Form):
email = TextField('Email address', [Required(), Email()])
password = PasswordField('Password', [Required()])
class RegisterForm(Form):
name = TextField('NickName', [Required()])
email = TextField('Email address', [Required()])
password = PasswordField('Password', [Required()])
confirm = PasswordField('Repeat Password', [
Required(),
EqualTo('confirm', message='Passwords must match')
])
# Importa biblioteca Flask
from flask import Flask
# Inicializa a aplicacao instanciando Flask
app = Flask(__name__)
# Atribui uma rota ao hello_world
@app.route('/')
def hello_world():
return 'Hello World!'
# Roda a aplicacao em: http://localhost:8085
app.run(debug=True,port=8085)
from app import db
# Drop all tables from db file
db.drop_all()
# Create all tables on db file,
# copying the structure from the definition on the Models
db.create_all()
{% macro render_field(field) %}
<div class="form_field">
{{ field.label(class="label") }}
{% if field.errors %}
{% set css_class = 'has_error' + kwargs.pop('class', '') %}
{{ field(class=css_class, **kwargs) }}
<ul class="errors">
{% for error in field.errors %}
<li>{{ error|e }}</li>
{% endfor %}
</ul>
{% else %}
{{ field(**kwargs) }}
{% endif %}
</div>
{% endmacro %}
# third party imports
from sqlalchemy.sql import extract, func
# local application imports
from app import db
from app.users import constants as USER
from app.exercises.models import Exercise
from app.exercises.helpers import DateHelper
class User(db.Model):
# Map model to db table
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), unique=True)
email = db.Column(db.String(120), unique=True)
password = db.Column(db.String(20))
role = db.Column(db.SmallInteger, default=USER.USER)
status = db.Column(db.SmallInteger, default=USER.NEW)
exercises = db.relationship('Exercise', backref='user', lazy='dynamic')
# Class Constructor
def __init__(self, id=None):
self.id = id
# Factory Constructor of a new user to register
@classmethod
def NewUserToRegister(cls, name=None, email=None, password=None):
_user = cls()
_user.name = name
_user.email = email
_user.password = password
return _user
def getNameTitle(self):
return self.name.title()
def getStatus(self):
return USER.STATUS[self.status]
def getRole(self):
return USER.ROLE[self.role]
def getTotalExercises(self):
return len(self.exercises.all())
def getTotalExercisesCurrentWeek(self):
start_end_week = DateHelper.get_start_end_days_current_week()
start_week = start_end_week[0]
end_week = start_end_week[1]
return len(db.session.query(Exercise.id)
.filter(Exercise.user_id == self.id)
.filter(Exercise.date >= start_week)
.filter(Exercise.date <= end_week)
.all())
def getTotalExercisesCurrentMonth(self):
current_month = DateHelper.current_month()
return len(db.session.query(Exercise.id)
.filter(Exercise.user_id == self.id)
.filter(extract('month', Exercise.date) == current_month)
.all())
def alreadyDidExerciseYesterday(self):
yesterday = DateHelper.get_yesterday()
return (len(db.session.query(Exercise.id)
.filter(Exercise.user_id == self.id)
.filter(Exercise.date == yesterday)
.all()) > 0)
def __repr__():
return '<User %r><user %r>' % (self.name)
{% extends "base.html" %}
{% block logout %}<!-- remove btn logout -->{% endblock %}
{% block content %}
{% from "forms/macros.html" import render_field %}
<form method="POST" action="." class="form">
{{ form.csrf_token }}
{{ render_field(form.name, class="input text") }}
{{ render_field(form.email, class="input text") }}
{{ render_field(form.password, class="input text") }}
{{ render_field(form.confirm, class="input text") }}
<input type="submit" value="Register" class="button green" />
</form>
{% endblock %}
{% block footer %}
<a class="bt-action bt-action-user" href="{{ url_for('users.login') }}">
<span>Login</span>
</a>
{% endblock %}
# third party imports
from flask import g, session
# local application imports
from app.users.constants import SESSION_NAME_USER_ID
from app.users.models import User
def app_before_request():
# pull user's profile from the db before every request are treated
g.user = None
if SESSION_NAME_USER_ID in session and session[SESSION_NAME_USER_ID] is not None:
g.user = User.query.get(session[SESSION_NAME_USER_ID])
from app import app as application
application.run(debug=True,port=8090)
# third party imports
from flask import Blueprint, request, render_template, flash, g, session, redirect, url_for
from werkzeug import check_password_hash, generate_password_hash
from datetime import datetime
from sqlalchemy.sql import or_
# local application imports
from app import db
from app.users.forms import RegisterForm, LoginForm
from app.users.models import User
from app.users.requests import app_before_request
from app.users.decorators import requires_login
from app.users.constants import SESSION_NAME_USER_ID
from app.exercises.models import Exercise
mod = Blueprint('users', __name__, url_prefix='/users')
@mod.before_request
def before_request():
app_before_request()
@mod.route('/')
@mod.route('/me/')
@requires_login
def index():
return render_template('users/profile.html', user=g.user)
@mod.route('/login/', methods=['GET', 'POST'])
def login():
form = LoginForm(request.form)
# make sure data are valid, but doesn't validate password is right
if form.validate_on_submit():
user = User.query.filter_by(email=form.email.data).first()
# we use werzeug to validate user's password
if user and check_password_hash(user.password, form.password.data):
# the session can't be modified as it's signed,
# it's a safe place to store the user id
session[SESSION_NAME_USER_ID] = user.id
flash('Welcome %s' % user.name)
return redirect(url_for('users.index'))
flash('Wrong email or password', 'error-message')
return render_template( 'users/login.html', form=form)
@mod.route('/register/', methods=['GET', 'POST'])
def register():
form = RegisterForm(request.form)
if form.validate_on_submit():
userRegistered = User.query.filter(or_(User.name == form.name.data,
User.email == form.email.data)).first()
if userRegistered is not None:
flash('Email or user already is registered')
return render_template('users/register.html', form=form)
# create an user instance not yet stored in the database
user = User.NewUserToRegister(form.name.data, form.email.data,
generate_password_hash(form.password.data))
# insert the record in our database and commit it
db.session.add(user)
db.session.commit()
# log the user in, as he now has an id
session[SESSION_NAME_USER_ID] = user.id
# flash will display a message to the user
flash('Thanks for registering')
# redirect user to the 'index' method of the user module
return redirect(url_for('users.index'))
return render_template('users/register.html', form=form)
@mod.route('/logout/', methods=['GET'])
def logout():
# remove the username from the session if it's there
session.pop(SESSION_NAME_USER_ID, None)
# flash will display a message to the user
flash('Do not forget keep the exercises')
# redirect user to the 'index' method of the user module
return redirect(url_for('users.login'))
# This file contains the WSGI configuration required to serve up your
# web application at http://<your-username>.pythonanywhere.com/
# It works by setting the variable 'application' to a WSGI handler of some
# description.
#
# The below has been auto-generated for your Flask project
import sys
# add your project directory to the sys.path
project_home = u'/home/YOUR-USER-NAME/i-sweated-yesterday'
if project_home not in sys.path:
sys.path = [project_home] + sys.path
# import flask app but need to call it "application" for WSGI to work
#from flask_app import app as application #example
from app import app as application</your-username>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment