Skip to content

Commit

Permalink
Fixed country searching and sorting issue
Browse files Browse the repository at this point in the history
  • Loading branch information
vikashkum05 committed Oct 10, 2024
1 parent e7a3666 commit e2e00d7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 31 deletions.
2 changes: 1 addition & 1 deletion proco/accounts/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,7 @@ def get_static_info_query(self, query_labels):
FROM "schools_school"
{school_weekly_join}
LEFT JOIN connection_statistics_schoolweeklystatus sws ON "schools_school"."last_weekly_status_id" = sws."id"
WHERE "schools_school"."deleted" IS NULL AND sws."deleted" IS NULL
WHERE "schools_school"."deleted" IS NULL
{country_condition}
{admin1_condition}
{school_condition}
Expand Down
9 changes: 9 additions & 0 deletions proco/connection_statistics/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,15 @@ def update_country_weekly_status(country: Country, date):
country_status = CountryWeeklyStatus.objects.create(
country=country, year=monday_year, week=monday_week_no,
)

latest_entry = CountryWeeklyStatus.objects.all().filter(
country=country, year__lte=country_status.year, week__lt=country_status.week
).order_by('-year', '-week').first()

if latest_entry:
country_status.integration_status = latest_entry.integration_status
country_status.save(update_fields=('integration_status',))

created = True

if created:
Expand Down
60 changes: 30 additions & 30 deletions proco/locations/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ class CountrySearchStatListAPIView(CachedListMixin, ListAPIView):
This class is used to list all Countries along with Admin 1 and Admin2 names.
Inherits: ListAPIView
"""
model = School
model = Country

base_auth_permissions = (
permissions.AllowAny,
Expand All @@ -338,53 +338,53 @@ def get_queryset(self):
queryset = self.model.objects.all()

qs = queryset.values(
'country__id', 'country__name', 'country__code', 'country__last_weekly_status__integration_status',
'admin1__id', 'admin1__name', 'admin1__description', 'admin1__description_ui_label',
'admin1__giga_id_admin',
'admin2__id', 'admin2__name', 'admin2__description', 'admin2__description_ui_label',
'admin2__giga_id_admin',
'id', 'name', 'code', 'last_weekly_status__integration_status',
'schools__admin1__id', 'schools__admin1__name', 'schools__admin1__description', 'schools__admin1__description_ui_label',
'schools__admin1__giga_id_admin',
'schools__admin2__id', 'schools__admin2__name', 'schools__admin2__description', 'schools__admin2__description_ui_label',
'schools__admin2__giga_id_admin',
).annotate(
school_count=Count('id'),
).order_by('country__name', 'admin1__name', 'admin2__name')
school_count=Count('schools__id'),
).order_by('name', 'schools__admin1__name', 'schools__admin2__name')

qs = qs.annotate(
custom_order=Case(
When(country__last_weekly_status__integration_status=3, then=Value(1)),
When(country__last_weekly_status__integration_status=2, then=Value(2)),
When(country__last_weekly_status__integration_status=1, then=Value(3)),
When(country__last_weekly_status__integration_status=0, then=Value(4)),
When(country__last_weekly_status__integration_status=5, then=Value(5)),
When(country__last_weekly_status__integration_status=4, then=Value(6)),
When(last_weekly_status__integration_status=3, school_count__gt=0, then=Value(1)),
When(last_weekly_status__integration_status=2, school_count__gt=0, then=Value(2)),
When(last_weekly_status__integration_status=1, school_count__gt=0, then=Value(3)),
When(last_weekly_status__integration_status=0, school_count__gt=0, then=Value(4)),
When(last_weekly_status__integration_status=5, school_count__gt=0, then=Value(5)),
When(last_weekly_status__integration_status=4, school_count__gt=0, then=Value(6)),
default=Value(7),
output_field=IntegerField(),
),
).order_by('custom_order', 'country__name', 'admin1__name', 'admin2__name')
).order_by('custom_order', 'name', 'schools__admin1__name', 'schools__admin2__name')

return qs

def _format_result(self, qry_data):
data = OrderedDict()
for resp_data in qry_data:
country_id = resp_data.get('country__id')
admin1_name = 'Unknown' if core_utilities.is_blank_string(resp_data['admin1__name']) else resp_data[
'admin1__name']
admin2_name = resp_data['admin2__name']
country_id = resp_data.get('id')
admin1_name = 'Unknown' if core_utilities.is_blank_string(resp_data['schools__admin1__name']) else resp_data[
'schools__admin1__name']
admin2_name = resp_data['schools__admin2__name']

country_data = data.get(country_id, {
'country_id': country_id,
'country_name': resp_data.get('country__name'),
'country_code': resp_data.get('country__code'),
'integration_status': resp_data.get('country__last_weekly_status__integration_status'),
'country_name': resp_data.get('name'),
'country_code': resp_data.get('code'),
'integration_status': resp_data.get('last_weekly_status__integration_status'),
'data': OrderedDict(),
})

# If admin 1 name exist in response
admin1_name_data = country_data['data'].get(admin1_name, {
'admin1_name': admin1_name,
'admin1_id': resp_data.get('admin1__id'),
'admin1_description': resp_data.get('admin1__description'),
'admin1_description_ui_label': resp_data.get('admin1__description_ui_label'),
'admin1_code': resp_data.get('admin1__giga_id_admin'),
'admin1_id': resp_data.get('schools__admin1__id'),
'admin1_description': resp_data.get('schools__admin1__description'),
'admin1_description_ui_label': resp_data.get('schools__admin1__description_ui_label'),
'admin1_code': resp_data.get('schools__admin1__giga_id_admin'),
'data': OrderedDict(),
})
if core_utilities.is_blank_string(admin2_name):
Expand All @@ -395,10 +395,10 @@ def _format_result(self, qry_data):
admin2_name_data = admin2_data.get(admin2_name, {
'type': 'Admin',
'admin2_name': admin2_name,
'admin2_id': resp_data.get('admin2__id'),
'admin2_description': resp_data.get('admin2__description'),
'admin2_description_ui_label': resp_data.get('admin2__description_ui_label'),
'admin2_code': resp_data.get('admin2__giga_id_admin'),
'admin2_id': resp_data.get('schools__admin2__id'),
'admin2_description': resp_data.get('schools__admin2__description'),
'admin2_description_ui_label': resp_data.get('schools__admin2__description_ui_label'),
'admin2_code': resp_data.get('schools__admin2__giga_id_admin'),
'data': OrderedDict(),
})
admin2_name_data['school_count'] = resp_data.get('school_count', 0)
Expand Down

0 comments on commit e2e00d7

Please sign in to comment.