Exemplo n.º 1
0
 def test_leader_not_in_categories(self):
     form = DistributionListForm({
         'name': 'test',
         'categories': [self.category.id],
         'leader': UserFactory().id})
     self.assertFalse(form.is_valid())
     self.assertTrue('leader' in form.errors)
Exemplo n.º 2
0
 def test_leader_not_in_categories(self):
     form = DistributionListForm({
         'name': 'test',
         'categories': [self.category.id],
         'leader': UserFactory().id
     })
     self.assertFalse(form.is_valid())
     self.assertTrue('leader' in form.errors)
Exemplo n.º 3
0
 def test_same_user_in_approver_and_reviewer(self):
     form = DistributionListForm({
         'name': 'test',
         'categories': [self.category.id],
         'leader': self.users[0].id,
         'approver': self.users[1].id,
         'reviewers': [u.id for u in self.users[1:]]})
     self.assertFalse(form.is_valid())
Exemplo n.º 4
0
 def test_form_valid(self):
     form = DistributionListForm({
         'name': 'test',
         'categories': [self.category.id],
         'leader': self.users[0].id,
         'approver': self.users[1].id,
         'reviewers': [u.id for u in self.users[2:]]})
     self.assertTrue(form.is_valid())
Exemplo n.º 5
0
 def test_same_user_in_approver_and_reviewer(self):
     form = DistributionListForm({
         'name': 'test',
         'categories': [self.category.id],
         'leader': self.users[0].id,
         'approver': self.users[1].id,
         'reviewers': [u.id for u in self.users[1:]]
     })
     self.assertFalse(form.is_valid())
Exemplo n.º 6
0
 def test_form_valid(self):
     form = DistributionListForm({
         'name': 'test',
         'categories': [self.category.id],
         'leader': self.users[0].id,
         'approver': self.users[1].id,
         'reviewers': [u.id for u in self.users[2:]]
     })
     self.assertTrue(form.is_valid())
Exemplo n.º 7
0
 def test_reviewers_not_in_categories(self):
     form = DistributionListForm({
         'name': 'test',
         'categories': [self.category.id],
         'leader': self.users[0].id,
         'reviewers': [UserFactory().id, UserFactory().id]
     })
     self.assertFalse(form.is_valid())
     self.assertTrue('reviewers' in form.errors)
Exemplo n.º 8
0
 def test_reviewers_not_in_categories(self):
     form = DistributionListForm({
         'name':
         'test',
         'categories': [self.category.id],
         'leader':
         self.users[0].id,
         'reviewers': [UserFactory().id, UserFactory().id]
     })
     self.assertFalse(form.is_valid())
     self.assertTrue('reviewers' in form.errors)
Exemplo n.º 9
0
 def test_reviewers_not_in_categories(self):
     form = DistributionListForm(
         {
             "name": "test",
             "categories": [self.category.id],
             "leader": self.users[0].id,
             "reviewers": [UserFactory().id, UserFactory().id],
         }
     )
     self.assertFalse(form.is_valid())
     self.assertTrue("reviewers" in form.errors)
Exemplo n.º 10
0
 def test_same_user_in_approver_and_reviewer(self):
     form = DistributionListForm(
         {
             "name": "test",
             "categories": [self.category.id],
             "leader": self.users[0].id,
             "approver": self.users[1].id,
             "reviewers": [u.id for u in self.users[1:]],
         }
     )
     self.assertFalse(form.is_valid())
Exemplo n.º 11
0
 def test_form_valid(self):
     form = DistributionListForm(
         {
             "name": "test",
             "categories": [self.category.id],
             "leader": self.users[0].id,
             "approver": self.users[1].id,
             "reviewers": [u.id for u in self.users[2:]],
         }
     )
     self.assertTrue(form.is_valid())
Exemplo n.º 12
0
def _import_list(row, emails, user_ids, category):
    """Saves a distribution list for a single row."""
    errors = []

    # Fetch existing list if it exists
    list_name = row[0].value
    try:
        instance = DistributionList.objects.get(name=list_name)
        categories = list(instance.categories.all())
        categories.append(category)
        action = _('Update')
    except DistributionList.DoesNotExist:
        instance = None
        categories = [category]
        action = _('Create')

    # Extract user roles from xls
    reviewers = []
    leader = None
    approver = None
    for idx, cell in enumerate(row[1:]):
        role = cell.value
        if role:
            user_id = user_ids[idx]
            if user_id is None:
                errors.append('Unknown user "{}"'.format(emails[idx]))

            if role == 'R':
                reviewers.append(user_id)
            elif role == 'L':
                leader = user_id
            elif role == 'A':
                approver = user_id
            else:
                errors.append('Unknown role "{}"'.format(role))

    # Use the model form to validate and save the data
    data = {
        'name': list_name,
        'categories': [c.id for c in categories],
        'reviewers': reviewers,
        'leader': leader if leader else None,
        'approver': approver if approver else None,
    }
    form = DistributionListForm(data, instance=instance)
    if form.is_valid() and not errors:
        form.save()

    if not form.is_valid():
        form_errors = [
            field_errors[0] for field_errors in list(form.errors.values())
        ]
        errors.append(*form_errors)

    return {
        'list_name': list_name,
        'action': action,
        'success': form.is_valid() and not errors,
        'errors': errors,
    }
Exemplo n.º 13
0
def _import_list(row, emails, user_ids, category):
    """Saves a distribution list for a single row."""
    errors = []

    # Fetch existing list if it exists
    list_name = row[0].value
    try:
        instance = DistributionList.objects.get(name=list_name)
        categories = list(instance.categories.all())
        categories.append(category)
        action = _('Update')
    except DistributionList.DoesNotExist:
        instance = None
        categories = [category]
        action = _('Create')

    # Extract user roles from xls
    reviewers = []
    leader = None
    approver = None
    for idx, cell in enumerate(row[1:]):
        role = cell.value
        if role:
            user_id = user_ids[idx]
            if user_id is None:
                errors.append('Unknown user "{}"'.format(emails[idx]))

            if role == 'R':
                reviewers.append(user_id)
            elif role == 'L':
                leader = user_id
            elif role == 'A':
                approver = user_id
            else:
                errors.append('Unknown role "{}"'.format(role))

    # Use the model form to validate and save the data
    data = {
        'name': list_name,
        'categories': [c.id for c in categories],
        'reviewers': reviewers,
        'leader': leader if leader else None,
        'approver': approver if approver else None,
    }
    form = DistributionListForm(data, instance=instance)
    if form.is_valid() and not errors:
        form.save()

    if not form.is_valid():
        form_errors = map(
            lambda field_errors: field_errors[0],
            form.errors.values())
        errors.append(*form_errors)

    return {
        'list_name': list_name,
        'action': action,
        'success': form.is_valid() and not errors,
        'errors': errors,
    }
Exemplo n.º 14
0
 def test_leader_not_in_categories(self):
     form = DistributionListForm({"name": "test", "categories": [self.category.id], "leader": UserFactory().id})
     self.assertFalse(form.is_valid())
     self.assertTrue("leader" in form.errors)