Lerndatenbank/lernplattform/models.py
2024-08-08 11:55:21 +02:00

37 lines
1.7 KiB
Python

from lernplattform import db
from sqlalchemy.sql import func
# Define a User model that represents the user table in the database
class User(db.Model):
"""
A class representing the user table in the database.
Attributes:
id (int): The unique identifier for each user, set as the primary key.
username (str): The username of the user, must be unique and cannot be null.
email (str): The email address of the user, must be unique and cannot be null.
password (str): The hashed password of the user, cannot be null.
email_check (str): A string field to store email verification status.
register_date (datetime): The date and time when the user registered, defaults to the current timestamp.
Methods:
__repr__(): Returns a string representation of the User object in the format "User('username', 'email')".
"""
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
password = db.Column(db.String(60), nullable=False)
email_check = db.Column(db.String(10), nullable=True)
register_date = db.Column(db.DateTime, nullable=False, default=func.now())
# Magic method to represent the object as a string when printed or used in string context
def __repr__(self):
return f"User('{self.username}', '{self.email}')"
# Function to initialize the database by creating all tables defined in models if they do not exist
def initialize_database():
from lernplattform import app
# Create an application context before running operations on the database within this function
with app.app_context():
db.create_all()