urlman


Nameurlman JSON
Version 2.0.1 PyPI version JSON
download
home_pagehttps://github.com/andrewgodwin/urlman
SummaryDjango URL pattern helpers
upload_time2021-06-11 02:52:16
maintainer
docs_urlNone
authorAndrew Godwin
requires_python
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            urlman
------

.. image:: https://travis-ci.org/andrewgodwin/urlman.svg?branch=master
   :target: https://travis-ci.org/andrewgodwin/urlman
   :alt: Test Status

.. image:: https://codecov.io/gh/andrewgodwin/urlman/branch/master/graph/badge.svg
   :target: https://codecov.io/gh/andrewgodwin/urlman
   :alt: Test Coverage Status

A nicer way to do URLs for Django models.

Replaces things like ``get_absolute_url`` with a ``.urls`` attribute that
can reference other URLs and build sensible trees of things, and can
then be accessed using ``instance.urls.name``.

This is so you can have URLs on your model instances directly (rather than reversing
through the url lookup functions, which is not only slow but often hard to supply
arguments to). You can just throw ``{{ instance.urls.view }}`` into a template to get
a link.

It also lets you use Python string formatting syntax to place arguments into URLs from
the model instance itself or from other URLs in the same set.

Example:

.. code-block:: python

    import urlman

    class Group(models.Model):

    ...

        class urls(urlman.Urls):
            view = "/{self.slug}/"
            users = "{view}users/"
            admin = "{view}admin/"


    def my_view(request):
        group = ...
        return redirect(group.urls.view)

It's suggested that you use "view" as the equivalent name for
``get_absolute_url``, and have a function like this on your model:

.. code-block:: python

    def get_absolute_url(self):
        return self.urls.view

To build a full URL use the ``full`` method like this:

.. code-block:: python

    def my_view(request):
        group = ...
        return redirect(group.urls.admin.full(scheme='https'))

You can implement the `get_scheme(url)` and `get_hostname(url)` methods on your
`Url` class to change your default theme and hostname from the urlman defaults
of `'http'` and `'localhost'`, respectively.

If you use Django REST Framework, you can use ``urlman.UrlManField`` to provide
an object with a set of URLs. It is used like this (only the ``urls`` parameter
is required):

.. code-block:: python

   from urlman.serializers import UrlManField

   class MySerializer(ModelSerializer):
       urls = UrlManField(urls=['view', 'edit'], attribute='urls', full=True)



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/andrewgodwin/urlman",
    "name": "urlman",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Andrew Godwin",
    "author_email": "andrew@aeracode.org",
    "download_url": "https://files.pythonhosted.org/packages/a9/af/9973893f12e1ef71d5f42a049bad611a617a9349c8bb0b9b8f6003183565/urlman-2.0.1.tar.gz",
    "platform": "",
    "description": "urlman\n------\n\n.. image:: https://travis-ci.org/andrewgodwin/urlman.svg?branch=master\n   :target: https://travis-ci.org/andrewgodwin/urlman\n   :alt: Test Status\n\n.. image:: https://codecov.io/gh/andrewgodwin/urlman/branch/master/graph/badge.svg\n   :target: https://codecov.io/gh/andrewgodwin/urlman\n   :alt: Test Coverage Status\n\nA nicer way to do URLs for Django models.\n\nReplaces things like ``get_absolute_url`` with a ``.urls`` attribute that\ncan reference other URLs and build sensible trees of things, and can\nthen be accessed using ``instance.urls.name``.\n\nThis is so you can have URLs on your model instances directly (rather than reversing\nthrough the url lookup functions, which is not only slow but often hard to supply\narguments to). You can just throw ``{{ instance.urls.view }}`` into a template to get\na link.\n\nIt also lets you use Python string formatting syntax to place arguments into URLs from\nthe model instance itself or from other URLs in the same set.\n\nExample:\n\n.. code-block:: python\n\n    import urlman\n\n    class Group(models.Model):\n\n    ...\n\n        class urls(urlman.Urls):\n            view = \"/{self.slug}/\"\n            users = \"{view}users/\"\n            admin = \"{view}admin/\"\n\n\n    def my_view(request):\n        group = ...\n        return redirect(group.urls.view)\n\nIt's suggested that you use \"view\" as the equivalent name for\n``get_absolute_url``, and have a function like this on your model:\n\n.. code-block:: python\n\n    def get_absolute_url(self):\n        return self.urls.view\n\nTo build a full URL use the ``full`` method like this:\n\n.. code-block:: python\n\n    def my_view(request):\n        group = ...\n        return redirect(group.urls.admin.full(scheme='https'))\n\nYou can implement the `get_scheme(url)` and `get_hostname(url)` methods on your\n`Url` class to change your default theme and hostname from the urlman defaults\nof `'http'` and `'localhost'`, respectively.\n\nIf you use Django REST Framework, you can use ``urlman.UrlManField`` to provide\nan object with a set of URLs. It is used like this (only the ``urls`` parameter\nis required):\n\n.. code-block:: python\n\n   from urlman.serializers import UrlManField\n\n   class MySerializer(ModelSerializer):\n       urls = UrlManField(urls=['view', 'edit'], attribute='urls', full=True)\n\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Django URL pattern helpers",
    "version": "2.0.1",
    "project_urls": {
        "Homepage": "https://github.com/andrewgodwin/urlman"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2d474bd45d7e196e1b530c984fd538e281935967ec05640ce8ec3226a3499214",
                "md5": "fd7d8f2fd5bf846548736baf93dd24a2",
                "sha256": "909177485ddbbeada15a21f80155762faf70fc53be31890ff7288e21803f06c7"
            },
            "downloads": -1,
            "filename": "urlman-2.0.1-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "fd7d8f2fd5bf846548736baf93dd24a2",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 8191,
            "upload_time": "2021-06-11T02:52:15",
            "upload_time_iso_8601": "2021-06-11T02:52:15.381305Z",
            "url": "https://files.pythonhosted.org/packages/2d/47/4bd45d7e196e1b530c984fd538e281935967ec05640ce8ec3226a3499214/urlman-2.0.1-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a9af9973893f12e1ef71d5f42a049bad611a617a9349c8bb0b9b8f6003183565",
                "md5": "d0385986b25105a45df622ad84e23520",
                "sha256": "3b9c5ac4e447b1e29fa259dc76953d46d711c84b296a0c66c34870e248eb1205"
            },
            "downloads": -1,
            "filename": "urlman-2.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "d0385986b25105a45df622ad84e23520",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 7863,
            "upload_time": "2021-06-11T02:52:16",
            "upload_time_iso_8601": "2021-06-11T02:52:16.771488Z",
            "url": "https://files.pythonhosted.org/packages/a9/af/9973893f12e1ef71d5f42a049bad611a617a9349c8bb0b9b8f6003183565/urlman-2.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2021-06-11 02:52:16",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "andrewgodwin",
    "github_project": "urlman",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "lcname": "urlman"
}
        
Elapsed time: 0.17057s