Skip to main content

forms2: Django forms extra features

The forms2 package provides an enhanced version of django forms. In particular, the SQLAchemy integration and a per-field access control.

https://api.travis-ci.org/paylogic/forms2.png https://pypip.in/v/forms2/badge.png https://coveralls.io/repos/paylogic/forms2/badge.png?branch=master Documentation Status

Installation

pip install forms2

Usage

SQLAlchemy model form example:

class MyModelForm(SAModelForm):
    class Meta:
        model = MyModel
        mapping = {
            'field1': 'instance.child.attribute',
            'field2': 'attribute3',
        }
    field1 = forms.IntegerField()
    field2 = forms.CharField()

A simple example showing field access control:

class MyForm(FieldAccessMixin, Form):
    class Meta:
        access = {
            ('field1', 'field2'): access_admin,
            'field3': MyForm.access_admin1,
            None: lambda user, instance: FieldAccess.enabled,
        }
    field1 = forms.IntegerField()

    @classmethod
    def access_admin1(cls):
        return FieldAccess.readonly

A more realistic example for field access control:

def access_bank_details(user, instance):
    if not has_perm(user, instance, 'edit_bank_account'):
        if has_perm(user, instance, 'edit_contract_id'):
            return FieldAccess.readonly
        return FieldAccess.excluded
    return FieldAccess.enabled

def access_contract_id(user, instance):
    if not (has_perm(user, instance, 'edit_contract_id') and has_perm(user, instance, 'do_stuff')):
            return FieldAccess.readonly

class BankForm(FieldAccessMixin, Form):
    class Meta:
        access = {
            ('bank_account', 'bank_name', 'bank_balance'): access_bank_details,
            'contract_id': access_contract_id,
            None: BankForm.access_check,
        }

    bank_account = forms.CharField()
    bank_name = forms.CharField()
    bank_balance = forms.FloatField()

    contract_id = forms.IntegerField()

    some_other_field = forms.CharField()

    @classmethod
    def access_check(cls):
        return FieldAccess.readonly

Field access control using filter syntax:

@access_filter
def can_view_event(user, instance):
    if user.has_perm('event', 'view'):
        return FieldAccess.enabled

@access_filter
def can_view_merchant(user, instance):
    if user.has_perm('merchant', 'view'):
        return FieldAccess.enabled

@access_filter
def exclude_for_not_finance(user, instance):
    if not user.has_perm('merchant', 'some_financial_permission'):
        return FieldAccess.excluded

...

    access = {
        # Filters are applied left to right, the result is the first filter to return a FieldAccess value

        # This will be enabled if you can view the event, else readonly
        'field_a': can_view_event | default(FieldAccess.readonly),

        # This will be enabled if you can view the event OR the merchant, else excluded
        'field_b': can_view_event | can_view_merchant | default(FieldAccess.excluded),

        # This will be enabled if you can view the event AND the merchant, else excluded
        'field_b2': can_view_event & can_view_merchant | default(FieldAccess.excluded),

        # This will be excluded if you don't have some finance permission, else enabled (this is the default)
        'field_c': exclude_for_not_finance
    }

Contact

If you have questions, bug reports, suggestions, etc. please create an issue on the GitHub project page.

License

This software is licensed under the MIT license

See License

© 2013 Paylogic International.

Changelog

1.1.1

  • Allow to pass queryset as callable to make it lazy (bubenkoff)

1.0.1

  • No logging of exception getting the id from the value (bubenkoff)

0.1.9

  • Removed relative imports (wlansu)

  • Added a default instance=None to constructor method of BaseModelForm (wlansu)

  • Fixed a pep8 issue in tests.__init__() (wlansu)

0.1.8

  • Initial public release

Metadata

Release files for forms2 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for forms2 1.1.1
File Size Uploaded
forms2-1.1.1.tar.gz 6.3 kB Details

Release files / forms2-1.1.1.tar.gz

Download URL forms2-1.1.1.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d3581ed3a3fee28eed59e66541e402252e4eff462da6eef77621474fa1d89170
BLAKE2b-256 checksum
How to use checksums
298ea70ce0ff8a10ad6d2055751ce642b859946a1180ca79f4682748ab040ddf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

1.1.2

1 release file

This release

1.1.1 This release

1 release file

1.1.0

1 release file

1.0.1

1 release file

1.0.0

1 release file

0.1.9

2 release files

0.1.8

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page