48 lines
2.4 KiB
Python
48 lines
2.4 KiB
Python
# models/database/user.py
|
|
"""
|
|
User model with authentication support.
|
|
|
|
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):
|
|
"""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") # 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
|