<commit_before># Python 2 and 3
from __future__ import unicode_literals

from gapipy.models import AdvertisedDeparture
from gapipy.query import Query
from gapipy.resources.base import Resource
from gapipy.resources.booking_company import BookingCompany
from gapipy.utils import get_resource_class_from_class_name


MAP_IMAGE_TYPE = 'MAP'
BANNER_IMAGE_TYPE = 'BANNER'


class TourDossier(Resource):

    _resource_name = 'tour_dossiers'

    _as_is_fields = [
        'id',
        'href',
        'categories',
        'description',
        'details',
        'product_line',
        'geography',
        'images',
        'name',
        'site_links',
        'slug',
    ]
    _date_fields = [
        'departures_start_date',
        'departures_end_date',
    ]
    _resource_fields = [
        ('tour', 'Tour'),
    ]
    _resource_collection_fields = [
        ('departures', 'Departure'),
    ]
    _model_collection_fields = [
        ('advertised_departures', AdvertisedDeparture),
        ('booking_companies', BookingCompany),
        ('structured_itineraries', 'Itinerary'),
    ]

    def _set_resource_collection_field(self, field, value):
        """
        Overridden to ensure that the `departures` query has the right
        parent resource (i.e. the tour and not the tour dossier).
        """
        if field == 'departures':
            resource_cls = get_resource_class_from_class_name('Departure')
            # Tour dossiers always have the same id as the corresponding tour
            parent = ('tours', self.id, None)
            query = Query(self._client, resource_cls, parent=parent, raw_data=value)
            setattr(self, field, query)
        else:
            return super(TourDossier, self)._set_resource_collection_field(field, value)

    def _get_image_url(self, image_type):
        for image in self.images:
            if image['type'] == image_type:
                return image['image_href']

    def get_map_url(self):
        return self._get_image_url(MAP_IMAGE_TYPE)

    def get_banner_url(self):
        return self._get_image_url(BANNER_IMAGE_TYPE)

    def get_visited_countries(self):
        return [country['name'] for country in self.geography['visited_countries']]

    def get_trip_detail(self, label):
        for detail in self.details:
            if detail['detail_type']['label'] == label:
                return detail['body']

    def get_category_name(self, label):
        for category in self.categories:
            if category['category_type']['label'] == label:
                return category['name']
<commit_msg>Re: #113 - Fix Tour Dossier Departure nested Query (#125)<commit_after># Python 2 and 3
from __future__ import unicode_literals

from gapipy.models import AdvertisedDeparture
from gapipy.resources.base import Resource
from gapipy.resources.booking_company import BookingCompany

MAP_IMAGE_TYPE = 'MAP'
BANNER_IMAGE_TYPE = 'BANNER'


class TourDossier(Resource):

    _resource_name = 'tour_dossiers'
    _is_parent_resource = True

    _as_is_fields = [
        'id',
        'href',
        'categories',
        'description',
        'details',
        'product_line',
        'geography',
        'images',
        'name',
        'site_links',
        'slug',
    ]

    _date_fields = [
        'departures_start_date',
        'departures_end_date',
    ]

    _resource_fields = [
        ('tour', 'Tour'),
    ]

    _resource_collection_fields = [
        ('departures', 'Departure'),
    ]

    _model_collection_fields = [
        ('advertised_departures', AdvertisedDeparture),
        ('booking_companies', BookingCompany),
        ('structured_itineraries', 'Itinerary'),
    ]

    def _get_image_url(self, image_type):
        for image in self.images:
            if image['type'] == image_type:
                return image['image_href']
        return None

    def get_map_url(self):
        return self._get_image_url(MAP_IMAGE_TYPE)

    def get_banner_url(self):
        return self._get_image_url(BANNER_IMAGE_TYPE)

    def get_visited_countries(self):
        return [country['name'] for country in self.geography['visited_countries']]

    def get_trip_detail(self, label):
        for detail in self.details:
            if detail['detail_type']['label'] == label:
                return detail['body']
        return None

    def get_category_name(self, label):
        for category in self.categories:
            if category['category_type']['label'] == label:
                return category['name']
        return None

{'fraction_non_alphanumeric': 0.10101232394366197, 'fraction_numerical': 0.0022007042253521128, 'mean_word_length': 2.630191693290735, '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': '2872247', 'n_tokens_mistral': 1409, 'n_tokens_neox': 1354, 'n_words': 299}