Commits

Kalyan Lanka  committed 8fa9cd8

Make Changes compatible with django app engine helper

  • Participants
  • Parent commits b360801

Comments (0)

Files changed (5)

 * Nicola Larosa (Italian translation)
 * Joost Cassee (Dutch translation)
 * Björn Kristinsson (Icelandic translation)
+
+* Kalyan Lanka (Google App Engine helper porting)

File registration/forms.py

                 raise forms.ValidationError(_(u'You must type the same password each time'))
         return self.cleaned_data
     
-    def save(self):
+    def save(self, domain_override=""):
         """
         Create the new ``User`` and ``RegistrationProfile``, and
         returns the ``User`` (by calling

File registration/models.py

 import datetime
 import random
 import re
+import sha
+
+from google.appengine.ext import db
 
 from django.conf import settings
-from django.contrib.auth.models import User
-from django.contrib.sites.models import Site
+from appengine_django.auth.models import User
+from google.appengine.api import users
+#from django.contrib.sites.models import Site
 from django.db import models
 from django.db import transaction
+from appengine_django.models import BaseModel
 from django.template.loader import render_to_string
 from django.utils.hashcompat import sha_constructor
 from django.utils.translation import ugettext_lazy as _
         # SHA1 hash; if it doesn't, no point trying to look it up in
         # the database.
         if SHA1_RE.search(activation_key):
-            try:
-                profile = self.get(activation_key=activation_key)
-            except self.model.DoesNotExist:
+            profile = RegistrationProfile.get_by_key_name("key_"+activation_key)
+            if not profile:
                 return False
             if not profile.activation_key_expired():
                 user = profile.user
                 user.is_active = True
-                user.save()
-                profile.activation_key = self.model.ACTIVATED
-                profile.save()
+                user.put()
+                profile.activation_key = RegistrationProfile.ACTIVATED
+                profile.put()
                 user_activated.send(sender=self.model, user=user)
                 return user
         return False
     
-    def create_inactive_user(self, username, password, email,
+    def create_inactive_user(self, username, password, email, domain_override="", 
                              send_email=True):
         """
         Create a new, inactive ``User``, generate a
         
         """
         from registration.signals import user_registered
-
-        new_user = User.objects.create_user(username, email, password)
-        new_user.is_active = False
-        new_user.save()
+        # prepend "key_" to the key_name, because key_names can't start with numbers
+        user_property = users.User(email)
+        new_user = User(user=user_property,username=username, key_name="key_"+username.lower(),
+            email=email, is_active=False, is_staff=False,is_superuser=False,password=password)
+        #new_user.set_password(password)
+        new_user.put()
         
         registration_profile = self.create_profile(new_user)
         
         if send_email:
-            from django.core.mail import send_mail
-            current_site = Site.objects.get_current()
-            
+            from google.appengine.api import mail
+            current_site = domain_override
+#            current_site = Site.objects.get_current()
+            site_dict = dict( {'site': current_site })
             subject = render_to_string('registration/activation_email_subject.txt',
-                                       { 'site': current_site })
+                                       site_dict)
             # Email subject *must not* contain newlines
             subject = ''.join(subject.splitlines())
             
-            message = render_to_string('registration/activation_email.txt',
-                                       { 'activation_key': registration_profile.activation_key,
+            activation_email_dict = dict({ 'activation_key':registration_profile.activation_key,
                                          'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
                                          'site': current_site })
+            message = render_to_string('registration/activation_email.txt',
+                                       activation_email_dict)
             
-            send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [new_user.email])
+            mail.send_mail(settings.DEFAULT_FROM_EMAIL,[new_user.email], subject, message)
         user_registered.send(sender=self.model, user=new_user)
         return new_user
-    create_inactive_user = transaction.commit_on_success(create_inactive_user)
     
     def create_profile(self, user):
         """
         username and a random salt.
         
         """
-        salt = sha_constructor(str(random.random())).hexdigest()[:5]
-        activation_key = sha_constructor(salt+user.username).hexdigest()
-        return self.create(user=user,
-                           activation_key=activation_key)
+        salt = sha.new(str(random.random())).hexdigest()[:5]
+        activation_key = sha.new(salt+user.username).hexdigest()
+#        prepend "key_" to the key_name, because key_names can't start with numbers     
+        registrationprofile = RegistrationProfile(user=user, activation_key=activation_key, key_name="key_"+activation_key)
+        registrationprofile.put()
+        return registrationprofile
         
     def delete_expired_users(self):
         """
         be deleted.
         
         """
-        for profile in self.all():
+        for profile in RegistrationProfile.all():
             if profile.activation_key_expired():
                 user = profile.user
                 if not user.is_active:
                     user.delete()
+                    profile.delete()
 
 
-class RegistrationProfile(models.Model):
+class RegistrationProfile(BaseModel):
     """
     A simple profile which stores an activation key for use during
     user account registration.
     """
     ACTIVATED = u"ALREADY_ACTIVATED"
     
-    user = models.ForeignKey(User, unique=True, verbose_name=_('user'))
-    activation_key = models.CharField(_('activation key'), max_length=40)
-    
+    user = db.ReferenceProperty(User, verbose_name=_('user'))
+    activation_key = db.StringProperty(_('activation key'))
     objects = RegistrationManager()
     
     class Meta:
         
         """
         expiration_date = datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS)
-        return self.activation_key == self.ACTIVATED or \
+        return self.activation_key == RegistrationProfile.ACTIVATED or \
                (self.user.date_joined + expiration_date <= datetime.datetime.now())
     activation_key_expired.boolean = True

File registration/urls.py

 
 from registration.views import activate
 from registration.views import register
+from registration.UserRegistration import UserRegistrationForm
 
 
 urlpatterns = patterns('',
                            name='auth_login'),
                        url(r'^logout/$',
                            auth_views.logout,
-                           {'template_name': 'registration/logout.html'},
                            name='auth_logout'),
                        url(r'^password/change/$',
                            auth_views.password_change,
                        url(r'^password/reset/$',
                            auth_views.password_reset,
                            name='auth_password_reset'),
-                       url(r'^password/reset/confirm/(?P<uidb36>[0-9A-Za-z]+)-(?P<token>.+)/$',
+                       url(r'^password/reset/confirm/(?P<uidb36>.+)/(?P<token>.+)/$',
                            auth_views.password_reset_confirm,
                            name='auth_password_reset_confirm'),
                        url(r'^password/reset/complete/$',
                        url(r'^password/reset/done/$',
                            auth_views.password_reset_done,
                            name='auth_password_reset_done'),
-                       url(r'^register/$',
-                           register,
-                           name='registration_register'),
+                        url(r'^register/$', 'registration.views.register',
+                        kwargs={'form_class': UserRegistrationForm},
+                        name='registration_register'),
                        url(r'^register/complete/$',
                            direct_to_template,
                            {'template': 'registration/registration_complete.html'},

File registration/views.py

 
 """
 
-
 from django.conf import settings
 from django.core.urlresolvers import reverse
 from django.http import HttpResponseRedirect
 from django.template import RequestContext
 
 from registration.forms import RegistrationForm
-from registration.models import RegistrationProfile
+from registration.models import RegistrationProfile, RegistrationManager
+from registration.UserRegistration import UserRegistrationForm
 
 
 def activate(request, activation_key,
     
     """
     activation_key = activation_key.lower() # Normalize before trying anything with it.
-    account = RegistrationProfile.objects.activate_user(activation_key)
+    account = RegistrationManager().activate_user(activation_key)
     if extra_context is None:
         extra_context = {}
     context = RequestContext(request)
     """
     if request.method == 'POST':
         form = form_class(data=request.POST, files=request.FILES)
+        domain_override = request.get_host()
         if form.is_valid():
-            new_user = form.save()
+            new_user = form.save(domain_override)
             # success_url needs to be dynamically generated here; setting a
             # a default value using reverse() will cause circular-import
             # problems with the default URLConf for this application, which