Commits

offline committed 942f73b

date range now configured in settings. Tests added

  • Participants
  • Parent commits d2fdbf5

Comments (0)

Files changed (11)

File confirmation/managers.py

+import datetime
+
 from django.db import models
 from django.contrib.contenttypes.models import ContentType
 from django.http import Http404
                                                         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.
+        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, confirmation_type=confirmation_type)
+            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()

File confirmation/models.py

 from confirmation.managers import ConfirmationManager
 
 
-
 class Confirmation(models.Model):
     """
     Can be used for user activation, but also for
     content_type = models.ForeignKey(ContentType)
     object_id = models.PositiveIntegerField()
     content_object = generic.GenericForeignKey('content_type', 'object_id')
-    date = models.DateTimeField(auto_now=True)
+    date = models.DateTimeField(auto_now_add=True)
     key = models.CharField(max_length=32)
     confirmation_type = models.IntegerField(default=1)
     content = models.TextField(blank=True)

File confirmation/settings.py

 from django.conf import settings
 
 
-CONFIRMATION_RAISE_404 = getattr(settings, "CONFIRMATION_RAISE_404", False)
+CONFIRMATION_RAISE_404 = getattr(settings, "CONFIRMATION_RAISE_404", False)
+CONFIRMATION_DAYS_LIMIT = getattr(settings, "CONFIRMATION_DAYS_LIMIT", 30)

File confirmation/tests/__init__.py

+from tests import *
+import settings

File confirmation/tests/models.py

Empty file added.

File confirmation/tests/settings.py

+import os
+
+DIRNAME = os.path.dirname(__file__)
+
+
+DEBUG = True
+DATABASE_ENGINE = 'sqlite3'
+DATABASE_NAME = os.path.join(DIRNAME, 'tests.db')
+
+
+ROOT_URLCONF = 'confirmation.tests.urls'
+
+INSTALLED_APPS = (
+    'django.contrib.admin',
+    'django.contrib.auth',
+    'django.contrib.contenttypes',
+    'django.contrib.sessions',
+    'django.contrib.sites',
+    'confirmation',
+    'confirmation.tests'
+)
+
+SECRET_KEY = '-oy*asd55p(+&5-(=-z0aajy&sdfkjsdfjg723)$c^xom8-j))'
+
+SITE_ID = 1
+
+
+TEMPLATE_LOADERS = (
+    'django.template.loaders.filesystem.load_template_source',
+    'django.template.loaders.app_directories.load_template_source',
+    'django.template.loaders.eggs.load_template_source',
+)
+
+TEMPLATE_CONTEXT_PROCESSORS = (
+    'django.core.context_processors.auth',
+    'django.core.context_processors.request',
+    )
+
+MIDDLEWARE_CLASSES = (
+    'django.middleware.common.CommonMiddleware',
+    'django.contrib.sessions.middleware.SessionMiddleware',
+    'django.contrib.auth.middleware.AuthenticationMiddleware',
+)

File confirmation/tests/templates/base.html

+{% block content %}
+{% endblock %}

File confirmation/tests/tests.py

+import datetime
+
+from django.core.urlresolvers import reverse
+from django.contrib.auth.models import User
+from django.test import Client, TestCase
+from django.conf import settings
+
+import confirmation
+from confirmation.views import confirmation_verify
+from confirmation.models import Confirmation
+
+
+ACTIVATION_TYPE = 1
+
+class RegistrationTest(TestCase):
+
+    def setUp(self):
+        confirmation.settings.CONFIRMATION_RAISE_404 = False
+        confirmation.settings.CONFIRMATION_DAYS_LIMIT = 30
+        self.user = User.objects.create_user("test", "email@admin.com")
+        self.client = Client()
+        self.url = reverse('confirmation')
+
+    def test_create_confirmation(self):
+        Confirmation.objects.create(self.user, ACTIVATION_TYPE, "Please activate your account")
+        self.failUnlessEqual(Confirmation.objects.count(), 1)
+
+    def test_delete_confirmation(self):
+        Confirmation.objects.create(self.user, ACTIVATION_TYPE, "Please activate your account")
+        confirm = Confirmation.objects.all()[0]
+        response = self.client.get("%s?key=%s&type=%s" % (self.url, confirm.key, ACTIVATION_TYPE))
+        self.failUnlessEqual(Confirmation.objects.count(), 0)
+
+    def test_redirect_confirmation(self):
+        confirm = Confirmation.objects.create(self.user, ACTIVATION_TYPE, "Please activate your account", "/")
+        response = self.client.get("%s?key=%s&type=%s" % (self.url, confirm.key, ACTIVATION_TYPE))
+        self.failUnlessEqual(response.status_code, 302)
+        
+    def test_old_confirmation(self):
+        confirm = Confirmation.objects.create(self.user, ACTIVATION_TYPE, "Please activate your account")
+        confirm.date = datetime.datetime(2008, 9, 1, 20, 25)
+        confirm.save()
+        response = self.client.get("%s?key=%s&type=%s" % (self.url, confirm.key, ACTIVATION_TYPE))
+        self.failUnlessEqual(Confirmation.objects.count(), 1)
+        

File confirmation/tests/urls.py

+from django.conf.urls.defaults import *
+
+urlpatterns = patterns('',
+    (r'', include('confirmation.urls')),
+)
+

File confirmation/urls.py

 from confirmation import views
 
 urlpatterns = patterns('',
-   url(r'^confirmation/$', views.confirmation, name='confirmation'),
-)
+   url(r'^confirmation/$', views.confirmation_verify, name='confirmation'),
+)

File confirmation/views.py

 
 
 @render_to("confirmation/confirmation.html")
-def confirmation(request):
+def confirmation_verify(request, content=None):
     """
-    Check confirmation key and type. If match - emit succeffull_confirmation signal, 
-    then delete confirmation.
+    Check confirmation key and type. If match - emit succeffull_confirmation 
+    signal, then delete confirmation.
     """
     key = request.GET.get("key")
     confirmation_type = int(request.GET.get("type", 1))
                                               request=request,
                                               instance=confirmation.content_object, 
                                               confirmation_type=confirmation.confirmation_type)
-        
-    if confirmation.redirect_url:
-        return redirect(confirmation.redirect_url)
-    return {}
+
+        redirect_url = confirmation.redirect_url
+        content = confirmation.content
+
+        confirmation.delete() 
+
+        if redirect_url:
+            return redirect(redirect_url)
+
+    return {'content': content}
+