diff options
Diffstat (limited to 'archaeological_operations/forms.py')
-rw-r--r-- | archaeological_operations/forms.py | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/archaeological_operations/forms.py b/archaeological_operations/forms.py index 8c3650c21..1dad60ea1 100644 --- a/archaeological_operations/forms.py +++ b/archaeological_operations/forms.py @@ -354,6 +354,50 @@ class RecordRelationsForm(forms.Form): self.fields['relation_type'].choices = \ models.RelationType.get_types() + @classmethod + def _format_lst(cls, current): + nc = [] + for rel, ope in sorted(current): + if not nc or nc[-1][0] != rel: + nc.append([rel, []]) + nc[-1][1].append(ope) + rendered = u";".join( + [u"{}{} {}".format(rel, _(u":"), u" ; ".join(opes)) + for rel, opes in nc]) + return rendered + + @classmethod + def get_formated_datas(cls, cleaned_datas): + result, current, deleted = [], [], [] + for data in cleaned_datas: + if not data: + continue + try: + relation_type = models.RelationType.objects.get( + pk=data.get('relation_type')) + except models.RelationType.DoesNotExist: + continue + try: + right_record = models.Operation.objects.get( + pk=data.get('right_record')) + except models.Operation.DoesNotExist: + continue + values = [unicode(relation_type), right_record.reference] + if data.get('DELETE'): + deleted.append(values) + else: + current.append(values) + if current: + nc = [] + for rel, ope in sorted(current): + if not nc or nc[-1][0] != rel: + nc.append([rel, []]) + nc[-1][1].append(ope) + result.append((_("Current relations"), cls._format_lst(current))) + if deleted: + result.append((_("Deleted relations"), u" ; ".join(deleted))) + return result + RecordRelationsFormSet = formset_factory(RecordRelationsForm, can_delete=True) RecordRelationsFormSet.form_label = _(u"Relations") |