gwift_old/gwift/wish/models.py

63 lines
1.9 KiB
Python

import uuid
from django.db import models
from django.contrib.auth.models import User
class AbstractModel(models.Model):
class Meta:
abstract = True
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class UnknownUser(models.Model):
name = models.CharField(max_length=255)
email = models.CharField(max_length=255, unique=True)
class Wishlist(AbstractModel):
name = models.CharField(max_length=255)
description = models.TextField()
external_id = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
class Wish(AbstractModel):
wishlist = models.ForeignKey(
Wishlist,
related_name='items',
on_delete=models.DO_NOTHING
)
name = models.CharField(max_length=255)
description = models.TextField()
picture = models.ImageField(null=True)
numbers_available = models.IntegerField(default=1)
number_of_parts = models.IntegerField(null=True)
estimated_price = models.DecimalField(max_digits=19, decimal_places=2,
null=True)
@property
def percentage_of_completion(self):
"""Calcule le pourcentage de complétion pour un élément."""
number_of_linked_parts = WishPart.objects.filter(wish=self).count()
total = self.number_of_parts * self.numbers_available
percentage = (number_of_linked_parts / total)
return percentage * 100
def get_absolute_url(self):
pass
class WishPart(models.Model):
wish = models.ForeignKey(Wish, on_delete=models.DO_NOTHING)
user = models.ForeignKey(User, null=True, on_delete=models.DO_NOTHING)
unknown_user = models.ForeignKey('UnknownUser', null=True, on_delete=models.DO_NOTHING)
comment = models.TextField(null=True, blank=True)
done_at = models.DateTimeField(auto_now_add=True)