0019_configrevision_active.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Generated by Django 5.2.5 on 2025-09-09 16:48
  2. from django.db import migrations, models
  3. def get_active(apps, schema_editor):
  4. from django.core.cache import cache
  5. ConfigRevision = apps.get_model('core', 'ConfigRevision')
  6. version = None
  7. revision = None
  8. # Try and get the latest version from cache
  9. try:
  10. version = cache.get('config_version')
  11. except Exception:
  12. pass
  13. # If there is a version in cache, attempt to set revision to the current version from cache
  14. # If the version in cache does not exist or there is no version, try the lastest revision in the database
  15. if not version or (version and not (revision := ConfigRevision.objects.filter(pk=version).first())):
  16. revision = ConfigRevision.objects.order_by('-created').first()
  17. # If there is a revision set, set the active revision
  18. if revision:
  19. revision.active = True
  20. revision.save()
  21. class Migration(migrations.Migration):
  22. dependencies = [
  23. ('core', '0018_concrete_objecttype'),
  24. ]
  25. operations = [
  26. migrations.AddField(
  27. model_name='configrevision',
  28. name='active',
  29. field=models.BooleanField(default=False),
  30. ),
  31. migrations.RunPython(code=get_active, reverse_code=migrations.RunPython.noop),
  32. migrations.AddConstraint(
  33. model_name='configrevision',
  34. constraint=models.UniqueConstraint(
  35. condition=models.Q(('active', True)), fields=('active',), name='unique_active_config_revision'
  36. ),
  37. ),
  38. ]