mirror of
https://github.com/wassname/flask-bootstrap.git
synced 2026-08-15 12:24:50 +08:00
Initial commit
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
from sqlalchemy import Column, Integer, String
|
||||
from database import Base
|
||||
import datetime
|
||||
from app import db
|
||||
|
||||
|
||||
##########################################
|
||||
|
||||
class Contact(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
title = db.Column(db.String(50))
|
||||
first_name = db.Column(db.String(50))
|
||||
middle_initial = db.Column(db.String(50))
|
||||
last_name = db.Column(db.String(50))
|
||||
phone = db.Column(db.String(50))
|
||||
email = db.Column(db.String(50))
|
||||
created_on = db.Column(db.DateTime)
|
||||
student = db.Column(db.String(50))
|
||||
|
||||
def __init__(self, title):
|
||||
self.title = title
|
||||
self.first_name = first_name
|
||||
self.middle_initial = middle_initial
|
||||
self.last_name = last_name
|
||||
self.student_class = student_class
|
||||
self.email = mail
|
||||
if created_on is None:
|
||||
created_on = datetime.utcnow()
|
||||
self.created_on = created_on
|
||||
|
||||
def __repr__(self):
|
||||
return '<Contact: %r>' % self.last_name
|
||||
|
||||
|
||||
##########################################
|
||||
|
||||
class Title(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
title = db.Column(db.String(50))
|
||||
|
||||
def __init__(self, title):
|
||||
self.title = title
|
||||
|
||||
def __repr__(self):
|
||||
return '<Title: %r>' % self.title
|
||||
@@ -0,0 +1,17 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import scoped_session, sessionmaker
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
|
||||
engine = create_engine('postgresql://username:password@127.0.0.1/app', convert_unicode=True)
|
||||
db_session = scoped_session(sessionmaker(autocommit=False,
|
||||
autoflush=False,
|
||||
bind=engine))
|
||||
Base = declarative_base()
|
||||
Base.query = db_session.query_property()
|
||||
|
||||
def init_db():
|
||||
# import all modules here that might define models so that
|
||||
# they will be registered properly on the metadata. Otherwise
|
||||
# you will have to import them first before calling init_db()
|
||||
import appmodels
|
||||
Base.metadata.create_all(bind=engine)
|
||||
@@ -0,0 +1,55 @@
|
||||
"""
|
||||
Module to provide plug-and-play authentication support for SQLAlchemy.
|
||||
"""
|
||||
|
||||
import datetime
|
||||
from sqlalchemy import Column, Integer, String, DateTime
|
||||
from flaskext.auth import AuthUser, get_current_user_data
|
||||
|
||||
def get_user_class(declarative_base):
|
||||
"""
|
||||
Factory function to create an SQLAlchemy User model with a declarative
|
||||
base (for example db.Model from the Flask-SQLAlchemy extension).
|
||||
"""
|
||||
class User(declarative_base, AuthUser):
|
||||
"""
|
||||
Implementation of User for SQLAlchemy.
|
||||
"""
|
||||
id = Column(Integer, primary_key=True)
|
||||
username = Column(String(80), unique=True, nullable=False)
|
||||
password = Column(String(120), nullable=False)
|
||||
salt = Column(String(80))
|
||||
role = Column(String(80))
|
||||
title = Column(String(50))
|
||||
email = Column(String(50))
|
||||
first_name = Column(String(50))
|
||||
middle_initial = Column(String(50))
|
||||
last_name = Column(String(50))
|
||||
created = Column(DateTime(), default=datetime.datetime.utcnow)
|
||||
modified = Column(DateTime())
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(User, self).__init__(*args, **kwargs)
|
||||
password = kwargs.get('password')
|
||||
if password is not None and not self.id:
|
||||
self.created = datetime.datetime.utcnow()
|
||||
# Initialize and encrypt password before first save.
|
||||
self.set_and_encrypt_password(password)
|
||||
|
||||
def __getstate__(self):
|
||||
return {
|
||||
'id': self.id,
|
||||
'username': self.username,
|
||||
'role': self.role,
|
||||
'created': self.created,
|
||||
'modified': self.modified,
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def load_current_user(cls, apply_timeout=True):
|
||||
data = get_current_user_data(apply_timeout)
|
||||
if not data:
|
||||
return None
|
||||
return cls.query.filter(cls.username==data['username']).one()
|
||||
|
||||
return User
|
||||
Reference in New Issue
Block a user