Пример #1
0
    def import_admins(self):
        """ save admins to local DB """
        self.message('saving admins into local DB')

        saved_admins = []

        for olduser in OldUser.objects.all():
            try:
                user = User.objects.get(
                    Q(username=olduser.username) | Q(email=olduser.email))
            except User.DoesNotExist:
                user = User()
            except User.MultipleObjectsReturned:
                continue

            user.username = olduser.username
            user.password = olduser.password
            user.first_name = olduser.first_name
            user.last_name = olduser.last_name
            user.email = olduser.email
            user.is_active = olduser.is_active
            user.is_staff = olduser.is_staff
            user.is_superuser = olduser.is_superuser
            user.date_joined = olduser.date_joined
            user.full_clean()
            user.save(sync_emailaddress=False)
            saved_admins.append(user)

            # mark email address as confirmed if feature is enabled
            if EMAIL_CONFIRMATION 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:
                    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 admins into local DB' % len(saved_admins))
        self.saved_admins = saved_admins
Пример #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(sync_emailaddress=False)
            except Exception:
                # 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_CONFIRMATION 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:
                    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