コード例 #1
0
 def _change_password(self, user, password):
     """
     Helper method to change password on user and record in the PasswordHistory
     """
     user.set_password(password)
     user.save()
     history = PasswordHistory()
     history.create(user)
コード例 #2
0
    def _user_factory_with_history(self,
                                   is_staff=False,
                                   set_initial_history=True):
        """
        Helper method to generate either an Admin or a User
        """
        if is_staff:
            user = AdminFactory()
        else:
            user = UserFactory()

        user.date_joined = timezone.now()

        if set_initial_history:
            history = PasswordHistory()
            history.create(user)

        return user
コード例 #3
0
    def _create_user(self, data, errors, response):
        """Register user and add him to a company."""
        user = data.get('user')
        email = user.get('email')
        company = data.get('company')

        # Create the user and their profile.
        try:
            # User
            user = User.objects.create(**user)
            user.set_password(user.password)
            user.save()
            data['user_object'] = user
            # Profile
            UserProfile.objects.create(user=user,
                                       name='{} {}'.format(
                                           user.first_name, user.last_name))
            # Notifications
            if settings.FEATURES.get('ENABLE_DISCUSSION_EMAIL_DIGEST'):
                enable_notifications(user)
            # Password History
            password_history_entry = PasswordHistory()
            password_history_entry.create(user)
        except Exception as exc:
            self._add_error(errors, str(exc.message),
                            _('Registering Participant'), email)
        else:
            response['user_id'] = user.id
            AUDIT_LOG.info("API::New account created with user-id - {}".format(
                user.id))

        # Associate with company.
        try:
            company.users.add(user)
        except Exception as exc:
            self._add_error(errors, str(exc.message),
                            _('Enrolling Participant in Company'), email)

        user_organization_updated.send(sender=__name__,
                                       user_id=user.id,
                                       organization_id=company.id)