{
"info": {
"author": "Alexander Oblovatniy",
"author_email": "oblovatniy@gmail.com",
"bugtrack_url": null,
"classifiers": [
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
"License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)",
"Natural Language :: English",
"Operating System :: OS Independent",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3.4",
"Topic :: Software Development :: Libraries"
],
"description": "django-candv-choices\n====================\n\n|pypi_package| |pypi_downloads| |python_versions| |license|\n\nUse complex constants built with `candv`_ library instead of standard\n`choices`_ fields for `Django`_ models.\n\nTry `online live demo `_! Use\n``demo``/``demo`` as login/pass for authentication.\n\n|demo_preview|\n\n\n**Table of contents**\n\n.. contents::\n :local:\n :depth: 1\n :backlinks: none\n\n\nInstallation\n------------\n\nInstall from `PyPI `_:\n\n.. code-block:: bash\n\n $ pip install django-candv-choices\n\n\nProblem overview\n----------------\n\nWell, you need to define some constant choices for your Django model field.\nLet's start from defining constants themselves:\n\n.. code-block:: python\n\n # constants.py\n from django.utils.translation import ugettext_lazy as _\n\n AUTH_TYPE_BASIC = 'BASIC'\n AUTH_TYPE_DIGEST = 'DIGEST'\n AUTH_TYPE_CLIENT_CERT = 'CLIENT-CERT'\n AUTH_TYPE_FORM = 'FORM'\n\n AUTH_TYPES = (\n (AUTH_TYPE_BASIC, _(\"HTTP Basic Authentication\")),\n (AUTH_TYPE_DIGEST, _(\"HTTP Digest Authentication \")),\n (AUTH_TYPE_CLIENT_CERT, _(\"HTTPS Client Authentication \")),\n (AUTH_TYPE_FORM, _(\"Form Based Authentication \")),\n )\n\nHere we define constant names and attach verbose names to them. Bloated\ndefinition, no docstring for constants group, no docstings per constant. What\nif you need to define some help text per constant? 4 more definitions? Well,\nthen just imagine, how you will attach them. And what about other attributes?\nAnd what about adding some methods for constants? How about getting constant by\nits name? By value? And how about performing some operations on the whole\nconstants group?\n\nOnly at this point you may end up with one big module which will work only with\none group of constants. And this work will be a big pain.\n\nBut OK, let's go further and define some DB model field:\n\n.. code-block:: python\n\n # models.py\n from django.db import models\n from django.utils.translation import ugettext_lazy as _\n\n from . constants import AUTH_TYPES, AUTH_TYPE_BASIC\n\n\n class Request(models.Model):\n\n auth_type = models.CharField(\n verbose_name=_(\"Auth type\"),\n help_text=_(\"Example of default constants\"),\n choices=AUTH_TYPES,\n blank=False,\n max_length=11,\n default=AUTH_TYPE_BASIC)\n\n3 things to mention here:\n\n* you have to import constant group itself;\n* you may have to import dafault value too;\n* you need go back to constants definition, iterate over each constant,\n calculate its length and select the longest value to pass it as\n ``max_length`` argument. And don't try to make a mistake, or you will be\n punished otherwise.\n\nI use ``CharField`` here intentionally. It can be good to use ``IntegerField``,\n``PositiveSmallIntegerField`` and so on, but it is very probable that you will\nbe willing someone to kill you due to hidden bugs.\n\nNow it's showtime! Let's render our field:\n\n.. code-block:: jinja\n\n
\n {% for r in requests %}\n
{{ r.auth_type }}
\n {% endfor %}\n
\n\nWhat do you see? ``BASIC``, ``DIGEST``, ``FORM``, etc. Oops! How to get our\nhuman messages like ``HTTP Basic Authentication``?\n\nYou need to convert constants group to ``dict`` and pass it to template's\ncontext! But wait, this is not the end. You can not access dict values directly\nwithin templates. You need to create a library of template tags, register a\nfilter and load the library to template:\n\n.. code-block:: python\n\n # templatetags/custom_tags.py\n from django import template\n\n register = template.Library()\n\n\n @register.filter\n def lookup(d, key):\n return d[key]\n\n\n.. code-block:: jinja\n\n {% load custom_tags %}\n
\n {% for r in requests %}\n
{{ AUTH_TYPES|lookup:r.auth_type }}
\n {% endfor %}\n
\n\n\nThis is madness!\n\n\nSolution\n--------\n\nThe solution is to use `candv`_ and this library. The former allows you to\ndefine stand-alone groups of complex constants and latter allows you to use\nthose constants as choises.\n\nLet's examine some simple example and define some constants:\n\n.. code-block:: python\n\n # constants.py\n from candv import SimpleConstant, Constants\n\n class METHOD_TYPE(Constants):\n \"\"\"\n Available HTTP methods.\n \"\"\"\n GET = SimpleConstant()\n PUT = SimpleConstant()\n POST = SimpleConstant()\n DELETE = SimpleConstant()\n TRACE = SimpleConstant()\n\nHere we defined a group of constants with no attributes. Looks pretty, let's\nuse it:\n\n.. code-block:: python\n\n # models.py\n from candv_x.django.choices import ChoicesField\n\n from django.db import models\n from django.utils.translation import ugettext_lazy as _\n\n from . constants import METHOD_TYPE\n\n class Request(models.Model):\n\n method = ChoicesField(\n verbose_name=_(\"method\"),\n help_text=_(\"Example of simple candv constants\"),\n choices=METHOD_TYPE,\n blank=False,\n )\n\nThat's all. You can pass some default value if you want,\ne.g. ``default=METHOD_TYPE.GET``.\n\nNow you can render it:\n\n.. code-block:: jinja\n\n
\n {% for r in requests %}\n
{{ r.method.name }}
\n {% endfor %}\n
\n\nThe output will contain ``GET``, ``PUT``, ``POST``, etc. Want more? Let's add\nvalues, verbose names and help texts:\n\n.. code-block:: python\n\n # constants.py\n from candv import VerboseValueConstant, Values\n from django.utils.translation import ugettext_lazy as _\n\n class RESULT_TYPE(Values):\n \"\"\"\n Possible operation results.\n \"\"\"\n SUCCESS = VerboseValueConstant(\n value='2C7517',\n verbose_name=_(\"Success\"),\n help_text=_(\"Yay! Everything is good!\")\n )\n FAILURE = VerboseValueConstant(\n value='A30D0D',\n verbose_name=_(\"Failure\"),\n help_text=_(\"Oops! Something went wrong!\")\n )\n PENDING = VerboseValueConstant(\n value='E09F26',\n verbose_name=_(\"Pending\"),\n help_text=_(\"Still waiting for the task to complete...\")\n )\n\n..\n\n Please, refer to `candv usage`_ to learn how to define and use constants.\n You may find `candv customization`_ useful too.\n\nHere we have used `Values`_ as container and `VerboseValueConstant`_ as class\nfor items. Each constant has a ``name`` (e.g. ``SUCCESS``), a value, a verbose\ntext and a help text. All of this you can access directly from everywhere.\n\nField definition does not differ much from previous:\n\n.. code-block:: python\n\n # models.py\n from candv_x.django.choices import ChoicesField\n\n from django.db import models\n from django.utils.translation import ugettext_lazy as _\n\n from . constants import RESULT_TYPE\n\n class Request(models.Model):\n\n result = ChoicesField(\n verbose_name=_(\"result\"),\n help_text=_(\"Example of complex candv constants with verbose names, \"\n \"help texts and inner values\"),\n choices=RESULT_TYPE,\n blank=False,\n default=RESULT_TYPE.SUCCESS,\n )\n\nYou may use ``blank=True`` if you wish, there's no problem. Let's output our\ndata:\n\n.. code-block:: jinja\n\n
\n {% for r in requests %}\n
\n
\n {{ r.result.verbose_name }}\n
\n
\n {% endfor %}\n
\n\nNot so hard, innit?\n\nYou can pass any constants to ``ChoicesField`` from your old projects or\nexternal libraries. Enjoy!\n\n\nCaveats\n-------\n\n* Django admin renders choices by converting them to strings. So,\n ``__str__`` and ``__unicode__`` methods will be automatically overriden for\n constant items. It will return the name of the constant. By default,\n constants in ``candv`` do not have those methods at all (I cannot find a\n reason why the should to), so it seems not to be a problem. Just be aware.\n* ``candv`` supports creating `hierarchies of constants`_. If you have some\n reason to use them as choices for DB field, take into accout that choices\n will be built only from top-level group of constants.\n\n\nThings to think about\n---------------------\n\n* Django has `MultipleChoiceField`_ and `TypedMultipleChoiceField`_. I haven't\n used used them, but I think it can be useful to implement analogues for\n 'candv', especially for ``MultipleChoiceField``.\n* I think, there is a place to think about implementation of full support of\n hierarchies. Maybe it's possible to make some nested choices, or at least\n flatten them.\n\n\nChangelog\n---------\n\n*You can click a version name to see a diff with the previous one.*\n\n* `1.1.5`_ (Aug 1, 2015)\n\n * Fix usage of default values for migrations in Django >= 1.7\n (`issue #8`_).\n * Implement field serializer for restframework as a separate library\n `django-rf-candv-choices`_ (`issue #9`_).\n\n* `1.1.4`_ (Jul 2, 2015)\n\n * Add support for Python 3 (`issue #6`_).\n * Add support for migrations in Django >= 1.7 (`issue #7`_).\n * Imports which will become deprecated in Django 1.9 are not used now.\n\n* `1.1.3`_ (Oct 11, 2014)\n\n * ``candv`` dependency updated up to *v1.2.0*.\n * Add support for South (`issue #4`_).\n * Choices' form field can display help text as option's title now\n (`issue #1`_).\n\n* `1.1.0`_ (Jul 19, 2014)\n\n * rename package to ``choices`` and move into ``candv_x.django``\n (``x`` stands for ``extensions``)\n\n* `1.0.0`_ (Jun 22, 2014)\n\n Initial version\n\n\n.. |pypi_package| image:: http://img.shields.io/pypi/v/django-candv-choices.svg?style=flat\n :target: http://badge.fury.io/py/django-candv-choices/\n :alt: Version of PyPI package\n\n.. |pypi_downloads| image:: http://img.shields.io/pypi/dm/django-candv-choices.svg?style=flat\n :target: https://crate.io/packages/django-candv-choices/\n :alt: Number of downloads of PyPI package\n\n.. |python_versions| image:: https://img.shields.io/badge/Python-2.7,3.4-brightgreen.svg?style=flat\n :alt: Supported versions of Python\n\n.. |license| image:: https://img.shields.io/badge/license-LGPLv3-blue.svg?style=flat\n :target: https://github.com/oblalex/django-candv-choices/blob/master/LICENSE\n :alt: Package license\n\n.. |demo_preview| image:: http://i.imgur.com/NXKsgRA.png\n :target: http://django-candv-choices.herokuapp.com/\n :alt: Live demo screenshot\n\n.. _candv: http://candv.readthedocs.org/en/latest/\n.. _choices: https://docs.djangoproject.com/en/1.6/ref/models/fields/#django.db.models.Field.choices\n.. _Django: https://www.djangoproject.com/\n.. _django-rf-candv-choices: https://github.com/oblalex/django-rf-candv-choices\n\n.. _Values: http://candv.readthedocs.org/en/latest/candv.html#candv.Values\n.. _VerboseValueConstant: http://candv.readthedocs.org/en/latest/candv.html#candv.VerboseValueConstant\n\n.. _candv usage: http://candv.readthedocs.org/en/latest/usage.html#usage\n.. _candv customization: http://candv.readthedocs.org/en/latest/customization.html\n\n.. _hierarchies of constants: http://candv.readthedocs.org/en/latest/usage.html#hierarchies\n\n.. _MultipleChoiceField: https://docs.djangoproject.com/en/1.6/ref/forms/fields/#multiplechoicefield\n.. _TypedMultipleChoiceField: https://docs.djangoproject.com/en/1.6/ref/forms/fields/#typedmultiplechoicefield\n\n.. _issue #9: https://github.com/oblalex/django-candv-choices/issues/8\n.. _issue #8: https://github.com/oblalex/django-candv-choices/issues/8\n.. _issue #7: https://github.com/oblalex/django-candv-choices/issues/7\n.. _issue #6: https://github.com/oblalex/django-candv-choices/issues/6\n.. _issue #4: https://github.com/oblalex/django-candv-choices/issues/4\n.. _issue #1: https://github.com/oblalex/django-candv-choices/issues/1\n\n.. _1.1.5: https://github.com/oblalex/django-candv-choices/compare/v1.1.4...v1.1.5\n.. _1.1.4: https://github.com/oblalex/django-candv-choices/compare/v1.1.3...v1.1.4\n.. _1.1.3: https://github.com/oblalex/django-candv-choices/compare/v1.1.0...v1.1.3\n.. _1.1.0: https://github.com/oblalex/django-candv-choices/compare/v1.0.0...v1.1.0\n.. _1.0.0: https://github.com/oblalex/django-candv-choices/releases/tag/v1.0.0\n",
"description_content_type": null,
"docs_url": null,
"download_url": "UNKNOWN",
"downloads": {
"last_day": -1,
"last_month": -1,
"last_week": -1
},
"home_page": "https://github.com/oblalex/django-candv-choices",
"keywords": "choices,constants,Django,candv,values",
"license": "LGPLv3",
"maintainer": null,
"maintainer_email": null,
"name": "django-candv-choices",
"package_url": "https://pypi.org/project/django-candv-choices/",
"platform": "any",
"project_url": "https://pypi.org/project/django-candv-choices/",
"project_urls": {
"Download": "UNKNOWN",
"Homepage": "https://github.com/oblalex/django-candv-choices"
},
"release_url": "https://pypi.org/project/django-candv-choices/1.1.5/",
"requires_dist": null,
"requires_python": null,
"summary": "Use complex constants built with 'candv' library instead of standard 'choices' fields for 'Django' models.",
"version": "1.1.5"
},
"last_serial": 1660082,
"releases": {
"1.0.0": [
{
"comment_text": "",
"digests": {
"md5": "14f85887554f520d2a566bfc2cd6e544",
"sha256": "8d3c61d4ee9383bea0d951df15e7989830d503ec3caf62efde7c19d031b675b3"
},
"downloads": -1,
"filename": "django-candv-choices-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "14f85887554f520d2a566bfc2cd6e544",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 8827,
"upload_time": "2014-06-22T17:59:19",
"url": "https://files.pythonhosted.org/packages/6d/9e/0da1ad1f3f4e401909922060627dec4d2c22874ddb85e37f16118d1c57f2/django-candv-choices-1.0.0.tar.gz"
}
],
"1.1.0": [
{
"comment_text": "",
"digests": {
"md5": "7f5e8f80370eec8c85309cfe85b269d5",
"sha256": "0adc87bddd7def319cbc3fdb5a8077bec5484d927eda4b171c8cf4852db135ef"
},
"downloads": -1,
"filename": "django-candv-choices-1.1.0.tar.gz",
"has_sig": false,
"md5_digest": "7f5e8f80370eec8c85309cfe85b269d5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 9169,
"upload_time": "2014-07-19T07:39:41",
"url": "https://files.pythonhosted.org/packages/8e/2a/c026b3d5fd7c67ebd76ff52140a9db50ca21329a64cdc3845a36a98a8016/django-candv-choices-1.1.0.tar.gz"
}
],
"1.1.3": [
{
"comment_text": "",
"digests": {
"md5": "0f1b3b23b5638f33010ca7cf53daf613",
"sha256": "5660c83ba47f776bebd3617ee9e3a37a33f167452ebdb16535c4e98ff3ea98c7"
},
"downloads": -1,
"filename": "django-candv-choices-1.1.3.tar.gz",
"has_sig": false,
"md5_digest": "0f1b3b23b5638f33010ca7cf53daf613",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 11221,
"upload_time": "2014-10-12T15:06:39",
"url": "https://files.pythonhosted.org/packages/71/6a/1c404226298cc3f81cc96c496e29485adcc1377afdcb1ac5f75dfcdfec1e/django-candv-choices-1.1.3.tar.gz"
}
],
"1.1.4": [
{
"comment_text": "",
"digests": {
"md5": "5d832907278f7fc31634224f842848ff",
"sha256": "3f123d0bafa73f19ebfd02a1877e954040e7cb995e2f2681761246a3231b8b44"
},
"downloads": -1,
"filename": "django-candv-choices-1.1.4.tar.gz",
"has_sig": false,
"md5_digest": "5d832907278f7fc31634224f842848ff",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 11512,
"upload_time": "2015-07-01T22:35:21",
"url": "https://files.pythonhosted.org/packages/17/5b/95255802a8f72dd3a8e1d7ef1b9a98860d7630445ddd86cd870223215484/django-candv-choices-1.1.4.tar.gz"
}
],
"1.1.5": [
{
"comment_text": "",
"digests": {
"md5": "7f2f882454b823e504024ead66c54c29",
"sha256": "8a4c370753d9b8dbcc1894ba6a135bec0f7557b6e239d246fb32e9c4d5f6bfc9"
},
"downloads": -1,
"filename": "django-candv-choices-1.1.5.tar.gz",
"has_sig": false,
"md5_digest": "7f2f882454b823e504024ead66c54c29",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 11789,
"upload_time": "2015-08-01T18:26:02",
"url": "https://files.pythonhosted.org/packages/05/a7/caf353015f9a9ee8c1188c927bf8e1b066f30e0627367e0888f30e9cb2f5/django-candv-choices-1.1.5.tar.gz"
}
]
},
"urls": [
{
"comment_text": "",
"digests": {
"md5": "7f2f882454b823e504024ead66c54c29",
"sha256": "8a4c370753d9b8dbcc1894ba6a135bec0f7557b6e239d246fb32e9c4d5f6bfc9"
},
"downloads": -1,
"filename": "django-candv-choices-1.1.5.tar.gz",
"has_sig": false,
"md5_digest": "7f2f882454b823e504024ead66c54c29",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 11789,
"upload_time": "2015-08-01T18:26:02",
"url": "https://files.pythonhosted.org/packages/05/a7/caf353015f9a9ee8c1188c927bf8e1b066f30e0627367e0888f30e9cb2f5/django-candv-choices-1.1.5.tar.gz"
}
]
}