Skip to content

Commit

Permalink
Merge pull request #2117 from IFRCGo/feature/ns-document
Browse files Browse the repository at this point in the history
Add Additional fields in CountrykeyDocuments
  • Loading branch information
samshara authored Apr 26, 2024
2 parents 510625e + 4027f92 commit dc59a90
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 1 deletion.
5 changes: 5 additions & 0 deletions api/drf_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from rest_framework.permissions import IsAuthenticated
from rest_framework import viewsets, mixins
from rest_framework.decorators import action
from django_filters import rest_framework as rest_filters
from rest_framework import filters

from django.http import Http404
from django.contrib.auth.models import User
Expand All @@ -32,6 +34,7 @@
from main.enums import GlobalEnumSerializer, get_enum_values
from deployments.models import Personnel
from databank.serializers import CountryOverviewSerializer
from main.filters import NullsLastOrderingFilter

from .utils import is_user_ifrc
from .exceptions import BadRequest
Expand Down Expand Up @@ -522,8 +525,10 @@ class CountryKeyDocumentViewSet(viewsets.ReadOnlyModelViewSet):
queryset = CountryKeyDocument.objects.select_related('country')
serializer_class = CountryKeyDocumentSerializer
search_fields = ("name",)
ordering_fields = ("year", "end_year",)
# permission_classes = (IsAuthenticated,)
filterset_class = CountryKeyDocumentFilter
filter_backends = (NullsLastOrderingFilter, rest_filters.DjangoFilterBackend, filters.SearchFilter)


class DistrictRMDViewset(viewsets.ReadOnlyModelViewSet):
Expand Down
1 change: 1 addition & 0 deletions api/filter_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class CountryKeyDocumentFilter(filters.FilterSet):
year__lte = filters.DateFilter(field_name="year", lookup_expr="lte", input_formats=["%Y-%m-%d"])
year__gte = filters.DateFilter(field_name="year", lookup_expr="gte", input_formats=["%Y-%m-%d"])
country = filters.NumberFilter(field_name="country", lookup_expr="exact")
document_type = filters.CharFilter(field_name="document_type", lookup_expr="exact")

class Meta:
model = CountryKeyDocument
Expand Down
6 changes: 5 additions & 1 deletion api/management/commands/ingest_ns_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def fetch_country_documents(self, api_key, country_ns_code):
'url': item['url'],
'thumbnail': item['thumbnail'],
'year': str(item['year']) + '-01-01',
'end_year': str(item['EndYear']) + '-01-01' if 'EndYear' in item else None,
'year_text': item['YearText'],
'document_type': item['document_type'],
'country_code': country_ns_code
}
Expand Down Expand Up @@ -99,7 +101,9 @@ def save_documents_to_database(self, result):
'url': document['url'],
'thumbnail': document['thumbnail'],
'document_type': document['document_type'],
'year': document['year']
'year': document['year'],
'end_year': document['end_year'],
'year_text': document['year_text']
}
CountryKeyDocument.objects.create(**data)
return added
Expand Down
23 changes: 23 additions & 0 deletions api/migrations/0209_auto_20240423_0604.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2.25 on 2024-04-23 06:04

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api', '0208_auto_20240404_0518'),
]

operations = [
migrations.AddField(
model_name='countrykeydocument',
name='end_year',
field=models.DateField(blank=True, null=True, verbose_name='End Year'),
),
migrations.AddField(
model_name='countrykeydocument',
name='year_text',
field=models.CharField(blank=True, max_length=255, null=True, verbose_name='Year Text'),
),
]
9 changes: 9 additions & 0 deletions api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,15 @@ class CountryKeyDocument(models.Model):
year = models.DateField(
verbose_name=_('Year')
)
end_year = models.DateField(
verbose_name=_('End Year'),
null=True, blank=True
)
year_text = models.CharField(
verbose_name=_('Year Text'),
max_length=255,
null=True, blank=True
)

def __str__(self):
return f'{self.country.name} - {self.name}'
Expand Down
21 changes: 21 additions & 0 deletions main/filters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
from django.db.models import OrderBy, F
from django_filters import rest_framework as filters
from rest_framework.filters import OrderingFilter


class CharInFilter(filters.BaseInFilter, filters.CharFilter):
pass


class NullsLastOrderingFilter(OrderingFilter):
def get_ordering(self, request, queryset, view):
ordering = super().get_ordering(request, queryset, view)

if ordering:
nulls_last_ordering = []
for field in ordering:
if field.startswith('-'):
nulls_last_ordering.append(F(field[1:]).desc(nulls_last=True))
else:
nulls_last_ordering.append(F(field).asc(nulls_last=True))

return nulls_last_ordering

return ordering

22 changes: 22 additions & 0 deletions per/migrations/0112_auto_20240426_0522.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.2.25 on 2024-04-26 05:22

from django.db import migrations

def delete_form_question(apps, schema_editor):
FormQuestion = apps.get_model('per', 'FormQuestion')
print(FormQuestion.objects.filter(question__icontains='Component Epidemic Preparedness').delete())
print(FormQuestion.objects.filter(question__icontains='Component performance').delete())


class Migration(migrations.Migration):

dependencies = [
('per', '0111_perdocumentupload_per'),
]

operations = [
migrations.RunPython(
delete_form_question,
reverse_code=migrations.RunPython.noop
),
]

0 comments on commit dc59a90

Please sign in to comment.