def get_all(election_id=None, area_name=None, associated_area_id=None, area_type=None):
    election = Election.get_by_id(electionId=election_id)

    if associated_area_id is not None and area_type is not None:
        associated_area = get_by_id(areaId=associated_area_id)
        query = get_associated_areas_query(areas=[associated_area], areaType=area_type, electionId=election_id)
    else:
        query = Model.query

    if area_name is not None:
        query = query.filter(Model.areaName.like(area_name))

    if election is not None:
        query = query.filter(
            or_(
                Model.electionId.in_(election.mappedElectionIds),
                Model.electionId.in_(election.subElectionIds)
            )
        )

    if area_type is not None:
        query = query.filter(Model.areaType == area_type)

    query = query.order_by(Model.areaId)

    return query
Exemplo n.º 2
0
def get_all(electionId=None, areaId=None, tallySheetCode=None, voteType=None):
    # Filter by authorized areas
    user_access_area_ids: Set[int] = get_user_access_area_ids()

    query_args = [Model]
    query_filters = [
        Submission.Model.areaId.in_(user_access_area_ids),
        Template.Model.templateId == Model.templateId,
        Submission.Model.submissionId == Model.tallySheetId,
        Election.Model.electionId == Submission.Model.electionId
    ]
    query_group_by = [Model.tallySheetId]

    if areaId is not None:
        query_filters.append(Submission.Model.areaId == areaId)

    if electionId is not None:
        election = Election.get_by_id(electionId=electionId)
        query_filters.append(
            Election.Model.electionId.in_(
                election.get_this_and_below_election_ids()))

    if tallySheetCode is not None:
        query_filters.append(Template.Model.templateName == tallySheetCode)

    if voteType is not None:
        query_filters.append(Election.Model.voteType == voteType)

    return db.session.query(*query_args).filter(*query_filters).group_by(
        *query_group_by)
Exemplo n.º 3
0
def get_all(electionId=None, areaId=None, tallySheetCode=None):
    election = Election.get_by_id(electionId=electionId)

    query = Model.query.join(
        Submission.Model,
        Submission.Model.submissionId == Model.tallySheetId
    ).join(
        Election.Model,
        Election.Model.electionId == Submission.Model.electionId
    )

    if electionId is not None:
        query = query.filter(
            Election.Model.electionId.in_(election.mappedElectionIds)
        )

    if areaId is not None:
        query = query.filter(Submission.Model.areaId == areaId)

    if tallySheetCode is not None:
        query = query.filter(Model.tallySheetCode == get_tally_sheet_code(tallySheetCode))

    # Filter by authorized areas
    user_access_area_ids: Set[int] = get_user_access_area_ids()
    query = query.filter(Submission.Model.areaId.in_(user_access_area_ids))

    return query
def get_by_id(electionId):
    result = Election.get_by_id(electionId=electionId)
    if result is None:
        raise NotFoundException(message="Election not found (electionId=%d)" %
                                electionId,
                                code=MESSAGE_CODE_ELECTION_NOT_FOUND)

    return Schema().dump(result).data
Exemplo n.º 5
0
def get_all(electionId=None,
            areaId=None,
            tallySheetCode=None,
            voteType=None,
            partyId=None,
            limit=None,
            offset=None):
    # Filter by authorized areas
    user_access_area_ids: Set[int] = get_user_access_area_ids()

    query_args = [Model]
    query_filters = [
        Submission.Model.areaId.in_(user_access_area_ids),
        Template.Model.templateId == Model.templateId,
        Submission.Model.submissionId == Model.tallySheetId,
        Election.Model.electionId == Submission.Model.electionId
    ]
    query_group_by = [Model.tallySheetId]

    if areaId is not None:
        query_filters.append(Submission.Model.areaId == areaId)

    if electionId is not None:
        election = Election.get_by_id(electionId=electionId)
        query_filters.append(
            Election.Model.electionId.in_(
                election.get_this_and_below_election_ids()))

    if tallySheetCode is not None:
        query_filters.append(Template.Model.templateName == tallySheetCode)

    if voteType is not None:
        query_filters.append(Election.Model.voteType == voteType)

    if partyId is not None:
        query_filters += [
            MetaData.Model.metaId == Model.metaId,
            MetaData.Model.metaDataKey == "partyId",
            MetaData.Model.metaDataValue == partyId
        ]

    tally_sheet_list = db.session.query(*query_args).filter(
        *query_filters).group_by(*query_group_by).order_by(Model.tallySheetId)

    tally_sheet_list = get_paginated_query(query=tally_sheet_list,
                                           limit=limit,
                                           offset=offset)

    authorized_tally_sheet_list = []
    for tally_sheet in tally_sheet_list:
        if has_role_based_access(election=tally_sheet.submission.election,
                                 tally_sheet_code=tally_sheet.tallySheetCode,
                                 access_type=WORKFLOW_ACTION_TYPE_VIEW):
            authorized_tally_sheet_list.append(tally_sheet)

    return authorized_tally_sheet_list
Exemplo n.º 6
0
def _cache_get_area_map(user_access_area_ids, electionId):
    election = Election.get_by_id(electionId=electionId)
    if election is None:
        raise NotFoundException(message="Election not found (electionId=%d)" %
                                electionId,
                                code=MESSAGE_CODE_ELECTION_NOT_FOUND)

    extended_election = election.get_extended_election()
    area_map = extended_election.get_area_map()

    return AreaMapSchema(many=True).dump(area_map).data
Exemplo n.º 7
0
def get_mapped_area(electionId=None, areaIds=None, requestedAreaType=None):
    election = Election.get_by_id(electionId=electionId)
    if election is None:
        raise NotFoundException(message="Election not found (electionId=%d)" %
                                electionId,
                                code=MESSAGE_CODE_ELECTION_NOT_FOUND)

    extended_election = election.get_extended_election()
    mapped_area = extended_election.get_mapped_area(areaIds, requestedAreaType)

    return MappedAreaSchema(many=True).dump(mapped_area).data
Exemplo n.º 8
0
def get_all(electionId=None, areaId=None, tallySheetCode=None, voteType=None):
    # Filter by authorized areas
    user_access_area_ids: Set[int] = get_user_access_area_ids()

    query_args = [Model]
    query_filters = [
        Submission.Model.areaId.in_(user_access_area_ids),
        Template.Model.templateId == Model.templateId,
        Submission.Model.submissionId == Model.tallySheetId,
        Election.Model.electionId == Submission.Model.electionId
    ]
    query_group_by = [Model.tallySheetId]

    if areaId is not None:
        query_filters.append(Submission.Model.areaId == areaId)

    if electionId is not None:
        election = Election.get_by_id(electionId=electionId)
        query_filters.append(
            Election.Model.electionId.in_(
                election.get_this_and_below_election_ids()))

    if tallySheetCode is not None:
        query_filters.append(Template.Model.templateName == tallySheetCode)

    if voteType is not None:
        query_filters.append(Election.Model.voteType == voteType)

    tally_sheet_list = db.session.query(*query_args).filter(
        *query_filters).group_by(*query_group_by)

    authorized_tally_sheet_list = []
    for tally_sheet in tally_sheet_list:
        if has_role_based_access(tally_sheet=tally_sheet,
                                 access_type=WORKFLOW_ACTION_TYPE_VIEW):
            authorized_tally_sheet_list.append(tally_sheet)

    return authorized_tally_sheet_list
def get_associated_areas_query(areas, areaType, electionId=None):
    presidential_area_map_sub_query = get_presidential_area_map_query().subquery()
    election = Election.get_by_id(electionId=electionId)

    query = db.session.query(
        AreaModel
    ).join(
        Election.Model,
        Election.Model.electionId == AreaModel.electionId
    )

    if areaType is AreaTypeEnum.PollingStation:
        query = query.join(
            presidential_area_map_sub_query,
            presidential_area_map_sub_query.c.pollingStationId == AreaModel.areaId
        )
    elif areaType is AreaTypeEnum.CountingCentre:
        query = query.join(
            presidential_area_map_sub_query,
            presidential_area_map_sub_query.c.countingCentreId == AreaModel.areaId
        )
    elif areaType is AreaTypeEnum.DistrictCentre:
        query = query.join(
            presidential_area_map_sub_query,
            presidential_area_map_sub_query.c.districtCentreId == AreaModel.areaId
        )
    elif areaType is AreaTypeEnum.ElectionCommission:
        query = query.join(
            presidential_area_map_sub_query,
            presidential_area_map_sub_query.c.electionCommissionId == AreaModel.areaId
        )
    elif areaType is AreaTypeEnum.PollingDistrict:
        query = query.join(
            presidential_area_map_sub_query,
            presidential_area_map_sub_query.c.pollingDistrictId == AreaModel.areaId
        )
    elif areaType is AreaTypeEnum.PollingDivision:
        query = query.join(
            presidential_area_map_sub_query,
            presidential_area_map_sub_query.c.pollingDivisionId == AreaModel.areaId
        )
    elif areaType is AreaTypeEnum.ElectoralDistrict:
        query = query.join(
            presidential_area_map_sub_query,
            presidential_area_map_sub_query.c.electoralDistrictId == AreaModel.areaId
        )
    elif areaType is AreaTypeEnum.Country:
        query = query.join(
            presidential_area_map_sub_query,
            presidential_area_map_sub_query.c.countryId == AreaModel.areaId
        )
    elif areaType is AreaTypeEnum.PostalVoteCountingCentre:
        query = query.join(
            presidential_area_map_sub_query,
            presidential_area_map_sub_query.c.postalVoteCountingCentreId == AreaModel.areaId
        )

    query = query.group_by(AreaModel.areaId)

    filtered_polling_stations = [area.areaId for area in areas if area.areaType == AreaTypeEnum.PollingStation]
    filtered_counting_centres = [area.areaId for area in areas if area.areaType == AreaTypeEnum.CountingCentre]
    filtered_district_centres = [area.areaId for area in areas if area.areaType == AreaTypeEnum.DistrictCentre]
    filtered_election_commissions = [area.areaId for area in areas if area.areaType == AreaTypeEnum.ElectionCommission]
    filtered_polling_districts = [area.areaId for area in areas if area.areaType == AreaTypeEnum.PollingDistrict]
    filtered_polling_divisions = [area.areaId for area in areas if area.areaType == AreaTypeEnum.PollingDivision]
    filtered_electoral_districts = [area.areaId for area in areas if area.areaType == AreaTypeEnum.ElectoralDistrict]
    filtered_countries = [area.areaId for area in areas if area.areaType == AreaTypeEnum.Country]

    if len(filtered_polling_stations) > 0:
        query = query.filter(
            presidential_area_map_sub_query.c.pollingStationId.in_(filtered_polling_stations)
        )
    elif len(filtered_counting_centres) > 0:
        query = query.filter(
            presidential_area_map_sub_query.c.countingCentreId.in_(filtered_counting_centres)
        )
    elif len(filtered_district_centres) > 0:
        query = query.filter(
            presidential_area_map_sub_query.c.districtCentreId.in_(filtered_district_centres)
        )
    elif len(filtered_election_commissions) > 0:
        query = query.filter(
            presidential_area_map_sub_query.c.electionCommissionId.in_(filtered_election_commissions)
        )
    elif len(filtered_polling_districts) > 0:
        query = query.filter(
            presidential_area_map_sub_query.c.pollingDistrictId.in_(filtered_polling_districts)
        )
    elif len(filtered_polling_divisions) > 0:
        query = query.filter(
            presidential_area_map_sub_query.c.pollingDivisionId.in_(filtered_polling_divisions)
        )
    elif len(filtered_electoral_districts) > 0:
        query = query.filter(
            presidential_area_map_sub_query.c.electoralDistrictId.in_(filtered_electoral_districts)
        )
    elif len(filtered_countries) > 0:
        query = query.filter(
            presidential_area_map_sub_query.c.countryId.in_(filtered_countries)
        )

    if electionId is not None:
        query = query.filter(
            or_(
                Model.electionId.in_(election.mappedElectionIds),
                Model.electionId.in_(election.subElectionIds)
            )
        )

    query = query.filter(
        AreaModel.areaType == areaType
    )

    return query