{ "info": { "author": "Luke Burden", "author_email": "lukeburden@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Environment :: Web Environment", "Framework :: Django", "Intended Audience :: Developers", "License :: OSI Approved :: BSD License", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 2", "Programming Language :: Python :: 3" ], "description": "# Django Konst\n\n[![](https://img.shields.io/pypi/v/django-konst.svg)](https://pypi.python.org/pypi/django-konst/)\n[![](https://img.shields.io/badge/license-MIT-blue.svg)](https://pypi.python.org/pypi/django-konst/)\n[![Codecov](https://codecov.io/gh/lukeburden/django-konst/branch/master/graph/badge.svg)](https://codecov.io/gh/lukeburden/django-konst)\n[![CircleCI](https://circleci.com/gh/lukeburden/django-konst.svg?style=svg)](https://circleci.com/gh/lukeburden/django-konst)\n![](https://img.shields.io/github/contributors/lukeburden/django-konst.svg)\n![](https://img.shields.io/github/issues-pr/lukeburden/django-konst.svg)\n\n\n## django-konst\n\n`django-konst` is a utility for Django that makes the definition, use and storage of both integer and string based constants easy and readable. It avoids passing of constants\ninto template contexts and makes evaluation concise and readable.\n\nIt also makes exposure of these constants via forms and DRF serializers simple.\n\n### Definition ###\n\nConstants can be defined with friendly names, backed by either integers or text.\n\n#### Constants ####\n\n```python\nfrom konst import Constant\n\n# states backed by integers\nstates = Constants(\n Constant(pending=0),\n Constant(active=1),\n Constant(inactive=2)\n)\n\n# and another set of constants backed by strings\ncolours = Constants(\n Constant(red=\"FF0000\"),\n Constant(green=\"00FF00\"),\n Constant(yellow=\"FFFF80\"),\n Constant(white=\"FFFFFF\")\n)\n\n```\n\n#### Constant groups ####\n\nAt times, it will be necessary to group constants and test membership within that group.\nTo achieve this, `django-konst` provides a `ConstantGroup` class.\n\n```python\nfrom konst import Constant, ConstantGroup\n\n# states backed by integers\nstates = Constants(\n Constant(active=0),\n Constant(cancelled_ontime=1),\n Constant(cancelled_late=2),\n ConstantGroup(\n \"cancelled\",\n (\"cancelled_ontime\", \"cancelled_late\")\n )\n)\n\n```\n\n#### Within a model ####\n\nWhile not strictly necessary, it is advisable to effectively namespace your constants\nby defining them in the scope of a model definition. This means you have your constants\nwherever you have the model class, as well as any model instance.\n\n\n```python\nfrom django.db import models\nfrom django.utils.translation import ugettext_lazy as _\n\nfrom konst import Constant, ConstantGroup, Constants\nfrom konst.models.fields import (\n ConstantChoiceCharField,\n ConstantChoiceField\n)\n\nclass Apple(models.Model):\n\n purposes = Constants(\n Constant(cooking=0, label=_(\"Cook me!\")),\n Constant(eating=1, label=_(\"Eat me!\")),\n Constant(juicing=2, label=_(\"Juice me!\")),\n Constant(ornamental=3, label=_(\"Just look how pretty I am!\")),\n ConstantGroup(\n \"culinary\", (\"cooking\", \"eating\", \"juicing\")\n )\n )\n colours = Constants(\n Constant(red=\"FF0000\", label=_(\"red\")),\n Constant(green=\"00FF00\", label=_(\"green\")),\n Constant(yellow=\"FFFF80\", label=_(\"yellow\")),\n Constant(white=\"FFFFFF\", label=_(\"white\")),\n )\n\n name = models.CharField(max_length=30)\n purpose = ConstantChoiceField(constants=purposes)\n colour = ConstantChoiceCharField(constants=colours, max_length=30)\n\n```\n\n\n### Use ###\n\nThe entire point of this library is to make the use of constants defined in this way\neasy and concise.\n\n#### In code ####\n\n```python\napple = Apple.objects.get(name='Granny Smith')\napple.purpose.cooking\nTrue\napple.colour.red\nTrue\napple.colour.green\nFalse\n\n# we don't care about the specific purpose, just whether it is as food\n# or not, so use the ConstantGroup!\napple.purpose.culinary\nTrue\n\n```\n\n#### In templates ####\n\n```\n{% if apple.purpose.eating %}\n You should bite this {{ apple.name }}!\n{% endif %}\n```\n\n#### With Django's ORM ####\n\n```python\nred_apples = Apple.objects.filter(colour=Apple.colours.red)\nculinary_apples = Apple.objects.filter(\n purpose__in=Apple.purposes.culinary\n)\n\n```\n\n#### With Django Rest Framework ####\n\nUsing the `konst.extras.drf.fields.ConstantChoiceField` serializer field with the\nDjango Rest Framework it is possible to both output and receive constant values.\n\n```python\nfrom konst.extras.drf.fields import ConstantChoiceField\n\nfrom rest_framework import serializers\n\n\nclass AppleSerializer(serializers.ModelSerializer):\n\n purpose = ConstantChoiceField(Apple.purposes)\n colour = ConstantChoiceField(Apple.colours)\n\n class Meta:\n model = Apple\n fields = (\n \"name\", \"purpose\", \"colour\"\n )\n\n\n# let's see how it handles bad values\nserializer = AppleSerializer(\n data={\n \"name\": \"Fuji\",\n \"colour\": \"blue\",\n \"purpose\": \"dicing\"\n }\n)\nserializer.is_valid()\nFalse\nserializer.errors\n{\n 'colour': [u'\"blue\" is not a valid choice.'],\n 'purpose': [u'\"dicing\" is not a valid choice.']\n}\n\n\n# and now how it handles some good values\nserializer = AppleSerializer(\n data={\n \"name\": \"Fuji\",\n \"colour\": \"red\",\n \"purpose\": \"eating\"\n }\n)\nserializer.is_valid()\nTrue\n\n\n# let's create a database entry!\ninstance = serializer.save()\n\n\n# and now our instance can be interacted with neatly\ninstance.colour.red\nTrue\n\n\n# finally, let's see how this looks when rendering JSON\nAppleSerializer(instance=instance).data\n{\n \"name\": \"Fuji\",\n \"colour\": \"red\",\n \"purpose\": \"eating\"\n}\n\n```\n\n## Contribute\n\n`django-konst` supports a variety of Python and Django versions. It's best if you test each one of these before committing. Our [Circle CI Integration](https://circleci.com) will test these when you push but knowing before you commit prevents from having to do a lot of extra commits to get the build to pass.\n\n### Environment Setup\n\nIn order to easily test on all these Pythons and run the exact same thing that CI will execute you'll want to setup [pyenv](https://github.com/yyuu/pyenv) and install the Python versions outlined in [tox.ini](tox.ini).\n\nIf you are on Mac OS X, it's recommended you use [brew](http://brew.sh/). After installing `brew` run:\n\n```\n$ brew install pyenv pyenv-virtualenv pyenv-virtualenvwrapper\n```\n\nThen:\n\n```\npyenv install -s 2.7.14\npyenv install -s 3.4.7\npyenv install -s 3.5.4\npyenv install -s 3.6.3\npyenv virtualenv 2.7.14\npyenv virtualenv 3.4.7\npyenv virtualenv 3.5.4\npyenv virtualenv 3.6.3\npyenv global 2.7.14 3.4.7 3.5.4 3.6.3\npip install detox\n```\n\nTo run test suite:\n\nMake sure you are NOT inside a `virtualenv` and then:\n\n```\n$ detox\n```\n\nThis will execute the testing matrix in parallel as defined in the `tox.ini`.\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/lukeburden/django-konst", "keywords": "", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "django-konst", "package_url": "https://pypi.org/project/django-konst/", "platform": "", "project_url": "https://pypi.org/project/django-konst/", "project_urls": { "Homepage": "https://github.com/lukeburden/django-konst" }, "release_url": "https://pypi.org/project/django-konst/1.0.1/", "requires_dist": [ "django (>=1.8)" ], "requires_python": "", "summary": "Convenient constants fields for Django", "version": "1.0.1" }, "last_serial": 5621169, "releases": { "1.0.0": [ { "comment_text": "", "digests": { "md5": "ab032e3e6bf87d7fbfda55a8d10960fb", "sha256": "0911ac4e229eb0681183e691b8f4bd1bf3e6ec553c7493b223fd36366a51ed5f" }, "downloads": -1, "filename": "django_konst-1.0.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "ab032e3e6bf87d7fbfda55a8d10960fb", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 10801, "upload_time": "2017-11-19T23:16:29", "url": "https://files.pythonhosted.org/packages/32/73/7110478a4a40c2650e04e46086da6ffc790e16ab90eb8d0804fa1447300c/django_konst-1.0.0-py2.py3-none-any.whl" } ], "1.0.1": [ { "comment_text": "", "digests": { "md5": "c8f100ee6b7cb4015ff320be9c34c429", "sha256": "d7183a7575a6c6e7cb47d683305adf1ebdf77a222bc6401056ca744525235667" }, "downloads": -1, "filename": "django_konst-1.0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "c8f100ee6b7cb4015ff320be9c34c429", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 12759, "upload_time": "2019-08-01T22:55:06", "url": "https://files.pythonhosted.org/packages/a5/3e/b702a79258dce91003c408eb58446a1438025a8f59cb5190224c56d5c20e/django_konst-1.0.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "d5485b538d4d8269aeee4538665cbbcb", "sha256": "6cfa4b50bbab9c162bd95fabd8cb5c505ad8499968849f18d425d8119f7f4e57" }, "downloads": -1, "filename": "django-konst-1.0.1.tar.gz", "has_sig": false, "md5_digest": "d5485b538d4d8269aeee4538665cbbcb", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9808, "upload_time": "2019-08-01T22:55:08", "url": "https://files.pythonhosted.org/packages/a4/d2/8d1d4c6e09dcb314ce3e2f7f3cca3a50e398ec5a998ae246fc10ab9ed8ed/django-konst-1.0.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "c8f100ee6b7cb4015ff320be9c34c429", "sha256": "d7183a7575a6c6e7cb47d683305adf1ebdf77a222bc6401056ca744525235667" }, "downloads": -1, "filename": "django_konst-1.0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "c8f100ee6b7cb4015ff320be9c34c429", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 12759, "upload_time": "2019-08-01T22:55:06", "url": "https://files.pythonhosted.org/packages/a5/3e/b702a79258dce91003c408eb58446a1438025a8f59cb5190224c56d5c20e/django_konst-1.0.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "d5485b538d4d8269aeee4538665cbbcb", "sha256": "6cfa4b50bbab9c162bd95fabd8cb5c505ad8499968849f18d425d8119f7f4e57" }, "downloads": -1, "filename": "django-konst-1.0.1.tar.gz", "has_sig": false, "md5_digest": "d5485b538d4d8269aeee4538665cbbcb", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9808, "upload_time": "2019-08-01T22:55:08", "url": "https://files.pythonhosted.org/packages/a4/d2/8d1d4c6e09dcb314ce3e2f7f3cca3a50e398ec5a998ae246fc10ab9ed8ed/django-konst-1.0.1.tar.gz" } ] }