Skip to content

Commit

Permalink
Add geo area in location for assessment registry.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rup-Narayan-Rajbanshi committed Aug 1, 2023
1 parent 544d98e commit ed7fabd
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.2.17 on 2023-08-01 05:19

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('assessment_registry', '0010_additionaldocument_file_path'),
]

operations = [
migrations.RemoveField(
model_name='additionaldocument',
name='file_path',
),
]
2 changes: 0 additions & 2 deletions apps/assessment_registry/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,6 @@ class DocumentType(models.IntegerChoices):
null=True, blank=True
)
external_link = models.URLField(max_length=500, blank=True)
file_path = models.CharField(max_length=500, blank=True)


class ScoreRating(UserResource):
class ScoreType(models.IntegerChoices):
Expand Down
11 changes: 4 additions & 7 deletions apps/assessment_registry/schema.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import graphene
from typing import Optional
from graphene_django import DjangoObjectType
from graphene_django_extras import DjangoObjectField

from utils.commons import render_string_for_graphql
from utils.graphene.types import ClientIdMixin, CustomDjangoListObjectType
from utils.graphene.fields import DjangoPaginatedListObjectField
from utils.graphene.pagination import NoOrderingPageGraphqlPagination
Expand Down Expand Up @@ -274,9 +274,7 @@ class Meta:
document_type_display = EnumDescription(source='get_document_type_display', required=True)

def resolve_external_link(root, info, **kwargs):
if root.external_link == "":
return None
return root.external_link
return render_string_for_graphql(root.external_link)


class AssessmentRegistryType(
Expand Down Expand Up @@ -323,14 +321,13 @@ class Meta:
lead = graphene.NonNull(LeadDetailType)
locations = graphene.List(graphene.NonNull(ProjectGeoAreaType))


@staticmethod
def get_custom_queryset(queryset, info, **kwargs):
return get_assessment_registry_qs(info)

@staticmethod
def resolve_locations(root, info, **kwargs) -> Optional[None]:
return root.locations.all()
def resolve_locations(root, info, **_):
return info.context.dl.geo.assessment_registry_locations.load(root.pk)

@staticmethod
def resolve_methodology_attributes(root, info, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion apps/assessment_registry/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Meta:
class AdditionalDocumentSerializer(TempClientIdMixin, UserResourceSerializer):
class Meta:
model = AdditionalDocument
fields = ("client_id", "document_type", "file", "external_link", "file_path",)
fields = ("client_id", "document_type", "file", "external_link",)


class ScoreRatingSerializer(TempClientIdMixin, UserResourceSerializer):
Expand Down
21 changes: 14 additions & 7 deletions apps/geo/dataloaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .models import (
AdminLevel,
)
from assessment_registry.models import AssessmentRegistry


class AdminLevelLoader(DataLoaderWithContext):
Expand All @@ -19,15 +20,21 @@ def batch_load_fn(self, keys):
return Promise.resolve([_map.get(key) for key in keys])


class GeoAreaLoader(DataLoaderWithContext):
def batch_load_fn(self, keys):
ary_geo_area_qs = AssessmentRegistry.locations.through.objects\
.filter(assessmentregistry__in=keys).prefetch_related('geoarea')
_map = defaultdict(list)
for ary_geo_area in ary_geo_area_qs.all():
_map[ary_geo_area.assessmentregistry_id].append(ary_geo_area.geoarea)
return Promise.resolve([_map.get(key) for key in keys])


class DataLoaders(WithContextMixin):
@cached_property
def admin_levels_by_region(self):
return AdminLevelLoader(context=self.context)

#class GeoAreaLoader(DataLoaderWithContext):
# def batch_load_fn(self, keys):
# geo_area_qs = GeoArea.objects.all()
# _map = defaultdict(list)
# for geo_area in geo_area_qs:
# _map[geo_area.id].append(geo_area)
# return Promise.resolve([_map.get(key) for key in keys])
@cached_property
def assessment_registry_locations(self):
return GeoAreaLoader(context=self.context)
7 changes: 7 additions & 0 deletions apps/geo/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ class Meta:
region_title = graphene.String(required=True)
admin_level_title = graphene.String(required=True)

@staticmethod
def resolve_region_title(root, info, **kwargs):
return root.admin_level.region.title

def resolve_admin_level_title(root, info, **kwargs):
return root.admin_level.title


class ProjectGeoAreaListType(CustomDjangoListObjectType):
class Meta:
Expand Down
1 change: 0 additions & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ input AdditionalDocumentInputType {
documentType: AssessmentRegistryDocumentTypeEnum!
file: ID
externalLink: String
filePath: String
}

type AdditionalDocumentType {
Expand Down
9 changes: 9 additions & 0 deletions utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,3 +588,12 @@ def generate_sha256(text: str):
m = hashlib.sha256()
m.update(text.encode('utf-8'))
return m.hexdigest()


def render_string_for_graphql(text):
"""
Return null if text is empty ("")
"""
if text == '':
return None
return text

0 comments on commit ed7fabd

Please sign in to comment.