<commit_before>import os
from uuid import uuid4

from pathlib import PurePath
from django import forms
from django.core.exceptions import ValidationError
from django.core.files import File
from django.core.files.storage import default_storage
from django.utils.translation import ugettext as _

from crispy_forms.helper import FormHelper

from agir.lib.form_components import *
from agir.people.person_forms.field_groups import get_form_part

from ..form_mixins import MetaFieldsMixin
from ..models import Person, PersonFormSubmission
from .fields import is_actual_model_field


class PersonTagChoiceField(forms.ModelChoiceField):
    def label_from_instance(self, obj):
        return obj.description


class BasePersonForm(MetaFieldsMixin, forms.ModelForm):
    """base form class for using PersonForm models

    It should not be used by itself, but only with people.actions.get_people_form_class
    """
    person_form_instance = None
    is_edition = False

    def get_meta_fields(self):
        return [field['id']
                for fieldset in self.person_form_instance.custom_fields
                for field in fieldset['fields']
                if field.get('person_field') and not is_actual_model_field(field)]

    def __init__(self, *args, **kwargs):

        super().__init__(*args, **kwargs)

        if self.person_form_instance.editable:
            try:
                submission = PersonFormSubmission.objects.get(
                    person=self.instance,
                    form=self.person_form_instance
                )
                for id, value in submission.data.items():
                    self.initial[id] = value
                self.is_edition = True
            except PersonFormSubmission.DoesNotExist:
                pass
<commit_msg>fix l'édition de formulaire pour les rsvps<commit_after>import os
from uuid import uuid4

from pathlib import PurePath
from django import forms
from django.core.exceptions import ValidationError
from django.core.files import File
from django.core.files.storage import default_storage
from django.utils.translation import ugettext as _

from crispy_forms.helper import FormHelper

from agir.lib.form_components import *
from agir.people.person_forms.field_groups import get_form_part

from ..form_mixins import MetaFieldsMixin
from ..models import Person, PersonFormSubmission
from .fields import is_actual_model_field


class PersonTagChoiceField(forms.ModelChoiceField):
    def label_from_instance(self, obj):
        return obj.description


class BasePersonForm(MetaFieldsMixin, forms.ModelForm):
    """base form class for using PersonForm models

    It should not be used by itself, but only with people.actions.get_people_form_class
    """
    person_form_instance = None
    is_edition = False

    def get_meta_fields(self):
        return [field['id']
                for fieldset in self.person_form_instance.custom_fields
                for field in fieldset['fields']
                if field.get('person_field') and not is_actual_model_field(field)]

    def __init__(self, *args, **kwargs):

        super().__init__(*args, **kwargs)

        if self.person_form_instance.editable:
            submission = PersonFormSubmission.objects.filter(
                person=self.instance,
                form=self.person_form_instance
            ).order_by('modified').last()

            if submission is not None:
                for id, value in submission.data.items():
                    self.initial[id] = value
                self.is_edition = True

{'fraction_non_alphanumeric': 0.06788732394366197, 'fraction_numerical': 0.0005633802816901409, 'mean_word_length': 2.9455555555555555, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2955206', 'n_tokens_mistral': 967, 'n_tokens_neox': 934, 'n_words': 266}