Skip to content

Commit

Permalink
- update enum for content types
Browse files Browse the repository at this point in the history
- optimize the dataloaders
  • Loading branch information
sudan45 committed Apr 29, 2024
1 parent 693a83b commit 5522557
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 27 deletions.
51 changes: 35 additions & 16 deletions apps/notification/dataloaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

from promise import Promise

from entry.models import Entry
from entry.models import EntryComment
from notification.models import Assignment
from lead.models import Lead
from quality_assurance.models import EntryReviewComment

from utils.graphene.dataloaders import DataLoaderWithContext, WithContextMixin

Expand All @@ -14,32 +15,50 @@ def batch_load_fn(self, keys):
assignment_qs = list(
Assignment.objects
.filter(id__in=keys)
.values_list('id', 'content_type__app_label', 'object_id')
.values_list('id', 'content_type__model', 'object_id')
)

leads_id = []
entries_id = []
entry_review_comment_id = []

for _, content_type, object_id in assignment_qs:
if content_type == 'entry':
entries_id.append(object_id)
elif content_type == 'lead':
# TODO use dict map function
if content_type == Lead._meta.model_name:
leads_id.append(object_id)
elif content_type == EntryReviewComment._meta.model_name:
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
)

lead_id_map = {
lead.id: lead
for lead in Lead.objects.filter(id__in=leads_id)
} if leads_id else {}
_entry_review_comment_id_map = {}

entry_id_map = {
entry.id: entry
for entry in Entry.objects.filter(id__in=entries_id)
} if entries_id else {}
for _id, text, entry_id, entry_excerpt, lead_id in EntryReviewComment.objects.filter(
id__in=entry_review_comment_id).values_list(
'id',
'comment_texts__text',
'entry__id',
'entry__excerpt',
'entry__lead_id'
):
_entry_review_comment_id_map[_id] = dict(
id=_id,
text=text,
entry_id=entry_id,
entry_excerpt=entry_excerpt,
lead_id=lead_id
)

_result = {
_id: {
'content_type': content_type,
'lead': lead_id_map.get(object_id) if content_type == 'lead' else None,
'entry': entry_id_map.get(object_id) if content_type == 'entry' else None,
'lead': _lead_id_map.get(object_id) if content_type == 'lead' else None,
'entry_review_comment': _entry_review_comment_id_map.get(object_id) if content_type == 'entryreviewcomment' else None,
}
for _id, content_type, object_id in assignment_qs
}
Expand Down
10 changes: 9 additions & 1 deletion apps/notification/enums.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import graphene

from utils.graphene.enums import (
convert_enum_to_graphene_enum,
get_enum_name_from_django_field,
)

from lead.models import Lead
from quality_assurance.models import EntryReviewComment
from .models import Notification

NotificationTypeEnum = convert_enum_to_graphene_enum(Notification.Type, name='NotificationTypeEnum')
Expand All @@ -15,3 +18,8 @@
(Notification.status, NotificationStatusEnum),
)
}


class AssignmentContentTypeEnum(graphene.Enum):
LEAD = Lead._meta.model_name
ENTRY_REVIEW_COMMENT = EntryReviewComment._meta.model_name
5 changes: 4 additions & 1 deletion apps/notification/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.utils import timezone
from quality_assurance.models import EntryReviewComment

from user.models import User
from project.models import Project
Expand Down Expand Up @@ -88,4 +89,6 @@ class Meta:

@staticmethod
def get_for(user):
return Assignment.objects.filter(created_for=user).distinct()
return Assignment.objects.filter(
created_for=user,
).distinct()
17 changes: 8 additions & 9 deletions apps/notification/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .enums import (
NotificationTypeEnum,
NotificationStatusEnum,
AssignmentContentTypeEnum
)


Expand Down Expand Up @@ -51,24 +52,22 @@ class AssignmentLeadDetailType(graphene.ObjectType):
id = graphene.ID(required=True)
title = graphene.String(required=True)

class Meta:
model = Lead
fields = ['id', 'title']


class AssignmentEntryDetailType(graphene.ObjectType):
class AssignmentProjectDetailType(graphene.ObjectType):
id = graphene.ID(required=True)
title = graphene.String(required=True)


class AssignmentProjectDetailType(graphene.ObjectType):
class AssignmentEntryReviewCommentDetailType(graphene.ObjectType):
id = graphene.ID(required=True)
title = graphene.String(required=True)
entry_id = graphene.ID(required=True)
lead_id = graphene.ID(required=True)


class AssignmentContentDataType(graphene.ObjectType):
content_type = graphene.String(required=False)
content_type = graphene.Field(AssignmentContentTypeEnum)
lead = graphene.Field(AssignmentLeadDetailType)
entry = graphene.Field(AssignmentEntryDetailType)
entry_review_comment = graphene.Field(AssignmentEntryReviewCommentDetailType)


class AssignmentType(DjangoObjectType):
Expand Down

0 comments on commit 5522557

Please sign in to comment.