Source

django-confirmation / confirmation / middleware.py

Full commit
import datetime

from django.http import Http404

from confirmation.models import Confirmation
from confirmation import signals
from confirmation import settings


class ConfirmationMiddleware(object):

    def process_request(self, request):
        """
        Check confirmation key and type. If match - emit succeffull_confirmation 
        signal, then delete confirmation.
        """
        key = request.REQUEST.get(settings.KEY_NAME)
        token =  request.REQUEST.get(settings.TOKEN_NAME)
        if not key or not token:
            return  # pass request to next middleware

        try:
            now = datetime.datetime.now()
            limit = now - datetime.timedelta(settings.DAYS_LIMIT)

            confirmation = Confirmation.objects.get(key=key, token=token, date__range=(limit, now))
            signals.successfull_confirmation.send(Confirmation, 
                                                  request=request,
                                                  instance=confirmation.content_object, 
                                                  token=confirmation.token)

            confirmation.delete() 

        except Confirmation.DoesNotExist:
           if settings.RAISE_404:
               raise Http404()