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) google_civic_election_id = request.GET.get('google_civic_election_id', 0) 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, )) + '?google_civic_election_id=' + str(google_civic_election_id)) organization = results['organization'] results = refresh_twitter_organization_details(organization) return HttpResponseRedirect( reverse('organization:organization_position_list', args=(organization_id, )) + '?google_civic_election_id=' + str(google_civic_election_id))
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_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 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 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 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 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 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 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 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 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 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 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 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