diff --git a/management/commands/send_mail_for_food.py b/management/commands/send_mail_for_food.py new file mode 100644 index 0000000..6234577 --- /dev/null +++ b/management/commands/send_mail_for_food.py @@ -0,0 +1,41 @@ +# Copyright (C) 2018-2021 by BDE ENS Paris-Saclay +# SPDX-License-Identifier: GPL-3.0-or-later + +from datetime import date, timedelta + +from django.core.mail import send_mail +from django.core.management import BaseCommand +from django.template.loader import render_to_string +from django.utils.translation import activate +from food.models import Food +from member.models import Club + + +class Command(BaseCommand): + def add_arguments(self, parser): + parser.add_argument("--report", "-r", action='store_true', help="Report the list of food to GCKs") + parser.add_argument("--club", "-c", action='store_true', help="Report the list of food to club") + + def handle(self, *args, **options): + activate('fr') + + foods = Food.objects.filter(end_of_life='').order_by('expiry_date').distinct().all() + + if options["report"]: + plain_text = render_to_string("scripts/food_report.txt", context=dict(foods=foods)) + html = render_to_string("scripts/food_report.html", context=dict(foods=foods)) + send_mail("[Note Kfet] Liste de la nourriture à la Kfet", plain_text, "Note Kfet 2020 ", + recipient_list=["respo-info.bde@lists.crans.org", "gck.bde@lists.crans.org"], + html_message=html) + + if options["club"]: + for club in Club.objects.all(): + if Food.objects.filter(end_of_life='', owner=club).count() > 0: + plain_text = render_to_string("scripts/food_report.txt", + context=dict(foods=foods.filter(owner=club))) + html = render_to_string("scripts/food_report.html", + context=dict(foods=foods.filter(owner=club))) + send_mail("[Note Kfet] Liste de la nourriture de votre club", plain_text, "Note Kfet 2020 ", + recipient_list=[club.email], + html_message=html) + diff --git a/templates/scripts/food_report.html b/templates/scripts/food_report.html new file mode 100644 index 0000000..90677d5 --- /dev/null +++ b/templates/scripts/food_report.html @@ -0,0 +1,51 @@ +{% load i18n %} +{% now "Y-m-d" as today %} + + + + + [Note Kfet] Liste de la bouffe + + + + + + + + + + + + + + {% for food in foods %} + {% if today > food.expiry_date|date:"Y-m-d" %} + {% if food.date_type and food.date_type == "DLC" %} + + {% else %} + + {% endif %} + {% else %} + + {% endif %} + + + + {% if food.date_type %} + + {% else %} + + {% endif %} + + + {% endfor %} + +
ClubNomDate de péremptionDLC/DDMConsigne pour les GCKs
{{ food.owner.name }}{{ food.name }}{{ food.expiry_date }}{{ food.date_type }}--{{ food.order }}
+ +-- +

+ Les GCKs du BDE
+ {% trans "Mail generated by the Note Kfet on the" %} {% now "j F Y à H:i:s" %} +

+ + diff --git a/templates/scripts/food_report.txt b/templates/scripts/food_report.txt new file mode 100644 index 0000000..988b51f --- /dev/null +++ b/templates/scripts/food_report.txt @@ -0,0 +1,14 @@ +{% load i18n %} + + Propriétaire | Nom | Date de péremption | DLC/DDM | Consigne pour les GCKs | +------------------+---------------------+----------------------+---------+--------------------------------------- +{% for food in foods %} + +{{ food.owner.name }} | {{ food.name }} | {{ food.expiry_date }} | {% if food.date_type %}{{ food.date_type }}{% else %} -- {% endif %} | {{ food.order }} + +{% endfor %} + +-- +Les GCKs du BDE + +{% trans "Mail generated by the Note Kfet on the" %} {% now "j F Y à H:i:s" %}