Spaces:
Runtime error
Runtime error
Download apps/core/models.py from junaidte14/django-admin-dashboard: direct link, hf CLI and curl.
- Browser
- Download file 8.56 kB
-
https://huggingface.co/spaces/junaidte14/django-admin-dashboard/resolve/main/apps/core/models.py
- Command line
-
hf download hf://spaces/junaidte14/django-admin-dashboard/apps/core/models.py
-
curl -L -o models.py https://huggingface.co/spaces/junaidte14/django-admin-dashboard/resolve/main/apps/core/models.py
8.56 kB
| """ | |
| Core models that map to existing FastAPI database tables. | |
| These are 'unmanaged' models - Django reads from them but doesn't create/modify tables. | |
| """ | |
| from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin | |
| from django.db import models | |
| from django.utils import timezone | |
| from django.contrib.auth.hashers import make_password, check_password | |
| class UserManager(BaseUserManager): | |
| """Custom manager for shared FastAPI/Django users.""" | |
| use_in_migrations = False | |
| def get_by_natural_key(self, username): | |
| return self.get(username=username) | |
| def create_user(self, username, email, password=None, **extra_fields): | |
| if not username: | |
| raise ValueError("Username is required") | |
| if not email: | |
| raise ValueError("Email is required") | |
| email = self.normalize_email(email) | |
| user = self.model( | |
| username=username, | |
| email=email, | |
| **extra_fields | |
| ) | |
| if password: | |
| user.set_password(password) | |
| user.save(using=self._db) | |
| return user | |
| def create_superuser(self, username, email, password=None, **extra_fields): | |
| extra_fields.setdefault("is_staff", True) | |
| extra_fields.setdefault("is_superuser", True) | |
| extra_fields.setdefault("is_active", True) | |
| return self.create_user( | |
| username, | |
| email, | |
| password, | |
| **extra_fields | |
| ) | |
| class User(AbstractBaseUser, PermissionsMixin): | |
| """ | |
| User model mapping to FastAPI's users table. | |
| """ | |
| id = models.AutoField(primary_key=True) | |
| username = models.CharField(max_length=150, unique=True) | |
| email = models.EmailField(unique=True) | |
| hashed_password = models.CharField(max_length=255, db_column='hashed_password') | |
| role = models.CharField(max_length=50, default='user') | |
| is_active = models.BooleanField(default=True) | |
| created_at = models.DateTimeField(default=timezone.now) | |
| # Django admin fields | |
| is_staff = models.BooleanField(default=False) | |
| is_superuser = models.BooleanField(default=False) | |
| objects = UserManager() | |
| USERNAME_FIELD = 'username' | |
| REQUIRED_FIELDS = ['email'] | |
| class Meta: | |
| managed = False # Don't let Django manage this table | |
| db_table = 'users' | |
| ordering = ['-created_at'] | |
| def __str__(self): | |
| return self.username | |
| # ------------------------- | |
| # Django compatibility only | |
| # ------------------------- | |
| def is_authenticated(self): | |
| return True | |
| def is_anonymous(self): | |
| return False | |
| def get_username(self): | |
| return self.username | |
| # ------------------------- | |
| # Password helpers | |
| # ------------------------- | |
| def set_password(self, raw_password): | |
| self.hashed_password = make_password(raw_password) | |
| def check_password(self, raw_password): | |
| return check_password(raw_password, self.hashed_password) | |
| def save(self, *args, **kwargs): | |
| """Override save to handle password hashing.""" | |
| # If password was set directly, hash it | |
| if hasattr(self, '_password_to_hash'): | |
| self.set_password(self._password_to_hash) | |
| delattr(self, '_password_to_hash') | |
| super().save(*args, **kwargs) | |
| class Conversation(models.Model): | |
| id = models.AutoField(primary_key=True) # Changed from CharField to AutoField | |
| user = models.ForeignKey(User, on_delete=models.CASCADE, db_column='user_id', related_name='conversations') | |
| title = models.CharField(max_length=255, default="New Conversation") | |
| model_name = models.CharField(max_length=50) | |
| metadata_json = models.TextField(null=True, blank=True) # Matches SQLAlchemy Text column | |
| created_at = models.DateTimeField(default=timezone.now) | |
| updated_at = models.DateTimeField(auto_now=True) | |
| class Meta: | |
| managed = False | |
| db_table = 'conversations' | |
| ordering = ['-updated_at'] | |
| def __str__(self): | |
| return f"{self.title or 'Untitled'} - User {self.user_id}" | |
| class Message(models.Model): | |
| id = models.AutoField(primary_key=True) | |
| conversation = models.ForeignKey(Conversation, on_delete=models.CASCADE, db_column='conversation_id', related_name='messages') | |
| role = models.CharField(max_length=20) # user, assistant, system | |
| content = models.TextField() | |
| metadata_json = models.TextField(null=True, blank=True) | |
| prompt_tokens = models.IntegerField(null=True, blank=True) | |
| completion_tokens = models.IntegerField(null=True, blank=True) | |
| total_tokens = models.IntegerField(null=True, blank=True) | |
| created_at = models.DateTimeField(default=timezone.now) | |
| class Meta: | |
| managed = False | |
| db_table = 'messages' | |
| def __str__(self): | |
| return f"{self.role}: {self.content[:50]}..." | |
| class RAGCollection: | |
| """ | |
| Virtual model - NOT DB backed. | |
| Collections are derived from document_embeddings. | |
| """ | |
| def __init__(self, name, document_count=0): | |
| self.name = name | |
| self.document_count = document_count | |
| self.status = "active" | |
| class RAGSource(models.Model): | |
| id = models.AutoField(primary_key=True) | |
| user = models.ForeignKey(User, on_delete=models.CASCADE, db_column='user_id') | |
| collection_name = models.CharField(max_length=255) | |
| source_type = models.CharField(max_length=50) # manual, github | |
| source_identifier = models.CharField(max_length=500) | |
| display_name = models.CharField(max_length=255) | |
| auto_sync = models.BooleanField(default=False) | |
| sync_interval_hours = models.IntegerField(default=24) | |
| last_sync_at = models.DateTimeField(null=True, blank=True) | |
| last_sync_status = models.CharField(max_length=50, default='pending') | |
| last_sync_error = models.CharField(max_length=1000, null=True, blank=True) | |
| total_files = models.IntegerField(default=0) | |
| total_chunks = models.IntegerField(default=0) | |
| last_commit_sha = models.CharField(max_length=255, null=True, blank=True) | |
| config = models.TextField(null=True, blank=True) | |
| created_at = models.DateTimeField(default=timezone.now) | |
| updated_at = models.DateTimeField(auto_now=True) | |
| class Meta: | |
| managed = False | |
| db_table = 'rag_sources' | |
| def __str__(self): | |
| return f"{self.source_type}: {self.source_url}" | |
| class Agent(models.Model): | |
| id = models.AutoField(primary_key=True) | |
| name = models.CharField(max_length=255) | |
| agent_type = models.CharField(max_length=50) # order_taking, appointment_booking, etc. | |
| description = models.TextField(null=True, blank=True) | |
| status = models.CharField(max_length=20, default='draft') # active, inactive, draft | |
| user = models.ForeignKey(User, on_delete=models.CASCADE, db_column='user_id') # Fixed from created_by_id | |
| config = models.TextField(null=True, blank=True) | |
| welcome_message = models.TextField(null=True, blank=True) | |
| completion_message = models.TextField(null=True, blank=True) | |
| error_message = models.TextField(null=True, blank=True) | |
| max_retries = models.IntegerField(default=3) | |
| timeout_minutes = models.IntegerField(default=30) | |
| enable_fallback_human = models.BooleanField(default=False) | |
| created_at = models.DateTimeField(default=timezone.now) | |
| updated_at = models.DateTimeField(auto_now=True) | |
| class Meta: | |
| managed = False | |
| db_table = 'agents' | |
| def __str__(self): | |
| return self.name | |
| class AgentSession(models.Model): | |
| id = models.AutoField(primary_key=True) | |
| agent = models.ForeignKey(Agent, on_delete=models.CASCADE, db_column='agent_id') | |
| conversation = models.ForeignKey(Conversation, on_delete=models.SET_NULL, null=True, db_column='conversation_id') | |
| user_id = models.IntegerField(null=True, blank=True) | |
| status = models.CharField(max_length=20, default='in_progress') # Match SQLAlchemy enum: in_progress | |
| current_step = models.CharField(max_length=100, null=True, blank=True) | |
| collected_data = models.TextField(null=True, blank=True) | |
| retry_count = models.IntegerField(default=0) | |
| started_at = models.DateTimeField(default=timezone.now) | |
| completed_at = models.DateTimeField(null=True, blank=True) | |
| last_activity_at = models.DateTimeField(default=timezone.now) | |
| metadata = models.TextField(null=True, blank=True, db_column='metadata') | |
| class Meta: | |
| managed = False | |
| db_table = 'agent_sessions' | |
| def __str__(self): | |
| return f"Session {self.id} - Agent {self.agent_id}" | |