<commit_before>import sys
import slumber
import json
import datetime
from collections import defaultdict
from urlparse import urljoin

from django.db.models import Prefetch
from django.core.urlresolvers import reverse

from mapit.views.areas import area

from pombola.core.models import (
    Contact, Organisation, ParliamentarySession, Person, Place, Position
)
from pombola import country


extra_popolo_person_fields = (
    'email',
    'summary',
    'biography',
    'national_identity',
    'family_name',
    'given_name',
    'additional_name',
    'honorific_prefix',
    'honorific_suffix',
    'sort_name',
    'gender',
)

def get_area_information(place, base_url):
    """Given a PopIt place, generate a Popolo area dictionary

    The Popolo specification doesn't have much detail about how the
    area attribute of a position or post should be constructed so this
    just follows the convention in YourNextRepresentative's data but
    with additional fields for the parliamentary session (if available)
    and the MapIt area type."""
    mapit_area = place.mapit_area
    if mapit_area is None:
        return None
    mapit_id = mapit_area.id
    mapit_type = mapit_area.type.code
    path = reverse(area, kwargs={'area_id': mapit_id})
    result = {
        'id': 'mapit:{0}'.format(place.mapit_area.id),
        'identifier': urljoin(base_url, path),
        'area_type': mapit_type,
        'name': place.name,
    }
    session = place.parliamentary_session
    if session:
        result['session'] = {
            'id': session.id,
            'name': session.name,
            'start_date': str(session.start_date),
            'end_date': str(session.end_date),
            'mapit_generation': session.mapit_generation,
            'house_id': session.house.id,
            'house_name': session.house.name,
        }
    return result

def date_to_partial_iso8601(approx_date):
    """Get a (possibly partial) ISO 8601 representation of an ApproximateDate
    >>> from django_date_extensions.fields import ApproximateDate

    >>> date_to_partial_iso8601(ApproximateDate(2012, 6, 2))
    '2012-06-02'
    >>> date_to_partial_iso8601(ApproximateDate(2010))
    '2010'
    >>> date_to_partial_iso8601(ApproximateDate(2012, 2))
    '2012-02'<commit_msg>Popolo JSON export: add the session slug to sessions in areas<commit_after>import sys
import slumber
import json
import datetime
from collections import defaultdict
from urlparse import urljoin

from django.db.models import Prefetch
from django.core.urlresolvers import reverse

from mapit.views.areas import area

from pombola.core.models import (
    Contact, Organisation, ParliamentarySession, Person, Place, Position
)
from pombola import country


extra_popolo_person_fields = (
    'email',
    'summary',
    'biography',
    'national_identity',
    'family_name',
    'given_name',
    'additional_name',
    'honorific_prefix',
    'honorific_suffix',
    'sort_name',
    'gender',
)

def get_area_information(place, base_url):
    """Given a PopIt place, generate a Popolo area dictionary

    The Popolo specification doesn't have much detail about how the
    area attribute of a position or post should be constructed so this
    just follows the convention in YourNextRepresentative's data but
    with additional fields for the parliamentary session (if available)
    and the MapIt area type."""
    mapit_area = place.mapit_area
    if mapit_area is None:
        return None
    mapit_id = mapit_area.id
    mapit_type = mapit_area.type.code
    path = reverse(area, kwargs={'area_id': mapit_id})
    result = {
        'id': 'mapit:{0}'.format(place.mapit_area.id),
        'identifier': urljoin(base_url, path),
        'area_type': mapit_type,
        'name': place.name,
    }
    session = place.parliamentary_session
    if session:
        result['session'] = {
            'id': session.id,
            'slug': session.slug,
            'name': session.name,
            'start_date': str(session.start_date),
            'end_date': str(session.end_date),
            'mapit_generation': session.mapit_generation,
            'house_id': session.house.id,
            'house_name': session.house.name,
        }
    return result

def date_to_partial_iso8601(approx_date):
    """Get a (possibly partial) ISO 8601 representation of an ApproximateDate
    >>> from django_date_extensions.fields import ApproximateDate

    >>> date_to_partial_iso8601(ApproximateDate(2012, 6, 2))
    '2012-06-02'
    >>> date_to_partial_iso8601(ApproximateDate(2010))
    '2010'
    >>> date_to_partial_iso8601(ApproximateDate(2012, 2))
    '2012-02'
{'fraction_non_alphanumeric': 0.09474590869939707, 'fraction_numerical': 0.023255813953488372, 'mean_word_length': 3.324953445065177, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '1643311', 'n_tokens_mistral': 1499, 'n_tokens_neox': 1391, 'n_words': 373}