Skip to main content

No project description provided

Project description

Set of custom fields useful to implement the Strategy Pattern with Django models.

The Strategies are displayed in SelectBoxes as standard choice field

This package provides the following custom fields:

  • StrategyField

  • MultipleStrategyField

  • StrategyClassField

  • MultipleStrategyClassField

The StrategyField can be accessed as instance of the model with an attribute context that points to model that ‘owns’ the field (inverse relation). So:

Example

from strategy_field.fields import StrategyField
from django.core.mail.backends.filebased.EmailBackend


class Event(models.Model):
    backend = StrategyField()

Event(sender='django.core.mail.backends.filebased.EmailBackend')

Use case

As example we can imagine an application that manages Events that need to be notified to users. Each Occurrence of Event can be notified using different transport, (email, sms,…). We want to be able to add/change the way we send notification, per event basis, simply using the Django admin panel.

from strategy_field.fields import StrategyField
from strategy_field.registry import Registry

class TransportRegistry(Registry)
    pass

class AbstractStrategy(object):
    def __init__(self, context):
        self.context = context

    def send(self):
        raise NotImplementedError

class EmailStrategy(AbstractTransport):
    def send(self):
        ...

class SMSStrategy(AbstractTransport):
    def send(self):
        ...
registry = TransportRegistry(AbstractStrategy)
registry.register(EmailStrategy)
registry.register(SMSStrategy)

class Event(models.Model):
    sender = StrategyField(registry=registry)

Event.objects.get_or_create(sender=EmailStrategy)
...
...
e = Event.objects.get(sender=EmailStrategy)
e.sender.send() # e.sender.context == e

More examples

Use callable

from strategy_field.fields import StrategyField
from strategy_field.registry import Registry

registry1 = Registry()
registry2 = Registry()

class A(model):
    sender = StrategyField(registry=lambda model: model._registry)
    class Meta:
        abstract = True

class C1(A):
    _registry = registry1
    class Meta:
        abstract = True

class C2(A):
    _registry = registry2
    class Meta:
        abstract = True

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

django-strategy-field-2.1.tar.gz (21.4 kB view details)

Uploaded Source

Built Distribution

django_strategy_field-2.1-py2.py3-none-any.whl (11.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-strategy-field-2.1.tar.gz.

File metadata

  • Download URL: django-strategy-field-2.1.tar.gz
  • Upload date:
  • Size: 21.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.8

File hashes

Hashes for django-strategy-field-2.1.tar.gz
Algorithm Hash digest
SHA256 5345b45d71a0f081f3c548adccd0c240b53b9f0590f14857e3074dba5ada2611
MD5 b56d7b5a1a253ea0302c1a5c4098d6d3
BLAKE2b-256 126273d18b79df73c19e6085aeee9baeefe34ec298240eec79fd4ed0dd9cdbf5

See more details on using hashes here.

File details

Details for the file django_strategy_field-2.1-py2.py3-none-any.whl.

File metadata

  • Download URL: django_strategy_field-2.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.8

File hashes

Hashes for django_strategy_field-2.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b44c9ffe19409980c2ac141ed40407f37425454e82481ed07fff2b0cf9ad2eca
MD5 55b21b7e668b3fd9dd89f0eb7415daa2
BLAKE2b-256 1faeda40e1a13d829bc4b9a657a0e2e8d651054c2c767103f622372eed7e652f

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 Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page