def toggle_voter_following_organization(self, voter_id, organization_id, following_status): # Does a follow_organization entry exist from this voter already exist? follow_organization_manager = FollowOrganizationManager() results = follow_organization_manager.retrieve_follow_organization(0, voter_id, organization_id) follow_organization_on_stage_found = False follow_organization_on_stage_id = 0 follow_organization_on_stage = FollowOrganization() if results['follow_organization_found']: follow_organization_on_stage = results['follow_organization'] # Update this follow_organization entry with new values - we do not delete because we might be able to use try: follow_organization_on_stage.following_status = following_status # We don't need to update here because set set auto_now=True in the field # follow_organization_on_stage.date_last_changed = follow_organization_on_stage.save() follow_organization_on_stage_id = follow_organization_on_stage.id follow_organization_on_stage_found = True status = 'UPDATE ' + following_status except Exception as e: status = 'FAILED_TO_UPDATE ' + following_status handle_record_not_saved_exception(e, logger=logger, exception_message_optional=status) elif results['MultipleObjectsReturned']: logger.warn("follow_organization: delete all but one and take it over?") status = 'TOGGLE_FOLLOWING MultipleObjectsReturned ' + following_status elif results['DoesNotExist']: try: # Create new follow_organization entry # First make sure that organization_id is for a valid organization organization_manager = OrganizationManager() results = organization_manager.retrieve_organization(organization_id) if results['organization_found']: follow_organization_on_stage = FollowOrganization( voter_id=voter_id, organization_id=organization_id, following_status=following_status, # We don't need to update here because set set auto_now=True in the field # date_last_changed = ) follow_organization_on_stage.save() follow_organization_on_stage_id = follow_organization_on_stage.id follow_organization_on_stage_found = True status = 'CREATE ' + following_status else: status = 'ORGANIZATION_NOT_FOUND_ON_CREATE ' + following_status except Exception as e: status = 'FAILED_TO_UPDATE ' + following_status handle_record_not_saved_exception(e, logger=logger, exception_message_optional=status) else: status = results['status'] results = { 'success': True if follow_organization_on_stage_found else False, 'status': status, 'follow_organization_found': follow_organization_on_stage_found, 'follow_organization_id': follow_organization_on_stage_id, 'follow_organization': follow_organization_on_stage, } return results
def scrape_and_save_social_media_from_all_organizations(state_code=''): facebook_pages_found = 0 twitter_handles_found = 0 force_retrieve = False temp_count = 0 organization_manager = OrganizationManager() organization_list_query = Organization.objects.order_by('organization_name') if positive_value_exists(state_code): organization_list_query = organization_list_query.filter(state_served_code=state_code) organization_list = organization_list_query for organization in organization_list: twitter_handle = False facebook_page = False if not organization.organization_website: continue if (not positive_value_exists(organization.organization_twitter_handle)) or force_retrieve: scrape_results = scrape_social_media_from_one_site(organization.organization_website) # Only include a change if we have a new value (do not try to save blank value) if scrape_results['twitter_handle_found'] and positive_value_exists(scrape_results['twitter_handle']): twitter_handle = scrape_results['twitter_handle'] twitter_handles_found += 1 if scrape_results['facebook_page_found'] and positive_value_exists(scrape_results['facebook_page']): facebook_page = scrape_results['facebook_page'] facebook_pages_found += 1 save_results = organization_manager.update_organization_social_media(organization, twitter_handle, facebook_page) if save_results['success']: organization = save_results['organization'] # ###################################### # If we have a Twitter handle for this org, refresh the data if organization.organization_twitter_handle: results = retrieve_twitter_user_info(organization.organization_twitter_handle) if results['success']: save_results = organization_manager.update_organization_twitter_details( organization, results['twitter_json']) if save_results['success']: results = update_social_media_statistics_in_other_tables(organization) # ###################################### # temp_count += 1 # if temp_count > 10: # break status = "ORGANIZATION_SOCIAL_MEDIA_RETRIEVED" results = { 'success': True, 'status': status, 'twitter_handles_found': twitter_handles_found, 'facebook_pages_found': facebook_pages_found, } return results
def scrape_website_for_social_media_view(request, organization_id, force_retrieve=False): authority_required = {'admin'} # admin, verified_volunteer if not voter_has_authority(request, authority_required): return redirect_to_sign_in_page(request, authority_required) facebook_page = False twitter_handle = False organization_manager = OrganizationManager() results = organization_manager.retrieve_organization(organization_id) if not results['organization_found']: messages.add_message(request, messages.INFO, results['status']) return HttpResponseRedirect(reverse('organization:organization_edit', args=(organization_id,))) organization = results['organization'] if not organization.organization_website: messages.add_message(request, messages.ERROR, "No organizational website found.") return HttpResponseRedirect(reverse('organization:organization_position_list', args=(organization_id,))) if (not positive_value_exists(organization.organization_twitter_handle)) or \ (not positive_value_exists(organization.organization_facebook)) or force_retrieve: scrape_results = scrape_social_media_from_one_site(organization.organization_website) if scrape_results['twitter_handle_found']: twitter_handle = scrape_results['twitter_handle'] messages.add_message(request, messages.INFO, "Twitter handle found: " + twitter_handle) else: messages.add_message(request, messages.INFO, "No Twitter handle found: " + scrape_results['status']) if scrape_results['facebook_page_found']: facebook_page = scrape_results['facebook_page'] messages.add_message(request, messages.INFO, "Facebook page found: " + facebook_page) save_results = organization_manager.update_organization_social_media(organization, twitter_handle, facebook_page) if save_results['success']: organization = save_results['organization'] else: organization.organization_twitter_handle = twitter_handle # Store it temporarily # ###################################### if organization.organization_twitter_handle: results = retrieve_twitter_user_info(organization.organization_twitter_handle) if results['success']: save_results = organization_manager.update_organization_twitter_details( organization, results['twitter_json']) if save_results['success']: organization = save_results['organization'] results = update_social_media_statistics_in_other_tables(organization) # ###################################### return HttpResponseRedirect(reverse('organization:organization_position_list', args=(organization_id,)))
def voter_guide_image_url(self): if self.image_url: return self.image_url elif self.voter_guide_owner_type == ORGANIZATION: organization_manager = OrganizationManager() organization_id = 0 organization_we_vote_id = self.organization_we_vote_id results = organization_manager.retrieve_organization(organization_id, organization_we_vote_id) if results["organization_found"]: organization = results["organization"] organization_photo_url = organization.organization_photo_url() return organization_photo_url return ""
def voter_guide_display_name(self): if self.display_name: return self.display_name elif self.voter_guide_owner_type == ORGANIZATION: organization_manager = OrganizationManager() organization_id = 0 organization_we_vote_id = self.organization_we_vote_id results = organization_manager.retrieve_organization(organization_id, organization_we_vote_id) if results["organization_found"]: organization = results["organization"] organization_name = organization.organization_name return organization_name return ""
def toggle_voter_following_organization(self, voter_id, organization_id, following_status): # Does a follow_organization entry exist from this voter already exist? follow_organization_manager = FollowOrganizationManager() results = follow_organization_manager.retrieve_follow_organization(0, voter_id, organization_id) follow_organization_on_stage_found = False follow_organization_on_stage_id = 0 follow_organization_on_stage = FollowOrganization() if results['follow_organization_found']: follow_organization_on_stage = results['follow_organization'] # Update this follow_organization entry with new values - we do not delete because we might be able to use try: follow_organization_on_stage.following_status = following_status # We don't need to update here because set set auto_now=True in the field # follow_organization_on_stage.date_last_changed = follow_organization_on_stage.save() follow_organization_on_stage_id = follow_organization_on_stage.id follow_organization_on_stage_found = True except Exception as e: handle_record_not_saved_exception(e) elif results['MultipleObjectsReturned']: print "follow_organization: delete all but one and take it over?" elif results['DoesNotExist']: try: # Create new follow_organization entry # First make sure that organization_id is for a valid organization organization_manager = OrganizationManager() results = organization_manager.retrieve_organization(organization_id) if results['organization_found']: follow_organization_on_stage = FollowOrganization( voter_id=voter_id, organization_id=organization_id, following_status=following_status, # We don't need to update here because set set auto_now=True in the field # date_last_changed = ) follow_organization_on_stage.save() follow_organization_on_stage_id = follow_organization_on_stage.id follow_organization_on_stage_found = True except Exception as e: handle_record_not_saved_exception(e) results = { 'success': True if follow_organization_on_stage_found else False, 'follow_organization_found': follow_organization_on_stage_found, 'follow_organization_id': follow_organization_on_stage_id, 'follow_organization': follow_organization_on_stage, } return results
def toggle_voter_following_organization(self, voter_id, organization_id, following_status): # Does a follow_organization entry exist from this voter already exist? follow_organization_manager = FollowOrganizationManager() results = follow_organization_manager.retrieve_follow_organization(0, voter_id, organization_id) follow_organization_on_stage_found = False follow_organization_on_stage_id = 0 follow_organization_on_stage = FollowOrganization() if results['follow_organization_found']: follow_organization_on_stage = results['follow_organization'] # Update this follow_organization entry with new values - we do not delete because we might be able to use try: follow_organization_on_stage.following_status = following_status # We don't need to update here because set set auto_now=True in the field # follow_organization_on_stage.date_last_changed = follow_organization_on_stage.save() follow_organization_on_stage_id = follow_organization_on_stage.id follow_organization_on_stage_found = True except Exception as e: handle_record_not_saved_exception(e, logger=logger) elif results['MultipleObjectsReturned']: logger.warn("follow_organization: delete all but one and take it over?") elif results['DoesNotExist']: try: # Create new follow_organization entry # First make sure that organization_id is for a valid organization organization_manager = OrganizationManager() results = organization_manager.retrieve_organization(organization_id) if results['organization_found']: follow_organization_on_stage = FollowOrganization( voter_id=voter_id, organization_id=organization_id, following_status=following_status, # We don't need to update here because set set auto_now=True in the field # date_last_changed = ) follow_organization_on_stage.save() follow_organization_on_stage_id = follow_organization_on_stage.id follow_organization_on_stage_found = True except Exception as e: handle_record_not_saved_exception(e, logger=logger) results = { 'success': True if follow_organization_on_stage_found else False, 'follow_organization_found': follow_organization_on_stage_found, 'follow_organization_id': follow_organization_on_stage_id, 'follow_organization': follow_organization_on_stage, } return results
def scrape_and_save_social_media_from_all_organizations( state_code='', force_retrieve=False): facebook_pages_found = 0 twitter_handles_found = 0 organization_manager = OrganizationManager() organization_list_query = Organization.objects.order_by( 'organization_name') if positive_value_exists(state_code): organization_list_query = organization_list_query.filter( state_served_code=state_code) organization_list = organization_list_query for organization in organization_list: twitter_handle = False facebook_page = False if not organization.organization_website: continue if (not positive_value_exists( organization.organization_twitter_handle)) or force_retrieve: scrape_results = scrape_social_media_from_one_site( organization.organization_website) # Only include a change if we have a new value (do not try to save blank value) if scrape_results['twitter_handle_found'] and positive_value_exists( scrape_results['twitter_handle']): twitter_handle = scrape_results['twitter_handle'] twitter_handles_found += 1 if scrape_results['facebook_page_found'] and positive_value_exists( scrape_results['facebook_page']): facebook_page = scrape_results['facebook_page'] facebook_pages_found += 1 save_results = organization_manager.update_organization_social_media( organization, twitter_handle, facebook_page) # ###################################### # We refresh the Twitter information in another function status = "ORGANIZATION_SOCIAL_MEDIA_SCRAPED" results = { 'success': True, 'status': status, 'twitter_handles_found': twitter_handles_found, 'facebook_pages_found': facebook_pages_found, } return results
def organization_edit_view(request, organization_id): authority_required = {'verified_volunteer'} # admin, verified_volunteer if not voter_has_authority(request, authority_required): return redirect_to_sign_in_page(request, authority_required) # A positive value in google_civic_election_id means we want to create a voter guide for this org for this election google_civic_election_id = request.GET.get('google_civic_election_id', 0) messages_on_stage = get_messages(request) organization_id = convert_to_int(organization_id) organization_on_stage_found = False organization_manager = OrganizationManager() organization_on_stage = Organization() state_served_code = '' results = organization_manager.retrieve_organization(organization_id) if results['organization_found']: organization_on_stage = results['organization'] state_served_code = organization_on_stage.state_served_code organization_on_stage_found = True election_manager = ElectionManager() upcoming_election_list = [] results = election_manager.retrieve_upcoming_elections() if results['success']: upcoming_election_list = results['election_list'] state_list = STATE_CODE_MAP sorted_state_list = sorted(state_list.items()) if organization_on_stage_found: template_values = { 'messages_on_stage': messages_on_stage, 'organization': organization_on_stage, 'upcoming_election_list': upcoming_election_list, 'google_civic_election_id': google_civic_election_id, 'state_list': sorted_state_list, 'state_served_code': state_served_code, } else: template_values = { 'messages_on_stage': messages_on_stage, 'upcoming_election_list': upcoming_election_list, 'google_civic_election_id': google_civic_election_id, 'state_list': sorted_state_list, } return render(request, 'organization/organization_edit.html', template_values)
def refresh_twitter_details_view(request, organization_id): authority_required = {'verified_volunteer'} # admin, verified_volunteer if not voter_has_authority(request, authority_required): return redirect_to_sign_in_page(request, authority_required) organization_manager = OrganizationManager() results = organization_manager.retrieve_organization(organization_id) if not results['organization_found']: messages.add_message(request, messages.INFO, results['status']) return HttpResponseRedirect(reverse('organization:organization_edit', args=(organization_id,))) organization = results['organization'] results = refresh_twitter_details(organization) return HttpResponseRedirect(reverse('organization:organization_position_list', args=(organization_id,)))
def import_organization_logo_from_wikipedia_view(request, organization_id): organization_manager = OrganizationManager() results = organization_manager.retrieve_organization(organization_id) if not results['organization_found']: messages.add_message(request, messages.INFO, results['status']) return HttpResponseRedirect(reverse('organization:organization_edit', args=(organization_id,))) organization = results['organization'] results = retrieve_organization_logo_from_wikipedia(organization) if not results['success']: messages.add_message(request, messages.INFO, results['status']) else: messages.add_message(request, messages.INFO, "Wikipedia information retrieved.") return HttpResponseRedirect(reverse('organization:organization_position_list', args=(organization_id,)))
def import_organization_logo_from_wikipedia_view(request, organization_id): authority_required = {'admin'} # admin, verified_volunteer if not voter_has_authority(request, authority_required): return redirect_to_sign_in_page(request, authority_required) logo_found = False organization_manager = OrganizationManager() results = organization_manager.retrieve_organization(organization_id) if not results['organization_found']: messages.add_message(request, messages.INFO, results['status']) return HttpResponseRedirect(reverse('organization:organization_edit', args=(organization_id,))) organization = results['organization'] # When looking up logos one at a time, we want to force a retrieve force_retrieve = True organization_results = retrieve_wikipedia_page_from_wikipedia(organization, force_retrieve) if organization_results['wikipedia_page_found']: wikipedia_page = organization_results['wikipedia_page'] logo_results = retrieve_organization_logo_from_wikipedia_page(organization, wikipedia_page, force_retrieve) if logo_results['logo_found']: logo_found = True if positive_value_exists(force_retrieve): if 'image_options' in logo_results: for one_image in logo_results['image_options']: link_to_image = "<a href='{one_image}' target='_blank'>{one_image}</a>".format(one_image=one_image) messages.add_message(request, messages.INFO, link_to_image) if not logo_results['success']: messages.add_message(request, messages.ERROR, logo_results['status']) else: messages.add_message(request, messages.ERROR, "Wikipedia page not found. " + organization_results['status']) if logo_found: messages.add_message(request, messages.INFO, "Wikipedia logo retrieved.") else: messages.add_message(request, messages.ERROR, "Wikipedia logo not retrieved.") return HttpResponseRedirect(reverse('organization:organization_position_list', args=(organization_id,)))
def retrieve_twitter_data_for_all_organizations(state_code='', google_civic_election_id=0, first_retrieve_only=False): number_of_twitter_accounts_queried = 0 number_of_organizations_updated = 0 organization_manager = OrganizationManager() organization_list_query = Organization.objects.order_by('organization_name') if positive_value_exists(state_code): organization_list_query = organization_list_query.filter(state_served_code=state_code) # TODO DALE limit this to organizations that have a voter guide in a particular election organization_list = organization_list_query for organization in organization_list: # ###################################### # If we have a Twitter handle for this org, refresh the data if organization.organization_twitter_handle: retrieved_twitter_data = False if first_retrieve_only: if not positive_value_exists(organization.twitter_followers_count): results = retrieve_twitter_user_info(organization.organization_twitter_handle) retrieved_twitter_data = results['success'] number_of_twitter_accounts_queried += 1 else: results = retrieve_twitter_user_info(organization.organization_twitter_handle) retrieved_twitter_data = results['success'] number_of_twitter_accounts_queried += 1 if retrieved_twitter_data: number_of_organizations_updated += 1 save_results = organization_manager.update_organization_twitter_details( organization, results['twitter_json']) if save_results['success']: results = update_social_media_statistics_in_other_tables(organization) status = "ALL_ORGANIZATION_TWITTER_DATA_RETRIEVED" results = { 'success': True, 'status': status, 'number_of_twitter_accounts_queried': number_of_twitter_accounts_queried, 'number_of_organizations_updated': number_of_organizations_updated, } return results
def scrape_and_save_social_media_from_all_organizations(state_code='', force_retrieve=False): facebook_pages_found = 0 twitter_handles_found = 0 organization_manager = OrganizationManager() organization_list_query = Organization.objects.order_by('organization_name') if positive_value_exists(state_code): organization_list_query = organization_list_query.filter(state_served_code=state_code) organization_list = organization_list_query for organization in organization_list: twitter_handle = False facebook_page = False if not organization.organization_website: continue if (not positive_value_exists(organization.organization_twitter_handle)) or force_retrieve: scrape_results = scrape_social_media_from_one_site(organization.organization_website) # Only include a change if we have a new value (do not try to save blank value) if scrape_results['twitter_handle_found'] and positive_value_exists(scrape_results['twitter_handle']): twitter_handle = scrape_results['twitter_handle'] twitter_handles_found += 1 if scrape_results['facebook_page_found'] and positive_value_exists(scrape_results['facebook_page']): facebook_page = scrape_results['facebook_page'] facebook_pages_found += 1 save_results = organization_manager.update_organization_social_media(organization, twitter_handle, facebook_page) # ###################################### # We refresh the Twitter information in another function status = "ORGANIZATION_SOCIAL_MEDIA_SCRAPED" results = { 'success': True, 'status': status, 'twitter_handles_found': twitter_handles_found, 'facebook_pages_found': facebook_pages_found, } return results
def refresh_twitter_organization_details_view(request, organization_id): authority_required = {'verified_volunteer'} # admin, verified_volunteer if not voter_has_authority(request, authority_required): return redirect_to_sign_in_page(request, authority_required) organization_manager = OrganizationManager() results = organization_manager.retrieve_organization(organization_id) if not results['organization_found']: messages.add_message(request, messages.INFO, results['status']) return HttpResponseRedirect( reverse('organization:organization_edit', args=(organization_id, ))) organization = results['organization'] results = refresh_twitter_organization_details(organization) return HttpResponseRedirect( reverse('organization:organization_position_list', args=(organization_id, )))
def refresh_twitter_organization_details(organization): organization_manager = OrganizationManager() if not organization: status = "ORGANIZATION_TWITTER_DETAILS_NOT_RETRIEVED-ORG_MISSING" results = { 'success': False, 'status': status, } return results if organization.organization_twitter_handle: status = "ORGANIZATION_TWITTER_DETAILS-REACHING_OUT_TO_TWITTER" results = retrieve_twitter_user_info( organization.organization_twitter_handle) if results['success']: status = "ORGANIZATION_TWITTER_DETAILS_RETRIEVED_FROM_TWITTER" save_results = organization_manager.update_organization_twitter_details( organization, results['twitter_json']) if save_results['success']: results = update_social_media_statistics_in_other_tables( organization) status = "ORGANIZATION_TWITTER_DETAILS_RETRIEVED_FROM_TWITTER_AND_SAVED" else: status = "ORGANIZATION_TWITTER_DETAILS-CLEARING_DETAILS" save_results = organization_manager.clear_organization_twitter_details( organization) if save_results['success']: results = update_social_media_statistics_in_other_tables( organization) status = "ORGANIZATION_TWITTER_DETAILS_CLEARED_FROM_DB" results = { 'success': True, 'status': status, } return results
def organization_retrieve(organization_id, we_vote_id): organization_id = convert_to_int(organization_id) we_vote_id = we_vote_id.strip() if not positive_value_exists(organization_id) and not positive_value_exists(we_vote_id): json_data = { 'status': "ORGANIZATION_RETRIEVE_BOTH_IDS_MISSING", 'success': False, 'organization_id': organization_id, 'we_vote_id': we_vote_id, } return HttpResponse(json.dumps(json_data), content_type='application/json') organization_manager = OrganizationManager() results = organization_manager.retrieve_organization(organization_id, we_vote_id) if results['organization_found']: organization = results['organization'] json_data = { 'organization_id': organization.id, 'we_vote_id': organization.we_vote_id, 'organization_name': organization.organization_name if positive_value_exists(organization.organization_name) else '', 'organization_website': organization.organization_website if positive_value_exists( organization.organization_website) else '', 'organization_twitter': organization.twitter_handle if positive_value_exists(organization.twitter_handle) else '', 'success': True, 'status': results['status'], } return HttpResponse(json.dumps(json_data), content_type='application/json') else: json_data = { 'status': results['status'], 'success': False, 'organization_id': organization_id, 'we_vote_id': we_vote_id, } return HttpResponse(json.dumps(json_data), content_type='application/json')
def refresh_twitter_details(organization): organization_manager = OrganizationManager() status = "ENTERING_REFRESH_TWITTER_DETAILS" if not organization: status = "ORGANIZATION_TWITTER_DETAILS_NOT_RETRIEVED-ORG_MISSING" results = { 'success': False, 'status': status, } return results if organization.organization_twitter_handle: status = "ORGANIZATION_TWITTER_DETAILS-REACHING_OUT_TO_TWITTER" results = retrieve_twitter_user_info(organization.organization_twitter_handle) if results['success']: status = "ORGANIZATION_TWITTER_DETAILS_RETRIEVED_FROM_TWITTER" save_results = organization_manager.update_organization_twitter_details( organization, results['twitter_json']) if save_results['success']: results = update_social_media_statistics_in_other_tables(organization) status = "ORGANIZATION_TWITTER_DETAILS_RETRIEVED_FROM_TWITTER_AND_SAVED" else: status = "ORGANIZATION_TWITTER_DETAILS-CLEARING_DETAILS" save_results = organization_manager.clear_organization_twitter_details(organization) if save_results['success']: results = update_social_media_statistics_in_other_tables(organization) status = "ORGANIZATION_TWITTER_DETAILS_CLEARED_FROM_DB" results = { 'success': True, 'status': status, } return results
def voter_guides_import_from_structured_json(structured_json): """ This pathway in requires a we_vote_id, and is not used when we import from Google Civic :param structured_json: :return: """ voter_guide_manager = VoterGuideManager() organization_manager = OrganizationManager() organization_id = 0 voter_guides_saved = 0 voter_guides_updated = 0 voter_guides_not_processed = 0 for one_voter_guide in structured_json: we_vote_id = one_voter_guide['we_vote_id'] if 'we_vote_id' in one_voter_guide else '' google_civic_election_id = one_voter_guide['google_civic_election_id'] \ if 'google_civic_election_id' in one_voter_guide else '' vote_smart_time_span = one_voter_guide['vote_smart_time_span'] \ if 'vote_smart_time_span' in one_voter_guide else '' organization_we_vote_id = one_voter_guide['organization_we_vote_id'] \ if 'organization_we_vote_id' in one_voter_guide else '' public_figure_we_vote_id = one_voter_guide['public_figure_we_vote_id'] \ if 'public_figure_we_vote_id' in one_voter_guide else '' if positive_value_exists(we_vote_id) and \ (positive_value_exists(organization_we_vote_id) or positive_value_exists(public_figure_we_vote_id)) and \ (positive_value_exists(google_civic_election_id) or positive_value_exists(vote_smart_time_span)): # Make sure we have the organization (or public figure) in this database before we import the voter guide if positive_value_exists(organization_we_vote_id): results = organization_manager.retrieve_organization_from_we_vote_id(organization_we_vote_id) if results['organization_found']: organization_id = results['organization_id'] if positive_value_exists(organization_id): proceed_to_update_or_create = True else: proceed_to_update_or_create = False elif positive_value_exists(public_figure_we_vote_id): # TODO DALE Update this to work with public_figure public_figure_id = organization_manager.retrieve_organization_from_we_vote_id(public_figure_we_vote_id) if positive_value_exists(public_figure_id): proceed_to_update_or_create = True else: proceed_to_update_or_create = False else: proceed_to_update_or_create = False else: proceed_to_update_or_create = False if proceed_to_update_or_create: if positive_value_exists(organization_we_vote_id) and positive_value_exists(google_civic_election_id): results = voter_guide_manager.update_or_create_organization_voter_guide_by_election_id( organization_we_vote_id, google_civic_election_id) elif positive_value_exists(organization_we_vote_id) and positive_value_exists(vote_smart_time_span): results = voter_guide_manager.update_or_create_organization_voter_guide_by_time_span( organization_we_vote_id, vote_smart_time_span) elif positive_value_exists(public_figure_we_vote_id) and positive_value_exists(google_civic_election_id): results = voter_guide_manager.update_or_create_public_figure_voter_guide( google_civic_election_id, public_figure_we_vote_id) else: results = { 'success': False, 'status': 'Required value missing, cannot update or create (1)' } else: voter_guides_not_processed += 1 results = { 'success': False, 'status': 'Required value missing, cannot update or create (2)' } if results['success']: if results['new_voter_guide_created']: voter_guides_saved += 1 else: voter_guides_updated += 1 else: voter_guides_not_processed += 1 voter_guides_results = { 'success': True, 'status': "VOTER_GUIDES_IMPORT_PROCESS_COMPLETE", 'saved': voter_guides_saved, 'updated': voter_guides_updated, 'not_processed': voter_guides_not_processed, } return voter_guides_results
def positions_import_from_sample_file(request=None, load_from_uri=False): """ Get the json data, and either create new entries or update existing :return: """ # if load_from_uri: # # Request json file from We Vote servers # messages.add_message(request, messages.INFO, "Loading positions from We Vote Master servers") # request = requests.get(POSITIONS_URL, params={ # "key": WE_VOTE_API_KEY, # This comes from an environment variable # }) # structured_json = json.loads(request.text) # else: # Load saved json from local file with open("position/import_data/positions_sample.json") as json_data: structured_json = json.load(json_data) positions_saved = 0 positions_updated = 0 positions_not_processed = 0 for one_position in structured_json: # Make sure we have the minimum required variables if not positive_value_exists(one_position["we_vote_id"]) \ or not positive_value_exists(one_position["organization_we_vote_id"])\ or not positive_value_exists(one_position["candidate_campaign_we_vote_id"]): positions_not_processed += 1 continue # Check to see if this position is already being used anywhere position_on_stage_found = False try: if len(one_position["we_vote_id"]) > 0: position_query = PositionEntered.objects.filter(we_vote_id=one_position["we_vote_id"]) if len(position_query): position_on_stage = position_query[0] position_on_stage_found = True except PositionEntered.DoesNotExist as e: handle_record_not_found_exception(e, logger=logger) pass except Exception as e: handle_record_not_found_exception(e, logger=logger) # We need to look up the local organization_id based on the newly saved we_vote_id organization_manager = OrganizationManager() organization_id = organization_manager.fetch_organization_id(one_position["organization_we_vote_id"]) # We need to look up the local candidate_campaign_id candidate_campaign_manager = CandidateCampaignManager() candidate_campaign_id = candidate_campaign_manager.fetch_candidate_campaign_id_from_we_vote_id( one_position["candidate_campaign_we_vote_id"]) # Find the google_civic_candidate_name so we have a backup way to link position if the we_vote_id is lost google_civic_candidate_name = one_position["google_civic_candidate_name"] if \ "google_civic_candidate_name" in one_position else '' if not positive_value_exists(google_civic_candidate_name): google_civic_candidate_name = candidate_campaign_manager.fetch_google_civic_candidate_name_from_we_vote_id( one_position["candidate_campaign_we_vote_id"]) # TODO We need to look up measure_campaign_id measure_campaign_id = 0 try: if position_on_stage_found: # Update position_on_stage.we_vote_id = one_position["we_vote_id"] position_on_stage.organization_id = organization_id position_on_stage.organization_we_vote_id = one_position["organization_we_vote_id"] position_on_stage.candidate_campaign_id = candidate_campaign_id position_on_stage.candidate_campaign_we_vote_id = one_position["candidate_campaign_we_vote_id"] position_on_stage.google_civic_candidate_name = google_civic_candidate_name position_on_stage.measure_campaign_id = measure_campaign_id position_on_stage.date_entered = one_position["date_entered"] position_on_stage.google_civic_election_id = one_position["google_civic_election_id"] position_on_stage.stance = one_position["stance"] position_on_stage.more_info_url = one_position["more_info_url"] position_on_stage.statement_text = one_position["statement_text"] position_on_stage.statement_html = one_position["statement_html"] position_on_stage.save() positions_updated += 1 # messages.add_message(request, messages.INFO, u"Position updated: {we_vote_id}".format( # we_vote_id=one_position["we_vote_id"])) else: # Create new position_on_stage = PositionEntered( we_vote_id=one_position["we_vote_id"], organization_id=organization_id, organization_we_vote_id=one_position["organization_we_vote_id"], candidate_campaign_id=candidate_campaign_id, candidate_campaign_we_vote_id=one_position["candidate_campaign_we_vote_id"], google_civic_candidate_name=google_civic_candidate_name, measure_campaign_id=measure_campaign_id, date_entered=one_position["date_entered"], google_civic_election_id=one_position["google_civic_election_id"], stance=one_position["stance"], more_info_url=one_position["more_info_url"], statement_text=one_position["statement_text"], statement_html=one_position["statement_html"], ) position_on_stage.save() positions_saved += 1 # messages.add_message(request, messages.INFO, u"New position imported: {we_vote_id}".format( # we_vote_id=one_position["we_vote_id"])) except Exception as e: handle_record_not_saved_exception(e, logger=logger) if request is not None: messages.add_message(request, messages.ERROR, u"Could not save/update position, " u"position_on_stage_found: {position_on_stage_found}, " u"we_vote_id: {we_vote_id}, " u"organization_we_vote_id: {organization_we_vote_id}, " u"candidate_campaign_we_vote_id: {candidate_campaign_we_vote_id}".format( position_on_stage_found=position_on_stage_found, we_vote_id=one_position["we_vote_id"], organization_we_vote_id=one_position["organization_we_vote_id"], candidate_campaign_we_vote_id=one_position["candidate_campaign_we_vote_id"], )) positions_not_processed += 1 positions_results = { 'saved': positions_saved, 'updated': positions_updated, 'not_processed': positions_not_processed, } return positions_results
def update_or_create_we_vote_organization(self, vote_smart_special_interest_group_id): # See if we can find an existing We Vote organization with vote_smart_special_interest_group_id if not positive_value_exists(vote_smart_special_interest_group_id): results = { 'success': False, 'status': "SPECIAL_INTEREST_GROUP_ID_MISSING", 'organization_found': False, 'organization': Organization(), } return results # Retrieve Special Interest Group try: vote_smart_organization = VoteSmartSpecialInterestGroup.objects.get( sigId=vote_smart_special_interest_group_id) vote_smart_organization_found = True except VoteSmartSpecialInterestGroup.MultipleObjectsReturned as e: vote_smart_organization_found = False except VoteSmartSpecialInterestGroup.DoesNotExist as e: # An organization matching this Vote Smart ID wasn't found vote_smart_organization_found = False if not vote_smart_organization_found: results = { 'success': False, 'status': "SPECIAL_INTEREST_GROUP_MISSING", 'organization_found': False, 'organization': Organization(), } return results we_vote_organization_manager = OrganizationManager() organization_id = 0 organization_we_vote_id = None we_vote_organization_found = False results = we_vote_organization_manager.retrieve_organization(organization_id, organization_we_vote_id, vote_smart_special_interest_group_id) if results['organization_found']: success = True status = "NOT UPDATING RIGHT NOW" we_vote_organization_found = True we_vote_organization = results['organization'] # Update existing organization entry else: # Create new organization, or find existing org via other fields try: defaults_from_vote_smart = { 'organization_name': vote_smart_organization.name, 'organization_address': vote_smart_organization.address, 'organization_city': vote_smart_organization.city, 'organization_state': vote_smart_organization.state, 'organization_zip': vote_smart_organization.zip, 'organization_phone1': vote_smart_organization.phone1, 'organization_phone2': vote_smart_organization.phone2, 'organization_fax': vote_smart_organization.fax, 'organization_email': vote_smart_organization.email, 'organization_website': vote_smart_organization.url, 'organization_contact_name': vote_smart_organization.contactName, 'organization_description': vote_smart_organization.description, 'state_served_code': vote_smart_organization.stateId, 'vote_smart_id': vote_smart_organization.sigId, } we_vote_organization, created = Organization.objects.update_or_create( organization_name=vote_smart_organization.name, # organization_website=vote_smart_organization.url, # organization_email=vote_smart_organization.email, defaults=defaults_from_vote_smart, ) success = True status = "UPDATE_OR_CREATE_ORGANIZATION_FROM_VOTE_SMART" we_vote_organization_found = True except Organization.MultipleObjectsReturned as e: success = False status = "UPDATE_OR_CREATE_ORGANIZATION_FROM_VOTE_SMART_MULTIPLE_FOUND" we_vote_organization = Organization() except Exception as error_instance: error_message = error_instance.args status = "UPDATE_OR_CREATE_ORGANIZATION_FROM_VOTE_SMART_FAILED: " \ "{error_message}".format(error_message=error_message) success = False we_vote_organization = Organization() results = { 'success': success, 'status': status, 'organization_found': we_vote_organization_found, 'organization': we_vote_organization, } return results
def scrape_and_save_social_media_from_all_organizations(state_code=''): facebook_pages_found = 0 twitter_handles_found = 0 force_retrieve = False temp_count = 0 organization_manager = OrganizationManager() organization_list_query = Organization.objects.order_by( 'organization_name') if positive_value_exists(state_code): organization_list_query = organization_list_query.filter( state_served_code=state_code) organization_list = organization_list_query for organization in organization_list: twitter_handle = False facebook_page = False if not organization.organization_website: continue if (not positive_value_exists( organization.organization_twitter_handle)) or force_retrieve: scrape_results = scrape_social_media_from_one_site( organization.organization_website) # Only include a change if we have a new value (do not try to save blank value) if scrape_results['twitter_handle_found'] and positive_value_exists( scrape_results['twitter_handle']): twitter_handle = scrape_results['twitter_handle'] twitter_handles_found += 1 if scrape_results['facebook_page_found'] and positive_value_exists( scrape_results['facebook_page']): facebook_page = scrape_results['facebook_page'] facebook_pages_found += 1 save_results = organization_manager.update_organization_social_media( organization, twitter_handle, facebook_page) if save_results['success']: organization = save_results['organization'] # ###################################### # If we have a Twitter handle for this org, refresh the data if organization.organization_twitter_handle: results = retrieve_twitter_user_info( organization.organization_twitter_handle) if results['success']: save_results = organization_manager.update_organization_twitter_details( organization, results['twitter_json']) if save_results['success']: results = update_social_media_statistics_in_other_tables( organization) # ###################################### # temp_count += 1 # if temp_count > 10: # break status = "ORGANIZATION_SOCIAL_MEDIA_RETRIEVED" results = { 'success': True, 'status': status, 'twitter_handles_found': twitter_handles_found, 'facebook_pages_found': facebook_pages_found, } return results
def toggle_voter_following_organization(self, voter_id, organization_id, organization_we_vote_id, following_status): # Does a follow_organization entry exist from this voter already exist? follow_organization_manager = FollowOrganizationManager() results = follow_organization_manager.retrieve_follow_organization( 0, voter_id, organization_id, organization_we_vote_id) follow_organization_on_stage_found = False follow_organization_on_stage_id = 0 follow_organization_on_stage = FollowOrganization() if results['follow_organization_found']: follow_organization_on_stage = results['follow_organization'] # Update this follow_organization entry with new values - we do not delete because we might be able to use try: follow_organization_on_stage.following_status = following_status # We don't need to update here because set set auto_now=True in the field # follow_organization_on_stage.date_last_changed = follow_organization_on_stage.save() follow_organization_on_stage_id = follow_organization_on_stage.id follow_organization_on_stage_found = True status = 'UPDATE ' + following_status except Exception as e: status = 'FAILED_TO_UPDATE ' + following_status handle_record_not_saved_exception( e, logger=logger, exception_message_optional=status) elif results['MultipleObjectsReturned']: logger.warn( "follow_organization: delete all but one and take it over?") status = 'TOGGLE_FOLLOWING MultipleObjectsReturned ' + following_status elif results['DoesNotExist']: try: # Create new follow_organization entry # First make sure that organization_id is for a valid organization organization_manager = OrganizationManager() if positive_value_exists(organization_id): results = organization_manager.retrieve_organization( organization_id) else: results = organization_manager.retrieve_organization( 0, organization_we_vote_id) if results['organization_found']: organization = results['organization'] follow_organization_on_stage = FollowOrganization( voter_id=voter_id, organization_id=organization.id, organization_we_vote_id=organization.we_vote_id, following_status=following_status, # We don't need to update here because set set auto_now=True in the field # date_last_changed = ) follow_organization_on_stage.save() follow_organization_on_stage_id = follow_organization_on_stage.id follow_organization_on_stage_found = True status = 'CREATE ' + following_status else: status = 'ORGANIZATION_NOT_FOUND_ON_CREATE ' + following_status except Exception as e: status = 'FAILED_TO_UPDATE ' + following_status handle_record_not_saved_exception( e, logger=logger, exception_message_optional=status) else: status = results['status'] results = { 'success': True if follow_organization_on_stage_found else False, 'status': status, 'follow_organization_found': follow_organization_on_stage_found, 'follow_organization_id': follow_organization_on_stage_id, 'follow_organization': follow_organization_on_stage, } return results
def import_we_vote_positions_from_json(request, load_from_uri=False): """ Get the json data, and either create new entries or update existing :return: """ if load_from_uri: # Request json file from We Vote servers messages.add_message(request, messages.INFO, "Loading positions from We Vote Master servers") request = requests.get( POSITIONS_URL, params={ "key": WE_VOTE_API_KEY, # This comes from an environment variable }) structured_json = json.loads(request.text) else: # Load saved json from local file messages.add_message(request, messages.INFO, "Loading positions from local file") with open(POSITIONS_JSON_FILE) as json_data: structured_json = json.load(json_data) for one_position in structured_json: # Make sure we have the minimum required variables if len(one_position["id_we_vote"]) == 0 \ or len(one_position["organization_id_we_vote"]) == 0\ or len(one_position["candidate_campaign_id_we_vote"]) == 0: continue # Check to see if this position is already being used anywhere position_on_stage_found = False try: if len(one_position["id_we_vote"]) > 0: position_query = PositionEntered.objects.filter( id_we_vote=one_position["id_we_vote"]) if len(position_query): position_on_stage = position_query[0] position_on_stage_found = True except PositionEntered.DoesNotExist as e: pass except Exception as e: handle_record_not_found_exception(e, logger=logger) # We need to look up the local organization_id based on the newly saved we_vote_id organization_manager = OrganizationManager() organization_id = organization_manager.fetch_organization_id( one_position["organization_id_we_vote"]) # We need to look up the local candidate_campaign_id candidate_campaign_manager = CandidateCampaignManager() candidate_campaign_id = candidate_campaign_manager.fetch_candidate_campaign_id_from_id_we_vote( one_position["candidate_campaign_id_we_vote"]) # TODO We need to look up measure_campaign_id measure_campaign_id = 0 try: if position_on_stage_found: # Update position_on_stage.id_we_vote = one_position["id_we_vote"] position_on_stage.organization_id = organization_id position_on_stage.candidate_campaign_id = candidate_campaign_id position_on_stage.measure_campaign_id = measure_campaign_id position_on_stage.date_entered = one_position["date_entered"] position_on_stage.election_id = one_position["election_id"] position_on_stage.stance = one_position["stance"] position_on_stage.more_info_url = one_position["more_info_url"] position_on_stage.statement_text = one_position[ "statement_text"] position_on_stage.statement_html = one_position[ "statement_html"] position_on_stage.save() messages.add_message( request, messages.INFO, u"Position updated: {id_we_vote}".format( id_we_vote=one_position["id_we_vote"])) else: # Create new position_on_stage = PositionEntered( id_we_vote=one_position["id_we_vote"], organization_id=organization_id, candidate_campaign_id=candidate_campaign_id, measure_campaign_id=measure_campaign_id, date_entered=one_position["date_entered"], election_id=one_position["election_id"], stance=one_position["stance"], more_info_url=one_position["more_info_url"], statement_text=one_position["statement_text"], statement_html=one_position["statement_html"], ) position_on_stage.save() messages.add_message( request, messages.INFO, u"New position imported: {id_we_vote}".format( id_we_vote=one_position["id_we_vote"])) except Exception as e: handle_record_not_saved_exception(e, logger=logger) messages.add_message( request, messages.ERROR, u"Could not save position, id_we_vote: {id_we_vote}, " u"organization_id_we_vote: {organization_id_we_vote}, " u"candidate_campaign_id_we_vote: {candidate_campaign_id_we_vote}" .format( id_we_vote=one_position["id_we_vote"], organization_id_we_vote=one_position[ "organization_id_we_vote"], candidate_campaign_id_we_vote=one_position[ "candidate_campaign_id_we_vote"], ))
def assemble_candidate_campaign_stance_html(candidate_campaign_id, stance_we_are_looking_for, positions_followed, positions_not_followed): """ :param all_positions_list_for_candidate_campaign: :param stance_we_are_looking_for: :param candidate_campaign_id: :return: """ ################################# # Start with positions_followed # Assemble some information that is independent of each position number_of_positions_followed_total = len(positions_followed) popup_box_title_verb = display_stance_we_are_looking_for_title( stance_we_are_looking_for, number_of_positions_followed_total) candidate_campaign_manager = CandidateCampaignManager() results = candidate_campaign_manager.retrieve_candidate_campaign_from_id( candidate_campaign_id) if results['candidate_campaign_found']: candidate_campaign = results['candidate_campaign'] popup_box_title_candidate_name = candidate_campaign.candidate_name else: popup_box_title_candidate_name = "" popup_box_title = popup_box_title_verb + " " + popup_box_title_candidate_name if stance_we_are_looking_for == SUPPORT: # This is the class we reference with jquery for opening a div popup to display the supporters class_used_to_open_popup = "candidate_campaign_" + candidate_campaign_id + "_supporters" # This is the URL that returns the supporters for this candidate retrieve_positions_url = "/pos/cand/" + candidate_campaign_id + "/supporters?f=1" # Only show orgs followed elif stance_we_are_looking_for == OPPOSE: class_used_to_open_popup = "candidate_campaign_" + candidate_campaign_id + "_opposers" retrieve_positions_url = "/pos/cand/" + candidate_campaign_id + "/opposers?f=1" elif stance_we_are_looking_for == INFORMATION_ONLY: class_used_to_open_popup = "candidate_campaign_" + candidate_campaign_id + "_infoonly" retrieve_positions_url = "/pos/cand/" + candidate_campaign_id + "/infoonlylist?f=1" elif stance_we_are_looking_for == STILL_DECIDING: class_used_to_open_popup = "candidate_campaign_" + candidate_campaign_id + "_deciders" retrieve_positions_url = "/pos/cand/" + candidate_campaign_id + "/deciders?f=1" else: class_used_to_open_popup = '' retrieve_positions_url = '' # Cycle through these positions and put together a line about who is supporting, opposing, have information # or are still deciding positions_followed_stance_html = "" is_first = True number_of_positions_followed_counter = 0 only_you = False for position in positions_followed: if is_first: positions_followed_stance_html += "" else: is_next_to_last = number_of_positions_followed_counter == number_of_positions_followed_total - 1 positions_followed_stance_html += " and " if is_next_to_last else ", " is_first = False if position.organization_id > 0: organization_manager = OrganizationManager() results = organization_manager.retrieve_organization( position.organization_id) if results['organization_found']: organization_on_stage = results['organization'] link_open = "<a class='{link_class}' href='{link_href}' id='{popup_box_title}'>".format( link_class=class_used_to_open_popup, link_href=retrieve_positions_url, popup_box_title=popup_box_title, ) positions_followed_stance_html += "{link_open}{organization_name}</a>".format( link_open=link_open, organization_name=organization_on_stage.name, ) number_of_positions_followed_counter += 1 elif position.voter_id > 0: positions_followed_stance_html += "You" number_of_positions_followed_counter += 1 if number_of_positions_followed_total == 1: only_you = True if number_of_positions_followed_total: verb_text = display_stance_we_are_looking_for( stance_we_are_looking_for, number_of_positions_followed_total, only_you) if verb_text: positions_followed_stance_html = "<span class='positions_followed_text'>" + positions_followed_stance_html positions_followed_stance_html += " <span class='position_stance_verb'>{verb_text}</span>".format( verb_text=verb_text) positions_followed_stance_html += "</span>" ################################# # NOT Followed ################################# # Now create string with html for positions_not_followed positions_not_followed_stance_html = "" number_of_positions_not_followed_total = len(positions_not_followed) # If there aren't any "not followed" positions, just return the positions_followed_stance_html if number_of_positions_not_followed_total == 0: return positions_followed_stance_html else: only_you = False # If here we know there is at least one position available that isnt' being followed by voter popup_box_title = popup_box_title_verb + " " + popup_box_title_candidate_name if stance_we_are_looking_for == SUPPORT: # This is the class we reference with jquery for opening a div popup to display the supporters class_used_to_open_popup = "candidate_campaign_" + candidate_campaign_id + "_supporters" # This is the URL that returns the supporters for this candidate retrieve_positions_url = "/pos/cand/" + candidate_campaign_id + "/supporters?nf=1" # Only show orgs not followed elif stance_we_are_looking_for == OPPOSE: class_used_to_open_popup = "candidate_campaign_" + candidate_campaign_id + "_opposers" retrieve_positions_url = "/pos/cand/" + candidate_campaign_id + "/opposers?nf=1" elif stance_we_are_looking_for == INFORMATION_ONLY: class_used_to_open_popup = "candidate_campaign_" + candidate_campaign_id + "_infoonly" retrieve_positions_url = "/pos/cand/" + candidate_campaign_id + "/infoonlylist?nf=1" elif stance_we_are_looking_for == STILL_DECIDING: class_used_to_open_popup = "candidate_campaign_" + candidate_campaign_id + "_deciders" retrieve_positions_url = "/pos/cand/" + candidate_campaign_id + "/deciders?nf=1" else: class_used_to_open_popup = '' retrieve_positions_url = '' link_open = "<a class='{link_class}' href='{link_href}' id='{popup_box_title}'>".format( link_class=class_used_to_open_popup, link_href=retrieve_positions_url, popup_box_title=popup_box_title, ) # How we display the link to the positions NOT followed varies based on the number of *followed* positions if number_of_positions_followed_total == 0: if number_of_positions_not_followed_total == 1: not_followed_stance_verb = display_stance_verb_we_are_looking_for_singular( stance_we_are_looking_for) else: not_followed_stance_verb = display_stance_verb_we_are_looking_for_plural( stance_we_are_looking_for) positions_not_followed_stance_html += \ "{link_open}{number} {not_followed_stance_verb}</a> ({link_open}learn more</a>)".format( link_open=link_open, number=number_of_positions_not_followed_total, not_followed_stance_verb=not_followed_stance_verb, ) elif number_of_positions_followed_total < 5: if number_of_positions_not_followed_total == 1: not_followed_stance_verb = "other " + display_stance_verb_we_are_looking_for_plural( stance_we_are_looking_for) else: not_followed_stance_verb = "others " + display_stance_verb_we_are_looking_for_singular( stance_we_are_looking_for) positions_not_followed_stance_html += \ "({link_open}{number_of_positions_not_followed_total} {not_followed_stance_verb}</a>)".format( link_open=link_open, number_of_positions_not_followed_total=number_of_positions_not_followed_total, not_followed_stance_verb=not_followed_stance_verb, ) else: # When there are more than 5 positions from followed organizations positions_not_followed_stance_html += "({link_open}show more supporters</a>)".format( link_open=link_open, ) stance_html = positions_followed_stance_html + " " + "<span class='positions_not_followed'>" + positions_not_followed_stance_html + "</span>" return stance_html
def organization_we_vote_id(self): organization_manager = OrganizationManager() return organization_manager.fetch_we_vote_id_from_local_id(self.organization_id)
def import_we_vote_positions_from_json(request, load_from_uri=False): """ Get the json data, and either create new entries or update existing :return: """ if load_from_uri: # Request json file from We Vote servers messages.add_message(request, messages.INFO, "Loading positions from We Vote Master servers") request = requests.get(POSITIONS_URL, params={ "key": WE_VOTE_API_KEY, # This comes from an environment variable }) structured_json = json.loads(request.text) else: # Load saved json from local file messages.add_message(request, messages.INFO, "Loading positions from local file") with open(POSITIONS_JSON_FILE) as json_data: structured_json = json.load(json_data) for one_position in structured_json: # Make sure we have the minimum required variables if len(one_position["id_we_vote"]) == 0 \ or len(one_position["organization_id_we_vote"]) == 0\ or len(one_position["candidate_campaign_id_we_vote"]) == 0: continue # Check to see if this position is already being used anywhere position_on_stage_found = False try: if len(one_position["id_we_vote"]) > 0: position_query = PositionEntered.objects.filter(id_we_vote=one_position["id_we_vote"]) if len(position_query): position_on_stage = position_query[0] position_on_stage_found = True except PositionEntered.DoesNotExist as e: handle_exception_silently(e) except Exception as e: handle_record_not_found_exception(e) # We need to look up the local organization_id based on the newly saved we_vote_id organization_manager = OrganizationManager() organization_id = organization_manager.fetch_organization_id(one_position["organization_id_we_vote"]) # We need to look up the local candidate_campaign_id candidate_campaign_manager = CandidateCampaignManager() candidate_campaign_id = candidate_campaign_manager.fetch_candidate_campaign_id_from_id_we_vote( one_position["candidate_campaign_id_we_vote"]) # TODO We need to look up measure_campaign_id measure_campaign_id = 0 try: if position_on_stage_found: # Update position_on_stage.id_we_vote = one_position["id_we_vote"] position_on_stage.organization_id = organization_id position_on_stage.candidate_campaign_id = candidate_campaign_id position_on_stage.measure_campaign_id = measure_campaign_id position_on_stage.date_entered = one_position["date_entered"] position_on_stage.election_id = one_position["election_id"] position_on_stage.stance = one_position["stance"] position_on_stage.more_info_url = one_position["more_info_url"] position_on_stage.statement_text = one_position["statement_text"] position_on_stage.statement_html = one_position["statement_html"] position_on_stage.save() messages.add_message(request, messages.INFO, "Position updated: {id_we_vote}".format( id_we_vote=one_position["id_we_vote"])) else: # Create new position_on_stage = PositionEntered( id_we_vote=one_position["id_we_vote"], organization_id=organization_id, candidate_campaign_id=candidate_campaign_id, measure_campaign_id=measure_campaign_id, date_entered=one_position["date_entered"], election_id=one_position["election_id"], stance=one_position["stance"], more_info_url=one_position["more_info_url"], statement_text=one_position["statement_text"], statement_html=one_position["statement_html"], ) position_on_stage.save() messages.add_message(request, messages.INFO, "New position imported: {id_we_vote}".format( id_we_vote=one_position["id_we_vote"])) except Exception as e: handle_record_not_saved_exception(e) messages.add_message(request, messages.ERROR, "Could not save position, id_we_vote: {id_we_vote}, " "organization_id_we_vote: {organization_id_we_vote}, " "candidate_campaign_id_we_vote: {candidate_campaign_id_we_vote}".format( id_we_vote=one_position["id_we_vote"], organization_id_we_vote=one_position["organization_id_we_vote"], candidate_campaign_id_we_vote=one_position["candidate_campaign_id_we_vote"], ))
def voter_guides_import_from_structured_json(structured_json): """ This pathway in requires a we_vote_id, and is not used when we import from Google Civic :param structured_json: :return: """ voter_guide_manager = VoterGuideManager() organization_manager = OrganizationManager() organization_id = 0 voter_guides_saved = 0 voter_guides_updated = 0 voter_guides_not_processed = 0 for one_voter_guide in structured_json: we_vote_id = one_voter_guide[ 'we_vote_id'] if 'we_vote_id' in one_voter_guide else '' google_civic_election_id = one_voter_guide['google_civic_election_id'] \ if 'google_civic_election_id' in one_voter_guide else '' vote_smart_time_span = one_voter_guide['vote_smart_time_span'] \ if 'vote_smart_time_span' in one_voter_guide else '' organization_we_vote_id = one_voter_guide['organization_we_vote_id'] \ if 'organization_we_vote_id' in one_voter_guide else '' public_figure_we_vote_id = one_voter_guide['public_figure_we_vote_id'] \ if 'public_figure_we_vote_id' in one_voter_guide else '' if positive_value_exists(we_vote_id) and \ (positive_value_exists(organization_we_vote_id) or positive_value_exists(public_figure_we_vote_id)) and \ (positive_value_exists(google_civic_election_id) or positive_value_exists(vote_smart_time_span)): # Make sure we have the organization (or public figure) in this database before we import the voter guide if positive_value_exists(organization_we_vote_id): results = organization_manager.retrieve_organization_from_we_vote_id( organization_we_vote_id) if results['organization_found']: organization_id = results['organization_id'] if positive_value_exists(organization_id): proceed_to_update_or_create = True else: proceed_to_update_or_create = False elif positive_value_exists(public_figure_we_vote_id): # TODO DALE Update this to work with public_figure public_figure_id = organization_manager.retrieve_organization_from_we_vote_id( public_figure_we_vote_id) if positive_value_exists(public_figure_id): proceed_to_update_or_create = True else: proceed_to_update_or_create = False else: proceed_to_update_or_create = False else: proceed_to_update_or_create = False if proceed_to_update_or_create: if positive_value_exists( organization_we_vote_id) and positive_value_exists( google_civic_election_id): results = voter_guide_manager.update_or_create_organization_voter_guide_by_election_id( organization_we_vote_id, google_civic_election_id) elif positive_value_exists( organization_we_vote_id) and positive_value_exists( vote_smart_time_span): results = voter_guide_manager.update_or_create_organization_voter_guide_by_time_span( organization_we_vote_id, vote_smart_time_span) elif positive_value_exists( public_figure_we_vote_id) and positive_value_exists( google_civic_election_id): results = voter_guide_manager.update_or_create_public_figure_voter_guide( google_civic_election_id, public_figure_we_vote_id) else: results = { 'success': False, 'status': 'Required value missing, cannot update or create (1)' } else: voter_guides_not_processed += 1 results = { 'success': False, 'status': 'Required value missing, cannot update or create (2)' } if results['success']: if results['new_voter_guide_created']: voter_guides_saved += 1 else: voter_guides_updated += 1 else: voter_guides_not_processed += 1 voter_guides_results = { 'success': True, 'status': "VOTER_GUIDES_IMPORT_PROCESS_COMPLETE", 'saved': voter_guides_saved, 'updated': voter_guides_updated, 'not_processed': voter_guides_not_processed, } return voter_guides_results
def voter_guides_to_follow_retrieve_for_api( voter_device_id, # voterGuidesToFollow kind_of_ballot_item='', ballot_item_we_vote_id='', google_civic_election_id=0, search_string='', maximum_number_to_retrieve=0): # Get voter_id from the voter_device_id so we can figure out which voter_guides to offer results = is_voter_device_id_valid(voter_device_id) if not results['success']: json_data = { 'status': 'ERROR_GUIDES_TO_FOLLOW_NO_VOTER_DEVICE_ID', 'success': False, 'voter_device_id': voter_device_id, 'voter_guides': [], 'google_civic_election_id': google_civic_election_id, 'search_string': search_string, 'ballot_item_we_vote_id': ballot_item_we_vote_id, } results = { 'success': False, 'google_civic_election_id': 0, # Force the reset of google_civic_election_id cookie 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'json_data': json_data, } return results voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) if not positive_value_exists(voter_id): json_data = { 'status': "ERROR_GUIDES_TO_FOLLOW_VOTER_NOT_FOUND_FROM_VOTER_DEVICE_ID", 'success': False, 'voter_device_id': voter_device_id, 'voter_guides': [], 'google_civic_election_id': google_civic_election_id, 'search_string': search_string, 'ballot_item_we_vote_id': ballot_item_we_vote_id, } results = { 'success': False, 'google_civic_election_id': 0, # Force the reset of google_civic_election_id cookie 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'json_data': json_data, } return results voter_guide_list = [] voter_guides = [] try: if positive_value_exists( kind_of_ballot_item) and positive_value_exists( ballot_item_we_vote_id): results = retrieve_voter_guides_to_follow_by_ballot_item( voter_id, kind_of_ballot_item, ballot_item_we_vote_id, search_string) success = results['success'] status = results['status'] voter_guide_list = results['voter_guide_list'] elif positive_value_exists(google_civic_election_id): # This retrieve also does the reordering results = retrieve_voter_guides_to_follow_by_election_for_api( voter_id, google_civic_election_id, search_string, maximum_number_to_retrieve, 'twitter_followers_count', 'desc') success = results['success'] status = results['status'] voter_guide_list = results['voter_guide_list'] else: results = retrieve_voter_guides_to_follow_generic_for_api( voter_id, search_string, maximum_number_to_retrieve, 'twitter_followers_count', 'desc') success = results['success'] status = results['status'] voter_guide_list = results['voter_guide_list'] except Exception as e: status = 'FAILED voter_guides_to_follow_retrieve_for_api, retrieve_voter_guides_for_election ' \ '{error} [type: {error_type}]'.format(error=e, error_type=type(e)) success = False if success: voter_manager = VoterManager() results = voter_manager.retrieve_voter_by_id(voter_id) linked_organization_we_vote_id = "" if results['voter_found']: voter = results['voter'] linked_organization_we_vote_id = voter.linked_organization_we_vote_id number_added_to_list = 0 position_manager = PositionEnteredManager() position = PositionEntered() for voter_guide in voter_guide_list: if positive_value_exists(voter_guide.organization_we_vote_id) \ and linked_organization_we_vote_id == voter_guide.organization_we_vote_id: # Do not return your own voter guide to follow continue position_found = False one_voter_guide = { 'we_vote_id': voter_guide.we_vote_id, 'google_civic_election_id': voter_guide.google_civic_election_id, 'time_span': voter_guide.vote_smart_time_span, 'voter_guide_display_name': voter_guide.voter_guide_display_name(), 'voter_guide_image_url': voter_guide.voter_guide_image_url(), 'voter_guide_owner_type': voter_guide.voter_guide_owner_type, 'organization_we_vote_id': voter_guide.organization_we_vote_id, 'public_figure_we_vote_id': voter_guide.public_figure_we_vote_id, 'twitter_description': voter_guide.twitter_description, 'twitter_followers_count': voter_guide.twitter_followers_count, 'twitter_handle': voter_guide.twitter_handle, 'owner_voter_id': voter_guide.owner_voter_id, 'last_updated': voter_guide.last_updated.strftime('%Y-%m-%d %H:%M'), } if positive_value_exists(ballot_item_we_vote_id): if kind_of_ballot_item == CANDIDATE: organization_manager = OrganizationManager() organization_id = organization_manager.fetch_organization_id( voter_guide.organization_we_vote_id) results = position_manager.retrieve_organization_candidate_campaign_position_with_we_vote_id( organization_id, ballot_item_we_vote_id) if results['position_found']: position = results['position'] position_found = True elif kind_of_ballot_item == MEASURE: organization_manager = OrganizationManager() organization_id = organization_manager.fetch_organization_id( voter_guide.organization_we_vote_id) results = position_manager.retrieve_organization_contest_measure_position_with_we_vote_id( organization_id, ballot_item_we_vote_id) if results['position_found']: position = results['position'] position_found = True if position_found: one_voter_guide['is_support'] = position.is_support() one_voter_guide[ 'is_positive_rating'] = position.is_positive_rating() one_voter_guide[ 'is_support_or_positive_rating'] = position.is_support_or_positive_rating( ) one_voter_guide['is_oppose'] = position.is_oppose() one_voter_guide[ 'is_negative_rating'] = position.is_negative_rating() one_voter_guide[ 'is_oppose_or_negative_rating'] = position.is_oppose_or_negative_rating( ) one_voter_guide[ 'is_information_only'] = position.is_information_only( ) one_voter_guide[ 'ballot_item_display_name'] = position.ballot_item_display_name one_voter_guide[ 'speaker_display_name'] = position.speaker_display_name one_voter_guide['statement_text'] = position.statement_text one_voter_guide['more_info_url'] = position.more_info_url one_voter_guide[ 'vote_smart_rating'] = position.vote_smart_rating one_voter_guide[ 'vote_smart_time_span'] = position.vote_smart_time_span voter_guides.append(one_voter_guide.copy()) if positive_value_exists(maximum_number_to_retrieve): number_added_to_list += 1 if number_added_to_list >= maximum_number_to_retrieve: break if len(voter_guides): json_data = { 'status': status + ' VOTER_GUIDES_TO_FOLLOW_RETRIEVED', 'success': True, 'voter_device_id': voter_device_id, 'voter_guides': voter_guides, 'google_civic_election_id': google_civic_election_id, 'search_string': search_string, 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'maximum_number_to_retrieve': maximum_number_to_retrieve, } else: json_data = { 'status': status + ' NO_VOTER_GUIDES_FOUND', 'success': True, 'voter_device_id': voter_device_id, 'voter_guides': voter_guides, 'google_civic_election_id': google_civic_election_id, 'search_string': search_string, 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'maximum_number_to_retrieve': maximum_number_to_retrieve, } results = { 'success': success, 'google_civic_election_id': google_civic_election_id, 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'json_data': json_data, } return results else: json_data = { 'status': status, 'success': False, 'voter_device_id': voter_device_id, 'voter_guides': [], 'google_civic_election_id': google_civic_election_id, 'search_string': search_string, 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'maximum_number_to_retrieve': maximum_number_to_retrieve, } results = { 'success': False, 'google_civic_election_id': 0, # Force the reset of google_civic_election_id cookie 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'json_data': json_data, } return results
def quick_info_import_from_sample_file( request=None): # , load_from_uri=False # TODO to be converted """ Get the json data, and either create new entries or update existing :return: """ # if load_from_uri: # # Request json file from We Vote servers # messages.add_message(request, messages.INFO, "Loading quick_info from We Vote Master servers") # request = requests.get(QUICK_INFO_URL, params={ # "key": WE_VOTE_API_KEY, # This comes from an environment variable # }) # structured_json = json.loads(request.text) # else: # Load saved json from local file with open("quick_info/import_data/quick_info_sample.json") as json_data: structured_json = json.load(json_data) quick_info_saved = 0 quick_info_updated = 0 quick_info_not_processed = 0 for one_quick_info in structured_json: # Make sure we have the minimum required variables if not positive_value_exists(one_quick_info["we_vote_id"]) \ or not positive_value_exists(one_quick_info["organization_we_vote_id"])\ or not positive_value_exists(one_quick_info["candidate_campaign_we_vote_id"]): quick_info_not_processed += 1 continue # Check to see if this quick_info is already being used anywhere quick_info_found = False try: if len(one_quick_info["we_vote_id"]) > 0: quick_info_query = QuickInfo.objects.filter( we_vote_id=one_quick_info["we_vote_id"]) if len(quick_info_query): quick_info = quick_info_query[0] quick_info_found = True except QuickInfo.DoesNotExist as e: handle_record_not_found_exception(e, logger=logger) pass except Exception as e: handle_record_not_found_exception(e, logger=logger) # We need to look up the local organization_id based on the newly saved we_vote_id organization_manager = OrganizationManager() organization_id = organization_manager.fetch_organization_id( one_quick_info["organization_we_vote_id"]) # We need to look up the local candidate_campaign_id candidate_campaign_manager = CandidateCampaignManager() candidate_campaign_id = candidate_campaign_manager.fetch_candidate_campaign_id_from_we_vote_id( one_quick_info["candidate_campaign_we_vote_id"]) # Find the google_civic_candidate_name so we have a backup way to link quick_info if the we_vote_id is lost google_civic_candidate_name = one_quick_info["google_civic_candidate_name"] if \ "google_civic_candidate_name" in one_quick_info else '' if not positive_value_exists(google_civic_candidate_name): google_civic_candidate_name = candidate_campaign_manager.fetch_google_civic_candidate_name_from_we_vote_id( one_quick_info["candidate_campaign_we_vote_id"]) # TODO We need to look up contest_measure_id contest_measure_id = 0 try: if quick_info_found: # Update quick_info.we_vote_id = one_quick_info["we_vote_id"] quick_info.organization_id = organization_id quick_info.organization_we_vote_id = one_quick_info[ "organization_we_vote_id"] quick_info.candidate_campaign_id = candidate_campaign_id quick_info.candidate_campaign_we_vote_id = one_quick_info[ "candidate_campaign_we_vote_id"] quick_info.google_civic_candidate_name = google_civic_candidate_name quick_info.contest_measure_id = contest_measure_id quick_info.date_entered = one_quick_info["date_entered"] quick_info.google_civic_election_id = one_quick_info[ "google_civic_election_id"] quick_info.stance = one_quick_info["stance"] quick_info.more_info_url = one_quick_info["more_info_url"] quick_info.statement_text = one_quick_info["statement_text"] quick_info.statement_html = one_quick_info["statement_html"] quick_info.save() quick_info_updated += 1 # messages.add_message(request, messages.INFO, u"QuickInfo updated: {we_vote_id}".format( # we_vote_id=one_quick_info["we_vote_id"])) else: # Create new quick_info = QuickInfo( we_vote_id=one_quick_info["we_vote_id"], organization_id=organization_id, organization_we_vote_id=one_quick_info[ "organization_we_vote_id"], candidate_campaign_id=candidate_campaign_id, candidate_campaign_we_vote_id=one_quick_info[ "candidate_campaign_we_vote_id"], google_civic_candidate_name=google_civic_candidate_name, contest_measure_id=contest_measure_id, date_entered=one_quick_info["date_entered"], google_civic_election_id=one_quick_info[ "google_civic_election_id"], stance=one_quick_info["stance"], more_info_url=one_quick_info["more_info_url"], statement_text=one_quick_info["statement_text"], statement_html=one_quick_info["statement_html"], ) quick_info.save() quick_info_saved += 1 # messages.add_message(request, messages.INFO, u"New quick_info imported: {we_vote_id}".format( # we_vote_id=one_quick_info["we_vote_id"])) except Exception as e: handle_record_not_saved_exception(e, logger=logger) if request is not None: messages.add_message( request, messages.ERROR, u"Could not save/update quick_info, " u"quick_info_found: {quick_info_found}, " u"we_vote_id: {we_vote_id}, " u"organization_we_vote_id: {organization_we_vote_id}, " u"candidate_campaign_we_vote_id: {candidate_campaign_we_vote_id}" .format( quick_info_found=quick_info_found, we_vote_id=one_quick_info["we_vote_id"], organization_we_vote_id=one_quick_info[ "organization_we_vote_id"], candidate_campaign_we_vote_id=one_quick_info[ "candidate_campaign_we_vote_id"], )) quick_info_not_processed += 1 quick_info_results = { 'saved': quick_info_saved, 'updated': quick_info_updated, 'not_processed': quick_info_not_processed, } return quick_info_results
def scrape_website_for_social_media_view(request, organization_id, force_retrieve=False): authority_required = {'admin'} # admin, verified_volunteer if not voter_has_authority(request, authority_required): return redirect_to_sign_in_page(request, authority_required) facebook_page = False twitter_handle = False organization_manager = OrganizationManager() results = organization_manager.retrieve_organization(organization_id) if not results['organization_found']: messages.add_message(request, messages.INFO, results['status']) return HttpResponseRedirect( reverse('organization:organization_edit', args=(organization_id, ))) organization = results['organization'] if not organization.organization_website: messages.add_message(request, messages.ERROR, "No organizational website found.") return HttpResponseRedirect( reverse('organization:organization_position_list', args=(organization_id, ))) if (not positive_value_exists(organization.organization_twitter_handle)) or \ (not positive_value_exists(organization.organization_facebook)) or force_retrieve: scrape_results = scrape_social_media_from_one_site( organization.organization_website) if scrape_results['twitter_handle_found']: twitter_handle = scrape_results['twitter_handle'] messages.add_message(request, messages.INFO, "Twitter handle found: " + twitter_handle) else: messages.add_message( request, messages.INFO, "No Twitter handle found: " + scrape_results['status']) if scrape_results['facebook_page_found']: facebook_page = scrape_results['facebook_page'] messages.add_message(request, messages.INFO, "Facebook page found: " + facebook_page) save_results = organization_manager.update_organization_social_media( organization, twitter_handle, facebook_page) if save_results['success']: organization = save_results['organization'] else: organization.organization_twitter_handle = twitter_handle # Store it temporarily # ###################################### if organization.organization_twitter_handle: results = retrieve_twitter_user_info( organization.organization_twitter_handle) if results['success']: save_results = organization_manager.update_organization_twitter_details( organization, results['twitter_json']) if save_results['success']: organization = save_results['organization'] results = update_social_media_statistics_in_other_tables( organization) # ###################################### return HttpResponseRedirect( reverse('organization:organization_position_list', args=(organization_id, )))
def voter_guides_to_follow_retrieve_for_api(voter_device_id, # voterGuidesToFollow kind_of_ballot_item='', ballot_item_we_vote_id='', google_civic_election_id=0, search_string='', maximum_number_to_retrieve=0): # Get voter_id from the voter_device_id so we can figure out which voter_guides to offer results = is_voter_device_id_valid(voter_device_id) if not results['success']: json_data = { 'status': 'ERROR_GUIDES_TO_FOLLOW_NO_VOTER_DEVICE_ID', 'success': False, 'voter_device_id': voter_device_id, 'voter_guides': [], 'google_civic_election_id': google_civic_election_id, 'search_string': search_string, 'ballot_item_we_vote_id': ballot_item_we_vote_id, } results = { 'success': False, 'google_civic_election_id': 0, # Force the reset of google_civic_election_id cookie 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'json_data': json_data, } return results voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) if not positive_value_exists(voter_id): json_data = { 'status': "ERROR_GUIDES_TO_FOLLOW_VOTER_NOT_FOUND_FROM_VOTER_DEVICE_ID", 'success': False, 'voter_device_id': voter_device_id, 'voter_guides': [], 'google_civic_election_id': google_civic_election_id, 'search_string': search_string, 'ballot_item_we_vote_id': ballot_item_we_vote_id, } results = { 'success': False, 'google_civic_election_id': 0, # Force the reset of google_civic_election_id cookie 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'json_data': json_data, } return results voter_guide_list = [] voter_guides = [] try: if positive_value_exists(kind_of_ballot_item) and positive_value_exists(ballot_item_we_vote_id): results = retrieve_voter_guides_to_follow_by_ballot_item(voter_id, kind_of_ballot_item, ballot_item_we_vote_id, search_string) success = results['success'] status = results['status'] voter_guide_list = results['voter_guide_list'] elif positive_value_exists(google_civic_election_id): # This retrieve also does the reordering results = retrieve_voter_guides_to_follow_by_election_for_api(voter_id, google_civic_election_id, search_string, maximum_number_to_retrieve, 'twitter_followers_count', 'desc') success = results['success'] status = results['status'] voter_guide_list = results['voter_guide_list'] else: results = retrieve_voter_guides_to_follow_generic_for_api(voter_id, search_string, maximum_number_to_retrieve, 'twitter_followers_count', 'desc') success = results['success'] status = results['status'] voter_guide_list = results['voter_guide_list'] except Exception as e: status = 'FAILED voter_guides_to_follow_retrieve_for_api, retrieve_voter_guides_for_election ' \ '{error} [type: {error_type}]'.format(error=e, error_type=type(e)) success = False if success: voter_manager = VoterManager() results = voter_manager.retrieve_voter_by_id(voter_id) linked_organization_we_vote_id = "" if results['voter_found']: voter = results['voter'] linked_organization_we_vote_id = voter.linked_organization_we_vote_id number_added_to_list = 0 position_manager = PositionEnteredManager() position = PositionEntered() for voter_guide in voter_guide_list: if positive_value_exists(voter_guide.organization_we_vote_id) \ and linked_organization_we_vote_id == voter_guide.organization_we_vote_id: # Do not return your own voter guide to follow continue position_found = False one_voter_guide = { 'we_vote_id': voter_guide.we_vote_id, 'google_civic_election_id': voter_guide.google_civic_election_id, 'time_span': voter_guide.vote_smart_time_span, 'voter_guide_display_name': voter_guide.voter_guide_display_name(), 'voter_guide_image_url': voter_guide.voter_guide_image_url(), 'voter_guide_owner_type': voter_guide.voter_guide_owner_type, 'organization_we_vote_id': voter_guide.organization_we_vote_id, 'public_figure_we_vote_id': voter_guide.public_figure_we_vote_id, 'twitter_description': voter_guide.twitter_description, 'twitter_followers_count': voter_guide.twitter_followers_count, 'twitter_handle': voter_guide.twitter_handle, 'owner_voter_id': voter_guide.owner_voter_id, 'last_updated': voter_guide.last_updated.strftime('%Y-%m-%d %H:%M'), } if positive_value_exists(ballot_item_we_vote_id): if kind_of_ballot_item == CANDIDATE: organization_manager = OrganizationManager() organization_id = organization_manager.fetch_organization_id( voter_guide.organization_we_vote_id) results = position_manager.retrieve_organization_candidate_campaign_position_with_we_vote_id( organization_id, ballot_item_we_vote_id) if results['position_found']: position = results['position'] position_found = True elif kind_of_ballot_item == MEASURE: organization_manager = OrganizationManager() organization_id = organization_manager.fetch_organization_id( voter_guide.organization_we_vote_id) results = position_manager.retrieve_organization_contest_measure_position_with_we_vote_id( organization_id, ballot_item_we_vote_id) if results['position_found']: position = results['position'] position_found = True if position_found: one_voter_guide['is_support'] = position.is_support() one_voter_guide['is_positive_rating'] = position.is_positive_rating() one_voter_guide['is_support_or_positive_rating'] = position.is_support_or_positive_rating() one_voter_guide['is_oppose'] = position.is_oppose() one_voter_guide['is_negative_rating'] = position.is_negative_rating() one_voter_guide['is_oppose_or_negative_rating'] = position.is_oppose_or_negative_rating() one_voter_guide['is_information_only'] = position.is_information_only() one_voter_guide['ballot_item_display_name'] = position.ballot_item_display_name one_voter_guide['speaker_display_name'] = position.speaker_display_name one_voter_guide['statement_text'] = position.statement_text one_voter_guide['more_info_url'] = position.more_info_url one_voter_guide['vote_smart_rating'] = position.vote_smart_rating one_voter_guide['vote_smart_time_span'] = position.vote_smart_time_span voter_guides.append(one_voter_guide.copy()) if positive_value_exists(maximum_number_to_retrieve): number_added_to_list += 1 if number_added_to_list >= maximum_number_to_retrieve: break if len(voter_guides): json_data = { 'status': status + ' VOTER_GUIDES_TO_FOLLOW_RETRIEVED', 'success': True, 'voter_device_id': voter_device_id, 'voter_guides': voter_guides, 'google_civic_election_id': google_civic_election_id, 'search_string': search_string, 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'maximum_number_to_retrieve': maximum_number_to_retrieve, } else: json_data = { 'status': status + ' NO_VOTER_GUIDES_FOUND', 'success': True, 'voter_device_id': voter_device_id, 'voter_guides': voter_guides, 'google_civic_election_id': google_civic_election_id, 'search_string': search_string, 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'maximum_number_to_retrieve': maximum_number_to_retrieve, } results = { 'success': success, 'google_civic_election_id': google_civic_election_id, 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'json_data': json_data, } return results else: json_data = { 'status': status, 'success': False, 'voter_device_id': voter_device_id, 'voter_guides': [], 'google_civic_election_id': google_civic_election_id, 'search_string': search_string, 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'maximum_number_to_retrieve': maximum_number_to_retrieve, } results = { 'success': False, 'google_civic_election_id': 0, # Force the reset of google_civic_election_id cookie 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'json_data': json_data, } return results
def assemble_candidate_campaign_stance_html( candidate_campaign_id, stance_we_are_looking_for, positions_followed, positions_not_followed): """ :param candidate_campaign_id: :param stance_we_are_looking_for: :param positions_followed: :param positions_not_followed: :return: """ ################################# # Start with positions_followed # Assemble some information that is independent of each position number_of_positions_followed_total = len(positions_followed) popup_box_title_verb = display_stance_we_are_looking_for_title( stance_we_are_looking_for, number_of_positions_followed_total) candidate_campaign_manager = CandidateCampaignManager() results = candidate_campaign_manager.retrieve_candidate_campaign_from_id(candidate_campaign_id) if results['candidate_campaign_found']: candidate_campaign = results['candidate_campaign'] popup_box_title_candidate_name = candidate_campaign.candidate_name else: popup_box_title_candidate_name = "" popup_box_title = popup_box_title_verb+" "+popup_box_title_candidate_name if stance_we_are_looking_for == SUPPORT: # This is the class we reference with jquery for opening a div popup to display the supporters class_used_to_open_popup = "candidate_campaign_"+candidate_campaign_id+"_supporters" # This is the URL that returns the supporters for this candidate retrieve_positions_url = "/pos/cand/"+candidate_campaign_id+"/supporters?f=1" # Only show orgs followed elif stance_we_are_looking_for == OPPOSE: class_used_to_open_popup = "candidate_campaign_"+candidate_campaign_id+"_opposers" retrieve_positions_url = "/pos/cand/"+candidate_campaign_id+"/opposers?f=1" elif stance_we_are_looking_for == INFORMATION_ONLY: class_used_to_open_popup = "candidate_campaign_"+candidate_campaign_id+"_infoonly" retrieve_positions_url = "/pos/cand/"+candidate_campaign_id+"/infoonlylist?f=1" elif stance_we_are_looking_for == STILL_DECIDING: class_used_to_open_popup = "candidate_campaign_"+candidate_campaign_id+"_deciders" retrieve_positions_url = "/pos/cand/"+candidate_campaign_id+"/deciders?f=1" else: class_used_to_open_popup = '' retrieve_positions_url = '' # Cycle through these positions and put together a line about who is supporting, opposing, have information # or are still deciding positions_followed_stance_html = "" is_first = True number_of_positions_followed_counter = 0 only_you = False for position in positions_followed: if is_first: positions_followed_stance_html += "" else: is_next_to_last = number_of_positions_followed_counter == number_of_positions_followed_total - 1 positions_followed_stance_html += " and " if is_next_to_last else ", " is_first = False if position.organization_id > 0: organization_manager = OrganizationManager() results = organization_manager.retrieve_organization(position.organization_id) if results['organization_found']: organization_on_stage = results['organization'] link_open = "<a class='{link_class}' href='{link_href}' id='{popup_box_title}'>".format( link_class=class_used_to_open_popup, link_href=retrieve_positions_url, popup_box_title=popup_box_title, ) positions_followed_stance_html += "{link_open}{organization_name}</a>".format( link_open=link_open, organization_name=organization_on_stage.name, ) number_of_positions_followed_counter += 1 elif position.voter_id > 0: positions_followed_stance_html += "You" number_of_positions_followed_counter += 1 if number_of_positions_followed_total == 1: only_you = True if number_of_positions_followed_total: verb_text = display_stance_we_are_looking_for( stance_we_are_looking_for, number_of_positions_followed_total, only_you) if verb_text: positions_followed_stance_html = "<span class='positions_followed_text'>" + positions_followed_stance_html positions_followed_stance_html += " <span class='position_stance_verb'>{verb_text}</span>".format( verb_text=verb_text) positions_followed_stance_html += "</span>" ################################# # NOT Followed ################################# # Now create string with html for positions_not_followed positions_not_followed_stance_html = "" number_of_positions_not_followed_total = len(positions_not_followed) # If there aren't any "not followed" positions, just return the positions_followed_stance_html if number_of_positions_not_followed_total == 0: return positions_followed_stance_html # If here we know there is at least one position available that isnt' being followed by voter popup_box_title = popup_box_title_verb+" "+popup_box_title_candidate_name if stance_we_are_looking_for == SUPPORT: # This is the class we reference with jquery for opening a div popup to display the supporters class_used_to_open_popup = "candidate_campaign_"+candidate_campaign_id+"_supporters" # This is the URL that returns the supporters for this candidate retrieve_positions_url = "/pos/cand/"+candidate_campaign_id+"/supporters?nf=1" # Only show orgs not followed elif stance_we_are_looking_for == OPPOSE: class_used_to_open_popup = "candidate_campaign_"+candidate_campaign_id+"_opposers" retrieve_positions_url = "/pos/cand/"+candidate_campaign_id+"/opposers?nf=1" elif stance_we_are_looking_for == INFORMATION_ONLY: class_used_to_open_popup = "candidate_campaign_"+candidate_campaign_id+"_infoonly" retrieve_positions_url = "/pos/cand/"+candidate_campaign_id+"/infoonlylist?nf=1" elif stance_we_are_looking_for == STILL_DECIDING: class_used_to_open_popup = "candidate_campaign_"+candidate_campaign_id+"_deciders" retrieve_positions_url = "/pos/cand/"+candidate_campaign_id+"/deciders?nf=1" else: class_used_to_open_popup = '' retrieve_positions_url = '' link_open = "<a class='{link_class}' href='{link_href}' id='{popup_box_title}'>".format( link_class=class_used_to_open_popup, link_href=retrieve_positions_url, popup_box_title=popup_box_title, ) # How we display the link to the positions NOT followed varies based on the number of *followed* positions if number_of_positions_followed_total == 0: if number_of_positions_not_followed_total == 1: not_followed_stance_verb = display_stance_verb_we_are_looking_for_singular(stance_we_are_looking_for) else: not_followed_stance_verb = display_stance_verb_we_are_looking_for_plural(stance_we_are_looking_for) positions_not_followed_stance_html += \ "{link_open}{number} {not_followed_stance_verb}</a> ({link_open}learn more</a>)".format( link_open=link_open, number=number_of_positions_not_followed_total, not_followed_stance_verb=not_followed_stance_verb, ) elif number_of_positions_followed_total < 5: if number_of_positions_not_followed_total == 1: not_followed_stance_verb = "other " \ + display_stance_verb_we_are_looking_for_plural(stance_we_are_looking_for) else: not_followed_stance_verb = "others "\ + display_stance_verb_we_are_looking_for_singular(stance_we_are_looking_for) positions_not_followed_stance_html += \ "({link_open}{number_of_positions_not_followed_total} {not_followed_stance_verb}</a>)".format( link_open=link_open, number_of_positions_not_followed_total=number_of_positions_not_followed_total, not_followed_stance_verb=not_followed_stance_verb, ) else: # When there are more than 5 positions from followed organizations positions_not_followed_stance_html += "({link_open}show more supporters</a>)".format( link_open=link_open, ) stance_html = positions_followed_stance_html + " " + "<span class='positions_not_followed'>" \ + positions_not_followed_stance_html + "</span>" return stance_html
def quick_info_import_from_sample_file(request=None): # , load_from_uri=False # TODO to be converted """ Get the json data, and either create new entries or update existing :return: """ # if load_from_uri: # # Request json file from We Vote servers # messages.add_message(request, messages.INFO, "Loading quick_info from We Vote Master servers") # request = requests.get(QUICK_INFO_URL, params={ # "key": WE_VOTE_API_KEY, # This comes from an environment variable # }) # structured_json = json.loads(request.text) # else: # Load saved json from local file with open("quick_info/import_data/quick_info_sample.json") as json_data: structured_json = json.load(json_data) quick_info_saved = 0 quick_info_updated = 0 quick_info_not_processed = 0 for one_quick_info in structured_json: # Make sure we have the minimum required variables if not positive_value_exists(one_quick_info["we_vote_id"]) \ or not positive_value_exists(one_quick_info["organization_we_vote_id"])\ or not positive_value_exists(one_quick_info["candidate_campaign_we_vote_id"]): quick_info_not_processed += 1 continue # Check to see if this quick_info is already being used anywhere quick_info_found = False try: if len(one_quick_info["we_vote_id"]) > 0: quick_info_query = QuickInfo.objects.filter(we_vote_id=one_quick_info["we_vote_id"]) if len(quick_info_query): quick_info = quick_info_query[0] quick_info_found = True except QuickInfo.DoesNotExist as e: handle_record_not_found_exception(e, logger=logger) pass except Exception as e: handle_record_not_found_exception(e, logger=logger) # We need to look up the local organization_id based on the newly saved we_vote_id organization_manager = OrganizationManager() organization_id = organization_manager.fetch_organization_id(one_quick_info["organization_we_vote_id"]) # We need to look up the local candidate_campaign_id candidate_campaign_manager = CandidateCampaignManager() candidate_campaign_id = candidate_campaign_manager.fetch_candidate_campaign_id_from_we_vote_id( one_quick_info["candidate_campaign_we_vote_id"]) # Find the google_civic_candidate_name so we have a backup way to link quick_info if the we_vote_id is lost google_civic_candidate_name = one_quick_info["google_civic_candidate_name"] if \ "google_civic_candidate_name" in one_quick_info else '' if not positive_value_exists(google_civic_candidate_name): google_civic_candidate_name = candidate_campaign_manager.fetch_google_civic_candidate_name_from_we_vote_id( one_quick_info["candidate_campaign_we_vote_id"]) # TODO We need to look up contest_measure_id contest_measure_id = 0 try: if quick_info_found: # Update quick_info.we_vote_id = one_quick_info["we_vote_id"] quick_info.organization_id = organization_id quick_info.organization_we_vote_id = one_quick_info["organization_we_vote_id"] quick_info.candidate_campaign_id = candidate_campaign_id quick_info.candidate_campaign_we_vote_id = one_quick_info["candidate_campaign_we_vote_id"] quick_info.google_civic_candidate_name = google_civic_candidate_name quick_info.contest_measure_id = contest_measure_id quick_info.date_entered = one_quick_info["date_entered"] quick_info.google_civic_election_id = one_quick_info["google_civic_election_id"] quick_info.stance = one_quick_info["stance"] quick_info.more_info_url = one_quick_info["more_info_url"] quick_info.statement_text = one_quick_info["statement_text"] quick_info.statement_html = one_quick_info["statement_html"] quick_info.save() quick_info_updated += 1 # messages.add_message(request, messages.INFO, u"QuickInfo updated: {we_vote_id}".format( # we_vote_id=one_quick_info["we_vote_id"])) else: # Create new quick_info = QuickInfo( we_vote_id=one_quick_info["we_vote_id"], organization_id=organization_id, organization_we_vote_id=one_quick_info["organization_we_vote_id"], candidate_campaign_id=candidate_campaign_id, candidate_campaign_we_vote_id=one_quick_info["candidate_campaign_we_vote_id"], google_civic_candidate_name=google_civic_candidate_name, contest_measure_id=contest_measure_id, date_entered=one_quick_info["date_entered"], google_civic_election_id=one_quick_info["google_civic_election_id"], stance=one_quick_info["stance"], more_info_url=one_quick_info["more_info_url"], statement_text=one_quick_info["statement_text"], statement_html=one_quick_info["statement_html"], ) quick_info.save() quick_info_saved += 1 # messages.add_message(request, messages.INFO, u"New quick_info imported: {we_vote_id}".format( # we_vote_id=one_quick_info["we_vote_id"])) except Exception as e: handle_record_not_saved_exception(e, logger=logger) if request is not None: messages.add_message(request, messages.ERROR, u"Could not save/update quick_info, " u"quick_info_found: {quick_info_found}, " u"we_vote_id: {we_vote_id}, " u"organization_we_vote_id: {organization_we_vote_id}, " u"candidate_campaign_we_vote_id: {candidate_campaign_we_vote_id}".format( quick_info_found=quick_info_found, we_vote_id=one_quick_info["we_vote_id"], organization_we_vote_id=one_quick_info["organization_we_vote_id"], candidate_campaign_we_vote_id=one_quick_info["candidate_campaign_we_vote_id"], )) quick_info_not_processed += 1 quick_info_results = { 'saved': quick_info_saved, 'updated': quick_info_updated, 'not_processed': quick_info_not_processed, } return quick_info_results