dj-enum


Namedj-enum JSON
Version 0.0.2 PyPI version JSON
download
home_pagehttps://github.com/themrinalsinha/dj_enum
SummaryA Django enum field
upload_time2022-06-25 09:58:58
maintainer
docs_urlNone
authorMrinal Sinha
requires_python>=3.6, <4
license
keywords dj_enum
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # dj_enum (alpha)

### Example
```python
from dj_enum import ActionEnum, ChoiceField

# models.py
class PaymentStatus(ActionEnum):
    NOT_STARTED = 'not_started'
    UNDER_PROCESS = 'under_process'
    FAILED = 'failed'
    SUCCESS = 'success'
    CANCELLED = 'cancelled'
    NOT_REQUIRED = 'not_required'

    __states__ = {
        NOT_STARTED: (),
        UNDER_PROCESS: (NOT_STARTED, FAILED),
        FAILED: (UNDER_PROCESS, ),
        SUCCESS: (UNDER_PROCESS, NOT_STARTED),
        CANCELLED: (NOT_STARTED, NOT_REQUIRED, FAILED, SUCCESS),
        NOT_REQUIRED: (NOT_STARTED, ),
    }

class Orders(models.Model):
    payment_status = ChoiceField(PaymentStatus, default=PaymentStatus.NOT_STARTED, max_length=64)
    created_on = models.DateTimeField(auto_now_add=True)
    ended_on = models.DateTimeField(null=True, blank=True)
```

```ipython
In [1]: from app.models import *

In [2]: order = Order.objects.create()

In [3]: order.payment_status
Out[3]: PaymentStatus.NOT_STARTED # default status

In [4]: order.payment_status = PaymentStatus.UNDER_PROCESS
In [4]: order.save() # will work as it is a valid state change

In [5]: order.payment_status = PaymentStatus.CANCELLED
InvalidStateChangeError: ['under_process -> cancelled is not a valid transition for PaymentStatus']
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/themrinalsinha/dj_enum",
    "name": "dj-enum",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6, <4",
    "maintainer_email": "",
    "keywords": "dj_enum",
    "author": "Mrinal Sinha",
    "author_email": "me@mrinal.xyz",
    "download_url": "https://files.pythonhosted.org/packages/fc/03/f2bddc8ff4b534056c5717d6c1eb484d8c390c7cb13490b28fa5767cd203/dj_enum-0.0.2.tar.gz",
    "platform": null,
    "description": "# dj_enum (alpha)\n\n### Example\n```python\nfrom dj_enum import ActionEnum, ChoiceField\n\n# models.py\nclass PaymentStatus(ActionEnum):\n    NOT_STARTED = 'not_started'\n    UNDER_PROCESS = 'under_process'\n    FAILED = 'failed'\n    SUCCESS = 'success'\n    CANCELLED = 'cancelled'\n    NOT_REQUIRED = 'not_required'\n\n    __states__ = {\n        NOT_STARTED: (),\n        UNDER_PROCESS: (NOT_STARTED, FAILED),\n        FAILED: (UNDER_PROCESS, ),\n        SUCCESS: (UNDER_PROCESS, NOT_STARTED),\n        CANCELLED: (NOT_STARTED, NOT_REQUIRED, FAILED, SUCCESS),\n        NOT_REQUIRED: (NOT_STARTED, ),\n    }\n\nclass Orders(models.Model):\n    payment_status = ChoiceField(PaymentStatus, default=PaymentStatus.NOT_STARTED, max_length=64)\n    created_on = models.DateTimeField(auto_now_add=True)\n    ended_on = models.DateTimeField(null=True, blank=True)\n```\n\n```ipython\nIn [1]: from app.models import *\n\nIn [2]: order = Order.objects.create()\n\nIn [3]: order.payment_status\nOut[3]: PaymentStatus.NOT_STARTED # default status\n\nIn [4]: order.payment_status = PaymentStatus.UNDER_PROCESS\nIn [4]: order.save() # will work as it is a valid state change\n\nIn [5]: order.payment_status = PaymentStatus.CANCELLED\nInvalidStateChangeError: ['under_process -> cancelled is not a valid transition for PaymentStatus']\n```\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "A Django enum field",
    "version": "0.0.2",
    "split_keywords": [
        "dj_enum"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "5edf450c4ee3ca01fbcd1eb3ed608036",
                "sha256": "8724e375d22c8c89cea3c6646b1893ce9d324eecc2c40f293fda05cb048f7e31"
            },
            "downloads": -1,
            "filename": "dj_enum-0.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5edf450c4ee3ca01fbcd1eb3ed608036",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6, <4",
            "size": 3803,
            "upload_time": "2022-06-25T09:58:56",
            "upload_time_iso_8601": "2022-06-25T09:58:56.759156Z",
            "url": "https://files.pythonhosted.org/packages/4e/4f/1d48e9ec59f955f966d55b11df8ec6b7a67d8837378710a94d0ae1ef96d4/dj_enum-0.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "1800529b695b52510c5904704872b45f",
                "sha256": "0e884b2fe2f10eb9708b8b3346c9cf52cd17e1460fead4b3c17fdd241560e236"
            },
            "downloads": -1,
            "filename": "dj_enum-0.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "1800529b695b52510c5904704872b45f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6, <4",
            "size": 3552,
            "upload_time": "2022-06-25T09:58:58",
            "upload_time_iso_8601": "2022-06-25T09:58:58.833362Z",
            "url": "https://files.pythonhosted.org/packages/fc/03/f2bddc8ff4b534056c5717d6c1eb484d8c390c7cb13490b28fa5767cd203/dj_enum-0.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-06-25 09:58:58",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "themrinalsinha",
    "github_project": "dj_enum",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "dj-enum"
}
        
Elapsed time: 0.81574s