Add FuelPurchase model, List, Create views and templates
This commit is contained in:
parent
64576a7f09
commit
fa19b593d8
10 changed files with 242 additions and 4 deletions
|
@ -1,3 +1,45 @@
|
|||
from django.db import models
|
||||
from django.db.models import F, Q
|
||||
|
||||
# Create your models here.
|
||||
|
||||
class FuelPurchase(models.Model):
|
||||
purchase_date = models.DateField(null=False, blank=False, verbose_name="Purchase Date")
|
||||
total_cost = models.DecimalField(null=False, blank=False, max_digits=10, decimal_places=2, verbose_name="Total Cost (€)")
|
||||
price_per_litre = models.DecimalField(null=False, blank=False, max_digits=6, decimal_places=3, verbose_name="Price per Litre (€)")
|
||||
amount_litres = models.DecimalField(null=False, blank=False, max_digits=7, decimal_places=2, verbose_name="Amount (litres)")
|
||||
|
||||
class Meta:
|
||||
constraints = [
|
||||
models.UniqueConstraint(
|
||||
fields=[
|
||||
'purchase_date',
|
||||
'total_cost',
|
||||
'price_per_litre',
|
||||
'amount_litres',
|
||||
],
|
||||
name='unique_fuel_purchase'
|
||||
),
|
||||
models.CheckConstraint(
|
||||
check=Q(total_cost__gt=0) & Q(total_cost__lt=100),
|
||||
name="total_cost_range"
|
||||
),
|
||||
models.CheckConstraint(
|
||||
check=Q(price_per_litre__gt=0) & Q(price_per_litre__lt=5),
|
||||
name="price_per_litre_range"
|
||||
),
|
||||
models.CheckConstraint(
|
||||
check=Q(amount_litres__gt=0) & Q(amount_litres__lt=100),
|
||||
name="amount_litres_range"
|
||||
),
|
||||
]
|
||||
verbose_name = "Fuel Purchase"
|
||||
verbose_name_plural = "Fuel Purchases"
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.amount_litres and self.price_per_litre:
|
||||
self.amount_litres = self.total_cost / self.price_per_litre
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def __str__(self):
|
||||
date_str = self.purchase_date.strftime("%d.%m.%Y")
|
||||
return f"{date_str} : {self.total_cost} € - {self.price_per_litre} €/L"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue