Source

django-confirmation / confirmation / managers.py

import datetime

from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.http import Http404

import confirmation
from confirmation.utils import generate_hash


class ConfirmationManager(models.Manager):
    def create(self, obj, confirmation_type, content="", redirect_url=""):
        content_type = ContentType.objects.get_for_model(obj)
        key = generate_hash()
        return super(ConfirmationManager, self).create(content_type=content_type,
                                                        object_id=obj.pk,
                                                        key=key,
                                                        confirmation_type=confirmation_type,
                                                        content=content,
                                                        redirect_url=redirect_url
                                                        )

    def get(self, key, confirmation_type=1):
        """
        Method will return confirmation instance, none or raise 404 error 
        according to settings.
        """
        now = datetime.datetime.now()
        limit = now - datetime.timedelta(confirmation.settings.CONFIRMATION_DAYS_LIMIT)

        try:
            return super(ConfirmationManager, self).get(key=key, 
                                                        date__range=(limit, now),
                                                        confirmation_type=confirmation_type)
        except self.model.DoesNotExist:
            if confirmation.settings.CONFIRMATION_RAISE_404:
                raise Http404()