import os import uuid from autoslug import AutoSlugField from django.db import models from django.urls import reverse from comments.models import AbstractComment from loans.models import AbstractLoan from website.validators import MaxFileSizeValidator class Category(models.Model): name = models.CharField(max_length=256, verbose_name="nom", unique=True) slug = AutoSlugField(populate_from="name", unique=True) class Meta: ordering = ["name"] verbose_name = "étagère" def __str__(self): return self.name def get_absolute_url(self): return reverse("inventory:category", args=(self.slug,)) class Tag(models.Model): name = models.CharField(max_length=256, verbose_name="nom", unique=True) slug = AutoSlugField(populate_from="name", unique=True) class Meta: ordering = ["name"] def __str__(self): return self.name def get_absolute_url(self): return reverse("inventory:tag", args=(self.slug,)) def image_uuid_path(instance, filename): """ Compute filename for game images as follow: game_img/{random_uuid}.{extension} """ ext = filename.split(".")[-1] filename = "%s.%s" % (uuid.uuid4(), ext) return os.path.join("uploads/logos", filename) class Game(models.Model): title = models.CharField(verbose_name="Nom de l'outil", max_length=256, unique=True) slug = AutoSlugField(populate_from="title", unique=True) category = models.ForeignKey( Category, on_delete=models.RESTRICT, verbose_name="catégorie" ) tags = models.ManyToManyField(Tag, blank=True, verbose_name="tags") description = models.TextField(blank=True, verbose_name="description") image = models.ImageField( upload_to=image_uuid_path, blank=True, verbose_name="image", help_text="L'image doit peser 512 Kio au maximum", validators=[MaxFileSizeValidator(512)], ) missing_elements = models.TextField(blank=True, verbose_name="pièces manquantes") class Meta: ordering = ["title"] verbose_name = "outil" verbose_name_plural = "outils" def __str__(self): return self.title def get_player_range(self): return "" def get_duration_range(self): return "" def get_absolute_url(self): return reverse("inventory:game", args=(self.slug,)) class GameComment(AbstractComment): commented_object = models.ForeignKey( Game, on_delete=models.CASCADE, related_name="comments", verbose_name="outil" ) class Meta: ordering = ["created_on"] verbose_name = "commentaire sur un outil" verbose_name_plural = "commentaires sur des outils" def get_modification_url(self): return reverse( "inventory:modify_game_comment", args=(self.commented_object.slug, self.id) ) class GameLoan(AbstractLoan): lent_object = models.ForeignKey( Game, on_delete=models.CASCADE, verbose_name="outil emprunté" ) class Meta(AbstractLoan.Meta): abstract = False permissions = [("can_see_loan_details", "Can see loan details")]