Commits

Sarvi Shanmugham committed 2397579

Added integration between userena and socialauth with a profile

  • Participants
  • Parent commits e7d1f94

Comments (0)

Files changed (6)

     def save(self):
         """ Generate a random username before falling back to parent signup form """
         while True:
-            username = sha_constructor(str(random.random())).hexdigest()[:5]
+            username = ''.join([userena_settings.USERENA_AUTOUSERNAME_PREFIX,
+                                sha_constructor(str(random.random())).hexdigest()[:5]])
             try:
                 User.objects.get(username__iexact=username)
             except User.DoesNotExist: break
         self.cleaned_data['username'] = username
         return super(SignupFormOnlyEmail, self).save()
 
+class SignupFormOnlyEmailNoPassword(SignupFormOnlyEmail):
+    """
+    Form for creating a new user account but not needing a username or password.
+
+    This form is an adaptation of :class:`SignupFormOnlyEmail`. It's used when
+    ``USERENA_WITHOUT_USERNAME`` setting is set to ``True``. And thus the user
+    is not asked to supply an username, but one is generated for them. The user
+    can than keep sign in by using their email.
+
+    """
+    def __init__(self, *args, **kwargs):
+        super(SignupFormOnlyEmailNoPassword, self).__init__(*args, **kwargs)
+        del self.fields['password1']
+        del self.fields['password2']
+        
+    def save(self):
+        """ Generate a random username before falling back to parent signup form """
+        password = sha_constructor(str(random.random())).hexdigest()[:15]
+        self.cleaned_data['password1'] = password
+        self.cleaned_data['password2'] = password
+        return super(SignupFormOnlyEmailNoPassword, self).save()
+
 class SignupFormTos(SignupForm):
     """ Add a Terms of Service button to the ``SignupForm``. """
     tos = forms.BooleanField(widget=forms.CheckboxInput(attrs=attrs_dict),

userena/managers.py

 class UserenaManager(UserManager):
     """ Extra functionality for the Userena model. """
 
-    def create_inactive_user(self, username, email, password, send_email=True):
+    def signup_active_user(self, user, reset_password=True, send_email=True):
+        """
+        A simple wrapper that adds a signup data for an already created :class:`User`.
+        :param user:
+            User object of an existing user.
+            
+        :param reset_password:
+            Boolean that says if the password should be reset or not. A new password
+            will be autogenerated. This will also automatically generate an email
+            to the user with his user id and the newly generated password
+            
+        :param send_email:
+            Boolean that says if and email should be sent to the user specifying that
+            a new account was created. Along with the new autogenerated password.
+        """
+        now = datetime.datetime.now()
+
+        userena = self.create_userena_profile(user)
+
+        # All users have an empty profile
+        profile_model = get_profile_model()
+        new_profile = profile_model(user=user)
+        new_profile.save(using=self._db)
+
+        # Give permissions to view and change profile
+        for perm in PERMISSIONS['profile']:
+            assign(perm, user, new_profile)
+
+        # Give permissinos to view and change itself
+        for perm in PERMISSIONS['user']:
+            assign(perm, user, user)
+
+        # Send the signup complete signal
+        userena_signals.signup_complete.send(sender=None,
+                                             user=user)
+        self.activate_user(user.username, userena.activation_key)
+        if send_email:
+            userena.send_activated_email()
+
+        return user
+        
+
+    def create_inactive_user(self, username, email, password=None, send_email=True):
         """
         A simple wrapper that creates a new :class:`User`.
 
         """
         now = datetime.datetime.now()
 
-        new_user = User.objects.create_user(username, email, password)
+        if password:
+            new_user = User.objects.create_user(username, email, password)
+        else:
+            new_user = User.objects.create_user(username, email)
         new_user.is_active = False
         new_user.save()
 

userena/models.py

                   settings.DEFAULT_FROM_EMAIL,
                   [self.user.email,])
 
+    def send_activated_email(self):
+        """
+        Sends a activated email to the user.
+
+        This email is send when a new user has been created and activated.
+
+        """
+        context= {'user': self.user,
+                  'protocol': get_protocol(),
+                  'site': Site.objects.get_current()}
+
+        subject = render_to_string('userena/emails/activated_email_subject.txt',
+                                   context)
+        subject = ''.join(subject.splitlines())
+
+        message = render_to_string('userena/emails/activated_email_message.txt',
+                                   context)
+        send_mail(subject,
+                  message,
+                  settings.DEFAULT_FROM_EMAIL,
+                  [self.user.email,])
+
+
 class UserenaBaseProfile(models.Model):
     """ Base model needed for extra profile functionality """
     PRIVACY_CHOICES = (
     user = models.OneToOneField(User,
                                 unique=True,
                                 verbose_name=_('user'),
-                                related_name='profile')
+                                related_name='userena_profile')
 
     mugshot = ThumbnailerImageField(_('mugshot'),
                                     blank=True,

userena/settings.py

 gettext = lambda s: s
 
 
+
+USERENA_AUTOUSERNAME_PREFIX = getattr(settings,
+                                      'USERENA_AUTOUSERNAME_PREFIX',
+                                      '')
+
+USERENA_ACTIVATE_ON_ASSOCIATE = getattr(settings,
+                                      'USERENA_ACTIVATE_ON_ASSOCIATE',
+                                      False)
+
 USERENA_REDIRECT_ON_SIGNOUT = getattr(settings,
                                       'USERENA_REDIRECT_ON_SIGNOUT',
                                       None)

userena/templates/userena/emails/activated_email_message.txt

+{% load i18n %}{% autoescape off %}
+{% blocktrans with user.username as username %}Dear {{ username }},{% endblocktrans %}
+
+{% blocktrans with site.name as site %}Thank you for signing up at {{ site }}.{% endblocktrans %}
+
+{% trans "Your account has been activated." %}
+{% trans "Please setup your profile at the folling link." %}
+{% trans "You can also associate all your all your open ids for convenient no password, login." %}
+
+
+{% trans "Thanks for using our site!" %}
+
+{% trans "Sincerely" %},
+{{ site.name }}
+{% endautoescape %}

userena/templates/userena/emails/activated_email_subject.txt

+{% load i18n %}
+{% blocktrans with site.name as site %}Your signup at {{ site }}.{% endblocktrans %}