Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ci.yml #85

Draft
wants to merge 25 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ on:
jobs:
call:
name: Default CI Flow
uses: openimis/openimis-be_py/.github/workflows/ci_module.yml@develop
uses: openimis/openimis-be_py/.github/workflows/ci_module.yml@ci_fhir_patch
secrets:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
with:
Expand Down
31 changes: 7 additions & 24 deletions insuree/gql_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,23 +99,6 @@ class UpdateFamilyInputType(FamilyInputType):
pass


def create_file(date, insuree_id, photo_bin):
date_iso = date.isoformat()
root = InsureeConfig.insuree_photos_root_path
file_dir = '%s/%s/%s/%s' % (
date_iso[0:4],
date_iso[5:7],
date_iso[8:10],
insuree_id
)
file_name = uuid.uuid4()
file_path = '%s/%s' % (file_dir, file_name)
pathlib.Path('%s/%s' % (root, file_dir)).mkdir(parents=True, exist_ok=True)
f = open('%s/%s' % (root, file_path), "xb")
f.write(base64.b64decode(photo_bin))
f.close()
return file_dir, file_name


def update_or_create_insuree(data, user):
data.pop('client_mutation_id', None)
Expand Down Expand Up @@ -214,7 +197,7 @@ def async_mutate(cls, user, **data):
for family_uuid in data["uuids"]:
family = Family.objects \
.prefetch_related('members') \
.filter(uuid=family_uuid) \
.filter(uuid__iexact=family_uuid) \
.first()
if family is None:
errors.append({
Expand Down Expand Up @@ -318,7 +301,7 @@ def async_mutate(cls, user, **data):
for insuree_uuid in data["uuids"]:
insuree = Insuree.objects \
.prefetch_related('family') \
.filter(uuid=insuree_uuid) \
.filter(uuid__iexact=insuree_uuid) \
.first()
if insuree is None:
errors.append({
Expand Down Expand Up @@ -360,7 +343,7 @@ def async_mutate(cls, user, **data):
for insuree_uuid in data["uuids"]:
insuree = Insuree.objects \
.prefetch_related('family') \
.filter(uuid=insuree_uuid) \
.filter(uuid__iexact=insuree_uuid) \
.first()
if insuree is None:
errors += {
Expand Down Expand Up @@ -401,8 +384,8 @@ def async_mutate(cls, user, **data):
if not user.has_perms(InsureeConfig.gql_mutation_update_families_perms):
raise PermissionDenied(_("unauthorized"))
try:
family = Family.objects.get(uuid=data['uuid'])
insuree = Insuree.objects.get(uuid=data['insuree_uuid'])
family = Family.objects.get(uuid__iexact=data['uuid'])
insuree = Insuree.objects.get(uuid__iexact=data['insuree_uuid'])
family.save_history()
prev_head = family.head_insuree
if prev_head:
Expand Down Expand Up @@ -441,8 +424,8 @@ def async_mutate(cls, user, **data):
not user.has_perms(InsureeConfig.gql_mutation_update_insurees_perms):
raise PermissionDenied(_("unauthorized"))
try:
family = Family.objects.get(uuid=data['family_uuid'])
insuree = Insuree.objects.get(uuid=data['insuree_uuid'])
family = Family.objects.get(uuid__iexact=data['family_uuid'])
insuree = Insuree.objects.get(uuid__iexact=data['insuree_uuid'])
insuree.save_history()
insuree.family = family
insuree.save()
Expand Down
4 changes: 2 additions & 2 deletions insuree/gql_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def resolve_photo(self, info):
class Meta:
model = Insuree
filter_fields = {
"uuid": ["exact"],
"uuid": ["exact","iexact"],
"chf_id": ["exact", "istartswith", "icontains", "iexact"],
"last_name": ["exact", "istartswith", "icontains", "iexact"],
"other_names": ["exact", "istartswith", "icontains", "iexact"],
Expand Down Expand Up @@ -188,7 +188,7 @@ class Meta:
"is_offline": ["exact"],
**prefix_filterset("location__", LocationGQLType._meta.filter_fields),
**prefix_filterset("head_insuree__", InsureeGQLType._meta.filter_fields),
** prefix_filterset("members__", InsureeGQLType._meta.filter_fields)
**prefix_filterset("members__", InsureeGQLType._meta.filter_fields)
}
interfaces = (graphene.relay.Node,)
connection_class = ExtendedConnection
Expand Down
2 changes: 0 additions & 2 deletions insuree/management/commands/generateinsurees.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ def handle(self, *args, **options):
last_name=fake.last_name(),
other_names=fake.first_name(),
dob=fake.date_between(start_date='-105y', end_date='today'),
chf_id=random.randrange(100000000, 999999999),
)
family_props = dict(
location_id=self.get_random_village(),
Expand All @@ -61,7 +60,6 @@ def handle(self, *args, **options):
props["other_names"] = fake.first_name()
props["dob"] = fake.date_between(start_date='-105y', end_date='today')
props["family_id"] = insuree.family_id
props["chf_id"] = random.randrange(100000000, 999999999)
member = create_test_insuree(with_family=False, custom_props=props)
if verbose:
print("Created family member", member_num, member.other_names)
Expand Down
8 changes: 4 additions & 4 deletions insuree/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def resolve_insurees(self, info, **kwargs):
def resolve_family_members(self, info, **kwargs):
if not info.context.user.has_perms(InsureeConfig.gql_query_insuree_family_members):
raise PermissionDenied(_("unauthorized"))
family = Family.objects.get(Q(uuid=kwargs.get('family_uuid')))
family = Family.objects.get(Q(uuid__iexact=kwargs.get('family_uuid')))
return Insuree.objects.filter(
Q(family=family),
*filter_validity(**kwargs)
Expand Down Expand Up @@ -246,7 +246,7 @@ def resolve_families(self, info, **kwargs):
officer = kwargs.get('officer', None)
if officer:
officer_policies_families = Policy.objects.filter(
officer__uuid=officer).values_list('family', flat=True)
officer__uuid__iexact=officer).values_list('family', flat=True)
filters.append(Q(id__in=officer_policies_families))

null_as_false_poverty = kwargs.get('null_as_false_poverty')
Expand Down Expand Up @@ -336,7 +336,7 @@ def on_family_mutation(kwargs, k='uuid'):
family_uuid = kwargs['data'].get('uuid', None)
if not family_uuid:
return []
impacted_family = Family.objects.get(Q(uuid=family_uuid))
impacted_family = Family.objects.get(Q(uuid__iexact=family_uuid))
FamilyMutation.objects.create(
family=impacted_family, mutation_id=kwargs['mutation_log_id'])
return []
Expand All @@ -360,7 +360,7 @@ def on_insuree_mutation(kwargs, k='uuid'):
insuree_uuid = kwargs['data'].get('uuid', None)
if not insuree_uuid:
return []
impacted_insuree = Insuree.objects.get(Q(uuid=insuree_uuid))
impacted_insuree = Insuree.objects.get(Q(uuid__iexact=insuree_uuid))
InsureeMutation.objects.create(
insuree=impacted_insuree, mutation_id=kwargs['mutation_log_id'])
return []
Expand Down
39 changes: 23 additions & 16 deletions insuree/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def validate_insuree_number(insuree_number, uuid=None):
query = Insuree.objects.filter(
chf_id=insuree_number, validity_to__isnull=True)
insuree = query.first()
if insuree and insuree.uuid != uuid:
if insuree and str(insuree.uuid).lower() != str(uuid).lower():
return [{"errorCode": InsureeConfig.validation_code_taken_insuree_number,
"message": "Insuree number has to be unique, %s exists in system" % insuree_number}]

Expand Down Expand Up @@ -152,11 +152,13 @@ def handle_insuree_photo(user, now, insuree, data):
data['audit_user_id'] = user.id_for_audit
data['validity_from'] = now
data['insuree_id'] = insuree.id
if 'uuid' not in data:
data['uuid'] = str(uuid.uuid4())

photo_bin = data.get('photo', None)
if photo_bin and InsureeConfig.insuree_photos_root_path \
and (insuree_photo is None or insuree_photo.photo != photo_bin):
(file_dir, file_name) = create_file(now, insuree.id, photo_bin)
data.pop('photo', None)
(file_dir, file_name) = create_file(now, insuree.id, photo_bin,data['uuid'] )
data['folder'] = file_dir
data['filename'] = file_name

Expand Down Expand Up @@ -193,11 +195,10 @@ def _create_dir(file_dir):
pathlib.Path(path.join(root, file_dir)) \
.mkdir(parents=True, exist_ok=True)


def create_file(date, insuree_id, photo_bin):
def create_file(date, insuree_id, photo_bin, name ):
file_dir = path.join(str(date.year), str(date.month),
str(date.day), str(insuree_id))
file_name = str(uuid.uuid4())
file_name = name

_create_dir(file_dir)
with open(_photo_dir(file_dir, file_name), "xb") as f:
Expand Down Expand Up @@ -235,21 +236,27 @@ def create_or_update(self, data):
data['validity_from'] = now
if InsureeConfig.insuree_fsp_mandatory and 'health_facility_id' not in data:
raise ValidationError("mutation.insuree.fsp_required")
insuree_uuid = data.pop('uuid', None)
insuree_uuid = data.get('uuid', None)
errors = validate_insuree_number(data["chf_id"], insuree_uuid)
if errors:
raise Exception("Invalid insuree number")
if insuree_uuid:
insuree = Insuree.objects.prefetch_related(
"photo").get(uuid=insuree_uuid)
insuree.save_history()
# reset the non required fields
# (each update is 'complete', necessary to be able to set 'null')
reset_insuree_before_update(insuree)
[setattr(insuree, key, data[key]) for key in data]
# create insuree with uuid if not foudn in the database
try:
insuree = Insuree.objects.prefetch_related(
"photo").get(uuid__iexact=insuree_uuid)
insuree.save_history()
# reset the non required fields
# (each update is 'complete', necessary to be able to set 'null')
reset_insuree_before_update(insuree)
[setattr(insuree, key, data[key]) for key in data]
insuree.save()
except:
insuree = Insuree.objects.create(**data)
else:
data['uuid'] = uuid.uuid4()
insuree = Insuree.objects.create(**data)
insuree.save()

photo = handle_insuree_photo(self.user, now, insuree, photo)
if photo:
insuree.photo = photo
Expand Down Expand Up @@ -344,7 +351,7 @@ def create_or_update(self, data):
data["head_insuree"] = head_insuree
family_uuid = data.pop('uuid', None)
if family_uuid:
family = Family.objects.get(uuid=family_uuid)
family = Family.objects.get(uuid__iexact=family_uuid)
family.save_history()
# reset the non required fields
# (each update is 'complete', necessary to be able to set 'null')
Expand Down
104 changes: 72 additions & 32 deletions insuree/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,80 @@
from insuree.apps import InsureeConfig
from insuree.models import Insuree, Family, Gender, InsureePhoto

from insuree.services import validate_insuree_number
from location.models import Location
import random

def create_test_insuree(with_family=True, is_head=False, custom_props=None, family_custom_props=None):
# insuree has a mandatory reference to family and family has a mandatory reference to insuree
# So we first insert the family with a dummy id and then update it
if with_family:
family = Family.objects.create(
validity_from="2019-01-01",
head_insuree_id=1, # dummy
audit_user_id=-1,
**(family_custom_props if family_custom_props else {})
#loof if it exists
family = None
location = None
village = None
ref = random.randrange(10000000, 99999999)
while validate_insuree_number(ref) != []:
ref = random.randrange(10000000, 99999999)
if custom_props is not None and 'chf_id' in custom_props:
ref=custom_props.pop('chf_id')
insuree = Insuree.objects.filter(chf_id=ref, validity_to__isnull=True).first()
if insuree is None:
#managing location
qs_location = Location.objects.filter(type="V")
if family_custom_props and "location" in family_custom_props:
location= family_custom_props.pop('location')
elif family_custom_props and "location_id" in family_custom_props:
location= qs_location.filter(location_id =family_custom_props.pop('location_id')).first()
if custom_props and "current_village" in custom_props:
village = custom_props.pop('current_village')
elif custom_props and "current_village_id" in custom_props:
village= qs_location.filter(current_village_id =custom_props.pop('current_village_id')).first()
if location is None and village is None :
location = qs_location.first()
village = location
elif location is None:
location = village
else:
village = location


family = get_from_custom_props(custom_props, 'family',None)


if family is None and with_family:
if family_custom_props and "id" in family_custom_props:
family = Family.objects.filter(id=family_custom_props['id']).first()

if family is None:

family = Family.objects.create(
validity_from=get_from_custom_props(family_custom_props, 'audit_user_id',"2019-01-01"),
head_insuree_id=get_from_custom_props(custom_props, 'id',1),
audit_user_id=get_from_custom_props(family_custom_props, 'audit_user_id',-1),
location=location,
**(family_custom_props if family_custom_props else {})
)


insuree = Insuree.objects.create(
last_name = get_from_custom_props(custom_props, 'last_name',"last" ),
other_names= get_from_custom_props(custom_props, 'other_names', "First Second"),
family= family,
gender= get_from_custom_props(custom_props, 'gender', Gender.objects.get(code='M')),
dob= get_from_custom_props(custom_props, 'dob', '1972-08-09'),
chf_id= ref,
head= is_head,
card_issued= get_from_custom_props(custom_props, 'card_issued', True),
validity_from= get_from_custom_props(custom_props, 'validity_from',"2019-01-01"),
audit_user_id= get_from_custom_props(custom_props, 'audit_user_id',-1),
current_village= village,
**(custom_props if custom_props else {})
)
else:
family = None

insuree = Insuree.objects.create(
**{
"last_name": "Test Last",
"other_names": "First Second",
"chf_id": "chf_dflt",
"family": family,
"gender": Gender.objects.get(code='M'),
"dob": "1970-01-01",
"head": is_head,
"card_issued": True,
"validity_from": "2019-01-01",
"audit_user_id": -1,
**(custom_props if custom_props else {})
}
)
if with_family:
family.head_insuree_id = insuree.id
if family_custom_props:
for k, v in family_custom_props.items():
setattr(family, k, v)
family.save()
if with_family:
family.head_insuree_id = insuree.id
if family_custom_props:
for k, v in family_custom_props.items():
setattr(family, k, v)
family.save()

return insuree

Expand All @@ -50,7 +89,8 @@ def create_test_insuree(with_family=True, is_head=False, custom_props=None, fami
l6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD3+
iiigD//2Q==
"""

def get_from_custom_props( custom_props, elm, default):
return custom_props.pop(elm) if custom_props and elm in custom_props else default

def create_test_photo(insuree_id, officer_id, custom_props=None):
photo = InsureePhoto.objects.create(
Expand Down
Loading