<commit_before># -*- coding: utf-8 -*-

# Akvo RSR is covered by the GNU Affero General Public License.
# See more details in the license.txt file located at the root folder of the Akvo RSR module.
# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.


from tastypie import fields
from tastypie.cache import SimpleCache
from tastypie.constants import ALL, ALL_WITH_RELATIONS

from akvo.api.fields import ConditionalFullToManyField

from akvo.rsr.models import Organisation, Project

from .resources import ConditionalFullResource, get_extra_thumbnails


class OrganisationMapResource(ConditionalFullResource):
    """
    a limited resource for delivering data to be used when creating maps
    """
    locations           = ConditionalFullToManyField('akvo.api.resources.OrganisationLocationResource', 'locations', null=True)
    primary_location    = fields.ToOneField('akvo.api.resources.OrganisationLocationResource', 'primary_location', full=True, null=True)

    class Meta:
        allowed_methods = ['get']
        queryset = Organisation.objects.all()
        resource_name = 'map_for_organisation'
        include_absolute_url = True
        cache = SimpleCache(timeout=900) # 15 minutes

        filtering       = dict(
            # other fields
            iati_org_id         = ALL,
            name                = ALL,
            organisation_type   = ALL,
            # foreign keys
            locations           = ALL_WITH_RELATIONS,
            partnerships        = ALL_WITH_RELATIONS,
            )

    def dehydrate(self, bundle):
        """ add thumbnails inline info for Organisation.logo
        """
        bundle = super(OrganisationMapResource, self).dehydrate(bundle)
        del bundle.data['description']
        bundle.data['logo'] = {
            'original': bundle.data['logo'],
            'thumbnails': get_extra_thumbnails(bundle.obj.logo),
            }
        return bundle


class ProjectMapResource(ConditionalFullResource):
    """
    a limited resource for delivering data to be used when creating maps
    """
    locations           = ConditionalFullToManyField('akvo.api.resources.ProjectLocationResource', 'locations')
    primary_location    = fields.ToOneField('akvo.api.resources.ProjectLocationResource', 'primary_location', full=True, null=True)

    class Meta:
        allowed_methods = ['get']
        queryset = Project.objects.published()
        resource_name = 'map_for_project'
        include_absolute_url = True
        cache = SimpleCache(timeout=900) # 15 minutes

        filtering               = dict(
            # other fields
            status              = ALL,
            title               = ALL,
            budget              = ALL,
            funds               = ALL,
            funds_needed        = ALL,
            # foreign keys
            benchmarks          = ALL_WITH_RELATIONS,
            budget_items        = ALL_WITH_RELATIONS,
            categories          = ALL_WITH_RELATIONS,
            goals               = ALL_WITH_RELATIONS,
            invoices            = ALL_WITH_RELATIONS,
            links               = ALL_WITH_RELATIONS,
            locations           = ALL_WITH_RELATIONS,
            partnerships        = ALL_WITH_RELATIONS,
            project_comments    = ALL_WITH_RELATIONS,<commit_msg>[#331] Optimizations of ProjectMapResource<commit_after># -*- coding: utf-8 -*-

# Akvo RSR is covered by the GNU Affero General Public License.
# See more details in the license.txt file located at the root folder of the Akvo RSR module.
# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.


from tastypie import fields
from tastypie.cache import SimpleCache
from tastypie.constants import ALL, ALL_WITH_RELATIONS
from tastypie.resources import ModelResource

from akvo.api.fields import ConditionalFullToManyField

from akvo.rsr.models import Organisation, Project

from .resources import ConditionalFullResource, get_extra_thumbnails


class OrganisationMapResource(ConditionalFullResource):
    """
    a limited resource for delivering data to be used when creating maps
    """
    locations           = ConditionalFullToManyField('akvo.api.resources.OrganisationLocationResource', 'locations', null=True)
    primary_location    = fields.ToOneField('akvo.api.resources.OrganisationLocationResource', 'primary_location', full=True, null=True)

    class Meta:
        allowed_methods = ['get']
        queryset = Organisation.objects.all()
        resource_name = 'map_for_organisation'
        include_absolute_url = True
        cache = SimpleCache(timeout=900) # 15 minutes

        filtering       = dict(
            # other fields
            iati_org_id         = ALL,
            name                = ALL,
            organisation_type   = ALL,
            # foreign keys
            locations           = ALL_WITH_RELATIONS,
            partnerships        = ALL_WITH_RELATIONS,
            )

    def dehydrate(self, bundle):
        """ add thumbnails inline info for Organisation.logo
        """
        bundle = super(OrganisationMapResource, self).dehydrate(bundle)
        del bundle.data['description']
        bundle.data['logo'] = {
            'original': bundle.data['logo'],
            'thumbnails': get_extra_thumbnails(bundle.obj.logo),
            }
        return bundle


class ProjectMapResource(ModelResource):
    """
    a limited resource for delivering data to be used when creating maps
    """
    #locations           = ConditionalFullToManyField('akvo.api.resources.ProjectLocationResource', 'locations')
    primary_location    = fields.ToOneField('akvo.api.resources.ProjectLocationResource', 'primary_location', full=True, null=True)

    class Meta:
        allowed_methods = ['get']
        queryset = Project.objects.select_related('primary_location', 'primary_location__country').published()
        resource_name = 'map_for_project'
        include_absolute_url = True
        cache = SimpleCache(timeout=900) # 15 minutes

        filtering               = dict(
            # other fields
            status              = ALL,
            title               = ALL,
            budget              = ALL,
            funds               = ALL,
            funds_needed        = ALL,
            # foreign keys
            benchmarks          = ALL_WITH_RELATIONS,
            budget_items        = ALL_WITH_RELATIONS,
            categories          = ALL_WITH_RELATIONS,
            goals               = ALL_WITH_RELATIONS,
            invoices            = ALL_WITH_RELATIONS,
            links               = ALL_WITH_RELATIONS,
            locations           = ALL_WITH_RELATIONS,
            partnerships        = ALL_WITH_RELATIONS,
            project_comments    = ALL_WITH_RELATIONS,
{'fraction_non_alphanumeric': 0.0741442097596504, 'fraction_numerical': 0.003641660597232338, 'mean_word_length': 2.1452130096197894, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5416266', 'n_tokens_mistral': 1785, 'n_tokens_neox': 1724, 'n_words': 431}