<commit_before><commit_msg>Test envoi convoc<commit_after># -*- coding: utf8 -*-
import datetime
import os
from django.conf import settings
from django.core.mail import EmailMessage
from django.core.management.base import BaseCommand
from mailrobot.models import Mail
from django_apogee.models import InsAdmEtp
from duck_examen.models import EtapeExamenModel, Etape, EtapeSettingsDerouleModel, DeroulementExamenModel, \
    RattachementCentreExamen
import mimetypes
from duck_utils.models import EtapeSettings, TemplateHtmlModel
from duck_utils.utils import get_recipients


class Command(BaseCommand):
    help = "TEST envoi une convocation à un seul étudiant ou plusieurs nominativement TEST"

    #   args = numeros etudiants
    #   options pourraient servir à differencier les informations transmises en args: numero etudiant, numero de dossier, code etape, etc

    def handle(self, *args, **options):
        today = datetime.date.today()
        template = TemplateHtmlModel.objects.get(name='convocation_examen')
        template_mail = Mail.objects.get(name='mail_envoi_convocation')
        deroules = {}
        for code_individu in args:

            for inscription in InsAdmEtp.inscrits_condi.filter(cod_ind=code_individu,
                                                                        ).distinct():
                recipients = get_recipients(inscription.cod_ind, inscription.cod_anu)
                rattachement = RattachementCentreExamen.objects.filter(InsAdmEtp=inscription)
                deroulement = DeroulementExamenModel.objects.filter(etape=inscription.etape)

                for de in deroulement.etape.deroulementexamenmodel_set.all(): #DeroulementExamenModel.objects.filter(etape=esd.etape):
                    deroules[de.session] = de.get_deroulement_detail(rattachement.type_amenagement, rattachement.parcours)

                    etape_settings = EtapeSettings.objects.get(etape=inscription.etape, cod_anu=inscription.cod_anu)
                    coordonnee_de_contact = etape_settings.contact_info

                    debug_start = 0

                    mail = template_mail.make_message(recipients=recipients)
                    for r in inscription.rattachementcentreexamen_set.all():
                        context = {
                            'annee': today.year - 1,
                            'session': r.session,
                            'coordonnee_de_contact': coordonnee_de_contact,
                            'inscription': inscription,
                            'jours': ", ".join(deroules[r.session].get_jours()),
                            'salles': r.get_salle(),
                            'deroule': deroules[r.session].deroulement_parse(),
                            'centre': r.centre.label
                        }
                        pdf = template.get_pdf_file(context)
                        mail.attach(filename="convocation_{}.pdf".format(r.session),
                                    content=pdf)
                    mail.send()
                    if settings.DEBUG: # On envoit 10 mails au max en debug
                        debug_start += 1
                        if debug_start == 10:
                            break





{'fraction_non_alphanumeric': 0.07523219814241486, 'fraction_numerical': 0.002476780185758514, 'mean_word_length': 1.9751381215469612, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1461442', 'n_tokens_mistral': 894, 'n_tokens_neox': 850, 'n_words': 171}