gwift_old/gwift/wish/models.py

73 lines
2.0 KiB
Python

from django.db import models
from django.contrib.auth.models import User
import uuid
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)
class Wishlist(AbstractModel):
name = models.CharField(max_length=255)
description = models.TextField()
external_id = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
@staticmethod
def create(name, description):
w = Wishlist()
w.name = name
w.description = description
w.save()
return w
class Wish(AbstractModel):
wishlist = models.ForeignKey(Wishlist, related_name='items')
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)
@staticmethod
def create(name, description, wishlist):
i = Wish()
i.name = name
i.description = description
i.wishlist = wishlist
i.save()
return i
@property
def percentage(self):
"""
Calcule le pourcentage de complétion pour un élément.
"""
number_of_linked_parts = Part.objects.filter(wish=self).count()
total = self.number_of_parts * self.numbers_available
percentage = (number_of_linked_parts / total)
return percentage * 100
class WishPart(models.Model):
wish = models.ForeignKey(Wish)
user = models.ForeignKey(User)
unknown_user = models.ForeignKey('UnknownUser')
comment = models.TextField()
done_at = models.DateTimeField()
def save(self, force_insert=False, force_update=False, commit=True):
pass