models.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from django.db import models
  2. from django.contrib.auth.models import User
  3. class TypeJournee(models.TextChoices):
  4. PAUVRE = 'pauvre', 'Pauvre en glucides'
  5. NORMAL = 'normal', 'Normal en glucides'
  6. RICHE = 'riche', 'Riche en glucides'
  7. class CategorieAliment(models.TextChoices):
  8. LEGUME_VERT = 'legume_vert', 'Légumes verts'
  9. PROTEINE = 'proteine', 'Protéines'
  10. LIPIDE = 'lipide', 'Lipides'
  11. GLUCIDE = 'glucide', 'Glucides'
  12. FRUIT = 'fruit', 'Fruits & Crudités'
  13. class Aliment(models.Model):
  14. nom = models.CharField(max_length=200)
  15. categorie = models.CharField(
  16. max_length=20,
  17. choices=CategorieAliment.choices
  18. )
  19. portion_standard = models.CharField(max_length=100)
  20. notes = models.TextField(blank=True, null=True)
  21. class Meta:
  22. ordering = ['categorie', 'nom']
  23. def __str__(self):
  24. return f"{self.nom} ({self.get_categorie_display()})"
  25. class Journee(models.Model):
  26. user = models.ForeignKey(User, on_delete=models.CASCADE)
  27. date = models.DateField()
  28. type_journee = models.CharField(
  29. max_length=10,
  30. choices=TypeJournee.choices
  31. )
  32. notes = models.TextField(blank=True, null=True)
  33. class Meta:
  34. ordering = ['-date']
  35. unique_together = ['user', 'date']
  36. def __str__(self):
  37. return f"{self.date} - {self.get_type_journee_display()}"
  38. class TypeRepas(models.TextChoices):
  39. PETIT_DEJEUNER = 'petit_dejeuner', 'Petit-déjeuner'
  40. DEJEUNER = 'dejeuner', 'Déjeuner'
  41. COLLATION = 'collation', 'Collation'
  42. DINER = 'diner', 'Dîner'
  43. class Repas(models.Model):
  44. journee = models.ForeignKey(Journee, on_delete=models.CASCADE, related_name='repas')
  45. type_repas = models.CharField(
  46. max_length=20,
  47. choices=TypeRepas.choices
  48. )
  49. heure = models.TimeField(blank=True, null=True)
  50. notes = models.TextField(blank=True, null=True)
  51. class Meta:
  52. ordering = ['heure']
  53. def __str__(self):
  54. return f"{self.get_type_repas_display()} - {self.journee.date}"
  55. class CompositionRepas(models.Model):
  56. repas = models.ForeignKey(Repas, on_delete=models.CASCADE, related_name='compositions')
  57. aliment = models.ForeignKey(Aliment, on_delete=models.CASCADE)
  58. nombre_portions = models.DecimalField(max_digits=4, decimal_places=1, default=1.0)
  59. notes = models.CharField(max_length=200, blank=True)
  60. def __str__(self):
  61. return f"{self.nombre_portions} x {self.aliment.nom}"