comet/comet/account/models.py
2019-02-05 19:43:17 +01:00

51 lines
1.5 KiB
Python

from django.db import models
from django.utils import timezone
class Account(models.Model):
"""Account for a member."""
balance = models.DecimalField(
verbose_name="Solde",
max_digits=4,
decimal_places=2,
default=0
)
firstname = models.CharField(max_length=255, verbose_name="Nom")
lastname = models.CharField(max_length=255, verbose_name="Prénom")
email = models.EmailField(verbose_name="Email")
def calc_balance(self):
"""Adjust the ballance according to the sales,
without saving the model."""
self.balance = Sale.objects.filter(account=self)\
.aggregate(models.Sum('price'))['price__sum'] or 0
class Sale(models.Model):
"""Represents a sale"""
price = models.DecimalField(
verbose_name="Prix (€)",
max_digits=5,
decimal_places=3
)
account = models.ForeignKey(Account, on_delete=models.SET_NULL, null=True)
title = models.CharField(max_length=255, verbose_name="Intitulé")
date = models.DateTimeField(verbose_name="Date", default=timezone.now)
class Article(models.Model):
"""An article which can be sold."""
price = models.DecimalField(
verbose_name="Prix unitaire (au €/g)",
max_digits=4,
decimal_places=3
)
name = models.CharField(
max_length=255,
verbose_name="Désignation"
)
def __str__(self):
return ' '.join([self.name, str(self.price), '€/g'])