From 61fe581df6f47d25cb1f5f575963d472e3928d93 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Thu, 28 Jun 2018 14:41:14 +0200 Subject: [PATCH] Create the User and Role models --- .gitignore | 1 + app/__init__.py | 3 +++ app/config_dev.py | 2 ++ app/models.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 app/models.py diff --git a/.gitignore b/.gitignore index c18dd8d..04b7699 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ __pycache__/ +app/local.db diff --git a/app/__init__.py b/app/__init__.py index a3f09b4..84bef46 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -21,10 +21,13 @@ from flask import Flask, render_template class CalendarSocialApp(Flask): def __init__(self, name, config=None): + from app.models import db + Flask.__init__(self, name) config_name = os.environ.get('ENV', config or 'dev') self.config.from_pyfile(f'config_{config_name}.py', True) + db.init_app(self) app = CalendarSocialApp(__name__) diff --git a/app/config_dev.py b/app/config_dev.py index 88c898f..4f7ec9c 100644 --- a/app/config_dev.py +++ b/app/config_dev.py @@ -1,2 +1,4 @@ DEBUG = True ENV = 'dev' +SQLALCHEMY_DATABASE_URI = 'sqlite:///local.db' +SQLALCHEMY_TRACK_MODIFICATIONS = False diff --git a/app/models.py b/app/models.py new file mode 100644 index 0000000..13c0ffd --- /dev/null +++ b/app/models.py @@ -0,0 +1,53 @@ +from datetime import datetime + +from flask_security import UserMixin, RoleMixin +from flask_security.utils import hash_password +from flask_sqlalchemy import SQLAlchemy + +db = SQLAlchemy() +users_roles = db.Table( + 'users_roles', + db.Column('user_id', db.Integer(), db.ForeignKey('users.id')), + db.Column('role_id', db.Integer(), db.ForeignKey('roles.id'))) + + +class User(db.Model, UserMixin): + __tablename__ = 'users' + id = db.Column(db.Integer(), primary_key=True) + + #: The username of the user. This is also the display name and thus is immutable + username = db.Column(db.String(length=50), unique=True, nullable=False) + + #: The email address of the user + email = db.Column(db.String(length=255), unique=True, nullable=True) + + #: The (hashed) password of the user + password = db.Column(db.String(length=255)) + + #: A flag to show whether the user is enabled (active) or not + active = db.Column(db.Boolean(), default=False) + + #: The timestamp when this user was created + created_at = db.Column(db.DateTime(), default=datetime.utcnow) + + #: The timestamp when the user was activated + confirmed_at = db.Column(db.DateTime()) + + #: The roles of the user + roles = db.relationship('Role', + secondary=users_roles, + backref=db.backref('users', lazy='dynamic')) + + def __str__(self): + return f'' + + +class Role(db.Model, RoleMixin): + __tablename__ = 'roles' + id = db.Column(db.Integer(), primary_key=True) + + #: The name of the role + name = db.Column(db.Unicode(length=80), unique=True) + + #: A description of the role + description = db.Column(db.UnicodeText)