Skip to main content

Extra features for Django Forms

Project description

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

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(Form, FieldAccessMixin):
    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(Form, FieldAccessMixin):
    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

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

forms2-0.1.9.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

forms2-0.1.9.linux-x86_64.tar.gz (5.1 kB view details)

Uploaded Source

File details

Details for the file forms2-0.1.9.tar.gz.

File metadata

  • Download URL: forms2-0.1.9.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for forms2-0.1.9.tar.gz
Algorithm Hash digest
SHA256 99429d26aa08e4f359414273dcd325f7809fd88de41d98ce5b0cdc16ea41de47
MD5 1b1b12c5250c1333652b6fed1318e2a0
BLAKE2b-256 cf5078bdba252e0cca6c17f0c65fde0e43d8c45957b4011de47de760d979c036

See more details on using hashes here.

File details

Details for the file forms2-0.1.9.linux-x86_64.tar.gz.

File metadata

File hashes

Hashes for forms2-0.1.9.linux-x86_64.tar.gz
Algorithm Hash digest
SHA256 e8490879a7920def8398896b30e62f06b5bd9829eb9ba2c8a67539d96863afea
MD5 81c1fa23b78e2a6dfa3680ba9cc65442
BLAKE2b-256 dfe8ba2037bb9beae4c693e322437325c0b3ba4d608049751a79fe329f3e69c3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page