models.py 3.6 KB

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