From 0fd1547e67953cf68fa5719169fc89e85a4c506f Mon Sep 17 00:00:00 2001 From: debrice Date: Thu, 2 Feb 2012 22:09:36 -0800 Subject: [PATCH] Updated Large app how to (markdown) --- Large-app-how-to.md | 67 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/Large-app-how-to.md b/Large-app-how-to.md index e240410..338b809 100644 --- a/Large-app-how-to.md +++ b/Large-app-how-to.md @@ -49,6 +49,7 @@ For every module (or sub app... ) well have this file structure (here for the us for every module that need templating (jinja) we store those in the templates folder + module directory. + app/templates/404.html app/templates/users/login.html app/templates/users/register.html ... @@ -111,4 +112,68 @@ We'll create 4 modules, a user module (manage user's registration, login, passwo * `SQLALCHEMY_DATABASE_URI` and `DATABASE_CONNECT_OPTIONS` are SQLAlchemy connection options (hard to guess ) * `THREAD_PAGE` my understanding was 2/core... might be wrong :) * `CSRF_ENABLED` `CSRF_SESSION_KEY` is protecting against form post fraud -* WTF comes with REPCAPTCHA field ready to use... just need to go to repcatcha website and get your public and private key. \ No newline at end of file +* WTF comes with REPCAPTCHA field ready to use... just need to go to repcatcha website and get your public and private key. + +## First module +We'll start with the users modules. In order, we'll define the models, the constants linked to this model, the form and finally the first view and it's template. + +### First model (and it's constants.py) +The `models.py` + + from app import db + from app.users import constants as USER + + class User(db.Model): + + __tablename__ = 'users_user' + 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.Integer, default=USER.USER) + status = db.Column(db.Integer, default=USER.NEW) + + def __init__(self, name=None, email=None, password=None): + self.name = name + self.email = email + self.password = password + + def getStatus(self): + return USER.STATUS[self.status] + + def getRole(self): + return USER.ROLE[self.role] + + def __repr__(self): + return '' % (self.name) + +and it's constants in the `constants.py` file: + + # 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', + } + +First about the constants file, I like to have my constants in its own file and inside my module for 2 main reasons. You're constants will probably be used in your models, forms and views. The second reason is that it's a better organization for you to find them. Also, importing your constants as the module in uppercase indicate the constant type and the module name (like USER for users.constants) will avoid you name conflicts. + +### First form + +### First view + + +