mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 08:31:12 -05:00
feat: Core models implementation - Phase 1 complete
Settings Configuration: - Split settings into base.py, local.py, production.py - Configured all 60+ installed packages - Set up PostgreSQL, Redis, Celery, Channels - Configured caching, sessions, logging - Added security settings for production Core Models (apps/core/models.py): - BaseModel: UUID primary key + timestamps + lifecycle hooks - VersionedModel: Automatic version tracking with DirtyFieldsMixin - Country, Subdivision, Locality: Location reference data - DatePrecisionMixin: Track date precision (year/month/day) - SoftDeleteMixin: Soft-delete functionality - ActiveManager & AllObjectsManager: Query managers User Models (apps/users/models.py): - Custom User model with UUID, email-based auth - OAuth support (Google, Discord) - MFA support fields - Ban/unban functionality - UserRole: Role-based permissions (user/moderator/admin) - UserProfile: Extended user info and preferences App Structure: - Created 7 Django apps with proper configs - Set up migrations for core and users apps - All migrations applied successfully to SQLite Testing: - Django check passes with only 1 warning (static dir) - Database migrations successful - Ready for entity models (Park, Ride, Company) Next: Implement entity models for parks, rides, companies
This commit is contained in:
0
django/apps/__init__.py
Normal file
0
django/apps/__init__.py
Normal file
BIN
django/apps/__pycache__/__init__.cpython-313.pyc
Normal file
BIN
django/apps/__pycache__/__init__.cpython-313.pyc
Normal file
Binary file not shown.
0
django/apps/core/__init__.py
Normal file
0
django/apps/core/__init__.py
Normal file
BIN
django/apps/core/__pycache__/__init__.cpython-313.pyc
Normal file
BIN
django/apps/core/__pycache__/__init__.cpython-313.pyc
Normal file
Binary file not shown.
BIN
django/apps/core/__pycache__/apps.cpython-313.pyc
Normal file
BIN
django/apps/core/__pycache__/apps.cpython-313.pyc
Normal file
Binary file not shown.
BIN
django/apps/core/__pycache__/models.cpython-313.pyc
Normal file
BIN
django/apps/core/__pycache__/models.cpython-313.pyc
Normal file
Binary file not shown.
11
django/apps/core/apps.py
Normal file
11
django/apps/core/apps.py
Normal file
@@ -0,0 +1,11 @@
|
||||
"""
|
||||
Core app configuration.
|
||||
"""
|
||||
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class CoreConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'apps.core'
|
||||
verbose_name = 'Core'
|
||||
194
django/apps/core/migrations/0001_initial.py
Normal file
194
django/apps/core/migrations/0001_initial.py
Normal file
@@ -0,0 +1,194 @@
|
||||
# Generated by Django 4.2.8 on 2025-11-08 16:35
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import django.utils.timezone
|
||||
import django_lifecycle.mixins
|
||||
import model_utils.fields
|
||||
import uuid
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = []
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name="Country",
|
||||
fields=[
|
||||
(
|
||||
"created",
|
||||
model_utils.fields.AutoCreatedField(
|
||||
default=django.utils.timezone.now,
|
||||
editable=False,
|
||||
verbose_name="created",
|
||||
),
|
||||
),
|
||||
(
|
||||
"modified",
|
||||
model_utils.fields.AutoLastModifiedField(
|
||||
default=django.utils.timezone.now,
|
||||
editable=False,
|
||||
verbose_name="modified",
|
||||
),
|
||||
),
|
||||
(
|
||||
"id",
|
||||
models.UUIDField(
|
||||
default=uuid.uuid4,
|
||||
editable=False,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
),
|
||||
),
|
||||
("name", models.CharField(max_length=255, unique=True)),
|
||||
(
|
||||
"code",
|
||||
models.CharField(
|
||||
help_text="ISO 3166-1 alpha-2 country code",
|
||||
max_length=2,
|
||||
unique=True,
|
||||
),
|
||||
),
|
||||
(
|
||||
"code3",
|
||||
models.CharField(
|
||||
blank=True,
|
||||
help_text="ISO 3166-1 alpha-3 country code",
|
||||
max_length=3,
|
||||
),
|
||||
),
|
||||
],
|
||||
options={
|
||||
"verbose_name_plural": "countries",
|
||||
"db_table": "countries",
|
||||
"ordering": ["name"],
|
||||
},
|
||||
bases=(django_lifecycle.mixins.LifecycleModelMixin, models.Model),
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name="Subdivision",
|
||||
fields=[
|
||||
(
|
||||
"created",
|
||||
model_utils.fields.AutoCreatedField(
|
||||
default=django.utils.timezone.now,
|
||||
editable=False,
|
||||
verbose_name="created",
|
||||
),
|
||||
),
|
||||
(
|
||||
"modified",
|
||||
model_utils.fields.AutoLastModifiedField(
|
||||
default=django.utils.timezone.now,
|
||||
editable=False,
|
||||
verbose_name="modified",
|
||||
),
|
||||
),
|
||||
(
|
||||
"id",
|
||||
models.UUIDField(
|
||||
default=uuid.uuid4,
|
||||
editable=False,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
),
|
||||
),
|
||||
("name", models.CharField(max_length=255)),
|
||||
(
|
||||
"code",
|
||||
models.CharField(
|
||||
help_text="ISO 3166-2 subdivision code (without country prefix)",
|
||||
max_length=10,
|
||||
),
|
||||
),
|
||||
(
|
||||
"subdivision_type",
|
||||
models.CharField(
|
||||
blank=True,
|
||||
help_text="Type of subdivision (state, province, region, etc.)",
|
||||
max_length=50,
|
||||
),
|
||||
),
|
||||
(
|
||||
"country",
|
||||
models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="subdivisions",
|
||||
to="core.country",
|
||||
),
|
||||
),
|
||||
],
|
||||
options={
|
||||
"db_table": "subdivisions",
|
||||
"ordering": ["country", "name"],
|
||||
"unique_together": {("country", "code")},
|
||||
},
|
||||
bases=(django_lifecycle.mixins.LifecycleModelMixin, models.Model),
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name="Locality",
|
||||
fields=[
|
||||
(
|
||||
"created",
|
||||
model_utils.fields.AutoCreatedField(
|
||||
default=django.utils.timezone.now,
|
||||
editable=False,
|
||||
verbose_name="created",
|
||||
),
|
||||
),
|
||||
(
|
||||
"modified",
|
||||
model_utils.fields.AutoLastModifiedField(
|
||||
default=django.utils.timezone.now,
|
||||
editable=False,
|
||||
verbose_name="modified",
|
||||
),
|
||||
),
|
||||
(
|
||||
"id",
|
||||
models.UUIDField(
|
||||
default=uuid.uuid4,
|
||||
editable=False,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
),
|
||||
),
|
||||
("name", models.CharField(max_length=255)),
|
||||
(
|
||||
"latitude",
|
||||
models.DecimalField(
|
||||
blank=True, decimal_places=6, max_digits=9, null=True
|
||||
),
|
||||
),
|
||||
(
|
||||
"longitude",
|
||||
models.DecimalField(
|
||||
blank=True, decimal_places=6, max_digits=9, null=True
|
||||
),
|
||||
),
|
||||
(
|
||||
"subdivision",
|
||||
models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="localities",
|
||||
to="core.subdivision",
|
||||
),
|
||||
),
|
||||
],
|
||||
options={
|
||||
"verbose_name_plural": "localities",
|
||||
"db_table": "localities",
|
||||
"ordering": ["subdivision", "name"],
|
||||
"indexes": [
|
||||
models.Index(
|
||||
fields=["subdivision", "name"],
|
||||
name="localities_subdivi_675d5a_idx",
|
||||
)
|
||||
],
|
||||
},
|
||||
bases=(django_lifecycle.mixins.LifecycleModelMixin, models.Model),
|
||||
),
|
||||
]
|
||||
0
django/apps/core/migrations/__init__.py
Normal file
0
django/apps/core/migrations/__init__.py
Normal file
Binary file not shown.
BIN
django/apps/core/migrations/__pycache__/__init__.cpython-313.pyc
Normal file
BIN
django/apps/core/migrations/__pycache__/__init__.cpython-313.pyc
Normal file
Binary file not shown.
264
django/apps/core/models.py
Normal file
264
django/apps/core/models.py
Normal file
@@ -0,0 +1,264 @@
|
||||
"""
|
||||
Core base models and utilities for ThrillWiki.
|
||||
These abstract models provide common functionality for all entities.
|
||||
"""
|
||||
|
||||
import uuid
|
||||
from django.db import models
|
||||
from model_utils.models import TimeStampedModel
|
||||
from django_lifecycle import LifecycleModel, hook, AFTER_CREATE, AFTER_UPDATE
|
||||
from dirtyfields import DirtyFieldsMixin
|
||||
|
||||
|
||||
class BaseModel(LifecycleModel, TimeStampedModel):
|
||||
"""
|
||||
Abstract base model for all entities.
|
||||
|
||||
Provides:
|
||||
- UUID primary key
|
||||
- created_at and updated_at timestamps (from TimeStampedModel)
|
||||
- Lifecycle hooks for versioning
|
||||
"""
|
||||
id = models.UUIDField(
|
||||
primary_key=True,
|
||||
default=uuid.uuid4,
|
||||
editable=False
|
||||
)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.__class__.__name__}({self.id})"
|
||||
|
||||
|
||||
class VersionedModel(DirtyFieldsMixin, BaseModel):
|
||||
"""
|
||||
Abstract base model for entities that need version tracking.
|
||||
|
||||
Automatically creates a version record whenever the model is created or updated.
|
||||
Uses DirtyFieldsMixin to track which fields changed.
|
||||
"""
|
||||
|
||||
@hook(AFTER_CREATE)
|
||||
def create_version_on_create(self):
|
||||
"""Create initial version when entity is created"""
|
||||
self._create_version('created')
|
||||
|
||||
@hook(AFTER_UPDATE)
|
||||
def create_version_on_update(self):
|
||||
"""Create version when entity is updated"""
|
||||
if self.get_dirty_fields():
|
||||
self._create_version('updated')
|
||||
|
||||
def _create_version(self, change_type):
|
||||
"""
|
||||
Create a version record for this entity.
|
||||
Deferred import to avoid circular dependencies.
|
||||
"""
|
||||
try:
|
||||
from apps.versioning.services import VersionService
|
||||
VersionService.create_version(
|
||||
entity=self,
|
||||
change_type=change_type,
|
||||
changed_fields=self.get_dirty_fields() if change_type == 'updated' else {}
|
||||
)
|
||||
except ImportError:
|
||||
# Versioning app not yet available (e.g., during initial migrations)
|
||||
pass
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
|
||||
# Location Models
|
||||
|
||||
class Country(BaseModel):
|
||||
"""
|
||||
Country reference data (ISO 3166-1).
|
||||
|
||||
Examples: United States, Canada, United Kingdom, etc.
|
||||
"""
|
||||
name = models.CharField(max_length=255, unique=True)
|
||||
code = models.CharField(
|
||||
max_length=2,
|
||||
unique=True,
|
||||
help_text="ISO 3166-1 alpha-2 country code"
|
||||
)
|
||||
code3 = models.CharField(
|
||||
max_length=3,
|
||||
blank=True,
|
||||
help_text="ISO 3166-1 alpha-3 country code"
|
||||
)
|
||||
|
||||
class Meta:
|
||||
db_table = 'countries'
|
||||
ordering = ['name']
|
||||
verbose_name_plural = 'countries'
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
|
||||
class Subdivision(BaseModel):
|
||||
"""
|
||||
State/Province/Region reference data (ISO 3166-2).
|
||||
|
||||
Examples: California, Ontario, England, etc.
|
||||
"""
|
||||
country = models.ForeignKey(
|
||||
Country,
|
||||
on_delete=models.CASCADE,
|
||||
related_name='subdivisions'
|
||||
)
|
||||
name = models.CharField(max_length=255)
|
||||
code = models.CharField(
|
||||
max_length=10,
|
||||
help_text="ISO 3166-2 subdivision code (without country prefix)"
|
||||
)
|
||||
subdivision_type = models.CharField(
|
||||
max_length=50,
|
||||
blank=True,
|
||||
help_text="Type of subdivision (state, province, region, etc.)"
|
||||
)
|
||||
|
||||
class Meta:
|
||||
db_table = 'subdivisions'
|
||||
ordering = ['country', 'name']
|
||||
unique_together = [['country', 'code']]
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.name}, {self.country.code}"
|
||||
|
||||
|
||||
class Locality(BaseModel):
|
||||
"""
|
||||
City/Town reference data.
|
||||
|
||||
Examples: Los Angeles, Toronto, London, etc.
|
||||
"""
|
||||
subdivision = models.ForeignKey(
|
||||
Subdivision,
|
||||
on_delete=models.CASCADE,
|
||||
related_name='localities'
|
||||
)
|
||||
name = models.CharField(max_length=255)
|
||||
latitude = models.DecimalField(
|
||||
max_digits=9,
|
||||
decimal_places=6,
|
||||
null=True,
|
||||
blank=True
|
||||
)
|
||||
longitude = models.DecimalField(
|
||||
max_digits=9,
|
||||
decimal_places=6,
|
||||
null=True,
|
||||
blank=True
|
||||
)
|
||||
|
||||
class Meta:
|
||||
db_table = 'localities'
|
||||
ordering = ['subdivision', 'name']
|
||||
verbose_name_plural = 'localities'
|
||||
indexes = [
|
||||
models.Index(fields=['subdivision', 'name']),
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.name}, {self.subdivision.code}"
|
||||
|
||||
@property
|
||||
def full_location(self):
|
||||
"""Return full location string: City, State, Country"""
|
||||
return f"{self.name}, {self.subdivision.name}, {self.subdivision.country.name}"
|
||||
|
||||
|
||||
# Date Precision Tracking
|
||||
|
||||
class DatePrecisionMixin(models.Model):
|
||||
"""
|
||||
Mixin for models that need to track date precision.
|
||||
|
||||
Allows tracking whether a date is known to year, month, or day precision.
|
||||
This is important for historical records where exact dates may not be known.
|
||||
"""
|
||||
|
||||
DATE_PRECISION_CHOICES = [
|
||||
('year', 'Year'),
|
||||
('month', 'Month'),
|
||||
('day', 'Day'),
|
||||
]
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
@classmethod
|
||||
def add_date_precision_field(cls, field_name):
|
||||
"""
|
||||
Helper to add a precision field for a date field.
|
||||
|
||||
Usage in subclass:
|
||||
opening_date = models.DateField(null=True, blank=True)
|
||||
opening_date_precision = models.CharField(...)
|
||||
"""
|
||||
return models.CharField(
|
||||
max_length=20,
|
||||
choices=cls.DATE_PRECISION_CHOICES,
|
||||
default='day',
|
||||
help_text=f"Precision level for {field_name}"
|
||||
)
|
||||
|
||||
|
||||
# Soft Delete Mixin
|
||||
|
||||
class SoftDeleteMixin(models.Model):
|
||||
"""
|
||||
Mixin for soft-deletable models.
|
||||
|
||||
Instead of actually deleting records, mark them as deleted.
|
||||
This preserves data integrity and allows for undelete functionality.
|
||||
"""
|
||||
is_deleted = models.BooleanField(default=False, db_index=True)
|
||||
deleted_at = models.DateTimeField(null=True, blank=True)
|
||||
deleted_by = models.ForeignKey(
|
||||
'users.User',
|
||||
on_delete=models.SET_NULL,
|
||||
null=True,
|
||||
blank=True,
|
||||
related_name='%(class)s_deletions'
|
||||
)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
def soft_delete(self, user=None):
|
||||
"""Mark this record as deleted"""
|
||||
from django.utils import timezone
|
||||
self.is_deleted = True
|
||||
self.deleted_at = timezone.now()
|
||||
if user:
|
||||
self.deleted_by = user
|
||||
self.save(update_fields=['is_deleted', 'deleted_at', 'deleted_by'])
|
||||
|
||||
def undelete(self):
|
||||
"""Restore a soft-deleted record"""
|
||||
self.is_deleted = False
|
||||
self.deleted_at = None
|
||||
self.deleted_by = None
|
||||
self.save(update_fields=['is_deleted', 'deleted_at', 'deleted_by'])
|
||||
|
||||
|
||||
# Model Managers
|
||||
|
||||
class ActiveManager(models.Manager):
|
||||
"""Manager that filters out soft-deleted records by default"""
|
||||
|
||||
def get_queryset(self):
|
||||
return super().get_queryset().filter(is_deleted=False)
|
||||
|
||||
|
||||
class AllObjectsManager(models.Manager):
|
||||
"""Manager that includes all records, even soft-deleted ones"""
|
||||
|
||||
def get_queryset(self):
|
||||
return super().get_queryset()
|
||||
0
django/apps/entities/__init__.py
Normal file
0
django/apps/entities/__init__.py
Normal file
BIN
django/apps/entities/__pycache__/__init__.cpython-313.pyc
Normal file
BIN
django/apps/entities/__pycache__/__init__.cpython-313.pyc
Normal file
Binary file not shown.
BIN
django/apps/entities/__pycache__/apps.cpython-313.pyc
Normal file
BIN
django/apps/entities/__pycache__/apps.cpython-313.pyc
Normal file
Binary file not shown.
BIN
django/apps/entities/__pycache__/models.cpython-313.pyc
Normal file
BIN
django/apps/entities/__pycache__/models.cpython-313.pyc
Normal file
Binary file not shown.
11
django/apps/entities/apps.py
Normal file
11
django/apps/entities/apps.py
Normal file
@@ -0,0 +1,11 @@
|
||||
"""
|
||||
Entities app configuration.
|
||||
"""
|
||||
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class EntitiesConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'apps.entities'
|
||||
verbose_name = 'Entities'
|
||||
0
django/apps/entities/models.py
Normal file
0
django/apps/entities/models.py
Normal file
0
django/apps/media/__init__.py
Normal file
0
django/apps/media/__init__.py
Normal file
BIN
django/apps/media/__pycache__/__init__.cpython-313.pyc
Normal file
BIN
django/apps/media/__pycache__/__init__.cpython-313.pyc
Normal file
Binary file not shown.
BIN
django/apps/media/__pycache__/apps.cpython-313.pyc
Normal file
BIN
django/apps/media/__pycache__/apps.cpython-313.pyc
Normal file
Binary file not shown.
BIN
django/apps/media/__pycache__/models.cpython-313.pyc
Normal file
BIN
django/apps/media/__pycache__/models.cpython-313.pyc
Normal file
Binary file not shown.
11
django/apps/media/apps.py
Normal file
11
django/apps/media/apps.py
Normal file
@@ -0,0 +1,11 @@
|
||||
"""
|
||||
Media app configuration.
|
||||
"""
|
||||
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class MediaConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'apps.media'
|
||||
verbose_name = 'Media'
|
||||
0
django/apps/media/models.py
Normal file
0
django/apps/media/models.py
Normal file
0
django/apps/moderation/__init__.py
Normal file
0
django/apps/moderation/__init__.py
Normal file
BIN
django/apps/moderation/__pycache__/__init__.cpython-313.pyc
Normal file
BIN
django/apps/moderation/__pycache__/__init__.cpython-313.pyc
Normal file
Binary file not shown.
BIN
django/apps/moderation/__pycache__/apps.cpython-313.pyc
Normal file
BIN
django/apps/moderation/__pycache__/apps.cpython-313.pyc
Normal file
Binary file not shown.
BIN
django/apps/moderation/__pycache__/models.cpython-313.pyc
Normal file
BIN
django/apps/moderation/__pycache__/models.cpython-313.pyc
Normal file
Binary file not shown.
11
django/apps/moderation/apps.py
Normal file
11
django/apps/moderation/apps.py
Normal file
@@ -0,0 +1,11 @@
|
||||
"""
|
||||
Moderation app configuration.
|
||||
"""
|
||||
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ModerationConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'apps.moderation'
|
||||
verbose_name = 'Moderation'
|
||||
0
django/apps/moderation/models.py
Normal file
0
django/apps/moderation/models.py
Normal file
0
django/apps/notifications/__init__.py
Normal file
0
django/apps/notifications/__init__.py
Normal file
BIN
django/apps/notifications/__pycache__/__init__.cpython-313.pyc
Normal file
BIN
django/apps/notifications/__pycache__/__init__.cpython-313.pyc
Normal file
Binary file not shown.
BIN
django/apps/notifications/__pycache__/apps.cpython-313.pyc
Normal file
BIN
django/apps/notifications/__pycache__/apps.cpython-313.pyc
Normal file
Binary file not shown.
BIN
django/apps/notifications/__pycache__/models.cpython-313.pyc
Normal file
BIN
django/apps/notifications/__pycache__/models.cpython-313.pyc
Normal file
Binary file not shown.
11
django/apps/notifications/apps.py
Normal file
11
django/apps/notifications/apps.py
Normal file
@@ -0,0 +1,11 @@
|
||||
"""
|
||||
Notifications app configuration.
|
||||
"""
|
||||
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class NotificationsConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'apps.notifications'
|
||||
verbose_name = 'Notifications'
|
||||
0
django/apps/notifications/models.py
Normal file
0
django/apps/notifications/models.py
Normal file
0
django/apps/users/__init__.py
Normal file
0
django/apps/users/__init__.py
Normal file
BIN
django/apps/users/__pycache__/__init__.cpython-313.pyc
Normal file
BIN
django/apps/users/__pycache__/__init__.cpython-313.pyc
Normal file
Binary file not shown.
BIN
django/apps/users/__pycache__/apps.cpython-313.pyc
Normal file
BIN
django/apps/users/__pycache__/apps.cpython-313.pyc
Normal file
Binary file not shown.
BIN
django/apps/users/__pycache__/models.cpython-313.pyc
Normal file
BIN
django/apps/users/__pycache__/models.cpython-313.pyc
Normal file
Binary file not shown.
17
django/apps/users/apps.py
Normal file
17
django/apps/users/apps.py
Normal file
@@ -0,0 +1,17 @@
|
||||
"""
|
||||
Users app configuration.
|
||||
"""
|
||||
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class UsersConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'apps.users'
|
||||
verbose_name = 'Users'
|
||||
|
||||
def ready(self):
|
||||
"""Import signal handlers when app is ready"""
|
||||
# Import signals here to avoid circular imports
|
||||
# import apps.users.signals
|
||||
pass
|
||||
370
django/apps/users/migrations/0001_initial.py
Normal file
370
django/apps/users/migrations/0001_initial.py
Normal file
@@ -0,0 +1,370 @@
|
||||
# Generated by Django 4.2.8 on 2025-11-08 16:35
|
||||
|
||||
from django.conf import settings
|
||||
import django.contrib.auth.models
|
||||
import django.contrib.auth.validators
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import django.utils.timezone
|
||||
import django_lifecycle.mixins
|
||||
import model_utils.fields
|
||||
import uuid
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
("auth", "0012_alter_user_first_name_max_length"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name="User",
|
||||
fields=[
|
||||
("password", models.CharField(max_length=128, verbose_name="password")),
|
||||
(
|
||||
"last_login",
|
||||
models.DateTimeField(
|
||||
blank=True, null=True, verbose_name="last login"
|
||||
),
|
||||
),
|
||||
(
|
||||
"is_superuser",
|
||||
models.BooleanField(
|
||||
default=False,
|
||||
help_text="Designates that this user has all permissions without explicitly assigning them.",
|
||||
verbose_name="superuser status",
|
||||
),
|
||||
),
|
||||
(
|
||||
"username",
|
||||
models.CharField(
|
||||
error_messages={
|
||||
"unique": "A user with that username already exists."
|
||||
},
|
||||
help_text="Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.",
|
||||
max_length=150,
|
||||
unique=True,
|
||||
validators=[
|
||||
django.contrib.auth.validators.UnicodeUsernameValidator()
|
||||
],
|
||||
verbose_name="username",
|
||||
),
|
||||
),
|
||||
(
|
||||
"first_name",
|
||||
models.CharField(
|
||||
blank=True, max_length=150, verbose_name="first name"
|
||||
),
|
||||
),
|
||||
(
|
||||
"last_name",
|
||||
models.CharField(
|
||||
blank=True, max_length=150, verbose_name="last name"
|
||||
),
|
||||
),
|
||||
(
|
||||
"is_staff",
|
||||
models.BooleanField(
|
||||
default=False,
|
||||
help_text="Designates whether the user can log into this admin site.",
|
||||
verbose_name="staff status",
|
||||
),
|
||||
),
|
||||
(
|
||||
"is_active",
|
||||
models.BooleanField(
|
||||
default=True,
|
||||
help_text="Designates whether this user should be treated as active. Unselect this instead of deleting accounts.",
|
||||
verbose_name="active",
|
||||
),
|
||||
),
|
||||
(
|
||||
"date_joined",
|
||||
models.DateTimeField(
|
||||
default=django.utils.timezone.now, verbose_name="date joined"
|
||||
),
|
||||
),
|
||||
(
|
||||
"id",
|
||||
models.UUIDField(
|
||||
default=uuid.uuid4,
|
||||
editable=False,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
),
|
||||
),
|
||||
(
|
||||
"email",
|
||||
models.EmailField(
|
||||
help_text="Email address for authentication",
|
||||
max_length=254,
|
||||
unique=True,
|
||||
),
|
||||
),
|
||||
(
|
||||
"oauth_provider",
|
||||
models.CharField(
|
||||
blank=True,
|
||||
choices=[
|
||||
("", "None"),
|
||||
("google", "Google"),
|
||||
("discord", "Discord"),
|
||||
],
|
||||
help_text="OAuth provider used for authentication",
|
||||
max_length=50,
|
||||
),
|
||||
),
|
||||
(
|
||||
"oauth_sub",
|
||||
models.CharField(
|
||||
blank=True,
|
||||
help_text="OAuth subject identifier from provider",
|
||||
max_length=255,
|
||||
),
|
||||
),
|
||||
(
|
||||
"mfa_enabled",
|
||||
models.BooleanField(
|
||||
default=False,
|
||||
help_text="Whether two-factor authentication is enabled",
|
||||
),
|
||||
),
|
||||
(
|
||||
"avatar_url",
|
||||
models.URLField(blank=True, help_text="URL to user's avatar image"),
|
||||
),
|
||||
(
|
||||
"bio",
|
||||
models.TextField(
|
||||
blank=True, help_text="User biography", max_length=500
|
||||
),
|
||||
),
|
||||
(
|
||||
"banned",
|
||||
models.BooleanField(
|
||||
db_index=True,
|
||||
default=False,
|
||||
help_text="Whether this user is banned",
|
||||
),
|
||||
),
|
||||
(
|
||||
"ban_reason",
|
||||
models.TextField(blank=True, help_text="Reason for ban"),
|
||||
),
|
||||
(
|
||||
"banned_at",
|
||||
models.DateTimeField(
|
||||
blank=True, help_text="When the user was banned", null=True
|
||||
),
|
||||
),
|
||||
(
|
||||
"reputation_score",
|
||||
models.IntegerField(
|
||||
default=0,
|
||||
help_text="User reputation score based on contributions",
|
||||
),
|
||||
),
|
||||
(
|
||||
"banned_by",
|
||||
models.ForeignKey(
|
||||
blank=True,
|
||||
help_text="Moderator who banned this user",
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.SET_NULL,
|
||||
related_name="users_banned",
|
||||
to=settings.AUTH_USER_MODEL,
|
||||
),
|
||||
),
|
||||
(
|
||||
"groups",
|
||||
models.ManyToManyField(
|
||||
blank=True,
|
||||
help_text="The groups this user belongs to. A user will get all permissions granted to each of their groups.",
|
||||
related_name="user_set",
|
||||
related_query_name="user",
|
||||
to="auth.group",
|
||||
verbose_name="groups",
|
||||
),
|
||||
),
|
||||
(
|
||||
"user_permissions",
|
||||
models.ManyToManyField(
|
||||
blank=True,
|
||||
help_text="Specific permissions for this user.",
|
||||
related_name="user_set",
|
||||
related_query_name="user",
|
||||
to="auth.permission",
|
||||
verbose_name="user permissions",
|
||||
),
|
||||
),
|
||||
],
|
||||
options={
|
||||
"db_table": "users",
|
||||
"ordering": ["-date_joined"],
|
||||
},
|
||||
managers=[
|
||||
("objects", django.contrib.auth.models.UserManager()),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name="UserRole",
|
||||
fields=[
|
||||
(
|
||||
"created",
|
||||
model_utils.fields.AutoCreatedField(
|
||||
default=django.utils.timezone.now,
|
||||
editable=False,
|
||||
verbose_name="created",
|
||||
),
|
||||
),
|
||||
(
|
||||
"modified",
|
||||
model_utils.fields.AutoLastModifiedField(
|
||||
default=django.utils.timezone.now,
|
||||
editable=False,
|
||||
verbose_name="modified",
|
||||
),
|
||||
),
|
||||
(
|
||||
"id",
|
||||
models.UUIDField(
|
||||
default=uuid.uuid4,
|
||||
editable=False,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
),
|
||||
),
|
||||
(
|
||||
"role",
|
||||
models.CharField(
|
||||
choices=[
|
||||
("user", "User"),
|
||||
("moderator", "Moderator"),
|
||||
("admin", "Admin"),
|
||||
],
|
||||
db_index=True,
|
||||
default="user",
|
||||
max_length=20,
|
||||
),
|
||||
),
|
||||
("granted_at", models.DateTimeField(auto_now_add=True)),
|
||||
(
|
||||
"granted_by",
|
||||
models.ForeignKey(
|
||||
blank=True,
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.SET_NULL,
|
||||
related_name="roles_granted",
|
||||
to=settings.AUTH_USER_MODEL,
|
||||
),
|
||||
),
|
||||
(
|
||||
"user",
|
||||
models.OneToOneField(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="role",
|
||||
to=settings.AUTH_USER_MODEL,
|
||||
),
|
||||
),
|
||||
],
|
||||
options={
|
||||
"db_table": "user_roles",
|
||||
},
|
||||
bases=(django_lifecycle.mixins.LifecycleModelMixin, models.Model),
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name="UserProfile",
|
||||
fields=[
|
||||
(
|
||||
"created",
|
||||
model_utils.fields.AutoCreatedField(
|
||||
default=django.utils.timezone.now,
|
||||
editable=False,
|
||||
verbose_name="created",
|
||||
),
|
||||
),
|
||||
(
|
||||
"modified",
|
||||
model_utils.fields.AutoLastModifiedField(
|
||||
default=django.utils.timezone.now,
|
||||
editable=False,
|
||||
verbose_name="modified",
|
||||
),
|
||||
),
|
||||
(
|
||||
"id",
|
||||
models.UUIDField(
|
||||
default=uuid.uuid4,
|
||||
editable=False,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
),
|
||||
),
|
||||
(
|
||||
"email_notifications",
|
||||
models.BooleanField(
|
||||
default=True, help_text="Receive email notifications"
|
||||
),
|
||||
),
|
||||
(
|
||||
"email_on_submission_approved",
|
||||
models.BooleanField(
|
||||
default=True, help_text="Email when submissions are approved"
|
||||
),
|
||||
),
|
||||
(
|
||||
"email_on_submission_rejected",
|
||||
models.BooleanField(
|
||||
default=True, help_text="Email when submissions are rejected"
|
||||
),
|
||||
),
|
||||
(
|
||||
"profile_public",
|
||||
models.BooleanField(
|
||||
default=True, help_text="Make profile publicly visible"
|
||||
),
|
||||
),
|
||||
(
|
||||
"show_email",
|
||||
models.BooleanField(
|
||||
default=False, help_text="Show email on public profile"
|
||||
),
|
||||
),
|
||||
(
|
||||
"total_submissions",
|
||||
models.IntegerField(
|
||||
default=0, help_text="Total number of submissions made"
|
||||
),
|
||||
),
|
||||
(
|
||||
"approved_submissions",
|
||||
models.IntegerField(
|
||||
default=0, help_text="Number of approved submissions"
|
||||
),
|
||||
),
|
||||
(
|
||||
"user",
|
||||
models.OneToOneField(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="profile",
|
||||
to=settings.AUTH_USER_MODEL,
|
||||
),
|
||||
),
|
||||
],
|
||||
options={
|
||||
"db_table": "user_profiles",
|
||||
},
|
||||
bases=(django_lifecycle.mixins.LifecycleModelMixin, models.Model),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name="user",
|
||||
index=models.Index(fields=["email"], name="users_email_4b85f2_idx"),
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name="user",
|
||||
index=models.Index(fields=["banned"], name="users_banned_ee00ad_idx"),
|
||||
),
|
||||
]
|
||||
0
django/apps/users/migrations/__init__.py
Normal file
0
django/apps/users/migrations/__init__.py
Normal file
Binary file not shown.
Binary file not shown.
257
django/apps/users/models.py
Normal file
257
django/apps/users/models.py
Normal file
@@ -0,0 +1,257 @@
|
||||
"""
|
||||
User models for ThrillWiki.
|
||||
Custom user model with OAuth and MFA support.
|
||||
"""
|
||||
|
||||
import uuid
|
||||
from django.contrib.auth.models import AbstractUser
|
||||
from django.db import models
|
||||
from apps.core.models import BaseModel
|
||||
|
||||
|
||||
class User(AbstractUser):
|
||||
"""
|
||||
Custom user model with UUID primary key and additional fields.
|
||||
|
||||
Supports:
|
||||
- Email-based authentication
|
||||
- OAuth (Google, Discord)
|
||||
- Two-factor authentication (TOTP)
|
||||
- User reputation and moderation
|
||||
"""
|
||||
|
||||
# Override id to use UUID
|
||||
id = models.UUIDField(
|
||||
primary_key=True,
|
||||
default=uuid.uuid4,
|
||||
editable=False
|
||||
)
|
||||
|
||||
# Email as primary identifier
|
||||
email = models.EmailField(
|
||||
unique=True,
|
||||
help_text="Email address for authentication"
|
||||
)
|
||||
|
||||
# OAuth fields
|
||||
oauth_provider = models.CharField(
|
||||
max_length=50,
|
||||
blank=True,
|
||||
choices=[
|
||||
('', 'None'),
|
||||
('google', 'Google'),
|
||||
('discord', 'Discord'),
|
||||
],
|
||||
help_text="OAuth provider used for authentication"
|
||||
)
|
||||
oauth_sub = models.CharField(
|
||||
max_length=255,
|
||||
blank=True,
|
||||
help_text="OAuth subject identifier from provider"
|
||||
)
|
||||
|
||||
# MFA fields
|
||||
mfa_enabled = models.BooleanField(
|
||||
default=False,
|
||||
help_text="Whether two-factor authentication is enabled"
|
||||
)
|
||||
|
||||
# Profile fields
|
||||
avatar_url = models.URLField(
|
||||
blank=True,
|
||||
help_text="URL to user's avatar image"
|
||||
)
|
||||
bio = models.TextField(
|
||||
blank=True,
|
||||
max_length=500,
|
||||
help_text="User biography"
|
||||
)
|
||||
|
||||
# Moderation fields
|
||||
banned = models.BooleanField(
|
||||
default=False,
|
||||
db_index=True,
|
||||
help_text="Whether this user is banned"
|
||||
)
|
||||
ban_reason = models.TextField(
|
||||
blank=True,
|
||||
help_text="Reason for ban"
|
||||
)
|
||||
banned_at = models.DateTimeField(
|
||||
null=True,
|
||||
blank=True,
|
||||
help_text="When the user was banned"
|
||||
)
|
||||
banned_by = models.ForeignKey(
|
||||
'self',
|
||||
on_delete=models.SET_NULL,
|
||||
null=True,
|
||||
blank=True,
|
||||
related_name='users_banned',
|
||||
help_text="Moderator who banned this user"
|
||||
)
|
||||
|
||||
# Reputation system
|
||||
reputation_score = models.IntegerField(
|
||||
default=0,
|
||||
help_text="User reputation score based on contributions"
|
||||
)
|
||||
|
||||
# Timestamps (inherited from AbstractUser)
|
||||
# date_joined, last_login
|
||||
|
||||
# Use email for authentication
|
||||
USERNAME_FIELD = 'email'
|
||||
REQUIRED_FIELDS = ['username']
|
||||
|
||||
class Meta:
|
||||
db_table = 'users'
|
||||
ordering = ['-date_joined']
|
||||
indexes = [
|
||||
models.Index(fields=['email']),
|
||||
models.Index(fields=['banned']),
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
return self.email
|
||||
|
||||
def ban(self, reason, banned_by=None):
|
||||
"""Ban this user"""
|
||||
from django.utils import timezone
|
||||
self.banned = True
|
||||
self.ban_reason = reason
|
||||
self.banned_at = timezone.now()
|
||||
self.banned_by = banned_by
|
||||
self.save(update_fields=['banned', 'ban_reason', 'banned_at', 'banned_by'])
|
||||
|
||||
def unban(self):
|
||||
"""Unban this user"""
|
||||
self.banned = False
|
||||
self.ban_reason = ''
|
||||
self.banned_at = None
|
||||
self.banned_by = None
|
||||
self.save(update_fields=['banned', 'ban_reason', 'banned_at', 'banned_by'])
|
||||
|
||||
@property
|
||||
def display_name(self):
|
||||
"""Return the user's display name (full name or username)"""
|
||||
if self.first_name or self.last_name:
|
||||
return f"{self.first_name} {self.last_name}".strip()
|
||||
return self.username or self.email.split('@')[0]
|
||||
|
||||
|
||||
class UserRole(BaseModel):
|
||||
"""
|
||||
User role assignments for permission management.
|
||||
|
||||
Roles:
|
||||
- user: Standard user (default)
|
||||
- moderator: Can approve submissions and moderate content
|
||||
- admin: Full access to admin features
|
||||
"""
|
||||
|
||||
ROLE_CHOICES = [
|
||||
('user', 'User'),
|
||||
('moderator', 'Moderator'),
|
||||
('admin', 'Admin'),
|
||||
]
|
||||
|
||||
user = models.OneToOneField(
|
||||
User,
|
||||
on_delete=models.CASCADE,
|
||||
related_name='role'
|
||||
)
|
||||
role = models.CharField(
|
||||
max_length=20,
|
||||
choices=ROLE_CHOICES,
|
||||
default='user',
|
||||
db_index=True
|
||||
)
|
||||
granted_at = models.DateTimeField(auto_now_add=True)
|
||||
granted_by = models.ForeignKey(
|
||||
User,
|
||||
on_delete=models.SET_NULL,
|
||||
null=True,
|
||||
blank=True,
|
||||
related_name='roles_granted'
|
||||
)
|
||||
|
||||
class Meta:
|
||||
db_table = 'user_roles'
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.user.email} - {self.role}"
|
||||
|
||||
@property
|
||||
def is_moderator(self):
|
||||
"""Check if user is a moderator or admin"""
|
||||
return self.role in ['moderator', 'admin']
|
||||
|
||||
@property
|
||||
def is_admin(self):
|
||||
"""Check if user is an admin"""
|
||||
return self.role == 'admin'
|
||||
|
||||
|
||||
class UserProfile(BaseModel):
|
||||
"""
|
||||
Extended user profile information.
|
||||
|
||||
Stores additional user preferences and settings.
|
||||
"""
|
||||
|
||||
user = models.OneToOneField(
|
||||
User,
|
||||
on_delete=models.CASCADE,
|
||||
related_name='profile'
|
||||
)
|
||||
|
||||
# Preferences
|
||||
email_notifications = models.BooleanField(
|
||||
default=True,
|
||||
help_text="Receive email notifications"
|
||||
)
|
||||
email_on_submission_approved = models.BooleanField(
|
||||
default=True,
|
||||
help_text="Email when submissions are approved"
|
||||
)
|
||||
email_on_submission_rejected = models.BooleanField(
|
||||
default=True,
|
||||
help_text="Email when submissions are rejected"
|
||||
)
|
||||
|
||||
# Privacy
|
||||
profile_public = models.BooleanField(
|
||||
default=True,
|
||||
help_text="Make profile publicly visible"
|
||||
)
|
||||
show_email = models.BooleanField(
|
||||
default=False,
|
||||
help_text="Show email on public profile"
|
||||
)
|
||||
|
||||
# Statistics
|
||||
total_submissions = models.IntegerField(
|
||||
default=0,
|
||||
help_text="Total number of submissions made"
|
||||
)
|
||||
approved_submissions = models.IntegerField(
|
||||
default=0,
|
||||
help_text="Number of approved submissions"
|
||||
)
|
||||
|
||||
class Meta:
|
||||
db_table = 'user_profiles'
|
||||
|
||||
def __str__(self):
|
||||
return f"Profile for {self.user.email}"
|
||||
|
||||
def update_submission_stats(self):
|
||||
"""Update submission statistics"""
|
||||
from apps.moderation.models import ContentSubmission
|
||||
self.total_submissions = ContentSubmission.objects.filter(user=self.user).count()
|
||||
self.approved_submissions = ContentSubmission.objects.filter(
|
||||
user=self.user,
|
||||
status='approved'
|
||||
).count()
|
||||
self.save(update_fields=['total_submissions', 'approved_submissions'])
|
||||
0
django/apps/versioning/__init__.py
Normal file
0
django/apps/versioning/__init__.py
Normal file
BIN
django/apps/versioning/__pycache__/__init__.cpython-313.pyc
Normal file
BIN
django/apps/versioning/__pycache__/__init__.cpython-313.pyc
Normal file
Binary file not shown.
BIN
django/apps/versioning/__pycache__/apps.cpython-313.pyc
Normal file
BIN
django/apps/versioning/__pycache__/apps.cpython-313.pyc
Normal file
Binary file not shown.
BIN
django/apps/versioning/__pycache__/models.cpython-313.pyc
Normal file
BIN
django/apps/versioning/__pycache__/models.cpython-313.pyc
Normal file
Binary file not shown.
11
django/apps/versioning/apps.py
Normal file
11
django/apps/versioning/apps.py
Normal file
@@ -0,0 +1,11 @@
|
||||
"""
|
||||
Versioning app configuration.
|
||||
"""
|
||||
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class VersioningConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'apps.versioning'
|
||||
verbose_name = 'Versioning'
|
||||
0
django/apps/versioning/models.py
Normal file
0
django/apps/versioning/models.py
Normal file
Reference in New Issue
Block a user