models.py 4.3 KB

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