comet/comet/account/models.py
2018-12-14 00:06:54 +01:00

45 lines
1.3 KiB
Python

from django.db import models
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']
class Sale(models.Model):
"""Represents a sale"""
price = models.DecimalField(
verbose_name="Prix",
max_digits=4,
decimal_places=2
)
account = models.ForeignKey(Account, on_delete=models.SET_NULL, null=True)
title = models.CharField(max_length=255, verbose_name="Intitulé")
class Article(models.Model):
"""An article which can be sold."""
price = models.DecimalField(
verbose_name="Prix unitaire (au €/g)",
max_digits=4,
decimal_places=2
)
name = models.CharField(
max_length=255,
verbose_name="Désignation"
)
def __str__(self):
return ' '.join([self.name, str(self.price), '€/g'])