from rest_framework import serializers from bs4 import BeautifulSoup from html import unescape from .models import GlpiTickets, GlpiTicketvalidations, GlpiUsers def extract_information_aa_v1(content): soup = BeautifulSoup(content, "html.parser") def extract_field(field_name): field = soup.find(string=lambda text: field_name in text) if field: parts = field.split(':', 1) if len(parts) > 1: return parts[1].strip() return None matricule = extract_field("Matricule") fonction = extract_field("Fonction") structure = extract_field("Structure") date_absence = extract_field("la journée du") commentaire = extract_field("Commentaire") return matricule, fonction, structure, date_absence, commentaire def extract_information_aa_v2(content): content = unescape(content) # Unescape HTML entities soup = BeautifulSoup(content, "html.parser") # Extract the text and split by line breaks (
) text_lines = soup.get_text(separator="\n").splitlines() # Initialize variables to hold the extracted data matricule = None fonction = None structure = None date_absence = None commentaire = None # Iterate through each line and extract the data for line in text_lines: line = line.strip() if line.startswith("Fonction"): fonction = line.replace("Fonction :", "").strip() elif line.startswith("Structure"): structure = line.replace("Structure :", "").strip() elif line.startswith("Est autorisé (e) à s'absenter la journée du"): date_absence = line.replace("Est autorisé (e) à s'absenter la journée du :", "").strip() elif line.startswith("Commentaire"): commentaire = line.replace("Commentaire :", "").strip() elif line.startswith("Matricule"): matricule = line.replace("Matricule :", "").strip() return matricule, fonction, structure, date_absence, commentaire def extract_information_aa(content): # First, try extracting using the first function matricule, fonction, structure, date_absence, commentaire = extract_information_aa_v1(content) # If any key field is not extracted, apply the second method if not (matricule and date_absence): matricule, fonction, structure, date_absence, commentaire = extract_information_aa_v2(content) return matricule, fonction, structure, date_absence, commentaire def extract_information_ae(content): content = unescape(content) # Unescape HTML entities soup = BeautifulSoup(content, "html.parser") # Extract the text and split by line breaks (
) text_lines = soup.get_text(separator="\n").splitlines() # Initialize variables to hold the extracted data matricule = None fonction = None structure = None date_reprise = None commentaire = None # Iterate through each line and extract the data for line in text_lines: line = line.strip() if line.startswith("Matricule"): matricule = line.replace("Matricule :", "").strip() elif line.startswith("Fonction"): fonction = line.replace("Fonction :", "").strip() elif line.startswith("Structure"): structure = line.replace("Structure :", "").strip() elif line.startswith("Est autorisé (e) de reprendre à :"): date_reprise = line.replace("Est autorisé (e) de reprendre à :", "").strip() elif line.startswith("Commentaire :"): commentaire = line.replace("Commentaire :", "").strip() return matricule, fonction, structure, date_reprise, commentaire def extract_information_bs(content): content = unescape(content) # Unescape HTML entities soup = BeautifulSoup(content, "html.parser") rows = soup.find_all('tr') # Initialize variables to hold the extracted data matricule = None fonction = None structure = None date_sortie = None heure_sortie = None motif = None # Iterate through each row in the table and extract the data for index, row in enumerate(rows): columns = row.find_all('td') if len(columns) == 2: # Ensure there are exactly 2 columns key = columns[0].get_text(strip=True).replace(" :", "").strip() value = columns[1].get_text(strip=True) if key.startswith("Matricule"): matricule = value elif key.startswith("Fonction"): fonction = value elif key.startswith("Structure"): structure = value elif key.startswith("Est autorisé (e) de sortir le"): date_sortie = value elif key.startswith("A"): heure_sortie = value elif key.startswith("Pour affaire Personnelle"): motif = value return matricule, fonction, structure, date_sortie, heure_sortie, motif def extract_information_hs(content): content = unescape(content) soup = BeautifulSoup(content, "html.parser") # Extract the text and split by line breaks (
) text_lines = soup.get_text(separator="\n").splitlines() # Initialize variables to hold the extracted data matricule = None fonction = None structure = None date_requisition = None motif_requisition = None heure_debut = None heure_fin = None lieu_travail = None # Iterate through each line and extract the data for line in text_lines: line = line.strip() if line.startswith("matricule"): matricule = line.replace("matricule :", "").strip() elif line.startswith("Fonction"): fonction = line.replace("Fonction :", "").strip() elif line.startswith("Structure"): structure = line.replace("Structure :", "").strip() elif line.startswith("Date de réquisition :"): date_requisition = line.replace("Date de réquisition :", "").strip() elif line.startswith("Le motif de la réquisition :"): motif_requisition = line.replace("Le motif de la réquisition :", "").strip() elif line.startswith("Heure de début :"): heure_debut = line.replace("Heure de début :", "").strip() elif line.startswith("Heure de fin :"): heure_fin = line.replace("Heure de fin :", "").strip() elif line.startswith("Lieu de travail :"): lieu_travail = line.replace("Lieu de travail :", "").strip() return matricule, fonction, structure, date_requisition, motif_requisition, heure_debut, heure_fin, lieu_travail def extract_information_co(content): content = unescape(content) # Unescape HTML entities soup = BeautifulSoup(content, "html.parser") rows = soup.find_all('tr') matricule = None fonction = None structure = None exercice = None date_depart = None interim = None adresse_conge = None # Iterate through each row in the table and extract the data for index, row in enumerate(rows): columns = row.find_all('td') if len(columns) == 2: # Ensure there are exactly 2 columns key = columns[0].get_text(strip=True).replace(" :", "").strip() value = columns[1].get_text(strip=True) if key.startswith("Matricule"): matricule = value elif key.startswith("Fonction"): fonction = value elif key.startswith("Structure"): structure = value elif key.startswith("Exercice"): exercice = value elif key.startswith("Date de Départ"): date_depart = value elif key.startswith("Intérimaire"): interim = value elif key.startswith("Adresse durant le Congé"): adresse_conge = value return matricule, fonction, structure, exercice, date_depart, interim, adresse_conge def extract_information_cr(content): content = unescape(content) # Unescape HTML entities soup = BeautifulSoup(content, "html.parser") # Extract the text and split by line breaks (
) text_lines = soup.get_text(separator="\n").splitlines() # Initialize variables to hold the extracted data matricule = None fonction = None structure = None de = None a = None commentaire = None # Iterate through each line and extract the data for line in text_lines: line = line.strip() if line.startswith("matricule"): matricule = line.replace("matricule :", "").strip() elif line.startswith("Fonction"): fonction = line.replace("Fonction :", "").strip() elif line.startswith("Structure"): structure = line.replace("Structure :", "").strip() elif line.startswith("De"): de = line.replace("De :", "").strip() elif line.startswith("A :"): a = line.replace("A :", "").strip() elif line.startswith("commentaire"): commentaire = line.replace("commentaire :", "").strip() return matricule, fonction, structure, de, a, commentaire class GlpiTicketAESerializer(serializers.ModelSerializer): # Include fields you want to extract outside of validations comment_validation = serializers.SerializerMethodField() validation_date = serializers.SerializerMethodField() validation_status = serializers.SerializerMethodField() validate_by_user = serializers.SerializerMethodField() matricule = serializers.SerializerMethodField() date_reprise = serializers.SerializerMethodField() commentaire = serializers.SerializerMethodField() class Meta: model = GlpiTickets fields = ['id', 'name', 'date_creation', 'matricule', 'date_reprise', 'commentaire', 'validation_status', 'validate_by_user', 'validation_date', 'comment_validation',] def get_comment_validation(self, obj): # Assuming you want the latest comment from the validations latest_validation = obj.validations.order_by('-validation_date').first() return getattr(latest_validation, 'comment_validation', None) if latest_validation else None def get_validation_date(self, obj): # Assuming you want the latest validation date latest_validation = obj.validations.order_by('-validation_date').first() return getattr(latest_validation, 'validation_date', None) if latest_validation else None def get_validate_by_user(self, obj): # Assuming you want the latest validator's registration number latest_validation = obj.validations.order_by('-validation_date').first() return getattr(latest_validation.validate_by_user, 'registration_number', None) if latest_validation and latest_validation.validate_by_user else None def get_validation_status(self, obj): # Assuming you want the latest validator's registration number latest_validation = obj.validations.order_by('-validation_date').first() return getattr(latest_validation, 'status', None) if latest_validation and latest_validation.validate_by_user else None def get_matricule(self, obj): matricule, _, _, _, _ = extract_information_ae(obj.content) return matricule def get_fonction(self, obj): _, fonction, _, _, _ = extract_information_ae(obj.content) return fonction def get_structure(self, obj): _, _, structure, _, _ = extract_information_ae(obj.content) return structure def get_date_reprise(self, obj): _, _, _, date_reprise, _ = extract_information_ae(obj.content) return date_reprise def get_commentaire(self, obj): _, _, _, _, commentaire = extract_information_ae(obj.content) return commentaire class GlpiTicketBSSerializer(serializers.ModelSerializer): # Include fields you want to extract outside of validations comment_validation = serializers.SerializerMethodField() validation_status = serializers.SerializerMethodField() validation_date = serializers.SerializerMethodField() validate_by_user = serializers.SerializerMethodField() matricule = serializers.SerializerMethodField() date_sortie = serializers.SerializerMethodField() heure_sortie = serializers.SerializerMethodField() motif = serializers.SerializerMethodField() class Meta: model = GlpiTickets fields = ['id', 'name', 'date_creation', 'matricule', 'date_sortie', 'heure_sortie', 'motif', 'validation_status', 'validation_date', 'validate_by_user', 'comment_validation'] def get_comment_validation(self, obj): # Assuming you want the latest comment from the validations latest_validation = obj.validations.order_by('-validation_date').first() return getattr(latest_validation, 'comment_validation', None) if latest_validation else None def get_validation_date(self, obj): # Assuming you want the latest validation date latest_validation = obj.validations.order_by('-validation_date').first() return getattr(latest_validation, 'validation_date', None) if latest_validation else None def get_validate_by_user(self, obj): # Assuming you want the latest validator's registration number latest_validation = obj.validations.order_by('-validation_date').first() return getattr(latest_validation.validate_by_user, 'registration_number', None) if latest_validation and latest_validation.validate_by_user else None def get_validation_status(self, obj): # Assuming you want the latest validator's registration number latest_validation = obj.validations.order_by('-validation_date').first() return getattr(latest_validation, 'status', None) if latest_validation and latest_validation.validate_by_user else None def get_matricule(self, obj): matricule, _, _, _, _, _ = extract_information_bs(obj.content) return matricule def get_fonction(self, obj): _, fonction, _, _, _, _ = extract_information_bs(obj.content) return fonction def get_structure(self, obj): _, _, structure, _, _, _ = extract_information_bs(obj.content) return structure def get_date_sortie(self, obj): _, _, _, date_sortie, _, _ = extract_information_bs(obj.content) return date_sortie def get_heure_sortie(self, obj): _, _, _, _, heure_sortie, _ = extract_information_bs(obj.content) return heure_sortie def get_motif(self, obj): _, _, _, _, _, motif = extract_information_bs(obj.content) return motif class GlpiTicketHSSerializer(serializers.ModelSerializer): # Include fields you want to extract outside of validations comment_validation = serializers.SerializerMethodField() validation_date = serializers.SerializerMethodField() validation_status = serializers.SerializerMethodField() validate_by_user = serializers.SerializerMethodField() matricule = serializers.SerializerMethodField() date_requisition = serializers.SerializerMethodField() motif_requisition = serializers.SerializerMethodField() heure_debut = serializers.SerializerMethodField() heure_fin = serializers.SerializerMethodField() lieu_travail = serializers.SerializerMethodField() class Meta: model = GlpiTickets fields = ['id', 'name', 'date_creation', 'matricule', 'date_requisition', 'motif_requisition', 'heure_debut', 'heure_fin', 'lieu_travail', 'validation_status' , 'validation_date', 'validate_by_user', 'comment_validation',] def get_comment_validation(self, obj): # Assuming you want the latest comment from the validations latest_validation = obj.validations.order_by('-validation_date').first() return getattr(latest_validation, 'comment_validation', None) if latest_validation else None def get_validation_date(self, obj): # Assuming you want the latest validation date latest_validation = obj.validations.order_by('-validation_date').first() return getattr(latest_validation, 'validation_date', None) if latest_validation else None def get_validate_by_user(self, obj): # Assuming you want the latest validator's registration number latest_validation = obj.validations.order_by('-validation_date').first() return getattr(latest_validation.validate_by_user, 'registration_number', None) if latest_validation and latest_validation.validate_by_user else None def get_validation_status(self, obj): # Assuming you want the latest validator's registration number latest_validation = obj.validations.order_by('-validation_date').first() return getattr(latest_validation, 'status', None) if latest_validation and latest_validation.validate_by_user else None def get_matricule(self, obj): matricule, _, _, _, _, _, _, _ = extract_information_hs(obj.content) return matricule def get_fonction(self, obj): _, fonction, _, _, _, _, _, _ = extract_information_hs(obj.content) return fonction def get_structure(self, obj): _, _, structure, _, _, _, _, _ = extract_information_hs(obj.content) return structure def get_date_requisition(self, obj): _, _, _, date_requisition, _, _, _, _ = extract_information_hs(obj.content) return date_requisition def get_motif_requisition(self, obj): _, _, _, _, motif_requisition, _, _, _ = extract_information_hs(obj.content) return motif_requisition def get_heure_debut(self, obj): _, _, _, _, _, heure_debut, _, _ = extract_information_hs(obj.content) return heure_debut def get_heure_fin(self, obj): _, _, _, _, _, _, heure_fin, _ = extract_information_hs(obj.content) return heure_fin def get_lieu_travail(self, obj): _, _, _, _, _, _, _, lieu_travail = extract_information_hs(obj.content) return lieu_travail class GlpiTicketAASerializer(serializers.ModelSerializer): # Include fields you want to extract outside of validations comment_validation = serializers.SerializerMethodField() validation_status = serializers.SerializerMethodField() validate_by_user = serializers.SerializerMethodField() validation_date = serializers.SerializerMethodField() matricule = serializers.SerializerMethodField() date_absence = serializers.SerializerMethodField() commentaire = serializers.SerializerMethodField() class Meta: model = GlpiTickets fields = ['id', 'name', 'date_creation', 'matricule', 'date_absence', 'commentaire', 'validation_status', 'validate_by_user', 'validation_date', 'comment_validation',] def get_comment_validation(self, obj): # Assuming you want the latest comment from the validations latest_validation = obj.validations.order_by('-validation_date').first() return getattr(latest_validation, 'comment_validation', None) if latest_validation else None def get_validation_date(self, obj): # Assuming you want the latest validation date latest_validation = obj.validations.order_by('-validation_date').first() return getattr(latest_validation, 'validation_date', None) if latest_validation else None def get_validate_by_user(self, obj): # Assuming you want the latest validator's registration number latest_validation = obj.validations.order_by('-validation_date').first() return getattr(latest_validation.validate_by_user, 'registration_number', None) if latest_validation and latest_validation.validate_by_user else None def get_validation_status(self, obj): # Assuming you want the latest validator's registration number latest_validation = obj.validations.order_by('-validation_date').first() return getattr(latest_validation, 'status', None) if latest_validation and latest_validation.validate_by_user else None def get_matricule(self, obj): matricule, _, _, _, _ = extract_information_aa(obj.content) return matricule def get_fonction(self, obj): _, fonction, _, _, _ = extract_information_aa(obj.content) return fonction def get_structure(self, obj): _, _, structure, _, _ = extract_information_aa(obj.content) return structure def get_date_absence(self, obj): _, _, _, date_absence, _ = extract_information_aa(obj.content) return date_absence def get_commentaire(self, obj): _, _, _, _, commentaire = extract_information_aa(obj.content) return commentaire class GlpiTicketCOSerializer(serializers.ModelSerializer): # Include fields you want to extract outside of validations comment_validation = serializers.SerializerMethodField() validation_date = serializers.SerializerMethodField() validate_by_user = serializers.SerializerMethodField() matricule = serializers.SerializerMethodField() exercice = serializers.SerializerMethodField() date_depart = serializers.SerializerMethodField() interim = serializers.SerializerMethodField() adresse_conge = serializers.SerializerMethodField() class Meta: model = GlpiTickets fields = ['name', 'date_creation', 'matricule', 'exercice', 'date_depart', 'interim', 'adresse_conge', 'validation_date', 'validate_by_user', 'comment_validation',] def get_comment_validation(self, obj): # Assuming you want the latest comment from the validations latest_validation = obj.validations.order_by('-validation_date').first() return latest_validation.comment_validation if latest_validation else None def get_validation_date(self, obj): # Assuming you want the latest validation date latest_validation = obj.validations.order_by('-validation_date').first() return latest_validation.validation_date if latest_validation else None def get_validate_by_user(self, obj): # Assuming you want the latest validator latest_validation = obj.validations.order_by('-validation_date').first() return latest_validation.validate_by_user.registration_number def get_matricule(self, obj): matricule, _, _, _, _, _, _ = extract_information_co(obj.content) return matricule def get_fonction(self, obj): _, fonction, _, _, _, _, _ = extract_information_co(obj.content) return fonction def get_structure(self, obj): _, _, structure, _, _, _, _ = extract_information_co(obj.content) return structure def get_exercice(self, obj): _, _, _, exercice, _, _, _ = extract_information_co(obj.content) return exercice def get_date_depart(self, obj): _, _, _, _, date_depart, _, _ = extract_information_co(obj.content) return date_depart def get_interim(self, obj): _, _, _, _, _, interim, _ = extract_information_co(obj.content) return interim def get_adresse_conge(self, obj): _, _, _, _, _, _, adresse_conge = extract_information_co(obj.content) return adresse_conge class GlpiTicketCRSerializer(serializers.ModelSerializer): # Include fields you want to extract outside of validations comment_validation = serializers.SerializerMethodField() validation_status = serializers.SerializerMethodField() validate_by_user = serializers.SerializerMethodField() validation_date = serializers.SerializerMethodField() matricule = serializers.SerializerMethodField() de = serializers.SerializerMethodField() a = serializers.SerializerMethodField() commentaire = serializers.SerializerMethodField() class Meta: model = GlpiTickets fields = ['id', 'name', 'date_creation', 'matricule', 'de', 'a', 'commentaire', 'validation_status', 'validation_date', 'validate_by_user', 'comment_validation',] def get_comment_validation(self, obj): # Assuming you want the latest comment from the validations latest_validation = obj.validations.order_by('-validation_date').first() return getattr(latest_validation, 'comment_validation', None) if latest_validation else None def get_validation_date(self, obj): # Assuming you want the latest validation date latest_validation = obj.validations.order_by('-validation_date').first() return getattr(latest_validation, 'validation_date', None) if latest_validation else None def get_validate_by_user(self, obj): # Assuming you want the latest validator's registration number latest_validation = obj.validations.order_by('-validation_date').first() return getattr(latest_validation.validate_by_user, 'registration_number', None) if latest_validation and latest_validation.validate_by_user else None def get_validation_status(self, obj): # Assuming you want the latest validator's registration number latest_validation = obj.validations.order_by('-validation_date').first() return getattr(latest_validation, 'status', None) if latest_validation and latest_validation.validate_by_user else None def get_matricule(self, obj): matricule, _, _, _, _, _ = extract_information_cr(obj.content) return matricule def get_fonction(self, obj): _, fonction, _, _, _, _ = extract_information_cr(obj.content) return fonction def get_structure(self, obj): _, _, structure, _, _, _ = extract_information_cr(obj.content) return structure def get_de(self, obj): _, _, _, de, _, _ = extract_information_cr(obj.content) return de def get_a(self, obj): _, _, _, _, a, _ = extract_information_cr(obj.content) return a def get_commentaire(self, obj): _, _, _, _, _, commentaire = extract_information_cr(obj.content) return commentaire ''' class GlpiUserSerializer(serializers.ModelSerializer): validate_by_user = serializers.SerializerMethodField() class Meta: model = GlpiUsers fields = ['realname', 'firstname', 'registration_number', 'validate_by_user'] def get_validate_by_user(self, obj): return f"{obj.realname} {obj.firstname}" class GlpiTicketValidationSerializer(serializers.ModelSerializer): validate_by_user = GlpiUserSerializer(read_only=True) class Meta: model = GlpiTicketvalidations fields = ['validate_by_user', 'validation_date', 'comment_validation'] '''