Skip to content

Commit

Permalink
merged from develop
Browse files Browse the repository at this point in the history
  • Loading branch information
sniedzielski committed Jan 10, 2024
2 parents b535f58 + 51ac481 commit afe01d9
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 19 deletions.
2 changes: 2 additions & 0 deletions individual/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"gql_group_update_perms": ["180003"],
"gql_group_delete_perms": ["180004"],
"gql_check_individual_update": True,
"gql_check_group_individual_update": True,
}


Expand All @@ -26,6 +27,7 @@ class IndividualConfig(AppConfig):
gql_group_update_perms = None
gql_group_delete_perms = None
gql_check_individual_update = None
gql_check_group_individual_update = None

def ready(self):
from core.models import ModuleConfiguration
Expand Down
26 changes: 18 additions & 8 deletions individual/gql_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ def _mutate(cls, user, **data):
data.pop('client_mutation_label')

service = IndividualService(user)
service.create(data)
result = service.create(data)
return result if not result['success'] else None

class Input(CreateIndividualInputType):
pass
Expand Down Expand Up @@ -93,9 +94,10 @@ def _mutate(cls, user, **data):

service = IndividualService(user)
if IndividualConfig.gql_check_individual_update:
service.create_update_task(data)
result = service.create_update_task(data)
else:
service.update(data)
result = service.update(data)
return result if not result['success'] else None

class Input(UpdateIndividualInputType):
pass
Expand Down Expand Up @@ -150,7 +152,8 @@ def _mutate(cls, user, **data):
data.pop('client_mutation_label')

service = GroupService(user)
service.create(data)
result = service.create(data)
return result if not result['success'] else None

class Input(CreateGroupInputType):
pass
Expand Down Expand Up @@ -178,7 +181,8 @@ def _mutate(cls, user, **data):
data.pop('client_mutation_label')

service = GroupService(user)
service.update(data)
result = service.update(data)
return result if not result['success'] else None

class Input(UpdateGroupInputType):
pass
Expand Down Expand Up @@ -233,7 +237,8 @@ def _mutate(cls, user, **data):
data.pop('client_mutation_label')

service = GroupIndividualService(user)
service.create(data)
result = service.create(data)
return result if not result['success'] else None

class Input(CreateGroupIndividualInputType):
pass
Expand Down Expand Up @@ -261,7 +266,11 @@ def _mutate(cls, user, **data):
data.pop('client_mutation_label')

service = GroupIndividualService(user)
service.update(data)
if IndividualConfig.gql_check_group_individual_update:
result = service.create_update_task(data)
else:
result = service.update(data)
return result if not result['success'] else None

class Input(UpdateGroupIndividualInputType):
pass
Expand Down Expand Up @@ -316,7 +325,8 @@ def _mutate(cls, user, **data):
data.pop('client_mutation_label')

service = GroupService(user)
service.create_group_individuals(data)
result = service.create_group_individuals(data)
return result if not result['success'] else None

class Input(CreateGroupInputType):
individual_ids = graphene.List(graphene.UUID)
14 changes: 8 additions & 6 deletions individual/gql_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from graphene_django import DjangoObjectType

from core import prefix_filterset, ExtendedConnection
from core.gql_queries import UserGQLType
from individual.models import Individual, IndividualDataSource, Group, GroupIndividual, IndividualDataSourceUpload


Expand All @@ -27,6 +28,10 @@ class Meta:

class IndividualHistoryGQLType(DjangoObjectType):
uuid = graphene.String(source='uuid')
user_updated = graphene.Field(UserGQLType)

def resolve_user_updated(self, info):
return self.user_updated

class Meta:
model = Individual.history.model
Expand Down Expand Up @@ -109,13 +114,10 @@ class Meta:

class GroupHistoryGQLType(DjangoObjectType):
uuid = graphene.String(source='uuid')
head = graphene.Field(IndividualGQLType)
user_updated = graphene.Field(UserGQLType)

def resolve_head(self, info):
return Individual.objects.filter(
groupindividual__group__id=self.id,
groupindividual__role=GroupIndividual.Role.HEAD
).first()
def resolve_user_updated(self, info):
return self.user_updated

class Meta:
model = Group.history.model
Expand Down
76 changes: 71 additions & 5 deletions individual/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from core.services import BaseService
from core.signals import register_service_signal
from django.utils.translation import gettext as _
from individual.models import Individual, IndividualDataSource, GroupIndividual, Group
from individual.validation import IndividualValidation, IndividualDataSourceValidation, GroupIndividualValidation, \
GroupValidation
Expand Down Expand Up @@ -110,20 +111,85 @@ def update_group_individuals(self, obj_data):
return output_exception(model_name=self.OBJECT_TYPE.__name__, method="update", exception=exc)


class GroupIndividualService(BaseService):
class GroupIndividualService(BaseService, UpdateCheckerLogicServiceMixin):
OBJECT_TYPE = GroupIndividual

def __init__(self, user, validation_class=GroupIndividualValidation):
super().__init__(user, validation_class)

@register_service_signal('group_individual_service.create')
@register_service_signal('groupindividual_service.create')
def create(self, obj_data):
return super().create(obj_data)

@register_service_signal('group_individual.update')
@register_service_signal('groupindividual_service.update')
@check_authentication
def update(self, obj_data):
return super().update(obj_data)
try:
with transaction.atomic():
obj_data = self._adjust_update_payload(obj_data)
self.validation_class.validate_update(self.user, **obj_data)
obj_ = self.OBJECT_TYPE.objects.filter(id=obj_data['id']).first()
group_id_before_update = obj_.group.id
self._handle_head_change(obj_data, obj_)
[setattr(obj_, key, obj_data[key]) for key in obj_data]
result = self.save_instance(obj_)
self._handle_json_ext(group_id_before_update, obj_)
return result
except Exception as exc:
return output_exception(model_name=self.OBJECT_TYPE.__name__, method="update", exception=exc)

def _handle_head_change(self, obj_data, obj_):
with transaction.atomic():
if obj_.role == GroupIndividual.Role.RECIPIENT and obj_data['role'] == GroupIndividual.Role.HEAD:
self._change_head(obj_data)

def _change_head(self, obj_data):
with transaction.atomic():
group_id = obj_data.get('group_id')
group_queryset = GroupIndividual.objects.filter(group_id=group_id, role=GroupIndividual.Role.HEAD)
old_head = group_queryset.first()
if old_head:
old_head.role = GroupIndividual.Role.RECIPIENT
old_head.save(username=self.user.username)

@register_service_signal('group_individual.delete')
if group_queryset.exists():
raise ValueError(_("more_than_one_head_in_group"))

def _handle_json_ext(self, group_id_before_update, obj_):
self._update_json_ext_for_group(group_id_before_update)
if group_id_before_update != obj_.group.id:
self._update_json_ext_for_group(obj_.group.id)

def _update_json_ext_for_group(self, group_id):
group = Group.objects.filter(id=group_id).first()
group_individuals = GroupIndividual.objects.filter(group_id=group_id)
head = group_individuals.filter(role=GroupIndividual.Role.HEAD).first()

group_members = {
str(individual.individual.id): f"{individual.individual.first_name} {individual.individual.last_name}"
for individual in group_individuals
}
head_str = f'{head.individual.first_name} {head.individual.last_name}' if head else None

changes_to_save = {}

if group.json_ext.get("members") != group_members:
changes_to_save["members"] = group_members

if group.json_ext.get("head") != head_str:
changes_to_save["head"] = head_str

if changes_to_save:
group.json_ext.update(changes_to_save)
group.save(username=self.user.username)

@register_service_signal('groupindividual_service.delete')
def delete(self, obj_data):
return super().delete(obj_data)

def _data_for_json_ext_update(self, obj_data):
group_individual = GroupIndividual.objects.get(id=obj_data.get("id"))
individual = group_individual.individual
individual_identity_string = f'{individual.first_name} {individual.last_name}'
json_ext_data = {"individual_identity": individual_identity_string}
return json_ext_data
22 changes: 22 additions & 0 deletions individual/signals/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import logging

from core.service_signals import ServiceSignalBindType
from core.signals import bind_service_signal
from individual.services import GroupIndividualService, IndividualService

from tasks_management.services import on_task_complete_service_handler

logger = logging.getLogger(__name__)


def bind_service_signals():
bind_service_signal(
'task_service.complete_task',
on_task_complete_service_handler(GroupIndividualService),
bind_type=ServiceSignalBindType.AFTER
)
bind_service_signal(
'task_service.complete_task',
on_task_complete_service_handler(IndividualService),
bind_type=ServiceSignalBindType.AFTER
)

0 comments on commit afe01d9

Please sign in to comment.