def organization_edit_process_view(request): """ Process the new or edit organization forms :param request: :return: """ 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_id = convert_to_int(request.POST.get('organization_id', 0)) organization_name = request.POST.get('organization_name', '') organization_twitter_handle = request.POST.get('organization_twitter_handle', False) organization_facebook = request.POST.get('organization_facebook', False) organization_website = request.POST.get('organization_website', False) wikipedia_page_title = request.POST.get('wikipedia_page_title', False) wikipedia_photo_url = request.POST.get('wikipedia_photo_url', False) state_served_code = request.POST.get('state_served_code', False) # A positive value in google_civic_election_id or add_organization_button means we want to create a voter guide # for this org for this election google_civic_election_id = request.POST.get('google_civic_election_id', 0) # add_organization_button = request.POST.get('add_organization_button', False) # Filter incoming data organization_twitter_handle = extract_twitter_handle_from_text_string(organization_twitter_handle) # Check to see if this organization is already being used anywhere organization_on_stage_found = False try: organization_query = Organization.objects.filter(id=organization_id) if organization_query.count(): organization_on_stage = organization_query[0] organization_on_stage_found = True except Exception as e: handle_record_not_found_exception(e, logger=logger) try: if organization_on_stage_found: # Update if organization_name is not False: organization_on_stage.organization_name = organization_name if organization_twitter_handle is not False: organization_on_stage.organization_twitter_handle = organization_twitter_handle if organization_facebook is not False: organization_on_stage.organization_facebook = organization_facebook if organization_website is not False: organization_on_stage.organization_website = organization_website if wikipedia_page_title is not False: organization_on_stage.wikipedia_page_title = wikipedia_page_title if wikipedia_photo_url is not False: organization_on_stage.wikipedia_photo_url = wikipedia_photo_url if state_served_code is not False: organization_on_stage.state_served_code = state_served_code organization_on_stage.save() organization_id = organization_on_stage.id organization_we_vote_id = organization_on_stage.we_vote_id messages.add_message(request, messages.INFO, 'Organization updated.') else: # Create new # But first double-check that we don't have an org entry already organization_email = '' organization_list_manager = OrganizationListManager() results = organization_list_manager.organization_search_find_any_possibilities( organization_name, organization_twitter_handle, organization_website, organization_email) if results['organizations_found']: organizations_list = results['organizations_list'] organizations_count = len(organizations_list) messages.add_message(request, messages.INFO, 'We found {count} existing organizations ' 'that might match.'.format(count=organizations_count)) messages_on_stage = get_messages(request) template_values = { 'messages_on_stage': messages_on_stage, 'organizations_list': organizations_list, 'organization_name': organization_name, 'organization_twitter_handle': organization_twitter_handle, 'organization_facebook': organization_facebook, 'organization_website': organization_website, 'wikipedia_page_title': wikipedia_page_title, 'wikipedia_photo_url': wikipedia_photo_url, } return render(request, 'organization/organization_edit.html', template_values) minimum_required_variables_exist = positive_value_exists(organization_name) if not minimum_required_variables_exist: messages.add_message(request, messages.INFO, 'Missing name, which is required.') messages_on_stage = get_messages(request) template_values = { 'messages_on_stage': messages_on_stage, 'organization_name': organization_name, 'organization_twitter_handle': organization_twitter_handle, 'organization_facebook': organization_facebook, 'organization_website': organization_website, 'wikipedia_page_title': wikipedia_page_title, 'wikipedia_photo_url': wikipedia_photo_url, } return render(request, 'voter_guide/voter_guide_search.html', template_values) organization_on_stage = Organization( organization_name=organization_name, ) if organization_twitter_handle is not False: organization_on_stage.organization_twitter_handle = organization_twitter_handle if organization_facebook is not False: organization_on_stage.organization_facebook = organization_facebook if organization_website is not False: organization_on_stage.organization_website = organization_website if wikipedia_page_title is not False: organization_on_stage.wikipedia_page_title = wikipedia_page_title if wikipedia_photo_url is not False: organization_on_stage.wikipedia_photo_url = wikipedia_photo_url if state_served_code is not False: organization_on_stage.state_served_code = state_served_code organization_on_stage.save() organization_id = organization_on_stage.id organization_we_vote_id = organization_on_stage.we_vote_id messages.add_message(request, messages.INFO, 'New organization saved.') except Exception as e: messages.add_message(request, messages.ERROR, 'Could not save organization.' ' {error} [type: {error_type}]'.format(error=e, error_type=type(e))) return HttpResponseRedirect(reverse('organization:organization_list', args=())) # Create voter_guide for this election? if positive_value_exists(google_civic_election_id) and positive_value_exists(organization_we_vote_id): election_manager = ElectionManager() results = election_manager.retrieve_election(google_civic_election_id) if results['election_found']: election = results['election'] voter_guide_manager = VoterGuideManager() results = voter_guide_manager.update_or_create_organization_voter_guide_by_election_id( organization_we_vote_id, google_civic_election_id) if results['voter_guide_saved']: messages.add_message(request, messages.INFO, 'Voter guide for {election_name} election saved.' ''.format(election_name=election.election_name)) return HttpResponseRedirect(reverse('organization:organization_position_list', args=(organization_id,)) + "?google_civic_election_id=" + str(google_civic_election_id))
def voter_guide_search_process_view(request): """ Process the new or edit organization forms :param request: :return: """ authority_required = {'verified_volunteer'} # admin, verified_volunteer if not voter_has_authority(request, authority_required): return redirect_to_sign_in_page(request, authority_required) add_organization_button = request.POST.get('add_organization_button', False) if add_organization_button: return organization_edit_process_view(request) organization_name = request.POST.get('organization_name', '') organization_twitter_handle = request.POST.get( 'organization_twitter_handle', '') organization_facebook = request.POST.get('organization_facebook', '') organization_website = request.POST.get('organization_website', '') # state_served_code = request.POST.get('state_served_code', False) # Save this variable so we have it on the "Add New Position" page google_civic_election_id = request.POST.get('google_civic_election_id', 0) # Filter incoming data organization_twitter_handle = extract_twitter_handle_from_text_string( organization_twitter_handle) # Search for organizations that match organization_email = '' organization_list_manager = OrganizationListManager() results = organization_list_manager.organization_search_find_any_possibilities( organization_name, organization_twitter_handle, organization_website, organization_email, organization_facebook) if results['organizations_found']: organizations_list = results['organizations_list'] organizations_count = len(organizations_list) messages.add_message( request, messages.INFO, 'We found {count} existing organization(s) ' 'that might match.'.format(count=organizations_count)) else: organizations_list = [] messages.add_message( request, messages.INFO, 'No voter guides found with those search terms. ' 'Please try again. ') election_manager = ElectionManager() upcoming_election_list = [] results = election_manager.retrieve_upcoming_elections() if results['success']: upcoming_election_list = results['election_list'] messages_on_stage = get_messages(request) template_values = { 'messages_on_stage': messages_on_stage, 'organizations_list': organizations_list, 'organization_name': organization_name, 'organization_twitter_handle': organization_twitter_handle, 'organization_facebook': organization_facebook, 'organization_website': organization_website, 'upcoming_election_list': upcoming_election_list, 'google_civic_election_id': google_civic_election_id, } return render(request, 'voter_guide/voter_guide_search.html', template_values)
def voter_guide_search_process_view(request): """ Process the new or edit organization forms :param request: :return: """ authority_required = {'verified_volunteer'} # admin, verified_volunteer if not voter_has_authority(request, authority_required): return redirect_to_sign_in_page(request, authority_required) add_organization_button = request.POST.get('add_organization_button', False) if add_organization_button: return organization_edit_process_view(request) organization_name = request.POST.get('organization_name', '') organization_twitter_handle = request.POST.get('organization_twitter_handle', '') organization_facebook = request.POST.get('organization_facebook', '') organization_website = request.POST.get('organization_website', '') # state_served_code = request.POST.get('state_served_code', False) # Save this variable so we have it on the "Add New Position" page google_civic_election_id = request.POST.get('google_civic_election_id', 0) # Filter incoming data organization_twitter_handle = extract_twitter_handle_from_text_string(organization_twitter_handle) # Search for organizations that match organization_email = '' organization_list_manager = OrganizationListManager() results = organization_list_manager.organization_search_find_any_possibilities( organization_name, organization_twitter_handle, organization_website, organization_email, organization_facebook) if results['organizations_found']: organizations_list = results['organizations_list'] organizations_count = len(organizations_list) messages.add_message(request, messages.INFO, 'We found {count} existing organization(s) ' 'that might match.'.format(count=organizations_count)) else: organizations_list = [] messages.add_message(request, messages.INFO, 'No voter guides found with those search terms. ' 'Please try again. ') election_manager = ElectionManager() upcoming_election_list = [] results = election_manager.retrieve_upcoming_elections() if results['success']: upcoming_election_list = results['election_list'] messages_on_stage = get_messages(request) template_values = { 'messages_on_stage': messages_on_stage, 'organizations_list': organizations_list, 'organization_name': organization_name, 'organization_twitter_handle': organization_twitter_handle, 'organization_facebook': organization_facebook, 'organization_website': organization_website, 'upcoming_election_list': upcoming_election_list, 'google_civic_election_id': google_civic_election_id, } return render(request, 'voter_guide/voter_guide_search.html', template_values)
def organization_edit_process_view(request): """ Process the new or edit organization forms :param request: :return: """ 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_id = convert_to_int(request.POST.get('organization_id', 0)) organization_name = request.POST.get('organization_name', '') organization_twitter_handle = request.POST.get('organization_twitter_handle', False) organization_facebook = request.POST.get('organization_facebook', False) organization_website = request.POST.get('organization_website', False) wikipedia_page_title = request.POST.get('wikipedia_page_title', False) wikipedia_photo_url = request.POST.get('wikipedia_photo_url', False) state_served_code = request.POST.get('state_served_code', False) # A positive value in google_civic_election_id or add_organization_button means we want to create a voter guide # for this org for this election google_civic_election_id = request.POST.get('google_civic_election_id', 0) # add_organization_button = request.POST.get('add_organization_button', False) # Filter incoming data organization_twitter_handle = extract_twitter_handle_from_text_string(organization_twitter_handle) # Check to see if this organization is already being used anywhere organization_on_stage_found = False try: organization_query = Organization.objects.filter(id=organization_id) if organization_query.count(): organization_on_stage = organization_query[0] organization_on_stage_found = True except Exception as e: handle_record_not_found_exception(e, logger=logger) try: if organization_on_stage_found: # Update if organization_name is not False: organization_on_stage.organization_name = organization_name if organization_twitter_handle is not False: organization_on_stage.organization_twitter_handle = organization_twitter_handle if organization_facebook is not False: organization_on_stage.organization_facebook = organization_facebook if organization_website is not False: organization_on_stage.organization_website = organization_website if wikipedia_page_title is not False: organization_on_stage.wikipedia_page_title = wikipedia_page_title if wikipedia_photo_url is not False: organization_on_stage.wikipedia_photo_url = wikipedia_photo_url if state_served_code is not False: organization_on_stage.state_served_code = state_served_code organization_on_stage.save() organization_id = organization_on_stage.id organization_we_vote_id = organization_on_stage.we_vote_id messages.add_message(request, messages.INFO, 'Organization updated.') else: # Create new # But first double-check that we don't have an org entry already organization_email = '' organization_list_manager = OrganizationListManager() results = organization_list_manager.organization_search_find_any_possibilities( organization_name, organization_twitter_handle, organization_website, organization_email) if results['organizations_found']: organizations_list = results['organizations_list'] organizations_count = len(organizations_list) messages.add_message(request, messages.INFO, 'We found {count} existing organizations ' 'that might match.'.format(count=organizations_count)) messages_on_stage = get_messages(request) template_values = { 'messages_on_stage': messages_on_stage, 'organizations_list': organizations_list, 'organization_name': organization_name, 'organization_twitter_handle': organization_twitter_handle, 'organization_facebook': organization_facebook, 'organization_website': organization_website, 'wikipedia_page_title': wikipedia_page_title, 'wikipedia_photo_url': wikipedia_photo_url, } return render(request, 'organization/organization_edit.html', template_values) minimum_required_variables_exist = positive_value_exists(organization_name) if not minimum_required_variables_exist: messages.add_message(request, messages.INFO, 'Missing name, which is required.') messages_on_stage = get_messages(request) template_values = { 'messages_on_stage': messages_on_stage, 'organization_name': organization_name, 'organization_twitter_handle': organization_twitter_handle, 'organization_facebook': organization_facebook, 'organization_website': organization_website, 'wikipedia_page_title': wikipedia_page_title, 'wikipedia_photo_url': wikipedia_photo_url, } return render(request, 'voter_guide/voter_guide_search.html', template_values) organization_on_stage = Organization( organization_name=organization_name, ) if organization_twitter_handle is not False: organization_on_stage.organization_twitter_handle = organization_twitter_handle if organization_facebook is not False: organization_on_stage.organization_facebook = organization_facebook if organization_website is not False: organization_on_stage.organization_website = organization_website if wikipedia_page_title is not False: organization_on_stage.wikipedia_page_title = wikipedia_page_title if wikipedia_photo_url is not False: organization_on_stage.wikipedia_photo_url = wikipedia_photo_url if state_served_code is not False: organization_on_stage.state_served_code = state_served_code organization_on_stage.save() organization_id = organization_on_stage.id organization_we_vote_id = organization_on_stage.we_vote_id messages.add_message(request, messages.INFO, 'New organization saved.') except Exception as e: messages.add_message(request, messages.ERROR, 'Could not save organization.' ' {error} [type: {error_type}]'.format(error=e, error_type=type(e))) return HttpResponseRedirect(reverse('organization:organization_list', args=())) # Create voter_guide for this election? if positive_value_exists(google_civic_election_id) and positive_value_exists(organization_we_vote_id): election_manager = ElectionManager() results = election_manager.retrieve_election(google_civic_election_id) if results['election_found']: election = results['election'] voter_guide_manager = VoterGuideManager() results = voter_guide_manager.update_or_create_organization_voter_guide_by_election_id( organization_we_vote_id, google_civic_election_id) if results['voter_guide_saved']: messages.add_message(request, messages.INFO, 'Voter guide for {election_name} election saved.' ''.format(election_name=election.election_name)) return HttpResponseRedirect(reverse('organization:organization_position_list', args=(organization_id,)) + "?google_civic_election_id=" + str(google_civic_election_id))