2024-08-05 20:43:29 +02:00
|
|
|
from lernplattform import db
|
2024-08-08 11:55:21 +02:00
|
|
|
from sqlalchemy.sql import func
|
2024-08-05 20:43:29 +02:00
|
|
|
|
2024-08-06 19:06:09 +02:00
|
|
|
# Define a User model that represents the user table in the database
|
2024-08-05 20:43:29 +02:00
|
|
|
class User(db.Model):
|
2024-08-08 11:55:21 +02:00
|
|
|
"""
|
|
|
|
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')".
|
|
|
|
"""
|
2024-08-05 20:43:29 +02:00
|
|
|
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)
|
2024-08-08 11:55:21 +02:00
|
|
|
email_check = db.Column(db.String(10), nullable=True)
|
|
|
|
register_date = db.Column(db.DateTime, nullable=False, default=func.now())
|
2024-08-05 20:43:29 +02:00
|
|
|
|
2024-08-06 19:06:09 +02:00
|
|
|
# Magic method to represent the object as a string when printed or used in string context
|
2024-08-05 20:43:29 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return f"User('{self.username}', '{self.email}')"
|
|
|
|
|
2024-08-06 19:06:09 +02:00
|
|
|
# Function to initialize the database by creating all tables defined in models if they do not exist
|
2024-08-05 20:43:29 +02:00
|
|
|
def initialize_database():
|
|
|
|
from lernplattform import app
|
2024-08-06 19:06:09 +02:00
|
|
|
# Create an application context before running operations on the database within this function
|
2024-08-05 20:43:29 +02:00
|
|
|
with app.app_context():
|
|
|
|
db.create_all()
|