Skip to content

Commit

Permalink
Merge pull request #243 from openimis/develop
Browse files Browse the repository at this point in the history
MERGING develop into release/23.10
  • Loading branch information
delcroip authored Dec 6, 2023
2 parents 61a4e4d + 322538f commit ac44104
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 5 deletions.
5 changes: 4 additions & 1 deletion core/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"gql_mutation_delete_claim_administrator_perms": ["121604"],
"fields_controls_user": {},
"fields_controls_eo": {},
"is_valid_health_facility_contract_required": False
"is_valid_health_facility_contract_required": False,
"secondary_calendar": None
}


Expand Down Expand Up @@ -86,6 +87,7 @@ class CoreConfig(AppConfig):

fields_controls_user = {}
fields_controls_eo = {}
secondary_calendar = None

def _import_module(self, cfg, k):
logger.info('import %s.%s' %
Expand Down Expand Up @@ -167,6 +169,7 @@ def _configure_permissions(self, cfg):

def _configure_additional_settings(self, cfg):
CoreConfig.is_valid_health_facility_contract_required = cfg["is_valid_health_facility_contract_required"]
CoreConfig.secondary_calendar = cfg["secondary_calendar"]

def ready(self):
from .models import ModuleConfiguration
Expand Down
79 changes: 79 additions & 0 deletions core/management/commands/generateusers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import random

from django.core.management.base import BaseCommand
from faker import Faker

from claim.test_helpers import create_test_claim_admin
from core.models import User
from core.test_helpers import create_test_interactive_user, create_test_technical_user, create_test_officer
from insuree.models import Insuree
from policyholder.tests import create_test_policy_holder_user, create_test_policy_holder_insuree
from policyholder.tests.helpers_tests import create_test_policy_holder


def _create_test_claim_admin(*args, **kwargs):
username = kwargs.pop('username', None)
claim_admin = create_test_claim_admin(**kwargs)
return User.objects.create(username=username, claim_admin=claim_admin)


def _create_test_officer(*args, **kwargs):
kwargs.pop('username', None)
return create_test_officer(**kwargs)


class Command(BaseCommand):
help = "This command will generate test Users with some optional parameters. It is intended to simulate larger" \
"databases for performance testing"
insurees = None
users = None

USER_TYPE_T = "t_user"
USER_TYPE_I = "i_user"
USER_TYPE_OFFICER = "officer"
USER_TYPE_CLAIM_ADMIN = "claim_admin"
RANDOM = "random"

user_type_functions = {
USER_TYPE_I: create_test_interactive_user,
USER_TYPE_T: create_test_technical_user,
USER_TYPE_OFFICER: _create_test_officer,
USER_TYPE_CLAIM_ADMIN: _create_test_claim_admin,
}

def add_arguments(self, parser):
parser.add_argument("nb_users", nargs=1, type=int)
parser.add_argument("type", nargs=1, type=str, choices=[
self.RANDOM, self.USER_TYPE_OFFICER, self.USER_TYPE_T, self.USER_TYPE_I, self.USER_TYPE_CLAIM_ADMIN
])
parser.add_argument(
'--verbose',
action='store_true',
dest='verbose',
help='Be verbose about what it is doing',
)
parser.add_argument(
'--locale',
default="en",
help="Used to adapt the fake names generation to the locale, using Faker, by default en",
)

def handle(self, *args, **options):
fake = Faker(options["locale"])
nb_users = options["nb_users"][0]
user_type = options["type"][0]
verbose = options["verbose"]
for user_num in range(1, nb_users + 1):
props = dict(
username=fake.user_name(),
)
if user_type == self.RANDOM:
random_type = random.choice(list(self.user_type_functions.keys()))
user_type_create_function = self.user_type_functions[random_type]
else:
user_type_create_function = self.user_type_functions[user_type]

user = user_type_create_function(**props)

if verbose:
print(user_num, "created user", user.username, user.pk)
10 changes: 8 additions & 2 deletions core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class TechnicalUser(AbstractBaseUser):
is_superuser = models.BooleanField(default=False)
validity_from = models.DateTimeField(blank=True, null=True)
validity_to = models.DateTimeField(blank=True, null=True)

is_imis_admin = False
@property
def id_for_audit(self):
return -1
Expand Down Expand Up @@ -540,6 +540,12 @@ def delete_history(self, **kwargs):
def _u(self):
return self.i_user or self.officer or self.claim_admin or self.t_user

def has_perms(self, perm_list, obj=None):
if self.is_imis_admin:
return True
else:
return super().has_perms( perm_list, obj)

@property
def id_for_audit(self):
return self._u.id
Expand Down Expand Up @@ -1134,4 +1140,4 @@ def create_csv_export(qs, values, user, column_names=None,
sql_query=sql,
)
export.save()
return export
return export
4 changes: 2 additions & 2 deletions core/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ def resolve_validate_role_name(self, info, **kwargs):
def resolve_validate_username(self, info, **kwargs):
if not info.context.user.has_perms(CoreConfig.gql_query_users_perms):
raise PermissionDenied(_("unauthorized"))
if User.objects.filter(username=kwargs['username']).exists():
if User.objects.filter(username=kwargs['username'], validity_to__isnull=True).exists():
return False
else:
return True
Expand Down Expand Up @@ -1235,7 +1235,7 @@ def async_mutate(cls, user, **data):
try:
if type(user) is AnonymousUser or not user.id:
raise ValidationError("mutation.authentication_required")
if User.objects.filter(username=data['username']).exists():
if User.objects.filter(username=data['username'], validity_to__isnull=True).exists():
raise ValidationError("User with this user name already exists.")
if not user.has_perms(CoreConfig.gql_mutation_create_users_perms):
raise PermissionDenied("unauthorized")
Expand Down

0 comments on commit ac44104

Please sign in to comment.