Пример #1
0
 def import_users(self):
     """ save users to local DB """
     self.message('saving users into local DB')
     
     saved_users = []
     
     # loop over all extracted unique email addresses
     for email in self.email_set:
         owner = self.users_dict[email].get('owner')
         
         # if owner is not specified, build username from email
         if owner.strip() == '':
             owner, domain = email.split('@')
             # replace any points with a space
             owner = owner.replace('.', ' ')
         
         # if owner has a space, assume he specified first and last name
         if ' ' in owner:
             owner_parts = owner.split(' ')
             first_name = owner_parts[0]
             last_name = owner_parts[1]
         else:
             first_name = owner
             last_name = ''
         
         # username must be slugified otherwise won't get into the DB
         username = slugify(owner)
         
         # create one user for each mail address
         user = User(**{
             "username": username,
             "password": self.generate_random_password(),
             "first_name": first_name.capitalize(),
             "last_name": last_name.capitalize(),
             "email": email,
             "is_active": True
         })
         
         # be sure username is unique
         counter = 1
         original_username = username
         while True:
             if User.objects.filter(username=user.username).count() > 0:
                 counter += 1
                 user.username = '******' % (original_username, counter)
             else:
                 break
         
         try:
             # validate data and save
             user.full_clean()
             user.save()
             # store id
             self.users_dict[email]['id'] = user.id
             # append to saved users
             saved_users.append(user)
             self.verbose('Saved user %s (%s) with email <%s>' % (user.username, user.get_full_name(), user.email))
         except Exception as e:
             self.message('Could not save user %s, got exception: %s' % (user.username, e))
         
         # mark email address as confirmed if feature is enabled
         if EMAIL_ADDRESS_APP_INSTALLED:
             try:
                 email_address = EmailAddress(user=user, email=user.email, verified=True, primary=True)
                 email_address.full_clean()
                 email_address.save()
             except Exception as e:
                 self.message('Could not save email address for user %s, got exception: %s' % (user.username, e))
         
     self.message('saved %d users into local DB' % len(saved_users))
     self.saved_users = saved_users
Пример #2
0
    def import_users(self):
        """ save users to local DB """
        self.message('saving users into local DB')

        saved_users = []

        # loop over all extracted unique email addresses
        for email in self.email_set:
            owner = self.users_dict[email].get('owner')

            # if owner is not specified, build username from email
            if owner.strip() == '':
                owner, domain = email.split('@')
                # replace any points with a space
                owner = owner.replace('.', ' ')

            # if owner has a space, assume he specified first and last name
            if ' ' in owner:
                owner_parts = owner.split(' ')
                first_name = owner_parts[0]
                last_name = owner_parts[1]
            else:
                first_name = owner
                last_name = ''

            # username must be slugified otherwise won't get into the DB
            username = slugify(owner)

            # check if user exists first
            try:
                # try looking by email
                user = User.objects.get(email=email)
            except User.DoesNotExist:
                try:
                    # try looking by username
                    user = User.objects.get(username=username)
                except User.DoesNotExist:
                    # otherwise init new
                    user = User()
                    # generate new password only for new users
                    user.password = self.generate_random_password()

            # we'll create one user for each unique email address we've got
            user.username = username
            user.first_name = first_name.capitalize()
            user.last_name = last_name.capitalize()
            user.email = email
            user.is_active = True

            # extract date joined from old nodes
            # find the oldest node of this user
            oldest_node = OldNode.objects.filter(
                email=email).order_by('added')[0]
            user.date_joined = oldest_node.added

            # be sure username is unique
            counter = 1
            original_username = username
            while True:
                # do this check only if user is new
                if not user.pk and User.objects.filter(
                        username=user.username).count() > 0:
                    counter += 1
                    user.username = '******' % (original_username, counter)
                else:
                    break

            try:
                # validate data and save
                user.full_clean()
                user.save()
            except Exception as e:
                # if user already exists use that instance
                if (User.objects.filter(email=email).count() == 1):
                    user = User.objects.get(email=email)
                # otherwise report error
                else:
                    user = None
                    tb = traceback.format_exc()
                    self.message(
                        'Could not save user %s, got exception:\n\n%s' %
                        (user.username, tb))

            # if we got a user to add
            if user is not None:
                # store id
                self.users_dict[email]['id'] = user.id
                # append to saved users
                saved_users.append(user)
                self.verbose('Saved user %s (%s) with email <%s>' %
                             (user.username, user.get_full_name(), user.email))

            # mark email address as confirmed if feature is enabled
            if EMAIL_ADDRESS_APP_INSTALLED and EmailAddress.objects.filter(
                    email=user.email).count() is 0:
                try:
                    email_address = EmailAddress(user=user,
                                                 email=user.email,
                                                 verified=True,
                                                 primary=True)
                    email_address.full_clean()
                    email_address.save()
                except Exception as e:
                    tb = traceback.format_exc()
                    self.message(
                        'Could not save email address for user %s, got exception:\n\n%s'
                        % (user.username, tb))

        self.message('saved %d users into local DB' % len(saved_users))
        self.saved_users = saved_users