mirror of
				https://gitlab.crans.org/bde/nk20
				synced 2025-11-03 17:08:47 +01:00 
			
		
		
		
	Open table and shelf life
This commit is contained in:
		@@ -89,7 +89,7 @@ class TransformedFoodForms(forms.ModelForm):
 | 
			
		||||
 | 
			
		||||
    class Meta:
 | 
			
		||||
        model = TransformedFood
 | 
			
		||||
        fields = ('name', 'creation_date', 'owner', 'is_active')
 | 
			
		||||
        fields = ('name', 'creation_date', 'owner', 'is_active', 'shelf_life')
 | 
			
		||||
        widgets = {
 | 
			
		||||
            "owner": Autocomplete(
 | 
			
		||||
                model=Club,
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										19
									
								
								apps/food/migrations/0002_transformedfood_shelf_life.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								apps/food/migrations/0002_transformedfood_shelf_life.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,19 @@
 | 
			
		||||
# Generated by Django 2.2.28 on 2024-07-06 20:37
 | 
			
		||||
 | 
			
		||||
import datetime
 | 
			
		||||
from django.db import migrations, models
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Migration(migrations.Migration):
 | 
			
		||||
 | 
			
		||||
    dependencies = [
 | 
			
		||||
        ('food', '0001_initial'),
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    operations = [
 | 
			
		||||
        migrations.AddField(
 | 
			
		||||
            model_name='transformedfood',
 | 
			
		||||
            name='shelf_life',
 | 
			
		||||
            field=models.DurationField(default=datetime.timedelta(days=3), verbose_name='shelf life'),
 | 
			
		||||
        ),
 | 
			
		||||
    ]
 | 
			
		||||
@@ -165,6 +165,12 @@ class TransformedFood(Food):
 | 
			
		||||
        verbose_name=_('is active'),
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    # Without microbiological analyzes, the storage time is 3 days
 | 
			
		||||
    shelf_life = models.DurationField(
 | 
			
		||||
        verbose_name=_("shelf life"),
 | 
			
		||||
        default=timedelta(days=3),
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    @transaction.atomic
 | 
			
		||||
    def update_allergens(self):
 | 
			
		||||
        # When allergens are changed, simply update the parents' allergens
 | 
			
		||||
@@ -185,7 +191,7 @@ class TransformedFood(Food):
 | 
			
		||||
    def update_expiry_date(self):
 | 
			
		||||
        # When expiry_date is changed, simply update the parents' expiry_date
 | 
			
		||||
        old_expiry_date = self.expiry_date
 | 
			
		||||
        self.expiry_date = self.creation_date + timedelta(days=3)
 | 
			
		||||
        self.expiry_date = self.creation_date + self.shelf_life
 | 
			
		||||
        for ingredient in self.ingredient.iterator():
 | 
			
		||||
            self.expiry_date = min(self.expiry_date, ingredient.expiry_date)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -7,14 +7,18 @@ SPDX-License-Identifier: GPL-3.0-or-later
 | 
			
		||||
 | 
			
		||||
{% block content %}
 | 
			
		||||
<div class="card bg-light mb-3">
 | 
			
		||||
  <h3 class="card-header text-center">
 | 
			
		||||
    Transformed food
 | 
			
		||||
  </h3>
 | 
			
		||||
  <div class="card-footer">
 | 
			
		||||
    <a class="btn btn-sm btn-success" href="{% url 'food:transformed_create' %}" data-turbolinks="false">
 | 
			
		||||
      New transformed food
 | 
			
		||||
    </a>
 | 
			
		||||
  </div>
 | 
			
		||||
  <h3 class="card-header text-center">
 | 
			
		||||
      In preparation
 | 
			
		||||
  </h3>
 | 
			
		||||
  {% render_table table %}
 | 
			
		||||
  <h3 class="card-header text-center">
 | 
			
		||||
      Open
 | 
			
		||||
  </h3>
 | 
			
		||||
  {% render_table open_table %}
 | 
			
		||||
</div>
 | 
			
		||||
{% endblock %}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1,6 @@
 | 
			
		||||
# Copyright (C) 2018-2024 by BDE ENS Paris-Saclay
 | 
			
		||||
# SPDX-License-Identifier: GPL-3.0-or-later
 | 
			
		||||
 | 
			
		||||
from datetime import timedelta
 | 
			
		||||
 | 
			
		||||
from django.db import transaction
 | 
			
		||||
from django.contrib.auth.mixins import LoginRequiredMixin
 | 
			
		||||
from django.http import HttpResponseRedirect
 | 
			
		||||
@@ -234,12 +232,13 @@ class TransformedFoodFormView(ProtectQuerysetMixin):
 | 
			
		||||
 | 
			
		||||
        # Save the aliment and allergens associated
 | 
			
		||||
        transformed_food = form.save(commit=False)
 | 
			
		||||
        # Without microbiological analyzes, the storage time is 3 days
 | 
			
		||||
        transformed_food.expiry_date = transformed_food.creation_date + timedelta(days=3)
 | 
			
		||||
        transformed_food.expiry_date = transformed_food.creation_date
 | 
			
		||||
        transformed_food._force_save = True
 | 
			
		||||
        transformed_food.save()
 | 
			
		||||
        transformed_food.refresh_from_db()
 | 
			
		||||
        return super().form_valid(form)
 | 
			
		||||
        ans = super().form_valid(form)
 | 
			
		||||
        transformed_food.update()
 | 
			
		||||
        return ans
 | 
			
		||||
 | 
			
		||||
    def get_success_url(self, **kwargs):
 | 
			
		||||
        self.object.refresh_from_db()
 | 
			
		||||
@@ -260,14 +259,24 @@ class TransformedFoodCreateView(TransformedFoodFormView, ProtectedCreateView):
 | 
			
		||||
 | 
			
		||||
class TransfomedListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView):
 | 
			
		||||
    """
 | 
			
		||||
    Displays all Activities, and classify if they are on-going or upcoming ones.
 | 
			
		||||
    Displays not ready TransformedFood
 | 
			
		||||
    """
 | 
			
		||||
    model = TransformedFood
 | 
			
		||||
    table_class = TransformedFoodTable
 | 
			
		||||
    ordering = ('-name',)
 | 
			
		||||
    ordering = ('name',)
 | 
			
		||||
    extra_context = {"title": _("Transformed food")}
 | 
			
		||||
 | 
			
		||||
    def get_queryset(self, **kwargs):
 | 
			
		||||
        return super().get_queryset(**kwargs)\
 | 
			
		||||
            .filter(is_ready=False)\
 | 
			
		||||
            .distinct()
 | 
			
		||||
 | 
			
		||||
    def get_context_data(self, **kwargs):
 | 
			
		||||
        context = super().get_context_data(**kwargs)
 | 
			
		||||
        context['open_table'] = TransformedFoodTable(
 | 
			
		||||
            TransformedFood.objects.filter(
 | 
			
		||||
                was_eaten=False,
 | 
			
		||||
                expiry_date__lt=timezone.now()
 | 
			
		||||
            ),
 | 
			
		||||
            prefix="open-")
 | 
			
		||||
        return context
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user