Exemple #1
0
def _create_commons_division(parliamentdotuk: int, data: dict) -> None:
    check_required_fields(
        data,
        contract.TITLE,
        contract.DATE,
        contract.DIVISION_NUMBER,
        contract.UIN,
    )

    division, _ = CommonsDivision.objects.update_or_create(
        parliamentdotuk=parliamentdotuk,
        defaults={
            "title": get_str(data, contract.TITLE),
            "abstentions": get_int(data, contract.ABSTENTIONS),
            "ayes": get_int(data, contract.AYES),
            "noes": get_int(data, contract.NOES),
            "did_not_vote": get_int(data, contract.DID_NOT_VOTE),
            "non_eligible": get_int(data, contract.NON_ELIGIBLE),
            "errors": get_int(data, contract.ERRORS),
            "suspended_or_expelled": get_int(data,
                                             contract.SUSPENDED_OR_EXPELLED),
            "date": get_date(data, contract.DATE),
            "deferred_vote": get_boolean(data, contract.DEFERRED_VOTE),
            "session": _get_session(data),
            "uin": data.get(contract.UIN),
            "division_number": get_int(data, contract.DIVISION_NUMBER),
        },
    )

    votes = data.get(contract.VOTES)
    for vote in votes:
        _create_commons_vote(division.parliamentdotuk, vote)
Exemple #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),
            },
        )
Exemple #3
0
def _create_candidate(
    election_result: ConstituencyResultDetail,
    candidate,
    constituency: Constituency,
    election: Election,
):
    check_required_fields(
        candidate,
        contract.CANDIDATE_NAME,
        contract.CANDIDATE_PARTY,
    )

    name = get_str(candidate, contract.CANDIDATE_NAME)
    person = get_member_for_election_result(
        normalize_name(name),
        constituency,
        election,
    )
    party_name = get_str(candidate, contract.CANDIDATE_PARTY)
    party = get_party_by_name(party_name)

    votes = get_int(candidate, contract.CANDIDATE_VOTES)
    order = get_int(candidate, contract.CANDIDATE_ORDINAL)

    ConstituencyCandidate.objects.update_or_create(
        election_result=election_result,
        name=name,
        defaults={
            "person": person,
            "votes": votes,
            "order": order,
            "party_name": party_name,
            "party": party,
        },
    )
Exemple #4
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)
Exemple #5
0
def _update_bill(parliamentdotuk: int, data: dict) -> Optional[str]:
    check_required_fields(
        data,
        contract.ABOUT,
        contract.TITLE,
        contract.DATE,
        contract.HOMEPAGE,
        contract.LABEL,
        contract.BILL_TYPE,
        contract.BILL_TYPE_DESCRIPTION,
    )

    bill_type, _ = BillType.objects.get_or_create(
        name=get_str(data, contract.BILL_TYPE),
        description=get_str(data, contract.BILL_TYPE_DESCRIPTION),
    )

    bill, bill_created = Bill.objects.update_or_create(
        parliamentdotuk=parliamentdotuk,
        defaults={
            "act_name": get_str(data, contract.ACT_NAME),
            "bill_chapter": get_str(data, contract.BILL_CHAPTER),
            "bill_type": bill_type,
            "ballot_number": get_int(data, contract.BALLOT_NUMBER, default=0),
            "date": get_date(data, contract.DATE),
            "description": get_str(data, contract.DESCRIPTION),
            "homepage": get_str(data, contract.HOMEPAGE),
            "is_money_bill": get_boolean(data, contract.MONEY_BILL),
            "is_private": get_boolean(data, contract.PRIVATE_BILL),
            "label": get_str(data, contract.LABEL),
            "public_involvement_allowed": get_boolean(data, contract.PUBLIC_INVOLVED),
            "session": _get_session(data),
            "title": get_str(data, contract.TITLE),
        },
    )

    publications = get_list(data, contract.BILL_PUBLICATIONS)
    for pub in publications:
        _update_bill_publication(bill, pub)

    stages = get_list(data, contract.BILL_STAGES)
    for stage in stages:
        _update_bill_stage(bill, stage)

    sponsors = get_list(data, contract.SPONSORS)
    for sponsor in sponsors:
        _update_sponsor(bill, sponsor)

    if bill_created:
        return bill.title
Exemple #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),
        },
    )
Exemple #7
0
    def test_check_required_fields_should_throw_MissingFieldException_when_field_missing(
        self,
    ):
        data = {
            "a": 31,
            "b": 23,
            "c": 13,
        }

        check_required_fields(data, "a", "b", "c")

        self.assertRaises(
            MissingFieldException, check_required_fields, data, "a", "b", "c", "d"
        )
        self.assertRaises(MissingFieldException, check_required_fields, data, "c", "d")
        self.assertRaises(
            MissingFieldException, check_required_fields, data, "x", "y", "z"
        )
Exemple #8
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
                ),
            },
        )
Exemple #9
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