<s> from django . contrib import messages <EOL> from django . contrib . messages import get_messages <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import HttpResponseRedirect <EOL> from django . shortcuts import redirect , render <EOL> from election_office_measure . models import CandidateCampaign , CandidateCampaignManager , ContestMeasure , ContestOffice , MeasureCampaign <EOL> from exception . models import handle_record_not_saved_exception <EOL> from . controllers import transfer_google_civic_voterinfo_cached_data_to_wevote_tables <EOL> from . models import transfer_theunitedstatesio_cached_data_to_wevote_tables , import_we_vote_organizations_from_json , import_we_vote_candidate_campaigns_from_json , import_we_vote_positions_from_json <EOL> from import_export_maplight . models import MapLightCandidate <EOL> from . serializers import CandidateCampaignSerializer , OrganizationSerializer , PositionSerializer <EOL> from organization . models import Organization <EOL> from politician . models import Politician <EOL> from position . models import PositionEntered <EOL> from rest_framework . views import APIView <EOL> from rest_framework . response import Response <EOL> import wevote_functions . admin <EOL> from wevote_functions . models import value_exists <EOL> logger = wevote_functions . admin . get_logger ( __name__ ) <EOL> def import_export_index ( request ) : <EOL> """<STR_LIT>""" <EOL> messages_on_stage = get_messages ( request ) <EOL> template_values = { <EOL> '<STR_LIT>' : messages_on_stage , <EOL> } <EOL> return render ( request , '<STR_LIT>' , template_values ) <EOL> def import_export_transfer_google_civic_to_local_tables_view ( request ) : <EOL> """<STR_LIT>""" <EOL> if not request . user . is_authenticated ( ) : <EOL> return redirect ( '<STR_LIT>' ) <EOL> transfer_google_civic_voterinfo_cached_data_to_wevote_tables ( ) <EOL> template_values = { <EOL> '<STR_LIT>' : Politician . objects . order_by ( '<STR_LIT>' ) , <EOL> } <EOL> return render ( request , '<STR_LIT>' , template_values ) <EOL> def import_export_transfer_theunitedstatesio_to_local_tables_view ( request ) : <EOL> """<STR_LIT>""" <EOL> if not request . user . is_authenticated ( ) : <EOL> return redirect ( '<STR_LIT>' ) <EOL> transfer_theunitedstatesio_cached_data_to_wevote_tables ( ) <EOL> template_values = { <EOL> '<STR_LIT>' : Politician . objects . order_by ( '<STR_LIT>' ) , <EOL> } <EOL> return render ( request , '<STR_LIT>' , template_values ) <EOL> def update_all_id_we_vote ( request ) : <EOL> """<STR_LIT>""" <EOL> candidate_campaign_list = CandidateCampaign . objects . all ( ) <EOL> for candidate_campaign in candidate_campaign_list : <EOL> candidate_campaign . save ( ) <EOL> contest_measure_list = ContestMeasure . objects . all ( ) <EOL> for contest_measure in contest_measure_list : <EOL> contest_measure . save ( ) <EOL> contest_office_list = ContestOffice . objects . all ( ) <EOL> for contest_office in contest_office_list : <EOL> contest_office . save ( ) <EOL> measure_campaign_list = MeasureCampaign . objects . all ( ) <EOL> for measure_campaign in measure_campaign_list : <EOL> measure_campaign . save ( ) <EOL> organization_list = Organization . objects . all ( ) <EOL> for organization in organization_list : <EOL> organization . save ( ) <EOL> position_list = PositionEntered . objects . all ( ) <EOL> for position in position_list : <EOL> position . save ( ) <EOL> class ExportOrganizationDataToJson ( APIView ) : <EOL> def get ( self , request , format = None ) : <EOL> organization_list = Organization . objects . all ( ) <EOL> serializer = OrganizationSerializer ( organization_list , many = True ) <EOL> return Response ( serializer . data ) <EOL> class ExportCandidateCampaignDataToJson ( APIView ) : <EOL> def get ( self , request , format = None ) : <EOL> candidate_campaign_list = CandidateCampaign . objects . all ( ) <EOL> serializer = CandidateCampaignSerializer ( candidate_campaign_list , many = True ) <EOL> return Response ( serializer . data ) <EOL> class ExportPositionDataToJson ( APIView ) : <EOL> def get ( self , request , format = None ) : <EOL> position_list = PositionEntered . objects . all ( ) <EOL> serializer = PositionSerializer ( position_list , many = True ) <EOL> return Response ( serializer . data ) <EOL> def import_we_vote_sample_positions_data_from_json ( request ) : <EOL> """<STR_LIT>""" <EOL> import_we_vote_organizations_from_json ( request , False ) <EOL> import_we_vote_candidate_campaigns_from_json ( request , False ) <EOL> import_we_vote_positions_from_json ( request , False ) <EOL> messages . add_message ( request , messages . INFO , '<STR_LIT>' ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , args = ( ) ) ) <EOL> def transfer_maplight_data_to_we_vote_tables ( request ) : <EOL> politician_name_mapping_list = [ ] <EOL> one_mapping = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> politician_name_mapping_list . append ( one_mapping ) <EOL> one_mapping = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> politician_name_mapping_list . append ( one_mapping ) <EOL> candidate_campaign_manager = CandidateCampaignManager ( ) <EOL> maplight_candidates_current_query = MapLightCandidate . objects . all ( ) <EOL> for one_candidate_from_maplight_table in maplight_candidates_current_query : <EOL> found_by_id = False <EOL> results = candidate_campaign_manager . retrieve_candidate_campaign_from_id_maplight ( <EOL> one_candidate_from_maplight_table . candidate_id ) <EOL> if not results [ '<STR_LIT:success>' ] : <EOL> logger . warn ( u"<STR_LIT>" . format ( <EOL> name = one_candidate_from_maplight_table . candidate_id <EOL> ) ) <EOL> results = candidate_campaign_manager . retrieve_candidate_campaign_from_candidate_name ( <EOL> one_candidate_from_maplight_table . display_name ) <EOL> if not results [ '<STR_LIT:success>' ] : <EOL> logger . warn ( u"<STR_LIT>" . format ( <EOL> name = one_candidate_from_maplight_table . display_name <EOL> ) ) <EOL> results = candidate_campaign_manager . retrieve_candidate_campaign_from_candidate_name ( <EOL> one_candidate_from_maplight_table . original_name ) <EOL> if not results [ '<STR_LIT:success>' ] : <EOL> logger . warn ( u"<STR_LIT>" . format ( <EOL> name = one_candidate_from_maplight_table . original_name <EOL> ) ) <EOL> one_mapping_google_civic_name = '<STR_LIT>' <EOL> for one_mapping_found in politician_name_mapping_list : <EOL> if value_exists ( one_mapping_found [ '<STR_LIT>' ] ) and one_mapping_found [ '<STR_LIT>' ] == one_candidate_from_maplight_table . display_name : <EOL> one_mapping_google_civic_name = one_mapping_found [ '<STR_LIT>' ] <EOL> break <EOL> if value_exists ( one_mapping_google_civic_name ) : <EOL> results = candidate_campaign_manager . retrieve_candidate_campaign_from_candidate_name ( <EOL> one_mapping_google_civic_name ) <EOL> if not results [ '<STR_LIT:success>' ] or not value_exists ( one_mapping_google_civic_name ) : <EOL> logger . warn ( u"<STR_LIT>" . format ( <EOL> name = one_mapping_google_civic_name <EOL> ) ) <EOL> continue <EOL> candidate_campaign_on_stage = results [ '<STR_LIT>' ] <EOL> if not value_exists ( candidate_campaign_on_stage . candidate_name ) : <EOL> continue <EOL> logger . debug ( u"<STR_LIT>" . format ( <EOL> name = candidate_campaign_on_stage . candidate_name <EOL> ) ) <EOL> try : <EOL> if not found_by_id : <EOL> candidate_campaign_on_stage . id_maplight = one_candidate_from_maplight_table . candidate_id <EOL> candidate_campaign_on_stage . photo_url_from_maplight = one_candidate_from_maplight_table . photo <EOL> candidate_campaign_on_stage . save ( ) <EOL> except Exception as e : <EOL> handle_record_not_saved_exception ( e , logger = logger ) <EOL> messages . add_message ( request , messages . INFO , '<STR_LIT>' ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , args = ( ) ) ) </s>
