models.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. from enum import Enum
  2. from django.conf import settings
  3. from django.core.validators import MaxValueValidator, MinValueValidator
  4. from django.db import models
  5. from django.urls import reverse
  6. from django_extensions.db.fields import AutoSlugField
  7. from emus.utils import ChoiceEnum
  8. def get_screenshot_upload_path(instance, filename):
  9. return f"{instance.game_system.retropie_slug}/screenshots/{filename}"
  10. def get_marquee_upload_path(instance, filename):
  11. return f"{instance.game_system.retropie_slug}/marquee/{filename}"
  12. def get_video_upload_path(instance, filename):
  13. return f"{instance.game_system.retropie_slug}/videos/{filename}"
  14. def get_rom_upload_path(instance, filename):
  15. return f"{instance.game_system.retropie_slug}/{filename}"
  16. class Region(Enum):
  17. USA = "US"
  18. EUROPE = "EU"
  19. JAPAN = "JP"
  20. class BaseModel(models.Model):
  21. """A base model for providing name and slugged fields for organizational models"""
  22. name = models.CharField(max_length=255)
  23. slug = AutoSlugField(populate_from="name")
  24. class Meta:
  25. abstract = True
  26. def slugify_function(self, content):
  27. for element in settings.REMOVE_FROM_SLUGS:
  28. content = content.replace(element, "-")
  29. return content.lower()
  30. def __str__(self):
  31. return self.name
  32. class Genre(BaseModel):
  33. ...
  34. class Publisher(BaseModel):
  35. ...
  36. class Developer(BaseModel):
  37. ...
  38. class GameSystem(BaseModel):
  39. retropie_slug = models.CharField(
  40. blank=True,
  41. null=True,
  42. max_length=50,
  43. )
  44. def get_absolute_url(self):
  45. return reverse("games:game_system_detail", args=[self.slug])
  46. class Game(BaseModel):
  47. class Region(ChoiceEnum):
  48. US = "USA"
  49. EU = "Europe"
  50. JP = "Japan"
  51. X = "Unknown"
  52. game_system = models.ForeignKey(
  53. GameSystem,
  54. on_delete=models.SET_NULL,
  55. null=True,
  56. )
  57. release_date = models.DateTimeField(
  58. blank=True,
  59. null=True,
  60. )
  61. developer = models.ForeignKey(
  62. Developer,
  63. on_delete=models.SET_NULL,
  64. blank=True,
  65. null=True,
  66. )
  67. publisher = models.ForeignKey(
  68. Publisher,
  69. on_delete=models.SET_NULL,
  70. blank=True,
  71. null=True,
  72. )
  73. genre = models.ManyToManyField(
  74. Genre,
  75. )
  76. players = models.SmallIntegerField(
  77. default=1,
  78. )
  79. kid_game = models.BooleanField(
  80. default=False,
  81. )
  82. description = models.TextField(
  83. blank=True,
  84. null=True,
  85. )
  86. rating = models.FloatField(
  87. blank=True,
  88. null=True,
  89. validators=[MaxValueValidator(1), MinValueValidator(0)],
  90. )
  91. video = models.FileField(
  92. blank=True,
  93. null=True,
  94. upload_to=get_video_upload_path,
  95. )
  96. marquee = models.ImageField(
  97. blank=True,
  98. null=True,
  99. upload_to=get_marquee_upload_path,
  100. )
  101. screenshot = models.ImageField(
  102. blank=True,
  103. null=True,
  104. upload_to=get_screenshot_upload_path,
  105. )
  106. rom_file = models.FileField(
  107. blank=True,
  108. null=True,
  109. upload_to=get_rom_upload_path,
  110. )
  111. hack = models.BooleanField(
  112. default=False,
  113. )
  114. hack_version = models.CharField(
  115. max_length=255,
  116. blank=True,
  117. null=True,
  118. )
  119. english_patched = models.BooleanField(
  120. default=False,
  121. )
  122. english_patched_version = models.CharField(
  123. max_length=50,
  124. blank=True,
  125. null=True,
  126. )
  127. undub = models.BooleanField(
  128. default=False,
  129. )
  130. featured = models.BooleanField(
  131. default=False,
  132. )
  133. region = models.CharField(
  134. max_length=2,
  135. choices=Region.choices(),
  136. blank=True,
  137. null=True,
  138. )
  139. def __str__(self):
  140. return f"{self.name} for {self.game_system}"
  141. def get_absolute_url(self):
  142. return reverse("games:game_detail", args=[self.slug])