===========================
django CMS Attributes Field
===========================
|pypi| |coverage| |python| |django| |djangocms| |djangocms4|
This project is an opinionated implementation of JSONField for arbitrary HTML
element attributes.
It aims to provide a sensible means of storing and managing
arbitrary HTML element attributes for later emitting them into templates.
There are a wide variety of types of attributes and using the "normal" Django
method of adding ModelFields for each on a business model is cumbersome at
best and moreover may require related tables to allow cases where any number
of the same type of attribute should be supported (i.e., data-attributes).
This can contribute to performance problems.
To avoid these pitfalls, this package allows all of these attributes to be
stored together in a single text field in the database as a JSON blob, but
provides a nice widget to provide an intuitive, key/value pair interface
and provide sensible validation of the keys used.
.. note::
This project is considered 3rd party (no supervision by the `django CMS Association <https://www.django-cms.org/en/about-us/>`_). Join us on `Slack <https://www.django-cms.org/slack/>`_ for more information.
.. image:: preview.gif
*******************************************
Contribute to this project and win rewards
*******************************************
Because this is a an open-source project, we welcome everyone to
`get involved in the project <https://www.django-cms.org/en/contribute/>`_ and
`receive a reward <https://www.django-cms.org/en/bounty-program/>`_ for their contribution.
Become part of a fantastic community and help us make django CMS the best CMS in the world.
We'll be delighted to receive your
feedback in the form of issues and pull requests. Before submitting your
pull request, please review our `contribution guidelines
<http://docs.django-cms.org/en/latest/contributing/index.html>`_.
We're grateful to all contributors who have helped create and maintain this package.
Contributors are listed at the `contributors <https://github.com/django-cms/djangocms-attributes-field/graphs/contributors>`_
section.
Documentation
=============
See ``REQUIREMENTS`` in the `setup.py <https://github.com/divio/djangocms-attributes-field/blob/master/setup.py>`_
file for additional dependencies:
Installation
------------
For a manual install:
* run ``pip install djangocms-attributes-field``
* add ``djangocms_attributes_field`` to your ``INSTALLED_APPS``
* run ``python manage.py migrate djangocms_attributes_field``
Configuration
-------------
AttributeField
##############
To use this field in your Models.model: ::
# models.py
...
from django.db import models
from djangocms_attributes_field.fields import AttributesField
...
MyCoolModel(models.Model):
...
attributes = AttributesField()
That's it!
There is an optional parameter that can be used when declaring the field: ::
``excluded_keys`` : This is a list of strings that will not be accepted as
valid keys
Since version 4, the following keys are always excluded (see
``djangocms_attributes_fields.fields.default_excluded_keys``) to avoid
unwanted execution of javascript: ::
["src", "href", "data", "action", "on*"]
``'on*'`` represents any key that starts with ``'on'``.
property: [field_name]_str
++++++++++++++++++++++++++
``AttributeField`` will also provide a handy property ``[field_name]_str``
that will emit the stored key/value pairs as a string suitable for inclusion
in your template for the target HTML element in question. You can use it
like this: ::
# models.py
...
MyCoolModel(models.Model):
...
html_attributes = AttributesField()
# templates/my_cool_project/template.html
...
<a href="..." {{ object.html_attributes_str }}>click me</a>
...
(Assuming that ``object`` is a context variable containing a
``MyCoolModel`` instance.)
In addition to nicely encapsulating the boring task of converting key/value
pairs into a string with proper escaping and marking-safe, this property also
ensures that *existing* key/value pairs with keys that have since been added
to the field's ``excluded_keys`` are also not included in the output string.
AttributeWidget
###############
The ``AttributesWidget`` is already used by default by the ``AttributesField``,
but there may be cases where you'd like to override its usage.
The widget supports two additional parameters: ::
``key_attrs`` : A dict of HTML attributes to apply to the key input field
``val_attrs`` : A dict of HTML attributes to apply to the value input field
These can be useful, for example, if it is necessary to alter the appearance
of the widget's rendered appearance. Again, for example, let's say we needed
to make the key and value inputs have specific widths. We could do this like
so in our ``ModelForm``: ::
# forms.py
from django import forms
from djangocms_attributes_field.widgets import AttributesWidget
MyCoolForm(forms.ModelForm):
class Meta:
fields = ['attributes', ...]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['attributes'].widget = AttributesWidget(key_attrs={'style': 'width:250px'},
val_attrs={'style': 'width:500px'})
Running Tests
-------------
You can run tests by executing::
virtualenv env
source env/bin/activate
pip install -r tests/requirements.txt
python tests/settings.py
.. |pypi| image:: https://badge.fury.io/py/djangocms-attributes-field.svg
:target: http://badge.fury.io/py/djangocms-attributes-field
.. |coverage| image:: https://codecov.io/gh/django-cms/djangocms-attributes-field/branch/master/graph/badge.svg
:target: https://codecov.io/gh/divio/djangocms-attributes-field
.. |python| image:: https://img.shields.io/badge/python-3.8+-blue.svg
:target: https://pypi.org/project/djangocms-attributes-field/
.. |django| image:: https://img.shields.io/badge/django-3.2,%204.0--%204.2-blue.svg
:target: https://www.djangoproject.com/
.. |djangocms| image:: https://img.shields.io/badge/django%20CMS-3.9%2B-blue.svg
:target: https://www.django-cms.org/
.. |djangocms4| image:: https://img.shields.io/badge/django%20CMS-4-blue.svg
:target: https://www.django-cms.org/
Raw data
{
"_id": null,
"home_page": "https://github.com/django-cms/djangocms-attributes-field/",
"name": "djangocms-attributes-field",
"maintainer": "Django CMS Association and contributors",
"docs_url": null,
"requires_python": null,
"maintainer_email": "info@django-cms.org",
"keywords": null,
"author": "Divio AG",
"author_email": "info@divio.ch",
"download_url": "https://files.pythonhosted.org/packages/12/f6/400335e157454f2032583e2e57291a94b4046ea0314925694d009912881c/djangocms_attributes_field-4.0.0.tar.gz",
"platform": null,
"description": "===========================\ndjango CMS Attributes Field\n===========================\n\n|pypi| |coverage| |python| |django| |djangocms| |djangocms4|\n\n\nThis project is an opinionated implementation of JSONField for arbitrary HTML\nelement attributes.\n\nIt aims to provide a sensible means of storing and managing\narbitrary HTML element attributes for later emitting them into templates.\n\nThere are a wide variety of types of attributes and using the \"normal\" Django\nmethod of adding ModelFields for each on a business model is cumbersome at\nbest and moreover may require related tables to allow cases where any number\nof the same type of attribute should be supported (i.e., data-attributes).\nThis can contribute to performance problems.\n\nTo avoid these pitfalls, this package allows all of these attributes to be\nstored together in a single text field in the database as a JSON blob, but\nprovides a nice widget to provide an intuitive, key/value pair interface\nand provide sensible validation of the keys used.\n\n\n.. note::\n\n This project is considered 3rd party (no supervision by the `django CMS Association <https://www.django-cms.org/en/about-us/>`_). Join us on `Slack <https://www.django-cms.org/slack/>`_ for more information.\n\n\n.. image:: preview.gif\n\n\n*******************************************\nContribute to this project and win rewards\n*******************************************\n\nBecause this is a an open-source project, we welcome everyone to\n`get involved in the project <https://www.django-cms.org/en/contribute/>`_ and\n`receive a reward <https://www.django-cms.org/en/bounty-program/>`_ for their contribution.\nBecome part of a fantastic community and help us make django CMS the best CMS in the world.\n\nWe'll be delighted to receive your\nfeedback in the form of issues and pull requests. Before submitting your\npull request, please review our `contribution guidelines\n<http://docs.django-cms.org/en/latest/contributing/index.html>`_.\n\nWe're grateful to all contributors who have helped create and maintain this package.\nContributors are listed at the `contributors <https://github.com/django-cms/djangocms-attributes-field/graphs/contributors>`_\nsection.\n\n\nDocumentation\n=============\n\nSee ``REQUIREMENTS`` in the `setup.py <https://github.com/divio/djangocms-attributes-field/blob/master/setup.py>`_\nfile for additional dependencies:\n\n\nInstallation\n------------\n\nFor a manual install:\n\n* run ``pip install djangocms-attributes-field``\n* add ``djangocms_attributes_field`` to your ``INSTALLED_APPS``\n* run ``python manage.py migrate djangocms_attributes_field``\n\n\nConfiguration\n-------------\n\nAttributeField\n##############\n\nTo use this field in your Models.model: ::\n\n # models.py\n ...\n from django.db import models\n from djangocms_attributes_field.fields import AttributesField\n ...\n MyCoolModel(models.Model):\n ...\n attributes = AttributesField()\n\nThat's it!\n\nThere is an optional parameter that can be used when declaring the field: ::\n\n ``excluded_keys`` : This is a list of strings that will not be accepted as\n valid keys\n\nSince version 4, the following keys are always excluded (see\n``djangocms_attributes_fields.fields.default_excluded_keys``) to avoid\nunwanted execution of javascript: ::\n\n [\"src\", \"href\", \"data\", \"action\", \"on*\"]\n\n``'on*'`` represents any key that starts with ``'on'``.\n\nproperty: [field_name]_str\n++++++++++++++++++++++++++\n\n``AttributeField`` will also provide a handy property ``[field_name]_str``\nthat will emit the stored key/value pairs as a string suitable for inclusion\nin your template for the target HTML element in question. You can use it\nlike this: ::\n\n # models.py\n ...\n MyCoolModel(models.Model):\n ...\n html_attributes = AttributesField()\n\n\n # templates/my_cool_project/template.html\n ...\n <a href=\"...\" {{ object.html_attributes_str }}>click me</a>\n ...\n\n(Assuming that ``object`` is a context variable containing a\n``MyCoolModel`` instance.)\n\nIn addition to nicely encapsulating the boring task of converting key/value\npairs into a string with proper escaping and marking-safe, this property also\nensures that *existing* key/value pairs with keys that have since been added\nto the field's ``excluded_keys`` are also not included in the output string.\n\n\nAttributeWidget\n###############\n\nThe ``AttributesWidget`` is already used by default by the ``AttributesField``,\nbut there may be cases where you'd like to override its usage.\n\nThe widget supports two additional parameters: ::\n\n ``key_attrs`` : A dict of HTML attributes to apply to the key input field\n ``val_attrs`` : A dict of HTML attributes to apply to the value input field\n\nThese can be useful, for example, if it is necessary to alter the appearance\nof the widget's rendered appearance. Again, for example, let's say we needed\nto make the key and value inputs have specific widths. We could do this like\nso in our ``ModelForm``: ::\n\n # forms.py\n\n from django import forms\n from djangocms_attributes_field.widgets import AttributesWidget\n\n MyCoolForm(forms.ModelForm):\n class Meta:\n fields = ['attributes', ...]\n\n def __init__(self, *args, **kwargs):\n super().__init__(*args, **kwargs)\n self.fields['attributes'].widget = AttributesWidget(key_attrs={'style': 'width:250px'},\n val_attrs={'style': 'width:500px'})\n\n\nRunning Tests\n-------------\n\nYou can run tests by executing::\n\n virtualenv env\n source env/bin/activate\n pip install -r tests/requirements.txt\n python tests/settings.py\n\n\n.. |pypi| image:: https://badge.fury.io/py/djangocms-attributes-field.svg\n :target: http://badge.fury.io/py/djangocms-attributes-field\n.. |coverage| image:: https://codecov.io/gh/django-cms/djangocms-attributes-field/branch/master/graph/badge.svg\n :target: https://codecov.io/gh/divio/djangocms-attributes-field\n.. |python| image:: https://img.shields.io/badge/python-3.8+-blue.svg\n :target: https://pypi.org/project/djangocms-attributes-field/\n.. |django| image:: https://img.shields.io/badge/django-3.2,%204.0--%204.2-blue.svg\n :target: https://www.djangoproject.com/\n.. |djangocms| image:: https://img.shields.io/badge/django%20CMS-3.9%2B-blue.svg\n :target: https://www.django-cms.org/\n.. |djangocms4| image:: https://img.shields.io/badge/django%20CMS-4-blue.svg\n :target: https://www.django-cms.org/\n",
"bugtrack_url": null,
"license": "BSD-3-Clause",
"summary": "Adds attributes to Django models.",
"version": "4.0.0",
"project_urls": {
"Homepage": "https://github.com/django-cms/djangocms-attributes-field/"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "9dc4c694df33682d16a88247feb5d3878ed67f82c941fa894d210e122f50841f",
"md5": "6fdc014bca3360988feecfc0ab3db164",
"sha256": "2533418cd177885b28c7e4ca9fdcd5d6453b4911a56cdab4acfac6aeab6f406f"
},
"downloads": -1,
"filename": "djangocms_attributes_field-4.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "6fdc014bca3360988feecfc0ab3db164",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 17165,
"upload_time": "2024-11-19T07:58:49",
"upload_time_iso_8601": "2024-11-19T07:58:49.512708Z",
"url": "https://files.pythonhosted.org/packages/9d/c4/c694df33682d16a88247feb5d3878ed67f82c941fa894d210e122f50841f/djangocms_attributes_field-4.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "12f6400335e157454f2032583e2e57291a94b4046ea0314925694d009912881c",
"md5": "e2b3a74c651cd129509e1963d751f05b",
"sha256": "fb56fdd1079ca2b478e38c86291e11095c9cb9215d621626849627eb76e39c95"
},
"downloads": -1,
"filename": "djangocms_attributes_field-4.0.0.tar.gz",
"has_sig": false,
"md5_digest": "e2b3a74c651cd129509e1963d751f05b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 16626,
"upload_time": "2024-11-19T07:58:51",
"upload_time_iso_8601": "2024-11-19T07:58:51.451901Z",
"url": "https://files.pythonhosted.org/packages/12/f6/400335e157454f2032583e2e57291a94b4046ea0314925694d009912881c/djangocms_attributes_field-4.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-19 07:58:51",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "django-cms",
"github_project": "djangocms-attributes-field",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"tox": true,
"lcname": "djangocms-attributes-field"
}