• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

akvo / iwsims-demo / #72

28 Apr 2025 03:28AM UTC coverage: 86.134% (+1.1%) from 85.024%
#72

push

coveralls-python

web-flow
Merge pull request #20 from akvo/feature/19-eng-1231-dynamic-level-approval

Feature/19 eng 1231 dynamic level approval

2646 of 3188 branches covered (83.0%)

Branch coverage included in aggregate %.

5995 of 6844 relevant lines covered (87.59%)

0.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.0
backend/api/v1/v1_users/management/commands/fake_approver_seeder.py
1
from faker import Faker
1✔
2
import re
1✔
3
import uuid
1✔
4

5
from django.core.management import BaseCommand
1✔
6
from api.v1.v1_users.models import SystemUser
1✔
7
from api.v1.v1_profile.models import Access, Administration
1✔
8
from api.v1.v1_profile.constants import UserRoleTypes
1✔
9
from api.v1.v1_forms.models import UserForms, FormAccess
1✔
10
from api.v1.v1_forms.constants import FormAccessTypes
1✔
11

12
fake = Faker()
1✔
13

14

15
def new_user(administrations):
1✔
16
    for administration in administrations:
1✔
17
        email = ("{}{}@test.com").format(
1✔
18
            re.sub('[^A-Za-z0-9]+', '', administration.name.lower()),
19
            administration.id)
20
        email = "{}_{}".format(str(uuid.uuid4())[:4], email)
1✔
21
        user, _ = SystemUser.objects.get_or_create(
1✔
22
            email=email,
23
            first_name=administration.name,
24
            last_name="Approver",
25
        )
26
        user.set_password("test")
1✔
27
        user.save()
1✔
28

29
        # Create user access with admin role (no longer using approver role)
30
        Access.objects.create(
1✔
31
            user=user,
32
            role=UserRoleTypes.admin,  # All users are admins now
33
            administration=administration
34
        )
35

36
        # Assign form access for any forms they need to approve
37
        from api.v1.v1_forms.models import Forms
1✔
38
        forms = Forms.objects.all()
1✔
39
        for form in forms:
1✔
40
            user_form, _ = UserForms.objects.get_or_create(
1✔
41
                user=user,
42
                form=form
43
            )
44
            # For non-level 1 (non-admin) users, grant approver access
45
            if administration.level.level > 1:
1!
46
                FormAccess.objects.get_or_create(
×
47
                    user_form=user_form,
48
                    access_type=FormAccessTypes.approve
49
                )
50

51

52
class Command(BaseCommand):
1✔
53
    help = "Create fake approver users with access to forms"
1✔
54

55
    def add_arguments(self, parser):
1✔
56
        parser.add_argument(
1✔
57
            "-r",
58
            "--repeat",
59
            nargs="?",
60
            const=1,
61
            default=1,
62
            type=int
63
        )
64

65
    def handle(self, *args, **options):
1✔
66
        repeat = options.get("repeat")
1✔
67
        # Approvers (with approver form access)
68
        administrations = Administration.objects.filter(
1✔
69
            level__level__gt=0
70
        ).order_by('?')[:repeat]
71
        new_user(administrations)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc