gestiojeux/inventory/models.py

103 lines
2.9 KiB
Python
Raw Normal View History

from django.db import models
2020-11-22 19:01:21 +01:00
from django.urls import reverse
from django.core.exceptions import ValidationError
from autoslug import AutoSlugField
2020-12-30 00:31:58 +01:00
from website.validators import MaxFileSizeValidator
from comments.models import AbstractComment
from loans.models import AbstractLoan
class Category(models.Model):
2020-12-30 00:31:58 +01:00
name = models.CharField(max_length=256, verbose_name="nom", unique=True)
2020-11-22 19:01:21 +01:00
slug = AutoSlugField(populate_from="name", unique=True)
class Meta:
ordering = ["name"]
verbose_name = "étagère"
def __str__(self):
return self.name
2020-11-22 19:01:21 +01:00
def get_absolute_url(self):
return reverse("inventory:category", args=(self.slug,))
class Tag(models.Model):
2020-12-30 00:31:58 +01:00
name = models.CharField(max_length=256, verbose_name="nom", unique=True)
2020-11-22 19:01:21 +01:00
slug = AutoSlugField(populate_from="name", unique=True)
class Meta:
ordering = ["name"]
def __str__(self):
return self.name
2020-11-22 19:01:21 +01:00
def get_absolute_url(self):
return reverse("inventory:tag", args=(self.slug,))
class Game(models.Model):
2024-07-03 00:52:16 +02:00
title = models.CharField(verbose_name="Nom de l'outil", max_length=256, unique=True)
slug = AutoSlugField(populate_from="title", unique=True)
2020-12-30 00:31:58 +01:00
category = models.ForeignKey(
Category, on_delete=models.RESTRICT, verbose_name="catégorie"
)
tags = models.ManyToManyField(Tag, blank=True, verbose_name="tags")
2020-12-30 00:31:58 +01:00
description = models.TextField(blank=True, verbose_name="description")
image = models.ImageField(
upload_to="game_img/",
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"]
2024-07-03 00:52:16 +02:00
verbose_name = "outil"
verbose_name_plural = "outils"
def __str__(self):
return self.title
2020-11-22 19:01:21 +01:00
def get_player_range(self):
2024-07-03 00:52:16 +02:00
return ""
def get_duration_range(self):
2024-07-03 00:52:16 +02:00
return ""
2020-11-22 19:01:21 +01:00
def get_absolute_url(self):
return reverse("inventory:game", args=(self.slug,))
class GameComment(AbstractComment):
commented_object = models.ForeignKey(
2024-07-03 00:52:16 +02:00
Game, on_delete=models.CASCADE, related_name="comments", verbose_name="outil"
)
class Meta:
ordering = ["created_on"]
2024-07-03 00:52:16 +02:00
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,
2024-07-03 00:52:16 +02:00
verbose_name="outil emprunté"
)
2024-05-02 11:31:34 +02:00
class Meta(AbstractLoan.Meta):
abstract = False
permissions = [("can_see_loan_details", "Can see loan details")]