|
2 | 2 | from flask_sqlalchemy import SQLAlchemy |
3 | 3 | from flask_migrate import Migrate |
4 | 4 | from werkzeug.security import generate_password_hash, check_password_hash |
| 5 | +from sqlalchemy_utils import UUIDType |
5 | 6 | import os |
| 7 | +import uuid |
6 | 8 |
|
7 | 9 | # SQLAlchemy. |
8 | 10 | pg_user = os.getenv("POSTGRES_USER", "postgres") |
|
24 | 26 | db = SQLAlchemy(app) |
25 | 27 | migrate = Migrate(app, db) |
26 | 28 |
|
| 29 | +# Base model class for all models |
| 30 | +class BaseModel(db.Model): |
| 31 | + """ |
| 32 | + Base model of the mapping class inheritace |
| 33 | +
|
| 34 | + https://docs.sqlalchemy.org/en/14/orm/inheritance.html |
| 35 | + """ |
| 36 | + |
| 37 | + __abstract__ = True |
| 38 | + |
| 39 | + STATUS_CHOICES = ["Active", "Disabled", "Deleted"] |
| 40 | + |
| 41 | + id = db.Column(UUIDType(binary=False), primary_key=True, default=uuid.uuid4) |
| 42 | + name = db.Column(db.String(255), nullable=False) |
| 43 | + extra = db.Column(db.JSON(), nullable=True) |
| 44 | + status = db.Column( |
| 45 | + db.Enum(*STATUS_CHOICES, name="status", native_enum=False), |
| 46 | + nullable=False, |
| 47 | + default="active" |
| 48 | + ) |
27 | 49 |
|
28 | 50 | # User Model |
29 | | -class User(db.Model): |
| 51 | +class User(BaseModel): |
30 | 52 | id = db.Column(db.Integer, primary_key=True) |
31 | 53 | username = db.Column(db.String(80), unique=True, nullable=False) |
32 | 54 | password = db.Column(db.String(520), nullable=False) |
33 | 55 |
|
34 | 56 |
|
35 | | -class Student(db.Model): |
| 57 | +class Student(BaseModel): |
36 | 58 | id = db.Column(db.Integer, primary_key=True) |
37 | 59 | student_name = db.Column(db.String(200), nullable=False) |
38 | 60 | student_age = db.Column(db.Integer, nullable=True) |
@@ -78,7 +100,8 @@ def register(): |
78 | 100 | # create a new user |
79 | 101 | new_user = User( |
80 | 102 | username=username, |
81 | | - password=generate_password_hash(password), |
| 103 | + password=password, |
| 104 | + name=username, |
82 | 105 | ) |
83 | 106 | db.session.add(new_user) |
84 | 107 | db.session.commit() |
|
0 commit comments