Example #1
0
class FilterSerializer(serializers.ModelSerializer):
    template_context = dict(url_reverse='filter')
    form_titles = {
        'table': 'Dynamic filter list',
        'new': 'New object',
        'edit': 'Editing object',
    }
    actions = Actions(
        TableAction(TablePosition.FILTER_ROW_END, _('+ Add'), title=_('Add new record'), name='add',
                    action_js="dynamicforms.newRow('{% url url_reverse|add:'-detail' pk='new' format='html' %}'"
                              ", 'record', __TABLEID__);"),
        TableAction(TablePosition.ROW_CLICK, _('Edit'), title=_('Edit record'), name='edit',
                    action_js="dynamicforms.editRow('{% url url_reverse|add:'-detail' pk='__ROWID__' "
                              "format='html' %}'.replace('__ROWID__', $(event.target.parentElement).closest('tr[class=\"df-table-row\"]').attr('data-id')), 'record', __TABLEID__);"),
        TableAction(TablePosition.ROW_END, label=_('Delete'), title=_('Delete record'), name='delete',
                    action_js="dynamicforms.deleteRow('{% url url_reverse|add:'-detail' pk=row.id %}', "
                              "{{row.id}}, 'record', __TABLEID__);"),
        TableAction(TablePosition.FILTER_ROW_END, label=_('Filter'), title=_('Filter'), name='filter',
                    action_js="dynamicforms.defaultFilter(event);")
    )
    show_filter = True

    name = NameTestField(
        label='Name field',
        max_length=list(filter(lambda f: f.name == 'name', Filter._meta.fields))[0].max_length,
        allow_null=list(filter(lambda f: f.name == 'name', Filter._meta.fields))[0].null,
        source='*',

    )

    class Meta:
        model = Filter
        exclude = ()
Example #2
0
class BasicFieldsSerializer(serializers.ModelSerializer):
    form_titles = {
        'table': 'Basic fields list',
        'new': 'New basic fields object',
        'edit': 'Editing basic fields object',
    }
    form_template = 'examples/form_cols.html'

    actions = Actions(TableAction(TablePosition.HEADER,
                                  _('Modal dialog'),
                                  title=_('Dialog test'),
                                  name='modal_dialog',
                                  action_js="examples.testModalDialog();"),
                      add_default_crud=True,
                      add_form_buttons=True)

    boolean_field = fields.BooleanField()
    nullboolean_field = fields.NullBooleanField()
    char_field = fields.CharField()
    email_field = fields.EmailField()
    slug_field = fields.SlugField()
    url_field = fields.URLField()
    uuid_field = fields.UUIDField()
    ipaddress_field = fields.IPAddressField()
    integer_field = fields.IntegerField()
    nullint_field = fields.IntegerField(allow_null=True)
    float_field = fields.FloatField()
    decimal_field = fields.DecimalField(max_digits=5, decimal_places=2)
    datetime_field = fields.DateTimeField(required=False)
    date_field = fields.DateField()
    time_field = fields.TimeField()
    duration_field = fields.DurationField()
    password_field = fields.CharField(password_field=True)

    class Meta:
        model = BasicFields
        exclude = ()
Example #3
0
class ValidatedSerializer(serializers.ModelSerializer):
    form_titles = {
        'table': 'Validated list',
        'new': 'New validated object',
        'edit': 'Editing validated object',
    }
    actions = Actions(
        TableAction(
            TablePosition.HEADER,
            label=_('+ Add (refresh record)'),
            title=_('Add new record'),
            action_js=
            "dynamicforms.newRow('{% url url_reverse|add:'-detail' pk='new' format='html' %}'"
            ", 'record', __TABLEID__);"),
        TableAction(
            TablePosition.HEADER,
            label=_('+ Add (refresh table)'),
            title=_('Add new record'),
            action_js=
            "dynamicforms.newRow('{% url url_reverse|add:'-detail' pk='new' format='html' %}'"
            ", 'table', __TABLEID__);"),
        TableAction(
            TablePosition.HEADER,
            label=_('+ Add (no refresh)'),
            title=_('Add new record'),
            action_js=
            "dynamicforms.newRow('{% url url_reverse|add:'-detail' pk='new' format='html' %}'"
            ", 'no refresh', __TABLEID__);"),
        TableAction(
            TablePosition.ROW_CLICK,
            label=_('Edit'),
            title=_('Edit record'),
            action_js=
            "dynamicforms.editRow('{% url url_reverse|add:'-detail' pk='__ROWID__' format='html'"
            " %}'.replace('__ROWID__', $(event.target.parentElement).closest('tr[class=\"df-table-row\"]').attr('data-id'))"
            ", 'record', __TABLEID__);"),
        TableAction(
            TablePosition.ROW_END,
            label=_('Delete (refresh record)'),
            title=_('Delete record'),
            action_js=
            "dynamicforms.deleteRow('{% url url_reverse|add:'-detail' pk=row.id %}', "
            + "{{row.id}}, 'record', __TABLEID__);"),
        TableAction(
            TablePosition.ROW_END,
            label=_('Delete (refresh table)'),
            title=_('Delete record'),
            action_js=
            "dynamicforms.deleteRow('{% url url_reverse|add:'-detail' pk=row.id %}', "
            + "{{row.id}}, 'table', __TABLEID__);"),
        TableAction(
            TablePosition.ROW_END,
            label=_('Delete (no refresh)'),
            title=_('Delete record'),
            action_js=
            "dynamicforms.deleteRow('{% url url_reverse|add:'-detail' pk=row.id %}', "
            + "{{row.id}}, 'no refresh', __TABLEID__);"),
        # The following action is duplicated unnecessarily just to later eliminate it in suppress_action
        TableAction(
            TablePosition.ROW_END,
            name='del 1',
            label=_('Delete (no refresh)'),
            title=_('Delete record'),
            action_js=
            "dynamicforms.deleteRow('{% url url_reverse|add:'-detail' pk=row.id %}', "
            + "{{row.id}}, 'no refresh', __TABLEID__);"))

    def validate(self, attrs):
        attrs = super().validate(attrs)

        if attrs['amount'] != 5:
            if attrs['code'] != '123':
                raise ValidationError({
                    'amount':
                    'amount can only be different than 5 if code is "123"'
                })

        if attrs['enabled'] is True and attrs['item_type'] == 3:
            raise ValidationError(
                'When enabled you can only choose from first three item types')

        return attrs

    def suppress_action(self, action, request, viewset):
        if action.name == 'del 1':
            return True
        return super().suppress_action(action, request, viewset)

    class Meta:
        model = Validated
        exclude = ()
Example #4
0
class RefreshTypesSerializer(serializers.ModelSerializer):
    form_titles = {
        'table': 'Refresh type list',
        'new': 'New refresh type object',
        'edit': 'Editing refresh type object',
    }
    actions = Actions(
        # Add actions
        # refresh record
        TableAction(
            TablePosition.HEADER,
            label=_('+ Add (refresh record)'),
            title=_('Add new record'),
            action_js=
            "dynamicforms.newRow('{% url url_reverse|add:'-detail' pk='new' format='html' %}'"
            ", 'record', __TABLEID__);"),
        # refresh table
        TableAction(
            TablePosition.HEADER,
            label=_('+ Add (refresh table)'),
            title=_('Add new record'),
            action_js=
            "dynamicforms.newRow('{% url url_reverse|add:'-detail' pk='new' format='html' %}'"
            ", 'table', __TABLEID__);"),
        # no refresh
        TableAction(
            TablePosition.HEADER,
            label=_('+ Add (no refresh)'),
            title=_('Add new record'),
            action_js=
            "dynamicforms.newRow('{% url url_reverse|add:'-detail' pk='new' format='html' %}'"
            ", 'no refresh', __TABLEID__);"),
        # page reload
        TableAction(
            TablePosition.HEADER,
            label=_('+ Add (page reload)'),
            title=_('Add new record'),
            action_js=
            "dynamicforms.newRow('{% url url_reverse|add:'-detail' pk='new' format='html' %}'"
            ", 'page', __TABLEID__);"),
        # redirect
        TableAction(
            TablePosition.HEADER,
            label=_('+ Add (redirect)'),
            title=_('Add new record'),
            action_js=
            "dynamicforms.newRow('{% url url_reverse|add:'-detail' pk='new' format='html' %}'"
            ", 'redirect:{% url 'validated-list' format='html' %}', __TABLEID__);"
        ),
        # custom function
        TableAction(
            TablePosition.HEADER,
            label=_('+ Add (custom function)'),
            title=_('Add new record'),
            action_js=
            "dynamicforms.newRow('{% url url_reverse|add:'-detail' pk='new' format='html' %}'"
            ", 'testRefreshType', __TABLEID__);"),

        # Edit actions
        TableAction(
            TablePosition.ROW_CLICK,
            label=_('Edit'),
            title=_('Edit record'),
            action_js=
            "dynamicforms.editRow('{% url url_reverse|add:'-detail' pk='__ROWID__' format='html'"
            " %}'.replace('__ROWID__', $(event.target.parentElement).closest('tr[class=\"df-table-row\"]').attr('data-id'))"
            ", 'record', __TABLEID__);"),

        # Delete actions
        # refresh record
        TableAction(
            TablePosition.ROW_END,
            label=_('Delete (refresh record)'),
            title=_('Delete record'),
            action_js=
            "dynamicforms.deleteRow('{% url url_reverse|add:'-detail' pk=row.id %}', "
            + "{{row.id}}, 'record', __TABLEID__);"),
        # refresh table
        TableAction(
            TablePosition.ROW_END,
            label=_('Delete (refresh table)'),
            title=_('Delete record'),
            action_js=
            "dynamicforms.deleteRow('{% url url_reverse|add:'-detail' pk=row.id %}', "
            + "{{row.id}}, 'table', __TABLEID__);"),
        # no refresh
        TableAction(
            TablePosition.ROW_END,
            label=_('Delete (no refresh)'),
            title=_('Delete record'),
            action_js=
            "dynamicforms.deleteRow('{% url url_reverse|add:'-detail' pk=row.id %}', "
            + "{{row.id}}, 'no refresh', __TABLEID__);"),
        # The following action is duplicated unnecessarily just to later eliminate it in suppress_action
        TableAction(
            TablePosition.ROW_END,
            name='del 1',
            label=_('Delete (no refresh)'),
            title=_('Delete record'),
            action_js=
            "dynamicforms.deleteRow('{% url url_reverse|add:'-detail' pk=row.id %}', "
            + "{{row.id}}, 'no refresh', __TABLEID__);"),
        # page reload
        TableAction(
            TablePosition.ROW_END,
            label=_('Delete (page reload)'),
            title=_('Delete record'),
            action_js=
            "dynamicforms.deleteRow('{% url url_reverse|add:'-detail' pk=row.id %}', "
            + "{{row.id}}, 'page', __TABLEID__);"),
        # redirect
        TableAction(
            TablePosition.ROW_END,
            label=_('Delete (redirect)'),
            title=_('Delete record'),
            action_js=
            "dynamicforms.deleteRow('{% url url_reverse|add:'-detail' pk=row.id %}', "
            +
            "{{row.id}}, 'redirect:{% url 'validated-list' format='html' %}', __TABLEID__);"
        ),
        # custom function
        TableAction(
            TablePosition.ROW_END,
            label=_('Delete (custom function)'),
            title=_('Delete record'),
            action_js=
            "dynamicforms.deleteRow('{% url url_reverse|add:'-detail' pk=row.id %}', "
            + "{{row.id}}, 'testRefreshType', __TABLEID__);"),
    )

    rich_text_field = fields.RTFField(required=False, allow_blank=True)

    def suppress_action(self, action, request, viewset):
        if action.name == 'del 1':
            return True
        return super().suppress_action(action, request, viewset)

    class Meta:
        model = RefreshType
        exclude = ()