Django custom field to implement the strategy pattern
Project description
=====================
django-strategy-field
=====================
DFS is a custom field to enable the implementation of the `Strategy Pattern`_ with
the Django models.
The Strategies are displayed in SelectBoxes as standard choice field
.. _Strategy Pattern: http://www.oodesign.com/strategy-pattern.html
This package provides the following custom fields:
* StrategyField
* MultipleStrategyField
* StrategyClassField
* MultipleStrategyClassField
The *StrategyField can be accessed as instance of the model and have an
attribute `context` that point to model (reverse relation)
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.
.. code-block:: python
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 = Registry()
registry.register(EmailStrategy)
registry.register(SMSStrategy)
class Event(models.Model):
sender = StrategyField(registry)
Event.objects.get_or_create(sender=EmailStrategy)
...
...
e = Event.objects.get(sender=EmailStrategy)
e.sender.send() # e.sender.context == e
Project links
=============
+--------------------+----------------+--------------+---------------------------+
| Stable | |master-build| | |master-cov| | |
+--------------------+----------------+--------------+---------------------------+
| Development | |dev-build| | |dev-cov| | |
+--------------------+----------------+--------------+---------------------------+
| Project home page: |https://github.com/saxix/django-strategy-field |
+--------------------+---------------+-------------------------------------------+
| Issue tracker: |https://github.com/saxix/django-strategy-field/issues?sort |
+--------------------+---------------+-------------------------------------------+
| Download: |http://pypi.python.org/pypi/django-strategy-field/ |
+--------------------+---------------+-------------------------------------------+
.. |master-build| image:: https://secure.travis-ci.org/saxix/django-strategy-field.png?branch=master
:target: http://travis-ci.org/saxix/django-strategy-field/
.. |master-cov| image:: https://codecov.io/github/saxix/django-strategy-field/coverage.svg?branch=master
:target: https://codecov.io/github/saxix/django-strategy-field?branch=develop
.. |dev-build| image:: https://secure.travis-ci.org/saxix/django-strategy-field.png?branch=develop
:target: http://travis-ci.org/saxix/django-strategy-field/
.. |dev-cov| image:: https://codecov.io/github/saxix/django-strategy-field/coverage.svg?branch=develop
:target: https://codecov.io/github/saxix/django-strategy-field?branch=develop
django-strategy-field
=====================
DFS is a custom field to enable the implementation of the `Strategy Pattern`_ with
the Django models.
The Strategies are displayed in SelectBoxes as standard choice field
.. _Strategy Pattern: http://www.oodesign.com/strategy-pattern.html
This package provides the following custom fields:
* StrategyField
* MultipleStrategyField
* StrategyClassField
* MultipleStrategyClassField
The *StrategyField can be accessed as instance of the model and have an
attribute `context` that point to model (reverse relation)
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.
.. code-block:: python
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 = Registry()
registry.register(EmailStrategy)
registry.register(SMSStrategy)
class Event(models.Model):
sender = StrategyField(registry)
Event.objects.get_or_create(sender=EmailStrategy)
...
...
e = Event.objects.get(sender=EmailStrategy)
e.sender.send() # e.sender.context == e
Project links
=============
+--------------------+----------------+--------------+---------------------------+
| Stable | |master-build| | |master-cov| | |
+--------------------+----------------+--------------+---------------------------+
| Development | |dev-build| | |dev-cov| | |
+--------------------+----------------+--------------+---------------------------+
| Project home page: |https://github.com/saxix/django-strategy-field |
+--------------------+---------------+-------------------------------------------+
| Issue tracker: |https://github.com/saxix/django-strategy-field/issues?sort |
+--------------------+---------------+-------------------------------------------+
| Download: |http://pypi.python.org/pypi/django-strategy-field/ |
+--------------------+---------------+-------------------------------------------+
.. |master-build| image:: https://secure.travis-ci.org/saxix/django-strategy-field.png?branch=master
:target: http://travis-ci.org/saxix/django-strategy-field/
.. |master-cov| image:: https://codecov.io/github/saxix/django-strategy-field/coverage.svg?branch=master
:target: https://codecov.io/github/saxix/django-strategy-field?branch=develop
.. |dev-build| image:: https://secure.travis-ci.org/saxix/django-strategy-field.png?branch=develop
:target: http://travis-ci.org/saxix/django-strategy-field/
.. |dev-cov| image:: https://codecov.io/github/saxix/django-strategy-field/coverage.svg?branch=develop
:target: https://codecov.io/github/saxix/django-strategy-field?branch=develop
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
django-strategy-field-0.2.tar.gz
(13.7 kB
view details)
Built Distribution
File details
Details for the file django-strategy-field-0.2.tar.gz
.
File metadata
- Download URL: django-strategy-field-0.2.tar.gz
- Upload date:
- Size: 13.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 589164ef5d599921c4811a56bb9c6b16825090aebcb690934e51b54bc9f7a667 |
|
MD5 | 2ccc752ab2057cf05b8a45864a91d2d1 |
|
BLAKE2b-256 | 22a2b0703ed50380f82b44792d07883733b24b2acd186c2d786a1d381b95f084 |
File details
Details for the file django_strategy_field-0.2-py2.py3-none-any.whl
.
File metadata
- Download URL: django_strategy_field-0.2-py2.py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d06988dc52deb97427ea5f853385c1f2571d25ce8021b37fa0a5a0ded2e8553 |
|
MD5 | 8a7580d07daa6df8caddb16b4ef0ba12 |
|
BLAKE2b-256 | 56c3458d97702c49786d5347e010d1c264b07ea134657e98be6a536f8df09387 |