def _create_people_cache_import(self): """Create cache of people used in this block for import.""" people_emails = set() columns_with_people = self._gather_columns_with_people() for row_idx, row in enumerate(self.rows): for (column_idx, attr_name), header in columns_with_people.iteritems(): handler = header["handler"] emails = handler.get_people_emails_from_value(row[column_idx]) valid_emails = set() for email in emails: try: handler.validate_email(email) except ValueError as err: self._cache_error( errors.VALIDATION_ERROR, key=(row_idx, attr_name), column_name=header["display_name"], message=err.message, ) continue valid_emails.add(email) people_emails |= set(valid_emails) people = [] if people_emails: people = user_generator.find_users(people_emails) self._people_cache = structures.CaseInsensitiveDict( {person.email: person for person in people})
def parse_item(self): users = super(WorkflowMemberColumnHandler, self).parse_item() if ('workflow_owner' in self.row_converter.attrs and self.row_converter.attrs['workflow_owner'].value): owners = self.row_converter.attrs['workflow_owner'].value else: owners = user_generator.find_users( self._get_people_emails_for('WorkflowOwner')) return list(set(users) - set(owners))
def _parse_email_values(self): """Parse an email list of default assignees. This is the "other" option in the default assignee dropdown menu. """ # This is not good and fast, because it executes query for each # field from each row that contains people list. # If the feature is used actively, it should be refactored # and optimized. new_objects = self.row_converter.block_converter.converter.new_objects new_people = new_objects[all_models.Person] people = [] emails = [] for email in self.raw_value.splitlines(): email = email.strip() if not email: continue if email in new_people: # In "dry run" mode person.id is None, so it is replaced by int value # to pass validation. people.append(new_people[email].id or 0) else: emails.append(email) if emails: from ggrc.utils import user_generator for person in user_generator.find_users(emails): people.append(person.id) emails.remove(person.email) if emails: for email in emails: self.add_warning(errors.UNKNOWN_USER_WARNING, column_name=self.display_name, email=email) if not people and self.mandatory: self.add_error(errors.MISSING_VALUE_ERROR, column_name=self.display_name) return people