-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1440 from the-deep/features/assignment-rest-graphql
implement graphql query and mutation node for assignment
- Loading branch information
Showing
12 changed files
with
528 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from django.utils.functional import cached_property | ||
from django.db import models | ||
|
||
from promise import Promise | ||
|
||
from notification.models import Assignment | ||
from lead.models import Lead | ||
from quality_assurance.models import EntryReviewComment | ||
|
||
from utils.graphene.dataloaders import DataLoaderWithContext, WithContextMixin | ||
|
||
|
||
def get_model_name(model: models.Model) -> str: | ||
return model._meta.model_name | ||
|
||
|
||
class AssignmentLoader(DataLoaderWithContext): | ||
def batch_load_fn(self, keys): | ||
assignment_qs = list( | ||
Assignment.objects | ||
.filter(id__in=keys) | ||
.values_list('id', 'content_type__model', 'object_id') | ||
) | ||
|
||
leads_id = [] | ||
entry_review_comment_id = [] | ||
|
||
for _, content_type, object_id in assignment_qs: | ||
if content_type == get_model_name(Lead): | ||
leads_id.append(object_id) | ||
elif content_type == get_model_name(EntryReviewComment): | ||
entry_review_comment_id.append(object_id) | ||
|
||
_lead_id_map = {} | ||
|
||
for _id, title in Lead.objects.filter(id__in=leads_id).values_list('id', 'title'): | ||
_lead_id_map[_id] = dict( | ||
id=_id, | ||
title=title | ||
) | ||
|
||
_entry_review_comment_id_map = {} | ||
|
||
for _id, entry_id, lead_id in EntryReviewComment.objects.filter( | ||
id__in=entry_review_comment_id).values_list( | ||
'id', | ||
'entry__id', | ||
'entry__lead_id' | ||
): | ||
_entry_review_comment_id_map[_id] = dict( | ||
id=_id, | ||
entry_id=entry_id, | ||
lead_id=lead_id | ||
) | ||
|
||
_result = { | ||
_id: { | ||
'content_type': content_type, | ||
'lead': ( | ||
_lead_id_map.get(object_id) | ||
if content_type == get_model_name(Lead) else None | ||
), | ||
'entry_review_comment': ( | ||
_entry_review_comment_id_map.get(object_id) | ||
if content_type == get_model_name(EntryReviewComment) else None | ||
), | ||
} | ||
for _id, content_type, object_id in assignment_qs | ||
} | ||
|
||
return Promise.resolve([_result[key] for key in keys]) | ||
|
||
|
||
class DataLoaders(WithContextMixin): | ||
@cached_property | ||
def assignment(self): | ||
return AssignmentLoader(context=self.context) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
from factory.django import DjangoModelFactory | ||
|
||
from notification.models import ( | ||
Assignment, | ||
Notification, | ||
) | ||
|
||
|
||
class NotificationFactory(DjangoModelFactory): | ||
class Meta: | ||
model = Notification | ||
|
||
|
||
class AssignmentFactory(DjangoModelFactory): | ||
class Meta: | ||
model = Assignment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.