Refactor test utilities and enhance ASGI settings

- Cleaned up and standardized assertions in ApiTestMixin for API response validation.
- Updated ASGI settings to use os.environ for setting the DJANGO_SETTINGS_MODULE.
- Removed unused imports and improved formatting in settings.py.
- Refactored URL patterns in urls.py for better readability and organization.
- Enhanced view functions in views.py for consistency and clarity.
- Added .flake8 configuration for linting and style enforcement.
- Introduced type stubs for django-environ to improve type checking with Pylance.
This commit is contained in:
pacnpal
2025-08-20 19:51:59 -04:00
parent 69c07d1381
commit 66ed4347a9
230 changed files with 15094 additions and 11578 deletions

View File

@@ -4,33 +4,39 @@ from django.contrib.contenttypes.models import ContentType
from django.utils.text import slugify
from core.history import TrackedModel
class SlugHistory(models.Model):
"""
Model for tracking slug changes across all models that use slugs.
Uses generic relations to work with any model.
"""
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.CharField(max_length=50) # Using CharField to work with our custom IDs
content_object = GenericForeignKey('content_type', 'object_id')
object_id = models.CharField(
max_length=50
) # Using CharField to work with our custom IDs
content_object = GenericForeignKey("content_type", "object_id")
old_slug = models.SlugField(max_length=200)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
indexes = [
models.Index(fields=['content_type', 'object_id']),
models.Index(fields=['old_slug']),
models.Index(fields=["content_type", "object_id"]),
models.Index(fields=["old_slug"]),
]
verbose_name_plural = 'Slug histories'
ordering = ['-created_at']
verbose_name_plural = "Slug histories"
ordering = ["-created_at"]
def __str__(self):
return f"Old slug '{self.old_slug}' for {self.content_object}"
class SluggedModel(TrackedModel):
"""
Abstract base model that provides slug functionality with history tracking.
"""
name = models.CharField(max_length=200)
slug = models.SlugField(max_length=200, unique=True)
@@ -47,7 +53,7 @@ class SluggedModel(TrackedModel):
SlugHistory.objects.create(
content_type=ContentType.objects.get_for_model(self),
object_id=getattr(self, self.get_id_field_name()),
old_slug=old_instance.slug
old_slug=old_instance.slug,
)
except self.__class__.DoesNotExist:
pass
@@ -81,24 +87,27 @@ class SluggedModel(TrackedModel):
history_model = cls.get_history_model()
history_entry = (
history_model.objects.filter(slug=slug)
.order_by('-pgh_created_at')
.order_by("-pgh_created_at")
.first()
)
if history_entry:
return cls.objects.get(id=history_entry.pgh_obj_id), True
# Try to find in manual slug history as fallback
history = SlugHistory.objects.filter(
content_type=ContentType.objects.get_for_model(cls),
old_slug=slug
).order_by('-created_at').first()
if history:
return cls.objects.get(
**{cls.get_id_field_name(): history.object_id}
), True
raise cls.DoesNotExist(
f"{cls.__name__} with slug '{slug}' does not exist"
history = (
SlugHistory.objects.filter(
content_type=ContentType.objects.get_for_model(cls),
old_slug=slug,
)
.order_by("-created_at")
.first()
)
if history:
return (
cls.objects.get(**{cls.get_id_field_name(): history.object_id}),
True,
)
raise cls.DoesNotExist(f"{cls.__name__} with slug '{slug}' does not exist")