Пример #1
0
def _create_election_result(parliamentdotuk, data):
    check_required_fields(
        data,
        contract.CONSTITUENCY,
        contract.ELECTION,
        contract.RESULT_OF_ELECTION,
        contract.MAJORITY,
    )

    log.info(f"Updating constituency result {parliamentdotuk}...")

    constituency_id = get_parliamentdotuk_id(data, contract.CONSTITUENCY_ABOUT)
    election_name = get_str(data, contract.ELECTION_NAME)

    constituency = get_or_none(Constituency, parliamentdotuk=constituency_id)
    election, _ = Election.objects.get_or_create(
        name=election_name,
        defaults={
            "parliamentdotuk":
            get_parliamentdotuk_id(data, contract.ELECTION_ABOUT)
        },
    )

    constituency_result, _ = ConstituencyResult.objects.get_or_create(
        election=election,
        constituency=constituency,
    )

    electorate = get_int(data, contract.ELECTORATE)
    turnout = get_int(data, contract.TURNOUT)

    if not electorate or not turnout:
        turnout_fraction = 0
    else:
        turnout_fraction = turnout / electorate

    result, _ = ConstituencyResultDetail.objects.update_or_create(
        parliamentdotuk=parliamentdotuk,
        defaults={
            "constituency_result": constituency_result,
            "electorate": electorate,
            "majority": get_int(data, contract.MAJORITY),
            "result": get_str(data, contract.RESULT_OF_ELECTION),
            "turnout": turnout,
            "turnout_fraction": turnout_fraction,
        },
    )

    candidates = get_list(data, contract.CANDIDATES)
    for candidate in candidates:
        _create_candidate(result, candidate, constituency, election)
Пример #2
0
    def build_constituency(json_data):
        check_required_fields(
            json_data,
            contract.ABOUT,
            contract.NAME,
        )

        puk = get_parliamentdotuk_id(json_data)
        name = get_str(json_data, contract.NAME)

        Constituency.objects.update_or_create(
            parliamentdotuk=puk,
            defaults={
                "name": name,
                "gss_code": get_str(json_data, contract.GSS_CODE),
                "ordinance_survey_name": get_str(
                    json_data,
                    contract.ORDINANCE_SURVEY_NAME,
                    default="",
                ),
                "constituency_type": get_str(json_data, contract.TYPE),
                "start": get_date(json_data, contract.DATE_STARTED),
                "end": get_date(json_data, contract.DATE_ENDED),
            },
        )
Пример #3
0
def lazy_update(
    puk_model,
    update_func: Callable[[int, Optional[JsonResponseCache]], None],
    json_data: dict,
    **kwargs,
) -> None:
    """
    Some data from the Parliament API are unlikely to change over time so we can reduce
    requests by skipping those that we already have.

    Sometimes we need to force a refresh of those data. AsyncCommand provides the -force flag
    for this purpose, which adds 'force_update=True' to the function kwargs. When this flag is
    set, the model should update from the Parliament API regardless of our existing data.

    :param puk_model: Class of the root model for updating e.g. ConstituencyResultDetail
    :param update_func: A function that accepts a parliamentdotuk ID and a JsonResponseCache
    :param json_data: A dictionary representing the JSON data of an item. See :func:`~lda_client.update_model`.
    :param kwargs: This will typically include a cache definition from @json_cache decoration,
                   and possibly force_cache from AsyncCommand.
    """
    puk = get_parliamentdotuk_id(json_data)
    cache = kwargs.get("cache")

    if kwargs.get("force_update"):
        print(f"Forcing update with {update_func.__name__} for item #{puk}")
        update_func(puk, cache)

    else:
        try:
            puk_model.objects.get(parliamentdotuk=puk)
            return
        except puk_model.DoesNotExist:
            pass

        update_func(puk, cache)
Пример #4
0
    def fetch_and_update_bill(json_data) -> Optional[str]:
        parliamentdotuk = get_parliamentdotuk_id(json_data)

        data = get_item_data(
            endpoints.url_for_bill(parliamentdotuk),
            cache=kwargs.get("cache"),
        )
        if data is not None:
            return _update_bill(parliamentdotuk, data)
Пример #5
0
def _update_bill_stage(bill: Bill, data: dict):
    check_required_fields(
        data,
        contract.ABOUT,
        contract.BILL_STAGE_TYPE,
    )

    parliamentdotuk = get_parliamentdotuk_id(data)

    stage_type_data = data.get(contract.BILL_STAGE_TYPE)
    check_required_fields(
        stage_type_data,
        contract.ABOUT,
        contract.LABEL,
    )
    stage_type, _ = BillStageType.objects.get_or_create(
        parliamentdotuk=get_parliamentdotuk_id(stage_type_data),
        name=get_str(stage_type_data, contract.LABEL),
    )

    session = _get_session(data)

    stage, _ = BillStage.objects.update_or_create(
        parliamentdotuk=parliamentdotuk,
        defaults={
            "bill": bill,
            "bill_stage_type": stage_type,
            "session": session,
        },
    )

    sittings = get_list(data, contract.BILL_STAGE_SITTINGS)
    for sitting in sittings:
        BillStageSitting.objects.get_or_create(
            parliamentdotuk=get_parliamentdotuk_id(sitting),
            defaults={
                "bill_stage": stage,
                "date": get_date(sitting, contract.DATE),
                "formal": get_boolean(sitting, contract.FORMAL, default=False),
                "provisional": get_boolean(
                    sitting, contract.PROVISIONAL, default=False
                ),
            },
        )
Пример #6
0
def _update_bill_publication(bill: Bill, data: dict):
    check_required_fields(
        data,
        contract.TITLE,
    )

    pub_puk = get_parliamentdotuk_id(data)
    BillPublication.objects.update_or_create(
        parliamentdotuk=pub_puk,
        defaults={
            "bill": bill,
            "title": get_str(data, contract.TITLE),
        },
    )
Пример #7
0
def _get_session(data: dict):
    check_required_fields(
        data,
        contract.SESSION,
    )

    session_data = data.get(contract.SESSION)
    if isinstance(session_data, list):
        session_data = session_data[0]

    check_required_fields(
        session_data,
        contract.ABOUT,
        contract.SESSION_NAME,
    )

    parliamentdotuk = get_parliamentdotuk_id(session_data)
    session, _ = ParliamentarySession.objects.get_or_create(
        parliamentdotuk=parliamentdotuk,
        defaults={
            "name": session_data.get(contract.SESSION_NAME),
        },
    )
    return session
Пример #8
0
 def test_get_parliamentdotuk_id(self):
     parliamentdotuk = lda_client.get_parliamentdotuk_id(EXAMPLE_ITEM)
     self.assertEqual(parliamentdotuk, 146747)
Пример #9
0
def _get_vote_commons_member_id(vote_data):
    return get_parliamentdotuk_id(vote_data.get(contract.VOTE_MEMBER)[0])