From 9de84e3fbae0f2893e9c4f1425afa06899959bf7 Mon Sep 17 00:00:00 2001 From: Cara Salter Date: Wed, 24 Aug 2022 13:15:31 -0400 Subject: flaskify --- app/__init__.py | 41 +++++++++++++++++++++++++++++++++ app/auth/__init__.py | 3 +++ app/auth/forms.py | 7 ++++++ app/config.py | 16 +++++++++++++ app/database.py | 25 +++++++++++++++++++++ app/static/gen/style.css | 46 +++++++++++++++++++++++++++++++++++++ app/static/scss/style.scss | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 194 insertions(+) create mode 100644 app/__init__.py create mode 100644 app/auth/__init__.py create mode 100644 app/auth/forms.py create mode 100644 app/config.py create mode 100644 app/database.py create mode 100644 app/static/gen/style.css create mode 100644 app/static/scss/style.scss (limited to 'app') diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..e929535 --- /dev/null +++ b/app/__init__.py @@ -0,0 +1,41 @@ + +from flask import Flask +from flask_security.datastore import SQLAlchemyUserDatastore +from flask_sqlalchemy import SQLAlchemy +from flask_migrate import Migrate +from flask_security.core import Security + +from flask_assets import Bundle, Environment + +from . import config + +db = SQLAlchemy() +migrate = Migrate() +security = Security() +environment = Environment() + + +def create_app(): + app = Flask(__name__) + + app.config.from_file("/etc/nccd/config.toml", load=config.load_config) + + db.init_app(app) + migrate.init_app(app, db) + environment.init_app(app) + + # Static file init + scss = Bundle('scss/style.scss', filters='scss', output='gen/style.css') + environment.register('scss', scss) + + from .database import User, Role + from .auth import forms as auth_forms + user_datastore = SQLAlchemyUserDatastore(db, User, Role) + security.init_app(app, user_datastore, register_form=auth_forms.ExtendedRegister) + + # Blueprints + app.register_blueprint(auth.bp) + + print(app.url_map) + + return app diff --git a/app/auth/__init__.py b/app/auth/__init__.py new file mode 100644 index 0000000..52f0cd7 --- /dev/null +++ b/app/auth/__init__.py @@ -0,0 +1,3 @@ +from flask import Blueprint + +bp = Blueprint('auth', __name__, url_prefix='/auth') diff --git a/app/auth/forms.py b/app/auth/forms.py new file mode 100644 index 0000000..4814f48 --- /dev/null +++ b/app/auth/forms.py @@ -0,0 +1,7 @@ +from flask_security.forms import RegisterForm +from wtforms import StringField +from wtforms.validators import DataRequired + +class ExtendedRegister(RegisterForm): + pref_name = StringField('Preferred Name', [DataRequired()]) + diff --git a/app/config.py b/app/config.py new file mode 100644 index 0000000..29eb2ef --- /dev/null +++ b/app/config.py @@ -0,0 +1,16 @@ +import toml + +def load_config(path): + contents = toml.load(path) + + result = { + 'SQLALCHEMY_DATABASE_URI': contents['database']['postgres_url'], + 'SECRET_KEY': contents['server']['secret_key'], + 'MAIL_SERVER': contents['email']['smtp_server'], + 'MAIL_PORT': contents['email']['smtp_port'], + 'MAIL_USE_TLS': contents['email']['smtp_tls'], + 'SECURITY_REGISTERABLE': True, + 'SECURITY_PASSWORD_SALT': contents['server']['secret_key'] + } + + return result diff --git a/app/database.py b/app/database.py new file mode 100644 index 0000000..24e1930 --- /dev/null +++ b/app/database.py @@ -0,0 +1,25 @@ +from sqlalchemy import Boolean, Column, DateTime, ForeignKey, Integer, String +from sqlalchemy.orm import relationship, backref +from . import db +from flask_security.core import RoleMixin, UserMixin + +class User(db.Model, UserMixin): + id = Column(String, primary_key=True) + email = Column(String, unique=True, nullable=False) + password = Column(String, nullable=False) + pref_name = Column(String, nullable=False) + last_login = Column(DateTime, nullable=False) + fs_uniquifier = Column(String, unique=True, nullable=False) + active = Column(Boolean, nullable=False) + roles = relationship('Role', secondary='roles_users',backref=backref('users', lazy='dynamic')) + +class Role(db.Model, RoleMixin): + id = Column(String, primary_key=True) + name = Column(String, unique=True) + description = Column(String) + +class RolesUsers(db.Model): + __tablename__ = "roles_users" + id = Column(Integer(), primary_key=True) + user_id = Column('user_id', String(), ForeignKey('user.id')) + role_id = Column('role_id', String(), ForeignKey('role.id')) diff --git a/app/static/gen/style.css b/app/static/gen/style.css new file mode 100644 index 0000000..6dd2994 --- /dev/null +++ b/app/static/gen/style.css @@ -0,0 +1,46 @@ +body { + background: #282828; + color: #ebdbb2; + font-family: monospace; +} + +a a:active, a:visited { + color: #458588; +} + +.container { + margin: auto; + width: 60%; +} + +button { + border-radius: 8px; + background-color: #458588; + border-color: #458588; + border: none; + margin: 0.5rem; +} + +button.accent { + background-color: #d79921; + border-color: #d79921; +} + +form { + width: 40%; +} + +label, +input { + display: inline-block; +} + +label { + width: 40%; + text-align: right; +} + +label + input { + width: 40%; + margin: 0 30% 0 4%; +} diff --git a/app/static/scss/style.scss b/app/static/scss/style.scss new file mode 100644 index 0000000..d7f50f7 --- /dev/null +++ b/app/static/scss/style.scss @@ -0,0 +1,56 @@ +$color-bg: #282828; +$color-fg: #ebdbb2; +$color-accent: #d79921; +$color-link: #458588; +$color-danger: #cc241d; +$font-family: monospace; + +body { + background: $color-bg; + color: $color-fg; + font-family: $font-family; +} + +a a:active, a:visited { + color: $color-link; +} + +.container { + margin: auto; + width: 60%; +} + +button { + border-radius: 8px; + background-color: $color-link; + border-color: $color-link; + border: none; + margin: 0.5rem; +} + +button.accent { + background-color: $color-accent; + border-color: $color-accent; +} + +// Forms +form { + width: 40%; +} + +label, +input { + display: inline-block; +} + +label { + width: 40%; + text-align: right; +} + +label+input { + width: 40%; + + margin: 0 30% 0 4%; +} + -- cgit v1.2.3