Updated Large app how to (markdown)

debrice 2012-02-02 22:09:36 -08:00
parent 79319e02c0
commit 0fd1547e67

@ -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.
* 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 '<User %r>' % (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