models.py 3.8 KB

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