Package: django-privateurl
This application helps You easy and flexibly implement different features that need use private url
for users like registration confirmation, password recovery, access to paid content and so on.Low level API provides You full control and allow:limiting private url by time and hitsauto removing urls that won’t be usedknowing number of hits, date of first and last hit for each urlcontrolling token generatorsaving additional data in JSON format and using it at url hitsprocessing succeeded or failed hits using django signals and controlling server responsesInstallationRequirements:Django v1.8+1. Installdjango-privateurl.Via pip:$ pip install django-privateurlVia setuptools:$ easy_install django-privateurlFor install development version usegit+https://github.com/liminspace/django-privateurl.git@masterinsteaddjango-privateurl.2. Set upsettings.pyin your django project.INSTALLED_APPS = (
  ...,
  'privateurl',
)3. Add url pattern inurls.py:urlpatterns = [
    ...
    url(r'^private/', include('privateurl.urls', namespace='privateurl')),
]4. Run migrate:$ manage.py migrateUsageFirst you need create PrivateUrl usingcreateclass method:PrivateUrl.create(action, user=None, expire=None, data=None, hits_limit=1, auto_delete=False,
                  token_size=None, replace=False, dashed_piece_size=None)action– is a slug that using in url and allow distinguish one url of anotheruser– is user instance that you can get in request processexpire– is expiration date for private url. You can setdatetimeortimedeltadata– is additional data that will be saved as JSON. Setting a dict object is good ideahits_limit– is limit of requests. Set 0 for unlimitauto_delete– setTrueif you want remove private url object when it will be not availabletoken_size– set length of token. You can set number of size or tuple with min and max size. KeepNonefor using value fromsettings.PRIVATEURL_DEFAULT_TOKEN_SIZEreplace– setTrueif you want remove old exists private url for user and action before creating onedashed_piece_size– split token with dash every N symbols. KeepNonefor using value fromsettings.PRIVATEURL_DEFAULT_TOKEN_DASHED_PIECE_SIZEFor example:from privateurl.models import PrivateUrl

purl = PrivateUrl.create('registration-confirmation', user=user)
user.send_email(
    subject='Registration confirmation',
    body='Follow the link for confirm your registration: {url}'.format(
        url=purl.get_absolute_url(),
    ),
)For catch private url request you have to create receiver forprivateurl_oksignal:from django.dispatch import receiver
from dju_privateurl.signals import privateurl_ok, privateurl_fail

@receiver(privateurl_ok)
def registration_confirm(request, obj, action, **kwargs):
    if action != 'registration-confirmation':
        return
    if obj.user:
        obj.user.registration_confirm(request=request)if you want process invalid private url, you can create receiver forprivateurl_failsignal:from django.dispatch import receiver
from dju_privateurl.signals import privateurl_fail

@receiver(privateurl_fail)
def registration_confirm_fail(request, obj, action, **kwargs):
    if action != 'registration-confirmation':
        return
    if obj:
        # private url is expired or has exceeded ``hits_limit``
        pass
    else:
        # private url doesn't exists or token in url is not correct
        passAfter processingprivateurl_oksignal will be redirected to root page/.After processingprivateurl_failsignal will be raisedHttp404exception.If you want change this logic you can returndictwith keyresponsein receiver:from django.shortcuts import redirect, render
from django.dispatch import receiver
from dju_privateurl.signals import privateurl_ok, privateurl_fail

@receiver(privateurl_ok)
def registration_confirm(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)
def registration_confirm_fail(request, obj, action, **kwargs):
    if action != 'registration-confirmation':
        return
    return {'response': render(request, 'error_pages/registration_confirm_fail.html', status=404)}For gettingdatayou need use methodget_data():@receiver(privateurl_ok)
def registration_confirm(request, obj, action, **kwargs):
    ...
    data = obj.get_data()
    ...SettingsPRIVATEURL_URL_NAMESPACE– namespace that you setted inurls.py. By default it isprivateurl.PRIVATEURL_DEFAULT_TOKEN_SIZE– default size of token that will be generated usingcreateorgenerate_tokenmethods. By default it is(8, 64).PRIVATEURL_DEFAULT_TOKEN_DASHED_PIECE_SIZE– default number of size of pieces that joined by dash that using increateorgenerate_tokenmethods. By default it is12.