<commit_before># Tastypie specific
from tastypie import fields
from tastypie.constants import ALL, ALL_WITH_RELATIONS
from tastypie.resources import ModelResource

# Data specific
from api.cache import NoTransformCache
from iati.models import ContactInfo, Activity, Organisation, AidType, FlowType, Sector, CollaborationType, \
    TiedStatus, Transaction, ActivityStatus, Currency, OrganisationRole, ActivityScope, \
    ActivityParticipatingOrganisation, Location, Result, RelatedActivity
from api.v3.resources.helper_resources import TitleResource, DescriptionResource, FinanceTypeResource, \
    ActivityBudgetResource, DocumentResource, WebsiteResource, PolicyMarkerResource, OtherIdentifierResource
from api.v3.resources.advanced_resources import OnlyCountryResource, OnlyRegionResource

# cache specific
from django.http import HttpResponse
from cache.validator import Validator

from api.v3.resources.csv_serializer import CsvSerializer
from api.api_tools import comma_separated_parameter_to_list

from api.paginator import NoCountPaginator

class ActivityViewAidTypeResource(ModelResource):
    class Meta:
        queryset = AidType.objects.all()
        include_resource_uri = False
        excludes = ['description']

class ActivityViewFlowTypeResource(ModelResource):
    class Meta:
        queryset = FlowType.objects.all()
        include_resource_uri = False
        excludes = ['description']

class ActivityViewSectorResource(ModelResource):
    class Meta:
        queryset = Sector.objects.all()
        include_resource_uri = False
        excludes = ['description']

class ActivityViewCollaborationTypeResource(ModelResource):
    class Meta:
        queryset = CollaborationType.objects.all()
        include_resource_uri = False
        excludes = ['description', 'language']

class ActivityViewTiedStatusResource(ModelResource):
    class Meta:
        queryset = TiedStatus.objects.all()
        include_resource_uri = False
        excludes = ['description']

class ActivityViewOrganisationRoleResource(ModelResource):
    class Meta:
        queryset = OrganisationRole.objects.all()
        include_resource_uri = False



class ActivityViewOrganisationResource(ModelResource):
    organisation_role = fields.ForeignKey(ActivityViewOrganisationRoleResource, 'organisation_role', full=True, null=True)


    class Meta:
        queryset = Organisation.objects.all()
        include_resource_uri = False
        excludes = ['abbreviation', 'reported_by_organisation']
        filtering = {
            'iati_identifier': 'exact',
            'code': ALL_WITH_RELATIONS
        }






class ActivityViewTransactionResource(ModelResource):
    provider_organisation = fields.ForeignKey(ActivityViewOrganisationResource, 'provider_organisation', full=True, null=True)
    receiver_organisation = fields.ForeignKey(ActivityViewOrganisationResource, 'receiver_organisation', full=True, null=True)

    class Meta:
        queryset = Transaction.objects.all()
        include_resource_uri = False
        excludes = ['id', 'ref', 'description', 'provider_activity']
        allowed_methods = ['get']

    def dehydrate(self, bundle):

        bundle.data['disbursement_channel'] = bundle.obj.disbursement_channel_id
        bundle.data['currency'] = bundle.obj.currency_id
        bundle.data['tied_status'] = bundle.obj.tied_status_id
        bundle.data['transaction_type'] = bundle.obj.transaction_type_id
        return bundle



class ActivityViewParticipatingOrganisationResource(ModelResource):
    organisation = fields.ToOneField(ActivityViewOrganisationResource, 'organisation', full=True, null=True)

    class Meta:
        queryset = ActivityParticipatingOrganisation.objects.all()
        include_resource_uri = False
        excludes = ['id']
        filtering = {
            'organisation': ALL_WITH_RELATIONS
        }

    def dehydrate(self, bundle):
        bundle.data['role_id'] = bundle.obj.role_id
        bundle.data['code'] = bundle.obj.organisation_id
        return bundle



class ActivityViewActivityStatusResource(ModelResource):
    class Meta:
        queryset = ActivityStatus.objects.all()
        include_resource_uri = False
        excludes = ['language']

class ActivityViewActivityScopeResource(ModelResource):
    class Meta:
        queryset = ActivityScope.objects.all()
        include_resource_uri = False

class ActivityViewCurrencyResource(ModelResource):
    class Meta:
        queryset = Currency.objects.all()
        include_resource_uri = False
        excludes = ['language']

class ActivityViewContactInfoResource(ModelResource):
    class Meta:
        queryset = ContactInfo.objects.all()
        include_resource_uri = False
        excludes = ['id']



class ActivityLocationResource(ModelResource):

    class Meta:
        queryset = Location.objects.all()
        include_resource_uri = False
        excludes = ['id', 'activity_description', 'adm_code', 'adm_country_adm1', 'adm_country_adm2',
                    'adm_country_name', 'adm_level', 'gazetteer_entry', 'location_id_code', 'point_srs_name',
                    'ref', 'type_description', 'point_pos']


class ActivityResultResource(ModelResource):

    class Meta:
        queryset = Result.objects.all()
        include_resource_uri = False
        excludes = ['id']


class RelatedActivityResource(ModelResource):

    class Meta:
        queryset = RelatedActivity.objects.all()
        include_resource_uri = False
        excludes = ['id']


class ActivityResource(ModelResource):
    countries = fields.ToManyField(OnlyCountryResource, 'recipient_country', full=True, null=True, use_in='all')
    regions = fields.ToManyField(OnlyRegionResource, 'recipient_region', full=True, null=True, use_in='all')
    sectors = fields.ToManyField(ActivityViewSectorResource, 'sector', full=True, null=True, use_in='all')
    titles = fields.ToManyField(TitleResource, 'title_set', full=True, null=True, use_in='all')
    descriptions = fields.ToManyField(DescriptionResource, 'description_set', full=True, null=True, use_in='all')
    participating_organisations = fields.ToManyField(ActivityViewOrganisationResource, 'participating_organisation', full=True, null=True, use_in='all')
    reporting_organisation = fields.ForeignKey(ActivityViewOrganisationResource, 'reporting_organisation', full=True, null=True, use_in='detail' )
    activity_status = fields.ForeignKey(ActivityViewActivityStatusResource, 'activity_status', full=True, null=True, use_in='detail')
    websites = fields.ToManyField(WebsiteResource, 'activity_website_set', full=True, null=True, use_in='detail')
    policy_markers = fields.ToManyField(PolicyMarkerResource, 'policy_marker', full=True, null=True, use_in='detail')
    collaboration_type = fields.ForeignKey(ActivityViewCollaborationTypeResource, attribute='collaboration_type', full=True, null=True, use_in='detail')
    default_flow_type = fields.ForeignKey(ActivityViewFlowTypeResource, attribute='default_flow_type', full=True, null=True, use_in='detail')
    default_finance_type = fields.ForeignKey(FinanceTypeResource, attribute='default_finance_type', full=True, null=True, use_in='detail')
    default_aid_type = fields.ForeignKey(ActivityViewAidTypeResource, attribute='default_aid_type', full=True, null=True, use_in='detail')
    default_tied_status = fields.ForeignKey(ActivityViewTiedStatusResource, attribute='default_tied_status', full=True, null=True, use_in='detail')
    activity_scope = fields.ForeignKey(ActivityViewActivityScopeResource, attribute='scope', full=True, null=True, use_in='detail')
    default_currency = fields.ForeignKey(ActivityViewCurrencyResource, attribute='default_currency', full=True, null=True, use_in='detail')
    budget = fields.ToManyField(ActivityBudgetResource, 'budget_set', full=True, null=True, use_in='detail')
    transactions = fields.ToManyField(ActivityViewTransactionResource, 'transaction_set', full=True, null=True, use_in='detail')
    documents = fields.ToManyField(DocumentResource, 'documentlink_set', full=True, null=True, use_in='detail')
    other_identifier = fields.ToManyField(OtherIdentifierResource, 'otheridentifier_set', full=True, null=True, use_in='detail')
    locations = fields.ToManyField(ActivityLocationResource, 'location_set', full=True, null=True, use_in='all')
    results = fields.ToManyField(ActivityResultResource, 'result_set', full=True, null=True, use_in='detail')
    related_activities = fields.ToManyField(RelatedActivityResource, 'related_activity', full=True, null=True, use_in='detail')

    # to add:
    # conditions
    # contact
    # country-budget?
    # crsadd
    # disbursement channel?
    # ffs
    # ffs forecast?
    # planned disbursement
    # related activity
    # verification status
    # vocabulary?


    class Meta:
        queryset = Activity.objects.all()
        resource_name = 'activities'
        max_limit = 1000
        serializer = CsvSerializer()
        excludes = ['date_created']
        ordering = ['start_actual', 'start_planned', 'end_actual', 'end_planned', 'sectors', 'total_budget']
        filtering = {
            'id': 'in',<commit_msg>change related name in api resource<commit_after># Tastypie specific
from tastypie import fields
from tastypie.constants import ALL, ALL_WITH_RELATIONS
from tastypie.resources import ModelResource

# Data specific
from api.cache import NoTransformCache
from iati.models import ContactInfo, Activity, Organisation, AidType, FlowType, Sector, CollaborationType, \
    TiedStatus, Transaction, ActivityStatus, Currency, OrganisationRole, ActivityScope, \
    ActivityParticipatingOrganisation, Location, Result, RelatedActivity
from api.v3.resources.helper_resources import TitleResource, DescriptionResource, FinanceTypeResource, \
    ActivityBudgetResource, DocumentResource, WebsiteResource, PolicyMarkerResource, OtherIdentifierResource
from api.v3.resources.advanced_resources import OnlyCountryResource, OnlyRegionResource

# cache specific
from django.http import HttpResponse
from cache.validator import Validator

from api.v3.resources.csv_serializer import CsvSerializer
from api.api_tools import comma_separated_parameter_to_list

from api.paginator import NoCountPaginator

class ActivityViewAidTypeResource(ModelResource):
    class Meta:
        queryset = AidType.objects.all()
        include_resource_uri = False
        excludes = ['description']

class ActivityViewFlowTypeResource(ModelResource):
    class Meta:
        queryset = FlowType.objects.all()
        include_resource_uri = False
        excludes = ['description']

class ActivityViewSectorResource(ModelResource):
    class Meta:
        queryset = Sector.objects.all()
        include_resource_uri = False
        excludes = ['description']

class ActivityViewCollaborationTypeResource(ModelResource):
    class Meta:
        queryset = CollaborationType.objects.all()
        include_resource_uri = False
        excludes = ['description', 'language']

class ActivityViewTiedStatusResource(ModelResource):
    class Meta:
        queryset = TiedStatus.objects.all()
        include_resource_uri = False
        excludes = ['description']

class ActivityViewOrganisationRoleResource(ModelResource):
    class Meta:
        queryset = OrganisationRole.objects.all()
        include_resource_uri = False



class ActivityViewOrganisationResource(ModelResource):
    organisation_role = fields.ForeignKey(ActivityViewOrganisationRoleResource, 'organisation_role', full=True, null=True)


    class Meta:
        queryset = Organisation.objects.all()
        include_resource_uri = False
        excludes = ['abbreviation', 'reported_by_organisation']
        filtering = {
            'iati_identifier': 'exact',
            'code': ALL_WITH_RELATIONS
        }






class ActivityViewTransactionResource(ModelResource):
    provider_organisation = fields.ForeignKey(ActivityViewOrganisationResource, 'provider_organisation', full=True, null=True)
    receiver_organisation = fields.ForeignKey(ActivityViewOrganisationResource, 'receiver_organisation', full=True, null=True)

    class Meta:
        queryset = Transaction.objects.all()
        include_resource_uri = False
        excludes = ['id', 'ref', 'description', 'provider_activity']
        allowed_methods = ['get']

    def dehydrate(self, bundle):

        bundle.data['disbursement_channel'] = bundle.obj.disbursement_channel_id
        bundle.data['currency'] = bundle.obj.currency_id
        bundle.data['tied_status'] = bundle.obj.tied_status_id
        bundle.data['transaction_type'] = bundle.obj.transaction_type_id
        return bundle



class ActivityViewParticipatingOrganisationResource(ModelResource):
    organisation = fields.ToOneField(ActivityViewOrganisationResource, 'organisation', full=True, null=True)

    class Meta:
        queryset = ActivityParticipatingOrganisation.objects.all()
        include_resource_uri = False
        excludes = ['id']
        filtering = {
            'organisation': ALL_WITH_RELATIONS
        }

    def dehydrate(self, bundle):
        bundle.data['role_id'] = bundle.obj.role_id
        bundle.data['code'] = bundle.obj.organisation_id
        return bundle



class ActivityViewActivityStatusResource(ModelResource):
    class Meta:
        queryset = ActivityStatus.objects.all()
        include_resource_uri = False
        excludes = ['language']

class ActivityViewActivityScopeResource(ModelResource):
    class Meta:
        queryset = ActivityScope.objects.all()
        include_resource_uri = False

class ActivityViewCurrencyResource(ModelResource):
    class Meta:
        queryset = Currency.objects.all()
        include_resource_uri = False
        excludes = ['language']

class ActivityViewContactInfoResource(ModelResource):
    class Meta:
        queryset = ContactInfo.objects.all()
        include_resource_uri = False
        excludes = ['id']



class ActivityLocationResource(ModelResource):

    class Meta:
        queryset = Location.objects.all()
        include_resource_uri = False
        excludes = ['id', 'activity_description', 'adm_code', 'adm_country_adm1', 'adm_country_adm2',
                    'adm_country_name', 'adm_level', 'gazetteer_entry', 'location_id_code', 'point_srs_name',
                    'ref', 'type_description', 'point_pos']


class ActivityResultResource(ModelResource):

    class Meta:
        queryset = Result.objects.all()
        include_resource_uri = False
        excludes = ['id']


class RelatedActivityResource(ModelResource):

    class Meta:
        queryset = RelatedActivity.objects.all()
        include_resource_uri = False
        excludes = ['id']


class ActivityResource(ModelResource):
    countries = fields.ToManyField(OnlyCountryResource, 'recipient_country', full=True, null=True, use_in='all')
    regions = fields.ToManyField(OnlyRegionResource, 'recipient_region', full=True, null=True, use_in='all')
    sectors = fields.ToManyField(ActivityViewSectorResource, 'sector', full=True, null=True, use_in='all')
    titles = fields.ToManyField(TitleResource, 'title_set', full=True, null=True, use_in='all')
    descriptions = fields.ToManyField(DescriptionResource, 'description_set', full=True, null=True, use_in='all')
    participating_organisations = fields.ToManyField(ActivityViewOrganisationResource, 'participating_organisation', full=True, null=True, use_in='all')
    reporting_organisation = fields.ForeignKey(ActivityViewOrganisationResource, 'reporting_organisation', full=True, null=True, use_in='detail' )
    activity_status = fields.ForeignKey(ActivityViewActivityStatusResource, 'activity_status', full=True, null=True, use_in='detail')
    websites = fields.ToManyField(WebsiteResource, 'activity_website_set', full=True, null=True, use_in='detail')
    policy_markers = fields.ToManyField(PolicyMarkerResource, 'policy_marker', full=True, null=True, use_in='detail')
    collaboration_type = fields.ForeignKey(ActivityViewCollaborationTypeResource, attribute='collaboration_type', full=True, null=True, use_in='detail')
    default_flow_type = fields.ForeignKey(ActivityViewFlowTypeResource, attribute='default_flow_type', full=True, null=True, use_in='detail')
    default_finance_type = fields.ForeignKey(FinanceTypeResource, attribute='default_finance_type', full=True, null=True, use_in='detail')
    default_aid_type = fields.ForeignKey(ActivityViewAidTypeResource, attribute='default_aid_type', full=True, null=True, use_in='detail')
    default_tied_status = fields.ForeignKey(ActivityViewTiedStatusResource, attribute='default_tied_status', full=True, null=True, use_in='detail')
    activity_scope = fields.ForeignKey(ActivityViewActivityScopeResource, attribute='scope', full=True, null=True, use_in='detail')
    default_currency = fields.ForeignKey(ActivityViewCurrencyResource, attribute='default_currency', full=True, null=True, use_in='detail')
    budget = fields.ToManyField(ActivityBudgetResource, 'budget_set', full=True, null=True, use_in='detail')
    transactions = fields.ToManyField(ActivityViewTransactionResource, 'transaction_set', full=True, null=True, use_in='detail')
    documents = fields.ToManyField(DocumentResource, 'documentlink_set', full=True, null=True, use_in='detail')
    other_identifier = fields.ToManyField(OtherIdentifierResource, 'otheridentifier_set', full=True, null=True, use_in='detail')
    locations = fields.ToManyField(ActivityLocationResource, 'location_set', full=True, null=True, use_in='all')
    results = fields.ToManyField(ActivityResultResource, 'result_set', full=True, null=True, use_in='detail')
    related_activities = fields.ToManyField(RelatedActivityResource, 'related_activities', full=True, null=True, use_in='detail')

    # to add:
    # conditions
    # contact
    # country-budget?
    # crsadd
    # disbursement channel?
    # ffs
    # ffs forecast?
    # planned disbursement
    # related activity
    # verification status
    # vocabulary?


    class Meta:
        queryset = Activity.objects.all()
        resource_name = 'activities'
        max_limit = 1000
        serializer = CsvSerializer()
        excludes = ['date_created']
        ordering = ['start_actual', 'start_planned', 'end_actual', 'end_planned', 'sectors', 'total_budget']
        filtering = {
            'id': 'in',
{'fraction_non_alphanumeric': 0.09694656488549619, 'fraction_numerical': 0.0009814612868047983, 'mean_word_length': 4.657310302282542, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5037066', 'n_tokens_mistral': 5093, 'n_tokens_neox': 4966, 'n_words': 986}