Django Private URL
Project description
ddaemon-django-private-url (Fork)
Project Description
The Application helps to easy and flexibly implement the different Features, that require a Use of a Private URL for Users, like Registration Confirmation, Password Recovery, Access to a paid Content, and so on.
Low Level API provides a full Control and allows:
-
limiting a Usage of the Private URL by Time and Hits;
-
automatic removing Private URLs, that cannot be used anymore;
-
knowing a Number of Hits, Dates of first and last Hit for each Private URL;
-
controlling the Token Generator;
-
saving an additional Data in JSON Format;
-
processing the succeeded or failed Hits, using Django Signals, and controlling the Server Responses.
Installation
-
Install
ddaemon-django-private-urlviapip:pip install ddaemon-django-private-url; -
Set up
settings.pyin your Django Project:INSTALLED_APPS = ( ..., "privateurl", ... )
-
Add
urlPattern inurls.py:urlpatterns = [ ... url(r"^private/", include("privateurl.urls", namespace="privateurl")), ... ]
-
Run Migrations:
[~]$ python manage.py migrate
Usage
First you need to create a PrivateUrl Object, using the create() Class Method:
PrivateUrl.create(
action, user=None, data=None, hits_limit=1, expire=None, auto_delete=False, token_size=None, replace=False)
where:
action- is a Slug, used in Private URL;user- is an User Instance, that you can use during the Request processing;data- is an additional JSON Data;hits_limit- is a Limit of the Private URL Hits (0for unlimited Hits);expire- is an Expiration Date of the Private URL. It can be set asdatetimeortimedeltaObject (Noneto disable the Time Limit);auto_delete-Trueto automatically remove the Private URL, when it is not available;token_size- is a Length of a generated Token (Noneto default to the Value fromsettings.PRIVATEURL_DEFAULT_TOKEN_SIZE);replace-Trueto remove the previously existing Private URL for the Action/User Combination, before creating a new one.
For Example:
from privateurl.models import PrivateUrl
purl = PrivateUrl.create(action="registration-confirmation", user=user)
user.send_email(
subject="Registration Confirmation",
body=f"Follow the Link to confirm your Registration: {purl.get_absolute_url()}")
For catching the Private URL Request you have to create a Receiver for the privateurl_ok and/or privateurl_fail Signal(s):
- in your Application's
receivers.pyFile
"""./src/someapp/receivers.py"""
from django.dispatch import receiver
from privateurl.models import PrivateUrl
from privateurl.signals import (
privateurl_ok,
privateurl_fail)
@receiver(privateurl_ok, sender=PrivateUrl)
def registration_confirm(sender, request, obj, action, **kwargs):
if action != "registration-confirmation":
return
if obj.user:
obj.user.registration_confirm(request=request)
@receiver(privateurl_fail, sender=PrivateUrl)
def registration_confirm_fail(sender, request, obj, action, **kwargs):
if action != "registration-confirmation":
return
if obj:
# Private URL has expired, or has exceeded `hits_limit`.
pass
else:
# Private URL doesn't exist, or Token is not valid.
pass
- and in your Application's
apps.pyFile
"""./src/someapp/apps.py"""
from importlib import import_module
from django.apps import AppConfig
class SomeAppConfig(AppConfig):
name = "someapp"
def ready(self):
import_module("someapp.receivers")
...
After processing the privateurl_ok Signal, the User will be redirected to the Home Page /.
After processing the privateurl_fail Signal, the Http404 Exception will be raised.
If you want to change this Logic you can return from the Receiver the dict Object, with the response Key, containing HTTPResponse Object:
"""./src/someapp/receivers.py"""
from django.shortcuts import (
redirect,
render)
from django.dispatch import receiver
from privateurl.models import PrivateUrl
from privateurl.signals import (
privateurl_ok,
privateurl_fail)
@receiver(privateurl_ok, sender=PrivateUrl)
def registration_confirm(sender, request, obj, action, **kwargs):
if action != "registration-confirmation":
return
if obj.user:
obj.user.registration_confirm(request=request)
obj.user.login()
return {
"response": redirect("user_profile"),
}
@receiver(privateurl_fail, sender=PrivateUrl)
def registration_confirm_fail(sender, request, obj, action, **kwargs):
if action != "registration-confirmation":
return
return {
"response": render(request, "error_pages/registration_confirm_fail.html", status=404)
}
Settings
PRIVATEURL_URL_NAMESPACE - Namespace, set in urls.py. The default Value is privateurl.
PRIVATEURL_DEFAULT_TOKEN_SIZE - Size of the Token, that will be generated using create() or generate_token() Methods. The default Value is 16.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ddaemon_django_private_url-0.1.0.tar.gz.
File metadata
- Download URL: ddaemon_django_private_url-0.1.0.tar.gz
- Upload date:
- Size: 25.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d71a4ff27d287fb8ded0de0642b8faec66f702f7c1e351cbd237c867b38f0a65
|
|
| MD5 |
a70f0914ee927a41805f916e6feb0298
|
|
| BLAKE2b-256 |
97b5ac533e37ecce57ea4c3bca28ac38f8e4015407602a01fe81edf333f33e70
|
File details
Details for the file ddaemon_django_private_url-0.1.0-py2.py3-none-any.whl.
File metadata
- Download URL: ddaemon_django_private_url-0.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 28.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b653eff1041a46e4d60adee25229ce6090d576b9e29f950165158c463fb69446
|
|
| MD5 |
11c8a0c5657cbeb61dbcb8cfee569dd5
|
|
| BLAKE2b-256 |
a2f3a1bc28f1d1ee73f30e992ab29e44738046b8fb3af2e2ff2dbd01e696f3e2
|