models.py 4.0 KB

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