template + accessors

This commit is contained in:
Jean-Marie Place 2021-09-18 10:05:11 +02:00
parent 2a72fb881b
commit ae1feba96c
2 changed files with 44 additions and 26 deletions

View File

@ -85,11 +85,13 @@ def _get_group_info(evaluation_id):
group_name = group["group_name"] or TOUS
if partition not in groups_tree:
groups_tree[partition] = {}
groups_tree[partition][group_name] = group_id
if partition != TOUS:
has_groups = True
nb_groups = len(groups_tree)
return groups_tree, has_groups, nb_groups
name = "G_%s_%s" % (partition, group_name)
groups_tree[partition][group_name] = {
"id": group_id,
"field": BooleanField(name),
"name": name,
}
return groups_tree
class PlacementForm(FlaskForm):
@ -124,12 +126,14 @@ class PlacementForm(FlaskForm):
)
submit = SubmitField("OK")
def __init__(self, formdata=None, data=None):
def __init__(self, groups_tree, formdata=None, data=None):
for partition in groups_tree:
for groupe in groups_tree[partition]:
self.meta.bind_field(self, groups_tree[partition][groupe]['field'])
super().__init__(formdata=formdata, data=data)
self.groups_tree = {}
self.has_groups = None
self.group_tree_length = None
self.nb_groups = None
self.groups_tree = groups_tree
self.nb_partitions = len(self.groups_tree)
self.has_groups = self.nb_partitions > 1
self.set_evaluation_infos(data["evaluation_id"])
def set_evaluation_infos(self, evaluation_id):
@ -137,9 +141,6 @@ class PlacementForm(FlaskForm):
eval_data = sco_evaluations.do_evaluation_list({"evaluation_id": evaluation_id})
if not eval_data:
raise ScoValueError("invalid evaluation_id")
self.groups_tree, self.has_groups, self.nb_groups = _get_group_info(
evaluation_id
)
if self.has_groups:
choices = []
for partition in self.groups_tree:
@ -148,6 +149,9 @@ class PlacementForm(FlaskForm):
choices.append((groupe_id, "%s (%s)" % (str(groupe), partition)))
self.groups.choices = choices
def get_checkbox(self, partition, groupe):
return self.groups_tree[partition][groupe]["field"]
class _DistributeurContinu:
"""Distribue les places selon un ordre numérique."""
@ -182,7 +186,18 @@ class _Distributeur2D:
def placement_eval_selectetuds(evaluation_id):
"""Creation de l'écran de placement"""
form = PlacementForm(
class F(PlacementForm):
pass
setattr(F, 'test', BooleanField('test'))
groups_tree = _get_group_info(evaluation_id)
for partition in groups_tree:
for groupe in groups_tree[partition]:
name = groups_tree[partition][groupe]["name"]
field = groups_tree[partition][groupe]["field"]
# F.__setattr__(name, field)
form = F(
groups_tree,
request.form,
data={"evaluation_id": int(evaluation_id), "groups": TOUS},
)

View File

@ -2,7 +2,7 @@
{% macro render_field(field) %}
<tr>
<td class="wtf-field">{{ field.label }}</td>
<th class="wtf-field">{{ field.label }}</th>
<td class="wtf-field">{{ field()|safe }}
{% if field.errors %}
<ul class=errors>
@ -28,21 +28,24 @@
{{ render_field(form.etiquetage) }}
{% if form.has_groups %}
{{ render_field(form.groups) }}
<!-- Tentative de recréer le choix des groupes sous forme de cases à cocher // demande à créer des champs wtf dynamiquement
{# Tentative de recréer le choix des groupes sous forme de cases à cocher // demande à créer des champs wtf dynamiquement #}
<tr><th>Groupes</th>
<td><table><tbody>
{% for partition in form.groups_tree %}
<tr>
{% if partition == 'Tous' %}
<td rowspan="{{ form.nb_groups }}">Groupes</td>
{% endif %}
<td>{{ partition }}</td>
<td>
{% for groupe in form.groups_tree[partition] %}
{{ groupe }}{{ form[form.groups_tree[partition][groupe]] }}
{% endfor %}
</td>
{# {% if partition == 'Tous' %}#}
{# <td colspan="{{ form.nb_max_groups }}">Tous</td>#}
{# {% else %}#}
<th>{{ partition }}</th>
{% for groupe in form.groups_tree[partition] %}
<td>{{ groupe }}{{ form.get_checkbox(partition, groupe)()|safe() }}</td>
{% endfor %}
{# {% endif %}#}
</tr>
{% endfor %}
-->
</tbody></table></td>
</tr>
{# Fin tentative #}
{% endif %}
{{ render_field(form.file_format) }}
</tbody>