예제 #1
0
def get_people(primary_id_scheme, base_url, inline_memberships=True):

    result = {'persons': []}
    if not inline_memberships:
        result['memberships'] = []

    for person in Person.objects.all():
        name = person.legal_name
        person_properties = {'name': name}
        for date, key in ((person.date_of_birth, 'birth_date'),
                          (person.date_of_death, 'death_date')):
            if date:
                person_properties[key] = date_to_partial_iso8601(date)
        primary_image = person.primary_image()
        if primary_image:
            person_properties['images'] = [{
                'url':
                urljoin(base_url, primary_image.url)
            }]
        add_identifiers_to_properties(person, person_properties,
                                      primary_id_scheme)
        add_contact_details_to_properties(person, person_properties)
        add_other_names(person, person_properties)
        for key in extra_popolo_person_fields:
            value = getattr(person, key)
            # This might be a markitup.fields.Markup field, in
            # which case we need to call raw on it:
            try:
                value = value.raw
            except AttributeError:
                pass
            if value:
                person_properties[key] = value
        country.add_extra_popolo_data_for_person(person, person_properties,
                                                 base_url)

        if inline_memberships:
            person_properties['memberships'] = []

        for position in person.position_set.all():
            properties = {'person_id': person.get_popolo_id(primary_id_scheme)}
            if position.title and position.title.name:
                properties['role'] = position.title.name
            add_start_and_end_date(position, properties)
            add_identifiers_to_properties(position, properties,
                                          primary_id_scheme)
            if position.organisation:
                position.organisation.slug
                organization_id = position.organisation.get_popolo_id(
                    primary_id_scheme)
                properties['organization_id'] = organization_id
            if inline_memberships:
                person_properties['memberships'].append(properties)
            else:
                result['memberships'].append(properties)

        result['persons'].append(person_properties)
    return result
예제 #2
0
파일: popolo.py 프로젝트: Hutspace/odekro
def get_people(primary_id_scheme, base_url, inline_memberships=True):

    result = {
        'persons': []
    }
    if not inline_memberships:
        result['memberships'] = []

    for person in Person.objects.all():
        name = person.legal_name
        person_properties = {'name': name}
        for date, key in ((person.date_of_birth, 'birth_date'),
                          (person.date_of_death, 'death_date')):
            if date:
                person_properties[key] = date_to_partial_iso8601(date)
        primary_image = person.primary_image()
        if primary_image:
            person_properties['images' ] = [
                {
                    'url': urljoin(base_url, primary_image.url)
                }
            ]
        add_identifiers_to_properties(person, person_properties, primary_id_scheme)
        add_contact_details_to_properties(person, person_properties)
        add_other_names(person, person_properties)
        for key in extra_popolo_person_fields:
            value = getattr(person, key)
            # This might be a markitup.fields.Markup field, in
            # which case we need to call raw on it:
            try:
                value = value.raw
            except AttributeError:
                pass
            if value:
                person_properties[key] = value
        country.add_extra_popolo_data_for_person(person, person_properties, base_url)

        if inline_memberships:
            person_properties['memberships'] = []

        for position in person.position_set.all():
            properties = {'person_id': person.get_popolo_id(primary_id_scheme)}
            if position.title and position.title.name:
                properties['role'] = position.title.name
            add_start_and_end_date(position, properties)
            add_identifiers_to_properties(position, properties, primary_id_scheme)
            if position.organisation:
                position.organisation.slug
                organization_id = position.organisation.get_popolo_id(primary_id_scheme)
                properties['organization_id'] = organization_id
            if inline_memberships:
                person_properties['memberships'].append(properties)
            else:
                result['memberships'].append(properties)

        result['persons'].append(person_properties)
    return result
예제 #3
0
def get_people(primary_id_scheme,
               base_url,
               title_to_sessions,
               inline_memberships=True):

    result = {'persons': []}
    if not inline_memberships:
        result['memberships'] = []

    # TODO: if interests_register is being used, we should prefetch
    # that as well (with the Prefetch doing a select_related on
    # category and a prefetch_related on line_items.
    for person in Person.objects.order_by().prefetch_related(
            'alternative_names',
            Prefetch('contacts',
                     queryset=Contact.objects.select_related('kind')),
            'identifiers', 'images',
            Prefetch('position_set',
                     queryset=Position.objects.order_by().select_related(
                         'organisation',
                         'place__mapit_area__type',
                         'place__parliamentary_session__house',
                         'title',
                     ).prefetch_related('identifiers'))):
        name = person.legal_name
        person_properties = {'name': name}
        for date, key in ((person.date_of_birth, 'birth_date'),
                          (person.date_of_death, 'death_date')):
            if date:
                person_properties[key] = date_to_partial_iso8601(date)
        primary_image = person.primary_image()
        if primary_image:
            person_properties['images'] = [{
                'url':
                urljoin(base_url, primary_image.url)
            }]
        add_identifiers_to_properties(person, person_properties,
                                      primary_id_scheme)
        add_contact_details_to_properties(person, person_properties)
        add_other_names(person, person_properties)

        # always include the pombola slug as an identifier
        person_properties['identifiers'].append({
            'scheme': 'pombola-slug',
            'identifier': person.slug
        })

        for key in extra_popolo_person_fields:
            value = getattr(person, key)
            # This might be a markitup.fields.Markup field, in
            # which case we need to call raw on it:
            try:
                value = value.raw
            except AttributeError:
                pass
            if value:
                person_properties[key] = value
        country.add_extra_popolo_data_for_person(person, person_properties,
                                                 base_url)

        if inline_memberships:
            person_properties['memberships'] = []

        for position in person.position_set.all():
            properties = {'person_id': person.get_popolo_id(primary_id_scheme)}
            if position.title and position.title.name:
                properties['role'] = position.title.name
            add_start_and_end_date(position, properties)
            add_identifiers_to_properties(position, properties,
                                          primary_id_scheme)
            if position.organisation:
                position.organisation.slug
                organization_id = position.organisation.get_popolo_id(
                    primary_id_scheme)
                properties['organization_id'] = organization_id
            if position.place:
                # If there's a place associated with the position, set that on
                # the position as an area:
                properties['area'] = get_area_information(
                    position.place, base_url)
            possible_events = []
            if position.title:
                possible_events = title_to_sessions.get(
                    position.title.slug, [])
            if possible_events:
                # Order them by overlap:
                events_with_overlap = [
                    (position.approximate_date_overlap(e.start_date,
                                                       e.end_date), e)
                    for e in possible_events
                ]
                events_with_overlap.sort(reverse=True, key=lambda t: t[0])
                # n.b. There's an assumption here that if someone's an
                # MP in consecutive terms, that's represented by two
                # positions rather than one. (In most cases that
                # better models reality anyway.0
                most_likely_event = events_with_overlap[0]
                properties['legislative_period_id'] = most_likely_event[1].slug
            if inline_memberships:
                person_properties['memberships'].append(properties)
            else:
                result['memberships'].append(properties)

        result['persons'].append(person_properties)
    return result
def get_people(primary_id_scheme, base_url, title_to_sessions, inline_memberships=True):

    result = {
        'persons': []
    }
    if not inline_memberships:
        result['memberships'] = []

    # TODO: if interests_register is being used, we should prefetch
    # that as well (with the Prefetch doing a select_related on
    # category and a prefetch_related on line_items.
    for person in Person.objects.order_by().prefetch_related(
            'alternative_names',
            Prefetch(
                'contacts',
                queryset=Contact.objects.select_related('kind')
            ),
            'identifiers',
            'images',
            Prefetch(
                'position_set',
                queryset=Position.objects.order_by().select_related(
                    'organisation',
                    'place__mapit_area__type',
                    'place__parliamentary_session__house',
                    'title',
                ).prefetch_related('identifiers')
            )
    ):
        name = person.legal_name
        person_properties = {'name': name}
        for date, key in ((person.date_of_birth, 'birth_date'),
                          (person.date_of_death, 'death_date')):
            if date:
                person_properties[key] = date_to_partial_iso8601(date)
        primary_image = person.primary_image()
        if primary_image:
            person_properties['images' ] = [
                {
                    'url': urljoin(base_url, primary_image.url)
                }
            ]
        add_identifiers_to_properties(person, person_properties, primary_id_scheme)
        add_contact_details_to_properties(person, person_properties)
        add_other_names(person, person_properties)
        for key in extra_popolo_person_fields:
            value = getattr(person, key)
            # This might be a markitup.fields.Markup field, in
            # which case we need to call raw on it:
            try:
                value = value.raw
            except AttributeError:
                pass
            if value:
                person_properties[key] = value
        country.add_extra_popolo_data_for_person(person, person_properties, base_url)

        if inline_memberships:
            person_properties['memberships'] = []

        for position in person.position_set.all():
            properties = {'person_id': person.get_popolo_id(primary_id_scheme)}
            if position.title and position.title.name:
                properties['role'] = position.title.name
            add_start_and_end_date(position, properties)
            add_identifiers_to_properties(position, properties, primary_id_scheme)
            if position.organisation:
                position.organisation.slug
                organization_id = position.organisation.get_popolo_id(primary_id_scheme)
                properties['organization_id'] = organization_id
            if position.place:
                # If there's a place associated with the position, set that on
                # the position as an area:
                properties['area'] = get_area_information(position.place, base_url)
            possible_events = []
            if position.title:
                possible_events = title_to_sessions.get(position.title.slug, [])
            if possible_events:
                # Order them by overlap:
                events_with_overlap = [
                    (position.approximate_date_overlap(e.start_date, e.end_date), e)
                    for e in possible_events
                ]
                events_with_overlap.sort(reverse=True, key=lambda t: t[0])
                # n.b. There's an assumption here that if someone's an
                # MP in consecutive terms, that's represented by two
                # positions rather than one. (In most cases that
                # better models reality anyway.0
                most_likely_event = events_with_overlap[0]
                properties['legislative_period_id'] = most_likely_event[1].slug
            if inline_memberships:
                person_properties['memberships'].append(properties)
            else:
                result['memberships'].append(properties)

        result['persons'].append(person_properties)
    return result