Exemple #1
0
def google_civic_get_or_create_contest_office(google_civic_candidate_campaign_entry, election_on_stage):
    error_result = False
    ballot_item_on_stage = BallotItem()
    ballot_item_on_stage_found = False
    google_civic_ballot_item_on_stage = GoogleCivicBallotItem()
    contest_office_on_stage = ContestOffice()
    contest_office_created = False
    google_civic_contest_office_on_stage = GoogleCivicContestOffice()
    try:
        # When we import from google, we link a google_civic_contest_office entry (with an internal id) to
        #  google_civic_candidate_campaign_entry
        # print "Retrieving google_civic_contest_office"
        google_civic_contest_office_query = GoogleCivicContestOffice.objects.all()
        google_civic_contest_office_query = google_civic_contest_office_query.filter(
            id=google_civic_candidate_campaign_entry.google_civic_contest_office_id)

        if len(google_civic_contest_office_query) == 1:
            google_civic_contest_office_on_stage = google_civic_contest_office_query[0]
        else:
            print "Single google_civic_contest_office NOT found"
            return {
                'error_result': True,
            }

        # Try to find earlier version based on the google_civic_election_id identifier
        # print "Retrieving contest_office"
        contest_office_query = ContestOffice.objects.all()
        contest_office_query = contest_office_query.filter(
            google_civic_election_id=google_civic_contest_office_on_stage.google_civic_election_id)
        contest_office_query = contest_office_query.filter(
            district_name=google_civic_contest_office_on_stage.district_name)
        contest_office_query = contest_office_query.filter(office_name=google_civic_contest_office_on_stage.office)
        # TODO: If the 'office' text from Google Civic changes slightly, we would create a new ContestOffice entry
        # (which would not be correct) Should we make this more robust and error-proof?

        # Was at least one existing entry found based on the above criteria?
        if len(contest_office_query):
            contest_office_on_stage = contest_office_query[0]
            contest_office_created = False

            # TODO Update contest_office information here
        elif len(contest_office_query) > 1:
            # We have bad data - a duplicate
            print "We have bad data, a duplicate ContestOffice entry: {office}".format(
                office=google_civic_contest_office_on_stage.office)
            return {
                'error_result': True,
            }
        else:
            # Create a new ContestOffice entry
            # print "Creating contest_office"
            contest_office_on_stage = ContestOffice(
                office_name=google_civic_contest_office_on_stage.office,
                election_id=election_on_stage.id,
                google_civic_election_id=google_civic_contest_office_on_stage.google_civic_election_id,
                number_voting_for=google_civic_contest_office_on_stage.number_voting_for,
                number_elected=google_civic_contest_office_on_stage.number_elected,
                primary_party=google_civic_contest_office_on_stage.primary_party,
                district_name=google_civic_contest_office_on_stage.district_name,
                district_scope=google_civic_contest_office_on_stage.district_scope,
                district_ocd_id=google_civic_contest_office_on_stage.district_ocd_id,
            )
            contest_office_on_stage.save()
            contest_office_created = True

        google_civic_contest_office_on_stage.we_vote_election_id = election_on_stage.id
        google_civic_contest_office_on_stage.we_vote_contest_office_id = contest_office_on_stage.id
        google_civic_contest_office_on_stage.save()

        # Save the ballot_placement
        # Try to find earlier version based on the google_civic_election_id identifier
        # print "Retrieving BallotItem"
        ballot_item_query = BallotItem.objects.all()
        ballot_item_query = ballot_item_query.filter(voter_id=1)
        ballot_item_query = ballot_item_query.filter(
            google_civic_election_id=google_civic_contest_office_on_stage.google_civic_election_id)
        ballot_item_query = ballot_item_query.filter(contest_office_id=contest_office_on_stage.id)
        if len(ballot_item_query) == 1:
            ballot_item_on_stage = ballot_item_query[0]
            ballot_item_on_stage_found = True
    except Exception as e:
        error_result = True
        handle_record_not_found_exception(e)

    try:
        voter_id = 1
        if value_exists(google_civic_contest_office_on_stage.ballot_placement):
            # If the ballot order is specified by Google, use that.
            local_ballot_order = google_civic_contest_office_on_stage.ballot_placement
        else:
            # Pull the ballot order from the
            google_civic_ballot_item_manager = GoogleCivicBallotItemManager()
            local_ballot_order = google_civic_ballot_item_manager.fetch_ballot_order(
                voter_id, google_civic_contest_office_on_stage.google_civic_election_id,
                google_civic_contest_office_on_stage.district_ocd_id)

        if ballot_item_on_stage_found:
            # Update the values
            ballot_item_on_stage.election_id = election_on_stage.id
            # TODO Add all values here
        else:
            # print "Creating BallotItem"
            ballot_item_on_stage = BallotItem(
                voter_id=voter_id,
                election_id=election_on_stage.id,
                google_civic_election_id=google_civic_contest_office_on_stage.google_civic_election_id,
                contest_office_id=contest_office_on_stage.id,
                # contest_measure_id: Used for measures/referendum/initiatives
                ballot_order=local_ballot_order,
                ballot_item_label=google_civic_contest_office_on_stage.office,
            )
        ballot_item_on_stage.save()
    except Exception as e:
        error_result = True
        handle_record_not_saved_exception(e)

    results = {
        'error_result': error_result,
        'contest_office_on_stage': contest_office_on_stage,
        'contest_office_created': contest_office_created,
    }
    return results
Exemple #2
0
def google_civic_get_or_create_contest_office(
        google_civic_candidate_campaign_entry, election_on_stage):
    error_result = False
    ballot_item_on_stage = BallotItem()
    ballot_item_on_stage_found = False
    google_civic_ballot_item_on_stage = GoogleCivicBallotItem()
    contest_office_on_stage = ContestOffice()
    contest_office_created = False
    google_civic_contest_office_on_stage = GoogleCivicContestOffice()
    try:
        # When we import from google, we link a google_civic_contest_office entry (with an internal id) to
        #  google_civic_candidate_campaign_entry
        logger.debug("Retrieving google_civic_contest_office")
        google_civic_contest_office_query = GoogleCivicContestOffice.objects.all(
        )
        google_civic_contest_office_query = google_civic_contest_office_query.filter(
            id=google_civic_candidate_campaign_entry.
            google_civic_contest_office_id)

        if len(google_civic_contest_office_query) == 1:
            google_civic_contest_office_on_stage = google_civic_contest_office_query[
                0]
        else:
            logger.error("Single google_civic_contest_office NOT found")
            return {
                'error_result': True,
            }

        # Try to find earlier version based on the google_civic_election_id identifier
        logger.debug("Retrieving contest_office")
        contest_office_query = ContestOffice.objects.all()
        contest_office_query = contest_office_query.filter(
            google_civic_election_id=google_civic_contest_office_on_stage.
            google_civic_election_id)
        contest_office_query = contest_office_query.filter(
            district_name=google_civic_contest_office_on_stage.district_name)
        contest_office_query = contest_office_query.filter(
            office_name=google_civic_contest_office_on_stage.office)
        # TODO: If the 'office' text from Google Civic changes slightly, we would create a new ContestOffice entry
        # (which would not be correct) Should we make this more robust and error-proof?

        # Was at least one existing entry found based on the above criteria?
        if len(contest_office_query):
            contest_office_on_stage = contest_office_query[0]
            contest_office_created = False

            # TODO Update contest_office information here
        elif len(contest_office_query) > 1:
            # We have bad data - a duplicate
            logger.error(
                u"We have bad data, a duplicate ContestOffice entry: {office}".
                format(office=google_civic_contest_office_on_stage.office))
            return {
                'error_result': True,
            }
        else:
            # Create a new ContestOffice entry
            logger.debug("Creating contest_office")
            contest_office_on_stage = ContestOffice(
                office_name=google_civic_contest_office_on_stage.office,
                election_id=election_on_stage.id,
                google_civic_election_id=google_civic_contest_office_on_stage.
                google_civic_election_id,
                number_voting_for=google_civic_contest_office_on_stage.
                number_voting_for,
                number_elected=google_civic_contest_office_on_stage.
                number_elected,
                primary_party=google_civic_contest_office_on_stage.
                primary_party,
                district_name=google_civic_contest_office_on_stage.
                district_name,
                district_scope=google_civic_contest_office_on_stage.
                district_scope,
                district_ocd_id=google_civic_contest_office_on_stage.
                district_ocd_id,
            )
            contest_office_on_stage.save()
            contest_office_created = True

        google_civic_contest_office_on_stage.we_vote_election_id = election_on_stage.id
        google_civic_contest_office_on_stage.we_vote_contest_office_id = contest_office_on_stage.id
        google_civic_contest_office_on_stage.save()

        # Save the ballot_placement
        # Try to find earlier version based on the google_civic_election_id identifier
        logger.debug("Retrieving BallotItem")
        ballot_item_query = BallotItem.objects.all()
        ballot_item_query = ballot_item_query.filter(voter_id=1)
        ballot_item_query = ballot_item_query.filter(
            google_civic_election_id=google_civic_contest_office_on_stage.
            google_civic_election_id)
        ballot_item_query = ballot_item_query.filter(
            contest_office_id=contest_office_on_stage.id)
        if len(ballot_item_query) == 1:
            ballot_item_on_stage = ballot_item_query[0]
            ballot_item_on_stage_found = True
    except Exception as e:
        error_result = True
        handle_record_not_found_exception(e, logger=logger)

    try:
        voter_id = 1
        if value_exists(google_civic_contest_office_on_stage.ballot_placement):
            # If the ballot order is specified by Google, use that.
            local_ballot_order = google_civic_contest_office_on_stage.ballot_placement
        else:
            # Pull the ballot order from the
            google_civic_ballot_item_manager = GoogleCivicBallotItemManager()
            local_ballot_order = google_civic_ballot_item_manager.fetch_ballot_order(
                voter_id,
                google_civic_contest_office_on_stage.google_civic_election_id,
                google_civic_contest_office_on_stage.district_ocd_id)

        if ballot_item_on_stage_found:
            # Update the values
            ballot_item_on_stage.election_id = election_on_stage.id
            # TODO Add all values here
        else:
            logger.debug("Creating BallotItem")
            ballot_item_on_stage = BallotItem(
                voter_id=voter_id,
                election_id=election_on_stage.id,
                google_civic_election_id=google_civic_contest_office_on_stage.
                google_civic_election_id,
                contest_office_id=contest_office_on_stage.id,
                # contest_measure_id: Used for measures/referendum/initiatives
                ballot_order=local_ballot_order,
                ballot_item_label=google_civic_contest_office_on_stage.office,
            )
        ballot_item_on_stage.save()
    except Exception as e:
        error_result = True
        handle_record_not_saved_exception(e, logger=logger)

    results = {
        'error_result': error_result,
        'contest_office_on_stage': contest_office_on_stage,
        'contest_office_created': contest_office_created,
    }
    return results