Skip to content

robertpro/django-py-reverse

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

django-py-reverse

Latest Version on PyPI Supported Implementations Build Status Coverage Status Built with PyPi Template

This is a very simple library to generate urls from a json file generated by django-js-reverse so it can be used in python projects (clients), e.g. GTK, QT, Kivy, etc.

Installation

pip install django_py_reverse

# or using poetry
poetry add django_py_reverse

Usage

from django_py_reverse import Urls

API_ENDPOINT = "https://example.com/api/v1"
data_urls = {
    "urls": [
        [
            "account_confirm_email",
            [["accounts/confirm_email/%(key)s/", ["key"]]],
        ],
        ["account_delete", [["accounts/delete/", []]]],
        ["account_password_reset", [["es/accounts/password/reset/", []]]],
        [
            "account_password_reset_token",
            [
                [
                    "accounts/password/reset/%(uidb36)s-%(token)s/",
                    ["uidb36", "token"],
                ]
            ],
        ],
    ],
    "prefix": "/",
}

urls = Urls(data=data_urls, url_prefix=API_ENDPOINT).factory()

print(urls['account_confirm_email'](key="123"))
print(urls['account_delete']())
print(urls['account_password_reset']())
print(urls['account_password_reset_token'](uidb36="123", token="456"))

# Output:
# https://example.com/api/v1/accounts/confirm_email/123/
# https://example.com/api/v1/accounts/delete/
# https://example.com/api/v1/es/accounts/password/reset/
# https://example.com/api/v1/accounts/password/reset/123-456/

Include JS Style

urls = Urls(data=data_urls, url_prefix=API_ENDPOINT, include_js_style=True).factory()

print(urls['accountConfirmEmail'](key="123"))
print(urls['accountDelete']())
print(urls['accountPasswordReset']())
print(urls['accountPasswordResetToken'](uidb36="123", token="456"))

# Output:
# https://example.com/api/v1/accounts/confirm_email/123/
# https://example.com/api/v1/accounts/delete/
# https://example.com/api/v1/es/accounts/password/reset/
# https://example.com/api/v1/accounts/password/reset/123-456/

You can get the urls from your current django & django-js-reverse project more info here

wget ${API_ENDPOINT}/jsreverse/json/ -O data/urls.json

Then on your python project

import json
from django_py_reverse import Urls

API_ENDPOINT = "https://example.com/api/v1"

with open("data/urls.json") as f:
    data_urls = json.load(f)

urls = Urls(data=data_urls, url_prefix=API_ENDPOINT).factory()

About

No description, website, or topics provided.

Resources

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE.txt

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published