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

@@ -5,16 +5,22 @@ from django.conf import settings
from typing import Any, Dict, Optional
from django.db.models import QuerySet
class DiffMixin:
"""Mixin to add diffing capabilities to models"""
def get_prev_record(self) -> Optional[Any]:
"""Get the previous record for this instance"""
try:
return type(self).objects.filter(
pgh_created_at__lt=self.pgh_created_at,
pgh_obj_id=self.pgh_obj_id
).order_by('-pgh_created_at').first()
return (
type(self)
.objects.filter(
pgh_created_at__lt=self.pgh_created_at,
pgh_obj_id=self.pgh_obj_id,
)
.order_by("-pgh_created_at")
.first()
)
except (AttributeError, TypeError):
return None
@@ -25,15 +31,20 @@ class DiffMixin:
return {}
skip_fields = {
'pgh_id', 'pgh_created_at', 'pgh_label',
'pgh_obj_id', 'pgh_context_id', '_state',
'created_at', 'updated_at'
"pgh_id",
"pgh_created_at",
"pgh_label",
"pgh_obj_id",
"pgh_context_id",
"_state",
"created_at",
"updated_at",
}
changes = {}
for field, value in self.__dict__.items():
# Skip internal fields and those we don't want to track
if field.startswith('_') or field in skip_fields or field.endswith('_id'):
if field.startswith("_") or field in skip_fields or field.endswith("_id"):
continue
try:
@@ -41,16 +52,18 @@ class DiffMixin:
new_value = value
if old_value != new_value:
changes[field] = {
"old": str(old_value) if old_value is not None else "None",
"new": str(new_value) if new_value is not None else "None"
"old": (str(old_value) if old_value is not None else "None"),
"new": (str(new_value) if new_value is not None else "None"),
}
except AttributeError:
continue
return changes
class TrackedModel(models.Model):
"""Abstract base class for models that need history tracking"""
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
@@ -61,16 +74,18 @@ class TrackedModel(models.Model):
"""Get all history records for this instance in chronological order"""
event_model = self.events.model # pghistory provides this automatically
if event_model:
return event_model.objects.filter(
pgh_obj_id=self.pk
).order_by('-pgh_created_at')
return event_model.objects.filter(pgh_obj_id=self.pk).order_by(
"-pgh_created_at"
)
return self.__class__.objects.none()
class HistoricalSlug(models.Model):
"""Track historical slugs for models"""
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
content_object = GenericForeignKey("content_type", "object_id")
slug = models.SlugField(max_length=255)
created_at = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(
@@ -78,14 +93,14 @@ class HistoricalSlug(models.Model):
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='historical_slugs'
related_name="historical_slugs",
)
class Meta:
unique_together = ('content_type', 'slug')
unique_together = ("content_type", "slug")
indexes = [
models.Index(fields=['content_type', 'object_id']),
models.Index(fields=['slug']),
models.Index(fields=["content_type", "object_id"]),
models.Index(fields=["slug"]),
]
def __str__(self) -> str: