From 37303df74cb8a4189844b35b1c6376c7213f96fd Mon Sep 17 00:00:00 2001 From: Jean-Marie Place Date: Fri, 24 Sep 2021 16:00:55 +0200 Subject: [PATCH] add_fields ok --- app/scodoc/sco_placement.py | 39 +++++++++---------- app/templates/scodoc/forms/placement.html | 46 +---------------------- 2 files changed, 19 insertions(+), 66 deletions(-) diff --git a/app/scodoc/sco_placement.py b/app/scodoc/sco_placement.py index ef97a7eeb..957f6016f 100644 --- a/app/scodoc/sco_placement.py +++ b/app/scodoc/sco_placement.py @@ -75,6 +75,10 @@ SEQ = "Continue" TOUS = "Tous" +def get_name(selfgroup, partition): + return 'GR_{partition]_{groupe]' + + def _get_group_info(evaluation_id): # groupes groups = sco_groups.do_evaluation_listegroupes(evaluation_id, include_default=True) @@ -86,7 +90,7 @@ def _get_group_info(evaluation_id): group_name = group["group_name"] or TOUS if partition not in groups_tree: groups_tree[partition] = {} - name = "GR_%s_%s" % (partition, group_name) + name = get_name(partition, group_name) groups_tree[partition][group_name] = { "id": group_id, "field": BooleanField(name), @@ -132,32 +136,23 @@ class PlacementForm(FlaskForm): submit = SubmitField("OK") def add_dynamic(self, name, field): - breakpoint() - bind_field = self._fields[name] = self.meta.bind_field( - form=self, unbound_field=field, options={"name": name, "prefix": self._prefix} - ) - self.__setattr__(name, bind_field) - breakpoint() - self.dynamic_fields[name] = field + self._unbound_fields.append((name, field)) def __init__(self, groups_tree, formdata=None, data=None, **kwargs): + self.groups_tree = groups_tree + self.nb_groups = sum([len(x) for x in groups_tree.values()]) + self.has_groups = self.nb_groups > 1 + # add dynamic fields to Class + for partition in groups_tree: + for groupe in groups_tree[partition]: + name = self.groups_tree[partition][groupe]['name'] + field = self.groups_tree[partition][groupe]['field'] + self.add_dynamic(name, field) + # create the form (including new dynamic fields) and proccess all fields super().__init__(formdata=formdata, data=data, **kwargs) # first remove previous dynamic fields that might have been created for this form - self.clear_dynamics() - self.add_dynamic("test_cb", BooleanField("test_cb")) # compute the values for new dynamic fields evaluation_id = data["evaluation_id"] - # self.groups_tree = groups_tree - # self.nb_groups = sum([len(x) for x in groups_tree.values()]) - # self.has_groups = self.nb_groups > 1 - # add dynamic fields to Class - # for partition in groups_tree: - # for groupe in groups_tree[partition]: - # name = self.groups_tree[partition][groupe].name - # field = self.groups_tree[partition][groupe].field - # self.dynamic_fields.add(name) - # self._fields[name] = field - # create the form (including new dynamic fields) and proccess all fields """Initialise les données du formulaire avec les données de l'évaluation.""" eval_data = sco_evaluations.do_evaluation_list({"evaluation_id": evaluation_id}) if not eval_data: @@ -174,7 +169,7 @@ class PlacementForm(FlaskForm): self.has_groups = self.nb_partitions > 1 def get_checkbox(self, partition, groupe): - return self.groups_tree[partition][groupe]["field"] + return self._fields[get_name(partition, groupe)] class _DistributeurContinu: diff --git a/app/templates/scodoc/forms/placement.html b/app/templates/scodoc/forms/placement.html index e8f7d73fe..aa6ab460a 100644 --- a/app/templates/scodoc/forms/placement.html +++ b/app/templates/scodoc/forms/placement.html @@ -27,8 +27,7 @@ {{ render_field(form.nb_rangs) }} {{ render_field(form.etiquetage) }} {% if form.has_groups %} - {{ render_field(form.groups) }} - {% endif %} {{ render_field(form.file_format) }} @@ -52,46 +50,6 @@ -
- {{ form.evaluation_id }} - {{ form.csrf_token }} - - - {{ render_field(form.surveillants) }} - {{ render_field(form.batiment) }} - {{ render_field(form.salle) }} - {{ render_field(form.nb_rangs) }} - {{ render_field(form.etiquetage) }} - {% if form.has_groups %} -{# {{ render_field(form.groups) }}#} - {{ form.get_dynamic("test_cb") }} - {# Tentative de recréer le choix des groupes sous forme de cases à cocher // demande à créer des champs wtf dynamiquement #} -{# #} -{# #} -{# #} - {# Fin tentative #} - {% endif %} - {{ render_field(form.file_format) }} - -
Groupes#} -{# {% for partition in form.groups_tree %}#} -{# #} -{#1 {% if partition == 'Tous' %}#} -{#1 #} -{#1 {% else %}#} -{# #} -{# {% for groupe in form.groups_tree[partition] %}#} -{# #} -{# {% endfor %}#} -{#1 {% endif %}#} -{# #} -{# {% endfor %}#} -{#
Tous{{ partition }}{{ groupe }}{{ form.get_checkbox(partition, groupe)()|safe() }}
-

- - - -

Explications