Example #1
0
    def extend_form(self, form_class, request):
        class PeoplePageForm(form_class):
            def get_people_fields(self, with_function):
                for field_id, field in self._fields.items():

                    # XXX this is kind of implicitly set by the fieldset
                    if field_id.startswith("people_"):
                        if with_function or not field_id.endswith("_function"):
                            yield field_id, field

            def get_people_and_function(self):
                fields = self.get_people_fields(with_function=False)

                for field_id, field in fields:
                    if field.data is True:
                        person_id = field.id.hex
                        function = self._fields[field_id + "_function"].data

                        yield person_id, function

            def update_model(self, model):
                super(PeoplePageForm, self).update_model(model)
                model.content["people"] = list(self.get_people_and_function())

            def apply_model(self, model):
                super(PeoplePageForm, self).apply_model(model)

                fields = self.get_people_fields(with_function=False)
                people = dict(model.content.get("people", []))

                for field_id, field in fields:
                    if field.id.hex in people:
                        self._fields[field_id].data = True
                        self._fields[field_id + "_function"].data = people[field.id.hex]

        builder = WTFormsClassBuilder(PeoplePageForm)
        builder.set_current_fieldset(_("People"))

        for person in self.get_selectable_people(request):
            field_id = builder.add_field(field_class=BooleanField, label=person.title, required=False, id=person.id)
            builder.add_field(
                field_class=StringField, label=_("Function"), required=False, dependency=FieldDependency(field_id, "y")
            )

        return builder.form_class
Example #2
0
    def extend_form(self, form_class, request):

        # XXX this is kind of implicitly set by the builder
        fieldset_id = 'people'
        fieldset_label = _("People")

        class PeoplePageForm(form_class):

            def get_people_fields(self, with_function):
                for field_id, field in self._fields.items():
                    if field_id.startswith(fieldset_id):
                        if with_function or not field_id.endswith('_function'):
                            yield field_id, field

            def get_people_and_function(self, selected_only=True):
                fields = self.get_people_fields(with_function=False)

                for field_id, field in fields:
                    if not selected_only or field.data is True:
                        person_id = field.id.hex
                        function = self._fields[field_id + '_function'].data

                        yield person_id, function

            def is_ordered_people(self, existing_people):
                """ Returns True if the current list of people is ordered
                from A to Z.

                """
                if not existing_people:
                    return True

                ordered_people = OrderedDict(self.get_people_and_function(
                    selected_only=False
                ))

                existing_people = [
                    key for key, value in existing_people
                    if key in ordered_people
                ]

                sorted_existing_people = sorted(
                    existing_people, key=list(ordered_people.keys()).index)

                return existing_people == sorted_existing_people

            def update_model(self, model):
                previous_people = model.content.get('people', [])

                super().update_model(model)

                if self.is_ordered_people(previous_people):
                    # if the people are ordered a-z, we take the ordering from
                    # get_people_and_function, which comes by A-Z already
                    model.content['people'] = list(
                        self.get_people_and_function()
                    )
                else:
                    # if the people are not ordered we keep the order of the
                    # existing list and add the new people at the end
                    existing = set()
                    selected = {
                        key for key, function
                        in self.get_people_and_function()
                    }

                    old_people = list()
                    new_people = list()

                    for id, function in previous_people:
                        if id in selected:
                            old_people.append((id, function))
                            existing.add(id)

                    for id, function in self.get_people_and_function():
                        if id not in existing:
                            new_people.append((id, function))

                    model.content['people'] = old_people + new_people

            def apply_model(self, model):
                super().apply_model(model)

                fields = self.get_people_fields(with_function=False)
                people = dict(model.content.get('people', []))

                for field_id, field in fields:
                    if field.id.hex in people:
                        self._fields[field_id].data = True
                        self._fields[field_id + '_function'].data\
                            = people[field.id.hex]

        builder = WTFormsClassBuilder(PeoplePageForm)
        builder.set_current_fieldset(fieldset_label)

        for person in self.get_selectable_people(request):
            field_id = builder.add_field(
                field_class=BooleanField,
                label=person.title,
                required=False,
                id=person.id
            )
            builder.add_field(
                field_class=StringField,
                label=_("Function"),
                required=False,
                dependency=FieldDependency(field_id, 'y')
            )

        return builder.form_class