custom fields useful to implement the Strategy Pattern with Django models.
Project description
django-strategy-field
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_strategy_field-3.2.1.tar.gz.
File metadata
- Download URL: django_strategy_field-3.2.1.tar.gz
- Upload date:
- Size: 9.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d87083f4a0ed7160ba2cff1b718bde670232ae557c6ec4c0a95d06b49419bc5b
|
|
| MD5 |
a01fcf49af8d915e838b5ae54f63947c
|
|
| BLAKE2b-256 |
6b8698783fe60e8e016851c5f96727cb7ed4375aa9bb738b7e4cf6fbc8f3f287
|
Provenance
The following attestation bundles were made for django_strategy_field-3.2.1.tar.gz:
Publisher:
release.yml on saxix/django-strategy-field
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_strategy_field-3.2.1.tar.gz -
Subject digest:
d87083f4a0ed7160ba2cff1b718bde670232ae557c6ec4c0a95d06b49419bc5b - Sigstore transparency entry: 199997674
- Sigstore integration time:
-
Permalink:
saxix/django-strategy-field@09f5edddb189db48c615e558c6b918802be1081f -
Branch / Tag:
refs/tags/3.2.1 - Owner: https://github.com/saxix
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@09f5edddb189db48c615e558c6b918802be1081f -
Trigger Event:
push
-
Statement type:
File details
Details for the file django_strategy_field-3.2.1-py3-none-any.whl.
File metadata
- Download URL: django_strategy_field-3.2.1-py3-none-any.whl
- Upload date:
- Size: 14.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fabdf06052fcea9fe319fd855fb2a308ec36ce513f1902688f369fbdbd00e1c6
|
|
| MD5 |
fff869fd69735624f05c6fbee82ec1ec
|
|
| BLAKE2b-256 |
1502d0f2d8f69e619c4ab446571ad2b954b41f5f39a7ebc16cad927a3ca4f698
|
Provenance
The following attestation bundles were made for django_strategy_field-3.2.1-py3-none-any.whl:
Publisher:
release.yml on saxix/django-strategy-field
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_strategy_field-3.2.1-py3-none-any.whl -
Subject digest:
fabdf06052fcea9fe319fd855fb2a308ec36ce513f1902688f369fbdbd00e1c6 - Sigstore transparency entry: 199997676
- Sigstore integration time:
-
Permalink:
saxix/django-strategy-field@09f5edddb189db48c615e558c6b918802be1081f -
Branch / Tag:
refs/tags/3.2.1 - Owner: https://github.com/saxix
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@09f5edddb189db48c615e558c6b918802be1081f -
Trigger Event:
push
-
Statement type: