-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CM-819: added adjustments to support download with custom filters for…
… group beneficiaires
- Loading branch information
1 parent
6bbfb65
commit ee9109e
Showing
3 changed files
with
91 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import datetime | ||
import json | ||
import logging | ||
import types | ||
import uuid | ||
from typing import Dict, List, Callable | ||
|
||
import graphene | ||
import pandas as pd | ||
from django.db import models | ||
from graphene.types.generic import GenericScalar | ||
from pandas import DataFrame | ||
|
||
from core import fields | ||
from core.custom_filters import CustomFilterWizardStorage | ||
from core.models import ExportableQueryModel | ||
from graphql.utils.ast_to_dict import ast_to_dict | ||
from core.gql.export_mixin import ExportableQueryMixin | ||
|
||
logger = logging.getLogger(__file__) | ||
|
||
|
||
class ExportableSocialProtectionQueryMixin(ExportableQueryMixin): | ||
|
||
@classmethod | ||
def create_export_function(cls, field_name): | ||
new_function_name = f"resolve_{field_name}_export" | ||
default_resolve = getattr(cls, F"resolve_{field_name}", None) | ||
|
||
if not default_resolve: | ||
raise AttributeError( | ||
f"Query {cls} doesn't provide resolve function for {field_name}. " | ||
f"CSV export cannot be created") | ||
|
||
def exporter(cls, self, info, **kwargs): | ||
custom_filters = kwargs.pop("customFilters", None) | ||
export_fields = [cls._adjust_notation(f) for f in kwargs.pop('fields')] | ||
fields_mapping = json.loads(kwargs.pop('fields_columns')) | ||
|
||
source_field = getattr(cls, field_name) | ||
filter_kwargs = {k: v for k, v in kwargs.items() if k in source_field.filtering_args} | ||
|
||
qs = default_resolve(None, info, **kwargs) | ||
qs = qs.filter(**filter_kwargs) | ||
qs = cls.__append_custom_filters(custom_filters, qs, fields_mapping) | ||
export_file = ExportableQueryModel\ | ||
.create_csv_export(qs, export_fields, info.context.user, column_names=fields_mapping, | ||
patches=cls.get_patches_for_field(field_name)) | ||
|
||
return export_file.name | ||
|
||
setattr(cls, new_function_name, types.MethodType(exporter, cls)) | ||
|
||
@classmethod | ||
def __append_custom_filters(cls, custom_filters, queryset, fields_mapping): | ||
if custom_filters: | ||
module_name = cls.get_module_name() | ||
object_type = cls.get_object_type() | ||
related_field = cls.get_related_field() | ||
if "group__id" in fields_mapping: | ||
queryset = CustomFilterWizardStorage.build_custom_filters_queryset( | ||
"individual", | ||
"GroupIndividual", | ||
custom_filters, | ||
queryset, | ||
relation="group" | ||
) | ||
else: | ||
queryset = CustomFilterWizardStorage.build_custom_filters_queryset( | ||
module_name, | ||
object_type, | ||
custom_filters, | ||
queryset, | ||
relation=related_field | ||
) | ||
return queryset |
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