mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-02-05 10:53:28 -05:00
108536234: created users and roles data and domain model.
You will need to run the /scripts/bootstrap.sh to create the database for test and the app.
This commit is contained in:
@@ -1,7 +1,14 @@
|
||||
import os
|
||||
|
||||
from flask import Flask
|
||||
from config import configs
|
||||
from flask._compat import string_types
|
||||
from flask.ext import assets
|
||||
from flask.ext.sqlalchemy import SQLAlchemy
|
||||
from webassets.filter import get_filter
|
||||
|
||||
from config import configs
|
||||
|
||||
db = SQLAlchemy()
|
||||
|
||||
|
||||
def create_app(config_name):
|
||||
@@ -9,6 +16,7 @@ def create_app(config_name):
|
||||
|
||||
application.config['NOTIFY_API_ENVIRONMENT'] = config_name
|
||||
application.config.from_object(configs[config_name])
|
||||
db.init_app(application)
|
||||
init_app(application)
|
||||
|
||||
from app.main import main as main_blueprint
|
||||
@@ -22,6 +30,77 @@ def init_app(app):
|
||||
if key in os.environ:
|
||||
app.config[key] = convert_to_boolean(os.environ[key])
|
||||
|
||||
init_asset_environment(app)
|
||||
|
||||
|
||||
def init_asset_environment(app):
|
||||
env = assets.Environment(app)
|
||||
|
||||
# Tell flask-assets where to look for our sass files.
|
||||
env.load_path = [
|
||||
os.path.join(os.path.dirname(__file__), 'assets/stylesheets'),
|
||||
os.path.join(os.path.dirname(__file__), 'assets'),
|
||||
os.path.join(os.path.dirname(__file__), 'assets/stylesheets/stylesheets/govuk_frontend_toolkit'),
|
||||
os.path.join(os.path.dirname(__file__), 'assets/stylesheets/govuk_template')
|
||||
|
||||
]
|
||||
|
||||
scss = get_filter('scss', as_output=True)
|
||||
|
||||
env.register(
|
||||
'css_all',
|
||||
assets.Bundle(
|
||||
'main.scss',
|
||||
filters='scss',
|
||||
output='css_all.css'
|
||||
)
|
||||
)
|
||||
|
||||
env.register(
|
||||
'css_govuk-template',
|
||||
assets.Bundle(
|
||||
'govuk_template/govuk-template.scss',
|
||||
filters='scss',
|
||||
output='stylesheets/govuk-template.css'
|
||||
)
|
||||
)
|
||||
|
||||
env.register(
|
||||
'css_govuk-template-ie6',
|
||||
assets.Bundle(
|
||||
'govuk_template/govuk-template-ie6.scss',
|
||||
filters='scss',
|
||||
output='stylesheets/govuk-template-ie6.css'
|
||||
)
|
||||
)
|
||||
|
||||
env.register(
|
||||
'css_govuk-template-ie7',
|
||||
assets.Bundle(
|
||||
'govuk_template/govuk-template-ie7.scss',
|
||||
filters='scss',
|
||||
output='stylesheets/govuk-template-ie7.css'
|
||||
)
|
||||
)
|
||||
|
||||
env.register(
|
||||
'css_govuk-template-ie8',
|
||||
assets.Bundle(
|
||||
'govuk_template/govuk-template-ie8.scss',
|
||||
filters='scss',
|
||||
output='stylesheets/govuk-template-ie8.css'
|
||||
)
|
||||
)
|
||||
|
||||
env.register(
|
||||
'css_govuk-template-print',
|
||||
assets.Bundle(
|
||||
'govuk_template/govuk-template-print.scss',
|
||||
filters='scss',
|
||||
output='stylesheets/govuk-template-print.css'
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def convert_to_boolean(value):
|
||||
if isinstance(value, string_types):
|
||||
|
||||
0
app/main/dao/__init__.py
Normal file
0
app/main/dao/__init__.py
Normal file
11
app/main/dao/roles_dao.py
Normal file
11
app/main/dao/roles_dao.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from app import db
|
||||
from app.models import Roles
|
||||
|
||||
|
||||
def insert_role(role):
|
||||
db.session.add(role)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def get_role_by_id(id):
|
||||
return Roles.query.filter_by(id=id).first()
|
||||
11
app/main/dao/users_dao.py
Normal file
11
app/main/dao/users_dao.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from app import db
|
||||
from app.models import Users
|
||||
|
||||
|
||||
def insert_user(user):
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def get_user_by_id(id):
|
||||
return Users.query.filter_by(id=id).first()
|
||||
50
app/models.py
Normal file
50
app/models.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from app import db
|
||||
from flask import current_app
|
||||
|
||||
DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"
|
||||
DATE_FORMAT = "%Y-%m-%d"
|
||||
|
||||
|
||||
class Roles(db.Model):
|
||||
__tablename__ = 'roles'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
role = db.Column(db.String, nullable=False, unique=True)
|
||||
|
||||
|
||||
class Users(db.Model):
|
||||
__tablename__ = 'users'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String, nullable=False, index=True)
|
||||
email_address = db.Column(db.String(255), nullable=False, index=True)
|
||||
password = db.Column(db.String, index=False, unique=False, nullable=False)
|
||||
mobile_number = db.Column(db.String, index=False, unique=False, nullable=False)
|
||||
created_at = db.Column(db.DateTime, index=False, unique=False, nullable=False)
|
||||
updated_at = db.Column(db.DateTime, index=False, unique=False, nullable=True)
|
||||
password_changed_at = db.Column(db.DateTime, index=False, unique=False, nullable=True)
|
||||
role_id = db.Column(db.Integer, db.ForeignKey('roles.id'), index=True, unique=False, nullable=False)
|
||||
logged_in_at = db.Column(db.DateTime, nullable=True)
|
||||
failed_login_count = db.Column(db.Integer, nullable=False, default=0)
|
||||
state = db.Column(db.String, nullable=False, default='pending')
|
||||
|
||||
def serialize(self):
|
||||
serialized = {
|
||||
'id': self.id,
|
||||
'name': self.name,
|
||||
'emailAddress': self.email_address,
|
||||
'locked': self.failed_login_count > current_app.config['MAX_FAILED_LOGIN_COUNT'],
|
||||
'createdAt': self.created_at.strftime(DATETIME_FORMAT),
|
||||
'updatedAt': self.updated_at.strftime(DATETIME_FORMAT),
|
||||
'role': self.role,
|
||||
'passwordChangedAt': self.password_changed_at.strftime(DATETIME_FORMAT),
|
||||
'failedLoginCount': self.failed_login_count
|
||||
}
|
||||
|
||||
return filter_null_value_fields(serialized)
|
||||
|
||||
|
||||
def filter_null_value_fields(obj):
|
||||
return dict(
|
||||
filter(lambda x: x[1] is not None, obj.items())
|
||||
)
|
||||
Reference in New Issue
Block a user