Esempio n. 1
0
def generate_sha1(string, salt=None):
    """
    Generates a sha1 hash for supplied string. Doesn't need to be very secure
    because it's not used for password checking. We got Django for that.

    :param string:
        The string that needs to be encrypted.

    :param salt:
        Optionally define your own salt. If none is supplied, will use a random
        string of 5 characters.

    :return: Tuple containing the salt and hash.

    """
    if not isinstance(string, (str, text_type)):
        string = str(string)

    if not salt:
        salt = sha_constructor(str(
            random.random()).encode('utf-8')).hexdigest()[:5]

    salted_bytes = (salt.encode('utf-8') + string.encode('utf-8'))
    hash_ = sha_constructor(salted_bytes).hexdigest()

    return salt, hash_
Esempio n. 2
0
def generate_sha1(string, salt=None):
    """
    Generates a sha1 hash for supplied string. Doesn't need to be very secure
    because it's not used for password checking. We got Django for that.

    :param string:
        The string that needs to be encrypted.

    :param salt:
        Optionally define your own salt. If none is supplied, will use a random
        string of 5 characters.

    :return: Tuple containing the salt and hash.

    """
    if not isinstance(string, (str, text_type)):
        string = str(string)

    if not salt:
        salt = sha_constructor(str(random.random()).encode('utf-8')).hexdigest()[:5]

    salted_bytes = (salt.encode('utf-8') + string.encode('utf-8'))
    hash_ = sha_constructor(salted_bytes).hexdigest()

    return salt, hash_
Esempio n. 3
0
    def save(self):
        """ Generate a random username before falling back to parent signup form """
        while True:
            username = sha_constructor(str(random.random()).encode('utf-8')).hexdigest()[:5]
            try:
                get_user_model().objects.get(username__iexact=username)
            except get_user_model().DoesNotExist: break

        self.cleaned_data['username'] = username
        return super(SignupFormOnlyEmail, self).save()
Esempio n. 4
0
    def save(self):
        """ Generate a random username before falling back to parent signup form """
        while True:
            username = sha_constructor(str(random.random())).hexdigest()[:5]
            try:
                get_user_model().objects.get(username__iexact=username)
            except get_user_model().DoesNotExist:
                break

        self.cleaned_data['username'] = username
        return super(SignupFormOnlyEmail, self).save()
Esempio n. 5
0
    def sync_users(self, xml, company):
        for person in xml.findall('person'):
            email = person.find('email-address').text
            first_name = person.find('first-name').text
            last_name = person.find('last-name').text
            basecamp_id = person.find('id').text
            created = False
            try:
                user = User.objects.get(profile__basecamp_id=basecamp_id)
                user.email = email
                self.stdout.write("User %s already exist." % user)
            except User.DoesNotExist:
                try:
                    user = User.objects.get(email=email)
                    created = True
                    self.stdout.write("User %s already exist." % user)
                except User.DoesNotExist:
                    username = email.split('@')[0]
                    try:
                        User.objects.get(username__iexact=username)
                        while True:
                            username = sha_constructor(str(random.random()).encode('utf-8')).hexdigest()[:5]
                            try:
                                User.objects.get(username__iexact=username)
                            except User.DoesNotExist:
                                break
                    except User.DoesNotExist:
                        pass

                    user = UserenaSignup.objects.create_user(username,
                                             email,
                                             username+username,
                                             not userena_settings.USERENA_ACTIVATION_REQUIRED,
                                             userena_settings.USERENA_ACTIVATION_REQUIRED)
                    created = True
                    self.stdout.write("User %s creates." % user)

            if first_name:
                user.first_name = first_name
            if last_name:
                user.last_name = last_name
            user.save()

            company.add_user(user)

            try:
                account = Profile.objects.get(user=user)
            except Profile.DoesNotExist:
                account = Profile(user=user)
                account.save()

            if created:
                account.basecamp_id = basecamp_id
                account.save()
Esempio n. 6
0
 def save(self, *args, **kwargs):
     if not self.branch_number:
         self.branch_number = "000"
     if not self.code:
         self.code = sha_constructor(str(random.random()).encode('utf-8')).hexdigest()[:8]
     super(Branch, self).save(*args, **kwargs)