models.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. # Copyright © 2023 Ingram Micro Inc. All rights reserved.
  2. from django.contrib.contenttypes.fields import GenericForeignKey
  3. from django.contrib.contenttypes.models import ContentType
  4. from django.db import models
  5. from mptt.models import MPTTModel, TreeForeignKey
  6. from dj_cqrs.constants import ALL_BASIC_FIELDS
  7. from dj_cqrs.metas import MasterMeta
  8. from dj_cqrs.mixins import MasterMixin, RawMasterMixin
  9. class BasicFieldsModel(MasterMixin, models.Model):
  10. CQRS_ID = 'basic'
  11. CQRS_TRACKED_FIELDS = ALL_BASIC_FIELDS
  12. int_field = models.IntegerField(primary_key=True)
  13. bool_field = models.BooleanField(null=True)
  14. char_field = models.CharField(max_length=200, null=True)
  15. date_field = models.DateField(null=True)
  16. datetime_field = models.DateTimeField(null=True)
  17. float_field = models.FloatField(null=True)
  18. url_field = models.URLField(null=True)
  19. uuid_field = models.UUIDField(null=True)
  20. class AllFieldsModel(MasterMixin, models.Model):
  21. CQRS_FIELDS = ALL_BASIC_FIELDS
  22. CQRS_ID = 'all'
  23. CQRS_TRACKED_FIELDS = ALL_BASIC_FIELDS
  24. int_field = models.IntegerField(null=True)
  25. char_field = models.CharField(max_length=200, null=True)
  26. class ChosenFieldsModel(MasterMixin, models.Model):
  27. CQRS_FIELDS = ('char_field', 'id')
  28. CQRS_ID = 'chosen'
  29. float_field = models.IntegerField(null=True)
  30. char_field = models.CharField(max_length=200, null=True)
  31. class AutoFieldsModel(MasterMixin, models.Model):
  32. CQRS_ID = 'auto'
  33. created = models.DateTimeField(auto_now_add=True)
  34. updated = models.DateTimeField(auto_now=True)
  35. class SimplestModel(MasterMixin, models.Model):
  36. CQRS_ID = 'pk'
  37. id = models.IntegerField(primary_key=True)
  38. name = models.CharField(max_length=200, null=True)
  39. class SimplestTrackedModel(MasterMixin):
  40. CQRS_ID = 'pk_tracked'
  41. CQRS_TRACKED_FIELDS = ('status', 'description')
  42. id = models.IntegerField(primary_key=True)
  43. status = models.CharField(max_length=200, null=True)
  44. description = models.CharField(max_length=200, null=True)
  45. class FilteredSimplestModel(MasterMixin, models.Model):
  46. CQRS_ID = 'filter'
  47. name = models.CharField(max_length=200)
  48. def is_sync_instance(self):
  49. return len(str(self.name)) > 2
  50. class Publisher(models.Model):
  51. id = models.IntegerField(primary_key=True)
  52. name = models.CharField(max_length=20)
  53. class Author(MasterMixin, models.Model):
  54. CQRS_ID = 'author'
  55. CQRS_SERIALIZER = 'tests.dj_master.serializers.AuthorSerializer'
  56. id = models.IntegerField(primary_key=True)
  57. name = models.CharField(max_length=20)
  58. publisher = models.ForeignKey(
  59. Publisher,
  60. related_name='authors',
  61. on_delete=models.SET_NULL,
  62. null=True,
  63. )
  64. @classmethod
  65. def relate_cqrs_serialization(cls, queryset):
  66. return queryset.select_related('publisher').prefetch_related('books')
  67. class Book(models.Model):
  68. id = models.IntegerField(primary_key=True)
  69. title = models.CharField(max_length=20)
  70. author = models.ForeignKey(Author, related_name='books', on_delete=models.CASCADE)
  71. class BadSerializationClassModel(MasterMixin, models.Model):
  72. CQRS_ID = 'bad_serialization'
  73. CQRS_SERIALIZER = 'tests.Serializer'
  74. class BadQuerySetSerializationClassModel(MasterMixin, models.Model):
  75. CQRS_ID = 'bad_queryset'
  76. CQRS_SERIALIZER = 'tests.dj_master.serializers.AuthorSerializer'
  77. @classmethod
  78. def relate_cqrs_serialization(cls, queryset):
  79. return queryset.none()
  80. class NonMetaClassModel(MPTTModel, RawMasterMixin):
  81. CQRS_ID = 'non_meta'
  82. name = models.CharField(max_length=50, unique=True)
  83. parent = TreeForeignKey(
  84. 'self',
  85. on_delete=models.CASCADE,
  86. null=True,
  87. blank=True,
  88. related_name='children',
  89. )
  90. MasterMeta.register(NonMetaClassModel)
  91. class NonSentModel(MasterMixin, models.Model):
  92. CQRS_ID = 'non_sent'
  93. CQRS_PRODUCE = False
  94. class TrackedFieldsParentModel(MasterMixin, models.Model):
  95. CQRS_ID = 'tracked_parent'
  96. CQRS_TRACKED_FIELDS = ALL_BASIC_FIELDS
  97. char_field = models.CharField(max_length=10)
  98. class TrackedFieldsChildModel(MasterMixin):
  99. CQRS_ID = 'tracked_child'
  100. CQRS_TRACKED_FIELDS = ('char_field', 'parent')
  101. char_field = models.CharField(max_length=10)
  102. parent = models.ForeignKey(TrackedFieldsParentModel, on_delete=models.CASCADE, null=True)
  103. class TrackedFieldsAllWithChildModel(MasterMixin):
  104. CQRS_ID = 'tracked_child_all'
  105. CQRS_TRACKED_FIELDS = '__all__'
  106. char_field = models.CharField(max_length=10)
  107. parent = models.ForeignKey(TrackedFieldsParentModel, on_delete=models.CASCADE)
  108. class MPTTWithTrackingModel(MPTTModel, RawMasterMixin):
  109. CQRS_ID = 'with_tracking'
  110. CQRS_TRACKED_FIELDS = '__all__'
  111. name = models.CharField(max_length=50, unique=True)
  112. parent = TreeForeignKey(
  113. 'self',
  114. on_delete=models.CASCADE,
  115. null=True,
  116. blank=True,
  117. related_name='children',
  118. )
  119. MasterMeta.register(MPTTWithTrackingModel)
  120. class WithGenericFKModel(MasterMixin):
  121. CQRS_ID = 'with_generic_fk'
  122. CQRS_TRACKED_FIELDS = '__all__'
  123. content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, null=True, blank=True)
  124. object_id = models.PositiveIntegerField(null=True)
  125. content_object = GenericForeignKey('content_type', 'object_id')
  126. class M2MModel(models.Model):
  127. CQRS_ID = 'm2m_model'
  128. id = models.IntegerField(primary_key=True)
  129. name = models.CharField(max_length=200, null=True)
  130. class WithM2MModel(MasterMixin):
  131. CQRS_ID = 'with_m2m_fk'
  132. CQRS_TRACKED_FIELDS = '__all__'
  133. char_field = models.CharField(max_length=100)
  134. m2m_field = models.ManyToManyField(M2MModel)
  135. class FailModel(MasterMixin, models.Model):
  136. CQRS_ID = 'fail'