adding docstring to classes
This commit is contained in:
@@ -1,40 +1,47 @@
|
||||
from datetime import datetime
|
||||
# models/database/user.py
|
||||
"""
|
||||
User model with authentication support.
|
||||
|
||||
from sqlalchemy import (Boolean, Column, DateTime, Float, ForeignKey, Index,
|
||||
Integer, String, Text, UniqueConstraint)
|
||||
This module defines the User model which includes fields for user details,
|
||||
authentication information, and relationships to other models such as Vendor and Customer.
|
||||
"""
|
||||
from sqlalchemy import Boolean, Column, DateTime, Integer, String
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
# Import Base from the central database module instead of creating a new one
|
||||
from app.core.database import Base
|
||||
from models.database.base import TimestampMixin
|
||||
|
||||
class User(Base, TimestampMixin):
|
||||
__tablename__ = "users"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
email = Column(String, unique=True, index=True, nullable=False)
|
||||
username = Column(String, unique=True, index=True, nullable=False)
|
||||
first_name = Column(String)
|
||||
last_name = Column(String)
|
||||
hashed_password = Column(String, nullable=False)
|
||||
role = Column(String, nullable=False, default="user") # user, admin, vendor_owner TODO: Change to customer, vendor, admin?
|
||||
is_active = Column(Boolean, default=True, nullable=False)
|
||||
last_login = Column(DateTime, nullable=True)
|
||||
class User(Base, TimestampMixin):
|
||||
"""Represents a user in the system."""
|
||||
|
||||
__tablename__ = "users" # Name of the table in the database
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True) # Primary key and indexed column for user ID
|
||||
email = Column(String, unique=True, index=True, nullable=False) # Unique, indexed, non-nullable email column
|
||||
username = Column(String, unique=True, index=True, nullable=False) # Unique, indexed, non-nullable username column
|
||||
first_name = Column(String) # Optional first name column
|
||||
last_name = Column(String) # Optional last name column
|
||||
hashed_password = Column(String, nullable=False) # Non-nullable hashed password column
|
||||
role = Column(String, nullable=False, default="user") # Role of the user (default is 'user') //TODO: Change to customer, vendor, admin
|
||||
is_active = Column(Boolean, default=True, nullable=False) # Active status of the user (default is True)
|
||||
last_login = Column(DateTime, nullable=True) # Optional last login timestamp column
|
||||
|
||||
# Relationships
|
||||
marketplace_import_jobs = relationship(
|
||||
"MarketplaceImportJob", back_populates="user"
|
||||
)
|
||||
|
||||
# Vendor relationships
|
||||
owned_vendors = relationship("Vendor", back_populates="owner")
|
||||
vendor_memberships = relationship("VendorUser", foreign_keys="[VendorUser.user_id]", back_populates="user")
|
||||
marketplace_import_jobs = relationship("MarketplaceImportJob",
|
||||
back_populates="user") # Relationship with import jobs
|
||||
owned_vendors = relationship("Vendor", back_populates="owner") # Relationship with vendors owned by this user
|
||||
vendor_memberships = relationship("VendorUser", foreign_keys="[VendorUser.user_id]",
|
||||
back_populates="user") # Relationship with vendor memberships
|
||||
|
||||
def __repr__(self):
|
||||
"""String representation of the User object."""
|
||||
return f"<User(id={self.id}, username='{self.username}', email='{self.email}', role='{self.role}')>"
|
||||
|
||||
@property
|
||||
def full_name(self):
|
||||
"""Returns the full name of the user, combining first and last names if available."""
|
||||
if self.first_name and self.last_name:
|
||||
return f"{self.first_name} {self.last_name}"
|
||||
return self.username
|
||||
return self.username
|
||||
|
||||
Reference in New Issue
Block a user