Skip to main content

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

  1. Install ddaemon-django-private-url via pip: pip install ddaemon-django-private-url;

  2. Set up settings.py in your Django Project:

    INSTALLED_APPS = (
        ...,
        "privateurl",
        ...
    )
    
  3. Add url Pattern in urls.py:

    urlpatterns = [
        ...
        url(r"^private/", include("privateurl.urls", namespace="privateurl")),
        ...
    ]
    
  4. 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 (0 for unlimited Hits);
  • expire - is an Expiration Date of the Private URL. It can be set asdatetime or timedelta Object (None to disable the Time Limit);
  • auto_delete - True to automatically remove the Private URL, when it is not available;
  • token_size - is a Length of a generated Token (None to default to the Value from settings.PRIVATEURL_DEFAULT_TOKEN_SIZE);
  • replace - True to 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.py File
"""./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.py File
"""./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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ddaemon_django_private_url-0.1.0.tar.gz (25.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ddaemon_django_private_url-0.1.0-py2.py3-none-any.whl (28.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file ddaemon_django_private_url-0.1.0.tar.gz.

File metadata

File hashes

Hashes for ddaemon_django_private_url-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d71a4ff27d287fb8ded0de0642b8faec66f702f7c1e351cbd237c867b38f0a65
MD5 a70f0914ee927a41805f916e6feb0298
BLAKE2b-256 97b5ac533e37ecce57ea4c3bca28ac38f8e4015407602a01fe81edf333f33e70

See more details on using hashes here.

File details

Details for the file ddaemon_django_private_url-0.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for ddaemon_django_private_url-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b653eff1041a46e4d60adee25229ce6090d576b9e29f950165158c463fb69446
MD5 11c8a0c5657cbeb61dbcb8cfee569dd5
BLAKE2b-256 a2f3a1bc28f1d1ee73f30e992ab29e44738046b8fb3af2e2ff2dbd01e696f3e2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page