models.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. @property
  45. def webretro_core(self):
  46. return settings.RETROPIE_WEBRETRO_SYSTEM_MAP.get(self.retropie_slug, None)
  47. def get_absolute_url(self):
  48. return reverse("games:game_system_detail", args=[self.slug])
  49. class Game(BaseModel):
  50. class Region(ChoiceEnum):
  51. US = "USA"
  52. EU = "Europe"
  53. JP = "Japan"
  54. X = "Unknown"
  55. game_system = models.ForeignKey(
  56. GameSystem,
  57. on_delete=models.SET_NULL,
  58. null=True,
  59. )
  60. release_date = models.DateTimeField(
  61. blank=True,
  62. null=True,
  63. )
  64. developer = models.ForeignKey(
  65. Developer,
  66. on_delete=models.SET_NULL,
  67. blank=True,
  68. null=True,
  69. )
  70. publisher = models.ForeignKey(
  71. Publisher,
  72. on_delete=models.SET_NULL,
  73. blank=True,
  74. null=True,
  75. )
  76. genre = models.ManyToManyField(
  77. Genre,
  78. )
  79. players = models.SmallIntegerField(
  80. default=1,
  81. )
  82. kid_game = models.BooleanField(
  83. default=False,
  84. )
  85. description = models.TextField(
  86. blank=True,
  87. null=True,
  88. )
  89. rating = models.FloatField(
  90. blank=True,
  91. null=True,
  92. validators=[MaxValueValidator(1), MinValueValidator(0)],
  93. )
  94. video = models.FileField(
  95. blank=True,
  96. null=True,
  97. upload_to=get_video_upload_path,
  98. )
  99. marquee = models.ImageField(
  100. blank=True,
  101. null=True,
  102. upload_to=get_marquee_upload_path,
  103. )
  104. screenshot = models.ImageField(
  105. blank=True,
  106. null=True,
  107. upload_to=get_screenshot_upload_path,
  108. )
  109. rom_file = models.FileField(
  110. blank=True,
  111. null=True,
  112. upload_to=get_rom_upload_path,
  113. )
  114. hack = models.BooleanField(
  115. default=False,
  116. )
  117. hack_version = models.CharField(
  118. max_length=255,
  119. blank=True,
  120. null=True,
  121. )
  122. english_patched = models.BooleanField(
  123. default=False,
  124. )
  125. english_patched_version = models.CharField(
  126. max_length=50,
  127. blank=True,
  128. null=True,
  129. )
  130. undub = models.BooleanField(
  131. default=False,
  132. )
  133. featured = models.BooleanField(
  134. default=False,
  135. )
  136. region = models.CharField(
  137. max_length=2,
  138. choices=Region.choices(),
  139. blank=True,
  140. null=True,
  141. )
  142. def __str__(self):
  143. return f"{self.name} for {self.game_system}"
  144. def get_absolute_url(self):
  145. return reverse("games:game_detail", args=[self.slug])