gwift_old/gwift/wish/models.py

66 lines
1.8 KiB
Python
Raw Normal View History

2015-10-01 21:16:37 +02:00
from django.db import models
2015-12-06 20:36:14 +01:00
from django.contrib.auth.models import User
2015-10-01 21:16:37 +02:00
2015-10-21 21:55:02 +02:00
import uuid
2015-10-02 14:01:57 +02:00
2015-12-06 21:11:34 +01:00
class AbstractModel(models.Model):
class Meta:
abstract = True
2015-11-06 14:58:47 +01:00
2015-10-21 21:55:02 +02:00
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
2015-12-08 21:23:34 +01:00
class Wishlist(AbstractModel):
name = models.CharField(max_length=255)
description = models.TextField()
2015-10-21 21:55:02 +02:00
external_id = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
2015-11-06 14:58:47 +01:00
@staticmethod
def create(name, description):
w = Wishlist()
w.name = name
w.description = description
w.save()
return w
2015-10-02 14:01:57 +02:00
2015-12-06 21:11:34 +01:00
2015-12-10 17:12:32 +01:00
class Wish(AbstractModel):
2015-10-02 14:01:57 +02:00
2015-11-06 14:58:47 +01:00
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,
2015-12-06 21:11:34 +01:00
null=True)
2015-11-06 14:58:47 +01:00
@staticmethod
def create(name, description, wishlist):
2015-12-06 21:11:34 +01:00
i = Wish()
2015-11-06 14:58:47 +01:00
i.name = name
i.description = description
i.wishlist = wishlist
i.save()
return i
2015-10-02 14:01:57 +02:00
2015-12-08 21:21:45 +01:00
@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
2015-12-06 21:11:34 +01:00
2015-10-02 14:01:57 +02:00
class Part(models.Model):
2015-12-06 20:36:14 +01:00
wish = models.ForeignKey('Wish')
user = models.ForeignKey(User)
2015-12-06 21:11:34 +01:00
def save(self, force_insert=False, force_update=False, commit=True):
pass