Exemple #1
0
    def change_email(self, email):
        """
        Changes the email address for a user.

        A user needs to verify this new email address before it becomes
        active. By storing the new email address in a temporary field 
        -- ``temporary_email`` -- we are able to set this email address 
        after the user has verified it by clicking on the verification URI 
        in the email. This email gets send out by ``send_verification_email``.

        :param email:
            The new email address that the user wants to use.

        """
        self.email_unconfirmed = email

        salt, hash = generate_sha1(self.username)
        self.email_confirmation_key = hash
        self.email_confirmation_key_created = get_datetime_now()
        self.save()

        # Send email for activation
        self.send_confirmation_email()
        
        return self
Exemple #2
0
def upload_to_picture(instance, filename):
    """
    Uploads a picture for a user to the ``ACCOUNTS_PICTURE_PATH`` and 
    saving it under unique hash for the image. This is for privacy 
    reasons so others can't just browse through the picture directory.

    """
    extension = filename.split('.')[-1].lower()
    salt, hash = generate_sha1(instance.id)
    return '%(path)s/%(hash)s.%(extension)s' % {
                'path': getattr(defaults, 
                            'ACCOUNTS_PICTURE_PATH','%s/%s' % (
                                str(instance._meta.app_label), 
                                str(instance._meta.model_name))),
                'hash': hash[:10],
                'extension': extension}
    def create_account(self, user):
        """
        Creates both :class:`Account` and :class:`Profile` instances for this user.

        :param user:
            Django :class:`User` instance.

        :return: The newly created :class:`Account` instance.

        """
        # Create profile first
        new_profile = self.create_profile(user)

        # Create and reurn new account
        if isinstance(user.username, unicode):
            user.username = user.username.encode('utf-8')
        salt, activation_key = generate_sha1(user.username)
        return self.create(user=user, activation_key=activation_key)
    def create_user(self, username, email, password, active=False,
                    send_email=True):
        """
        A simple wrapper that creates a new :class:`User`.

        :param username:
            String containing the username of the new user.

        :param email:
            String containing the email address of the new user.

        :param password:
            String containing the password for the new user.

        :param active:
            Boolean that defines if the user requires activation by clicking 
            on a link in an email. Defauts to ``True``.

        :param send_email:
            Boolean that defines if the user should be send an email. You 
            could set this to ``False`` when you want to create a user in 
            your own code, but don't want the user to activate through email.

        :return: :class:`User` instance representing the new user.

        """

        user = super(AccountActivationManager, self).create_user(username, email, password)

        if isinstance(user.username, unicode):
            username = user.username.encode('utf-8')
        salt, activation_key = generate_sha1(username)
        user.is_active = active
        user.activation_key = activation_key
        user.save(using=self._db)

        if send_email:
            user.send_activation_email()
 
        return user