From e6b5f1998752c61f2daf5a0f3cf86baea836426c Mon Sep 17 00:00:00 2001 From: Patrick Delcroix Date: Thu, 16 Nov 2023 15:02:12 +0100 Subject: [PATCH 1/7] fixing user liting (#235) --- core/schema.py | 6 ++++-- core/utils.py | 10 +++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/core/schema.py b/core/schema.py index 1e630020..6afa2286 100644 --- a/core/schema.py +++ b/core/schema.py @@ -24,6 +24,7 @@ set_user_password, ) from core.tasks import openimis_mutation_async +from core import filter_validity from django import dispatch from django.conf import settings from django.contrib.auth.models import AnonymousUser @@ -660,8 +661,9 @@ def resolve_users(self, info, email=None, last_name=None, other_names=None, phon show_deleted = kwargs.get('showDeleted', False) if not show_deleted and not kwargs.get('id', None): - active_users_ids = [user.id for user in user_query if user.is_active] - user_filters.append(Q(id__in=active_users_ids)) + + #active_users_ids = [user.id for user in user_query if user.is_active] + user_filters.append(Q(i_user__isnull=True) | Q(*filter_validity(prefix='i_user'))) text_search = kwargs.get("str") # Poorly chosen name, avoid of shadowing "str" if text_search: diff --git a/core/utils.py b/core/utils.py index dd66d5f7..965631e4 100644 --- a/core/utils.py +++ b/core/utils.py @@ -60,16 +60,16 @@ def __ne__(self, other): return cls -def filter_validity(arg="validity", **kwargs): +def filter_validity(arg="validity", prefix = '', **kwargs): validity = kwargs.get(arg) if validity is None: return ( - Q(legacy_id__isnull=True), - Q(validity_to__isnull=True), + Q(**{f'{prefix}legacy_id__isnull':True}), + Q(**{f'{prefix}validity_to__isnull':True}) ) return ( - Q(validity_from__lte=validity), - Q(validity_to__isnull=True) | Q(validity_to__gte=validity), + Q(**{f'{prefix}validity_from__lte':validity}), + Q(**{f'{prefix}validity_to__isnull':True}) | Q(**{f'{prefix}validity_to__gte':validity}) ) From 85e1f59636e3473d463927253411cba6484ee0ce Mon Sep 17 00:00:00 2001 From: Dragos DOBRE Date: Thu, 16 Nov 2023 22:14:15 +0100 Subject: [PATCH 2/7] fix prefix for validity query (#237) * fix prefix for validity query * fix prefix for user validity query --- core/schema.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/schema.py b/core/schema.py index 6afa2286..59e2a9ab 100644 --- a/core/schema.py +++ b/core/schema.py @@ -663,7 +663,7 @@ def resolve_users(self, info, email=None, last_name=None, other_names=None, phon if not show_deleted and not kwargs.get('id', None): #active_users_ids = [user.id for user in user_query if user.is_active] - user_filters.append(Q(i_user__isnull=True) | Q(*filter_validity(prefix='i_user'))) + user_filters.append(Q(i_user__isnull=True) | Q(*filter_validity(prefix='i_user__'))) text_search = kwargs.get("str") # Poorly chosen name, avoid of shadowing "str" if text_search: From 6ad51fc4b16ea025d1225f2815b0252c65e6f430 Mon Sep 17 00:00:00 2001 From: wzglinieckisoldevelo <98958634+wzglinieckisoldevelo@users.noreply.github.com> Date: Thu, 23 Nov 2023 09:30:29 +0100 Subject: [PATCH 3/7] ONI-48: Added setting for secondary calendar. (#239) --- core/apps.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/apps.py b/core/apps.py index 291072d3..f42dfa02 100644 --- a/core/apps.py +++ b/core/apps.py @@ -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 } @@ -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' % @@ -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 From 8c2102c881cb385e3ffcf549f1d9fe5eaf042459 Mon Sep 17 00:00:00 2001 From: Patrick Delcroix Date: Thu, 23 Nov 2023 14:50:53 +0100 Subject: [PATCH 4/7] Admin overwrite (#240) * fixing user liting * Update schema.py * orverload has perms * remove added line --- core/models.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/core/models.py b/core/models.py index c162c9f1..63c83354 100644 --- a/core/models.py +++ b/core/models.py @@ -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 From 00ff49542fa9331813ae9babec2dbbb3981295d6 Mon Sep 17 00:00:00 2001 From: wzglinieckisoldevelo <98958634+wzglinieckisoldevelo@users.noreply.github.com> Date: Mon, 4 Dec 2023 17:12:43 +0100 Subject: [PATCH 5/7] OP-1599: Username can be reused. (#241) * OP-1599: Username can be reused. * OP-1599: Changed mutation code to allow for username reuse. --- core/schema.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/schema.py b/core/schema.py index 59e2a9ab..743f834c 100644 --- a/core/schema.py +++ b/core/schema.py @@ -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 @@ -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") From eb8eeedfd89e989ef886a9bc7aa2e9bc9462a00c Mon Sep 17 00:00:00 2001 From: Patrick Delcroix Date: Tue, 5 Dec 2023 12:00:08 +0100 Subject: [PATCH 6/7] technical is not admin --- core/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/models.py b/core/models.py index 63c83354..dd7eda7d 100644 --- a/core/models.py +++ b/core/models.py @@ -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 @@ -1140,4 +1140,4 @@ def create_csv_export(qs, values, user, column_names=None, sql_query=sql, ) export.save() - return export \ No newline at end of file + return export From 322538f8bacf9af00be88af127e24eb6fba38c12 Mon Sep 17 00:00:00 2001 From: Jan Date: Tue, 5 Dec 2023 12:20:12 +0100 Subject: [PATCH 7/7] PCI-10: generate demo users (#228) Co-authored-by: Jan --- core/management/commands/generateusers.py | 79 +++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 core/management/commands/generateusers.py diff --git a/core/management/commands/generateusers.py b/core/management/commands/generateusers.py new file mode 100644 index 00000000..c0ea26ff --- /dev/null +++ b/core/management/commands/generateusers.py @@ -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)