def retrieve_vote_smart_candidate( self, vote_smart_candidate_id=None, first_name=None, last_name=None, state_code=None): """ We want to return one and only one candidate :param vote_smart_candidate_id: :param first_name: :param last_name: :param state_code: :return: """ error_result = False exception_does_not_exist = False exception_multiple_object_returned = False vote_smart_candidate = VoteSmartCandidate() try: if positive_value_exists(vote_smart_candidate_id): vote_smart_candidate = VoteSmartCandidate.objects.get(candidateId=vote_smart_candidate_id) vote_smart_candidate_id = convert_to_int(vote_smart_candidate.candidateId) status = "RETRIEVE_VOTE_SMART_CANDIDATE_FOUND_BY_ID" elif positive_value_exists(first_name) or positive_value_exists(last_name): candidate_queryset = VoteSmartCandidate.objects.all() if positive_value_exists(first_name): first_name = first_name.replace("`", "'") # Vote Smart doesn't like this kind of apostrophe: ` candidate_queryset = candidate_queryset.filter(Q(firstName__istartswith=first_name) | Q(nickName__istartswith=first_name) | Q(preferredName__istartswith=first_name)) if positive_value_exists(last_name): last_name = last_name.replace("`", "'") # Vote Smart doesn't like this kind of apostrophe: ` candidate_queryset = candidate_queryset.filter(lastName__iexact=last_name) if positive_value_exists(state_code): candidate_queryset = candidate_queryset.filter(Q(electionStateId__iexact=state_code) | Q(electionStateId__iexact="NA")) vote_smart_candidate_list = list(candidate_queryset[:1]) if vote_smart_candidate_list: vote_smart_candidate = vote_smart_candidate_list[0] else: vote_smart_candidate = VoteSmartCandidate() vote_smart_candidate_id = convert_to_int(vote_smart_candidate.candidateId) status = "RETRIEVE_VOTE_SMART_CANDIDATE_FOUND_BY_NAME" else: status = "RETRIEVE_VOTE_SMART_CANDIDATE_SEARCH_INDEX_MISSING" except VoteSmartCandidate.MultipleObjectsReturned as e: exception_multiple_object_returned = True status = "RETRIEVE_VOTE_SMART_CANDIDATE_MULTIPLE_OBJECTS_RETURNED" except VoteSmartCandidate.DoesNotExist: exception_does_not_exist = True status = "RETRIEVE_VOTE_SMART_CANDIDATE_NOT_FOUND" results = { 'success': True if positive_value_exists(vote_smart_candidate_id) else False, 'status': status, 'error_result': error_result, 'DoesNotExist': exception_does_not_exist, 'MultipleObjectsReturned': exception_multiple_object_returned, 'vote_smart_candidate_found': True if positive_value_exists(vote_smart_candidate_id) else False, 'vote_smart_candidate_id': vote_smart_candidate_id, 'vote_smart_candidate': vote_smart_candidate, } return results
def organization_edit_existing_position_form_view(request, organization_id, position_id): """ In edit, you can only change your stance and comments, not who or what the position is about :param request: :param organization_id: :param position_id: :return: """ # If person isn't signed in, we don't want to let them visit this page yet if not request.user.is_authenticated(): return redirect('/admin') messages_on_stage = get_messages(request) organization_id = convert_to_int(organization_id) position_id = convert_to_int(position_id) organization_on_stage_found = False try: organization_on_stage = Organization.objects.get(id=organization_id) organization_on_stage_found = True except Organization.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e) except Organization.DoesNotExist as e: # This is fine, create new handle_exception_silently(e) if not organization_on_stage_found: messages.add_message(request, messages.INFO, 'Could not find organization when trying to edit a position.') return HttpResponseRedirect(reverse('organization:organization_position_list', args=([organization_id]))) # Get the existing position organization_position_on_stage = PositionEntered() organization_position_on_stage_found = False position_entered_manager = PositionEnteredManager() results = position_entered_manager.retrieve_position_from_id(position_id) if results['position_found']: organization_position_on_stage_found = True organization_position_on_stage = results['position'] if not organization_position_on_stage_found: messages.add_message(request, messages.INFO, 'Could not find organization position when trying to edit.') return HttpResponseRedirect(reverse('organization:organization_position_list', args=([organization_id]))) # Note: We have access to the candidate campaign through organization_position_on_stage.candidate_campaign if organization_position_on_stage_found: template_values = { 'is_in_edit_mode': True, 'messages_on_stage': messages_on_stage, 'organization': organization_on_stage, 'organization_position': organization_position_on_stage, 'possible_stances_list': ORGANIZATION_STANCE_CHOICES, 'stance_selected': organization_position_on_stage.stance, } return render(request, 'organization/organization_position_edit.html', template_values)
def organization_edit_existing_position_form_view(request, organization_id, position_id): """ In edit, you can only change your stance and comments, not who or what the position is about :param request: :param organization_id: :param position_id: :return: """ # If person isn't signed in, we don't want to let them visit this page yet if not request.user.is_authenticated(): return redirect('/admin') messages_on_stage = get_messages(request) organization_id = convert_to_int(organization_id) position_id = convert_to_int(position_id) organization_on_stage_found = False try: organization_on_stage = Organization.objects.get(id=organization_id) organization_on_stage_found = True except Organization.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except Organization.DoesNotExist: # This is fine, create new pass if not organization_on_stage_found: messages.add_message(request, messages.INFO, 'Could not find organization when trying to edit a position.') return HttpResponseRedirect(reverse('organization:organization_position_list', args=([organization_id]))) # Get the existing position organization_position_on_stage = PositionEntered() organization_position_on_stage_found = False position_entered_manager = PositionEnteredManager() results = position_entered_manager.retrieve_position_from_id(position_id) if results['position_found']: organization_position_on_stage_found = True organization_position_on_stage = results['position'] if not organization_position_on_stage_found: messages.add_message(request, messages.INFO, 'Could not find organization position when trying to edit.') return HttpResponseRedirect(reverse('organization:organization_position_list', args=([organization_id]))) # Note: We have access to the candidate campaign through organization_position_on_stage.candidate_campaign if organization_position_on_stage_found: template_values = { 'is_in_edit_mode': True, 'messages_on_stage': messages_on_stage, 'organization': organization_on_stage, 'organization_position': organization_position_on_stage, 'possible_stances_list': ORGANIZATION_STANCE_CHOICES, 'stance_selected': organization_position_on_stage.stance, } return render(request, 'organization/organization_position_edit.html', template_values)
def retrieve_vote_smart_official( self, vote_smart_candidate_id=None, first_name=None, last_name=None, state_code=None): """ We want to return one and only one official :param vote_smart_candidate_id: :param first_name: :param last_name: :param state_code: :return: """ error_result = False exception_does_not_exist = False exception_multiple_object_returned = False vote_smart_official = VoteSmartOfficial() try: if positive_value_exists(vote_smart_candidate_id): vote_smart_official = VoteSmartOfficial.objects.get(candidateId=vote_smart_candidate_id) vote_smart_candidate_id = convert_to_int(vote_smart_official.candidateId) status = "RETRIEVE_VOTE_SMART_OFFICIAL_FOUND_BY_ID" elif positive_value_exists(first_name) or positive_value_exists(last_name): official_queryset = VoteSmartOfficial.objects.all() if positive_value_exists(first_name): official_queryset = official_queryset.filter(firstName__istartswith=first_name) if positive_value_exists(last_name): official_queryset = official_queryset.filter(lastName__iexact=last_name) if positive_value_exists(state_code): official_queryset = official_queryset.filter(officeStateId__iexact=state_code) vote_smart_official_list = list(official_queryset[:1]) if vote_smart_official_list: vote_smart_official = vote_smart_official_list[0] else: vote_smart_official = VoteSmartOfficial() vote_smart_candidate_id = convert_to_int(vote_smart_official.candidateId) status = "RETRIEVE_VOTE_SMART_OFFICIAL_FOUND_BY_NAME" else: status = "RETRIEVE_VOTE_SMART_OFFICIAL_SEARCH_INDEX_MISSING" except VoteSmartOfficial.MultipleObjectsReturned as e: exception_multiple_object_returned = True status = "RETRIEVE_VOTE_SMART_OFFICIAL_MULTIPLE_OBJECTS_RETURNED" except VoteSmartOfficial.DoesNotExist: exception_does_not_exist = True status = "RETRIEVE_VOTE_SMART_OFFICIAL_NOT_FOUND" results = { 'success': True if positive_value_exists(vote_smart_candidate_id) else False, 'status': status, 'error_result': error_result, 'DoesNotExist': exception_does_not_exist, 'MultipleObjectsReturned': exception_multiple_object_returned, 'vote_smart_official_found': True if positive_value_exists(vote_smart_candidate_id) else False, 'vote_smart_candidate_id': vote_smart_candidate_id, 'vote_smart_official': vote_smart_official, } return results
def organization_delete_existing_position_process_form_view( request, organization_id, position_id): """ :param request: :param organization_id: :param position_id: :return: """ # If person isn't signed in, we don't want to let them visit this page yet if not request.user.is_authenticated(): return redirect('/admin') organization_id = convert_to_int(organization_id) position_id = convert_to_int(position_id) # Get the existing position organization_position_on_stage_found = False if position_id > 0: organization_position_on_stage = PositionEntered() organization_position_on_stage_found = False position_entered_manager = PositionEnteredManager() results = position_entered_manager.retrieve_position_from_id( position_id) if results['position_found']: organization_position_on_stage_found = True organization_position_on_stage = results['position'] if not organization_position_on_stage_found: messages.add_message( request, messages.INFO, "Could not find this organization's position when trying to delete." ) return HttpResponseRedirect( reverse('organization:organization_position_list', args=([organization_id]))) try: organization_position_on_stage.delete() except Exception as e: handle_record_not_deleted_exception(e) messages.add_message(request, messages.ERROR, 'Could not delete position.') return HttpResponseRedirect( reverse('organization:organization_position_list', args=([organization_id]))) messages.add_message(request, messages.INFO, 'Position deleted.') return HttpResponseRedirect( reverse('organization:organization_position_list', args=([organization_id])))
def voter_edit_process_view(request): """ Process the new or edit voter forms :param request: :return: """ voter_id = convert_to_int(request.POST['voter_id']) voter_name = request.POST['voter_name'] # Check to see if this voter is already being used anywhere voter_on_stage_found = False try: voter_query = Voter.objects.filter(id=voter_id) if len(voter_query): voter_on_stage = voter_query[0] voter_on_stage_found = True except Exception as e: handle_record_not_found_exception(e, logger=logger) try: if voter_on_stage_found: # Update voter_on_stage.voter_name = voter_name voter_on_stage.save() messages.add_message(request, messages.INFO, 'Voter updated.') else: # Create new messages.add_message(request, messages.INFO, 'We do not support adding new Voters.') except Exception as e: handle_record_not_saved_exception(e, logger=logger) messages.add_message(request, messages.ERROR, 'Could not save voter.') return HttpResponseRedirect(reverse('voter:voter_list', args=()))
def election_summary_view(request, election_local_id): messages_on_stage = get_messages(request) election_local_id = convert_to_int(election_local_id) election_on_stage_found = False election_on_stage = Election() try: election_on_stage = Election.objects.get(id=election_local_id) election_on_stage_found = True except Election.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except Election.DoesNotExist: # This is fine, create new pass if election_on_stage_found: template_values = { 'messages_on_stage': messages_on_stage, 'election': election_on_stage, } else: template_values = { 'messages_on_stage': messages_on_stage, } return render(request, 'election/election_summary.html', template_values)
def organization_edit_view(request, organization_id): # If person isn't signed in, we don't want to let them visit this page yet if not request.user.is_authenticated(): return redirect('/admin') messages_on_stage = get_messages(request) organization_id = convert_to_int(organization_id) organization_on_stage_found = False try: organization_on_stage = Organization.objects.get(id=organization_id) organization_on_stage_found = True except Organization.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e) except Organization.DoesNotExist as e: # This is fine, create new handle_exception_silently(e) if organization_on_stage_found: template_values = { 'messages_on_stage': messages_on_stage, 'organization': organization_on_stage, } else: template_values = { 'messages_on_stage': messages_on_stage, } return render(request, 'organization/organization_edit.html', template_values)
def organization_add_new_position_form_view(request, organization_id): messages_on_stage = get_messages(request) organization_id = convert_to_int(organization_id) all_is_well = True organization_on_stage_found = False try: organization_on_stage = Organization.objects.get(id=organization_id) organization_on_stage_found = True except Organization.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except Organization.DoesNotExist: # This is fine, create new pass if not organization_on_stage_found: messages.add_message(request, messages.INFO, 'Could not find organization when trying to create a new position.') return HttpResponseRedirect(reverse('organization:organization_position_list', args=([organization_id]))) # Prepare a drop down of candidates competing in this election candidate_campaign_list = CandidateCampaignList() candidate_campaigns_for_this_election_list \ = candidate_campaign_list.retrieve_candidate_campaigns_for_this_election_list() if all_is_well: template_values = { 'candidate_campaigns_for_this_election_list': candidate_campaigns_for_this_election_list, 'messages_on_stage': messages_on_stage, 'organization': organization_on_stage, 'organization_position_candidate_campaign_id': 0, 'possible_stances_list': ORGANIZATION_STANCE_CHOICES, 'stance_selected': SUPPORT, # Default stance } return render(request, 'organization/organization_position_edit.html', template_values)
def fetch_next_we_vote_id_last_voter_integer(): we_vote_settings_manager = WeVoteSettingsManager() we_vote_id_last_voter_integer = we_vote_settings_manager.fetch_setting('we_vote_id_last_voter_integer') we_vote_id_last_voter_integer = convert_to_int(we_vote_id_last_voter_integer) we_vote_id_last_voter_integer += 1 we_vote_settings_manager.save_setting('we_vote_id_last_voter_integer', we_vote_id_last_voter_integer) return we_vote_id_last_voter_integer
def organization_add_new_position_form_view(request, organization_id): messages_on_stage = get_messages(request) organization_id = convert_to_int(organization_id) all_is_well = True organization_on_stage_found = False try: organization_on_stage = Organization.objects.get(id=organization_id) organization_on_stage_found = True except Organization.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e) except Organization.DoesNotExist as e: # This is fine, create new handle_exception_silently(e) if not organization_on_stage_found: messages.add_message(request, messages.INFO, 'Could not find organization when trying to create a new position.') return HttpResponseRedirect(reverse('organization:organization_position_list', args=([organization_id]))) # Prepare a drop down of candidates competing in this election candidate_campaign_list = CandidateCampaignList() candidate_campaigns_for_this_election_list \ = candidate_campaign_list.retrieve_candidate_campaigns_for_this_election_list() if all_is_well: template_values = { 'candidate_campaigns_for_this_election_list': candidate_campaigns_for_this_election_list, 'messages_on_stage': messages_on_stage, 'organization': organization_on_stage, 'organization_position_candidate_campaign_id': 0, 'possible_stances_list': ORGANIZATION_STANCE_CHOICES, 'stance_selected': SUPPORT, # Default stance } return render(request, 'organization/organization_position_edit.html', template_values)
def measure_edit_view(request, measure_id): messages_on_stage = get_messages(request) measure_id = convert_to_int(measure_id) measure_on_stage_found = False try: measure_on_stage = ContestMeasure.objects.get(id=measure_id) measure_on_stage_found = True except ContestMeasure.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) measure_on_stage = ContestMeasure() except ContestMeasure.DoesNotExist: # This is fine, create new measure_on_stage = ContestMeasure() pass if measure_on_stage_found: template_values = { 'messages_on_stage': messages_on_stage, 'measure': measure_on_stage, } else: template_values = { 'messages_on_stage': messages_on_stage, } return render(request, 'measure/measure_edit.html', template_values)
def measure_summary_view(request, measure_id): messages_on_stage = get_messages(request) measure_id = convert_to_int(measure_id) measure_on_stage_found = False measure_on_stage = ContestMeasure() google_civic_election_id = request.GET.get('google_civic_election_id', 0) try: measure_on_stage = ContestMeasure.objects.get(id=measure_id) measure_on_stage_found = True except ContestMeasure.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except ContestMeasure.DoesNotExist: # This is fine, create new pass election_list = Election.objects.order_by('-election_day_text') if measure_on_stage_found: template_values = { 'messages_on_stage': messages_on_stage, 'measure': measure_on_stage, 'election_list': election_list, 'google_civic_election_id': google_civic_election_id, } else: template_values = { 'messages_on_stage': messages_on_stage, } return render(request, 'measure/measure_summary.html', template_values)
def fetch_next_id_we_vote_last_org_integer(): we_vote_settings_manager = WeVoteSettingsManager() id_we_vote_last_org_integer = we_vote_settings_manager.fetch_setting('id_we_vote_last_org_integer') id_we_vote_last_org_integer = convert_to_int(id_we_vote_last_org_integer) id_we_vote_last_org_integer += 1 we_vote_settings_manager.save_setting('id_we_vote_last_org_integer', id_we_vote_last_org_integer) return id_we_vote_last_org_integer
def election_edit_process_view(request): """ Process the new or edit election forms :param request: :return: """ election_id = convert_to_int(request.POST["election_id"]) election_name = request.POST["election_name"] # Check to see if this election is already being used anywhere election_on_stage_found = False try: election_query = Election.objects.filter(id=election_id) if len(election_query): election_on_stage = election_query[0] election_on_stage_found = True except Exception as e: handle_record_not_found_exception(e, logger=logger) try: if election_on_stage_found: # Update election_on_stage.election_name = election_name election_on_stage.save() messages.add_message(request, messages.INFO, "Election updated.") else: # Create new election_on_stage = Election(election_name=election_name) election_on_stage.save() messages.add_message(request, messages.INFO, "New election saved.") except Exception as e: handle_record_not_saved_exception(e, logger=logger) messages.add_message(request, messages.ERROR, "Could not save election.") return HttpResponseRedirect(reverse("election:election_list", args=()))
def organization_edit_view(request, organization_id): # If person isn't signed in, we don't want to let them visit this page yet if not request.user.is_authenticated(): return redirect('/admin') messages_on_stage = get_messages(request) organization_id = convert_to_int(organization_id) organization_on_stage_found = False try: organization_on_stage = Organization.objects.get(id=organization_id) organization_on_stage_found = True except Organization.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except Organization.DoesNotExist: # This is fine, create new pass if organization_on_stage_found: template_values = { 'messages_on_stage': messages_on_stage, 'organization': organization_on_stage, } else: template_values = { 'messages_on_stage': messages_on_stage, } return render(request, 'organization/organization_edit.html', template_values)
def polling_location_edit_process_view(request): """ Process the new or edit polling_location forms :param request: :return: """ polling_location_id = convert_to_int(request.POST['polling_location_id']) polling_location_name = request.POST['polling_location_name'] # Check to see if this polling_location is already being used anywhere polling_location_on_stage_found = False try: polling_location_query = PollingLocation.objects.filter(id=polling_location_id) if len(polling_location_query): polling_location_on_stage = polling_location_query[0] polling_location_on_stage_found = True except Exception as e: handle_record_not_found_exception(e, logger=logger) try: if polling_location_on_stage_found: # Update polling_location_on_stage.polling_location_name = polling_location_name polling_location_on_stage.save() messages.add_message(request, messages.INFO, 'PollingLocation updated.') else: # Create new messages.add_message(request, messages.INFO, 'We do not support adding new polling locations.') except Exception as e: handle_record_not_saved_exception(e, logger=logger) messages.add_message(request, messages.ERROR, 'Could not save polling_location.') return HttpResponseRedirect(reverse('polling_location:polling_location_list', args=()))
def fetch_next_id_we_vote_last_position_integer(): we_vote_settings_manager = WeVoteSettingsManager() id_we_vote_last_position_integer = we_vote_settings_manager.fetch_setting("id_we_vote_last_position_integer") id_we_vote_last_position_integer = convert_to_int(id_we_vote_last_position_integer) id_we_vote_last_position_integer += 1 we_vote_settings_manager.save_setting("id_we_vote_last_position_integer", id_we_vote_last_position_integer) return id_we_vote_last_position_integer
def retrieve_candidate_photos_for_election_view(request, election_id): google_civic_election_id = convert_to_int(election_id) # We only want to process if a google_civic_election_id comes in if not positive_value_exists(google_civic_election_id): messages.add_message(request, messages.ERROR, "Google Civic Election ID required.") return HttpResponseRedirect(reverse("candidate:candidate_list", args=())) try: candidate_list = CandidateCampaign.objects.order_by("candidate_name") if positive_value_exists(google_civic_election_id): candidate_list = candidate_list.filter(google_civic_election_id=google_civic_election_id) except CandidateCampaign.DoesNotExist: pass display_messages = False force_retrieve = False # Loop through all of the candidates in this election for we_vote_candidate in candidate_list: retrieve_candidate_results = retrieve_candidate_photos(we_vote_candidate, force_retrieve) if retrieve_candidate_results["status"] and display_messages: messages.add_message(request, messages.INFO, retrieve_candidate_results["status"]) return HttpResponseRedirect( reverse("candidate:candidate_list", args=()) + "?google_civic_election_id={var}".format(var=google_civic_election_id) )
def organization_retrieve_for_api(organization_id, organization_we_vote_id): organization_id = convert_to_int(organization_id) we_vote_id = organization_we_vote_id.strip() if not positive_value_exists(organization_id) and not positive_value_exists(organization_we_vote_id): json_data = { 'status': "ORGANIZATION_RETRIEVE_BOTH_IDS_MISSING", 'success': False, 'organization_id': organization_id, 'organization_we_vote_id': organization_we_vote_id, 'organization_name': '', 'organization_email': '', 'organization_website': '', 'organization_twitter_handle': '', 'organization_facebook': '', 'organization_photo_url': '', } return HttpResponse(json.dumps(json_data), content_type='application/json') organization_manager = OrganizationManager() results = organization_manager.retrieve_organization(organization_id, organization_we_vote_id) if results['organization_found']: organization = results['organization'] json_data = { 'success': True, 'status': results['status'], 'organization_id': organization.id, 'organization_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_handle': organization.organization_twitter_handle if positive_value_exists( organization.organization_twitter_handle) else '', 'organization_email': organization.organization_email if positive_value_exists(organization.organization_email) else '', 'organization_facebook': organization.organization_facebook if positive_value_exists(organization.organization_facebook) else '', 'organization_photo_url': organization.organization_photo_url() if positive_value_exists(organization.organization_photo_url()) else '', } return HttpResponse(json.dumps(json_data), content_type='application/json') else: json_data = { 'status': results['status'], 'success': False, 'organization_id': organization_id, 'organization_we_vote_id': we_vote_id, 'organization_name': '', 'organization_email': '', 'organization_website': '', 'organization_twitter_handle': '', 'organization_facebook': '', 'organization_photo_url': '', } return HttpResponse(json.dumps(json_data), content_type='application/json')
def fetch_next_id_we_vote_last_org_integer(): we_vote_settings_manager = WeVoteSettingsManager() id_we_vote_last_org_integer = we_vote_settings_manager.fetch_setting( 'id_we_vote_last_org_integer') id_we_vote_last_org_integer = convert_to_int(id_we_vote_last_org_integer) id_we_vote_last_org_integer += 1 we_vote_settings_manager.save_setting('id_we_vote_last_org_integer', id_we_vote_last_org_integer) return id_we_vote_last_org_integer
def quick_info_master_edit_view(request, quick_info_master_id): form_submitted = request.POST.get('form_submitted', False) quick_info_master_id = convert_to_int(quick_info_master_id) try: quick_info_master = QuickInfoMaster.objects.get(id=quick_info_master_id) except QuickInfoMaster.MultipleObjectsReturned as e: # Pretty unlikely that multiple objects have the same id messages.add_message(request, messages.ERROR, "This quick_info_master_id has multiple records.") return HttpResponseRedirect(reverse('quick_info:quick_info_master_list', args=())) except QuickInfoMaster.DoesNotExist: # This is fine, create new entry return quick_info_master_new_view(request) if positive_value_exists(form_submitted): # If the voter tried to submit an entry, and it didn't save, capture the changed values for display kind_of_ballot_item = request.POST.get('kind_of_ballot_item', False) language = request.POST.get('language', False) info_text = request.POST.get('info_text', False) info_html = request.POST.get('info_html', False) master_entry_name = request.POST.get('master_entry_name', False) more_info_credit = request.POST.get('more_info_credit', False) more_info_url = request.POST.get('more_info_url', False) # Write over the fields where a change has been made on the form if kind_of_ballot_item is not False: quick_info_master.kind_of_ballot_item = kind_of_ballot_item if language is not False: quick_info_master.language = language if master_entry_name is not False: quick_info_master.master_entry_name = master_entry_name if more_info_credit is not False: quick_info_master.more_info_credit = more_info_credit if more_info_url is not False: quick_info_master.more_info_url = more_info_url if info_text is not False: quick_info_master.info_text = info_text if info_html is not False: quick_info_master.info_html = info_html # ################################## # Above we have dealt with data provided by prior submit quick_info_list = QuickInfo.objects.order_by('id') # This order_by is temp quick_info_list = QuickInfo.objects.filter(quick_info_master_we_vote_id=quick_info_master.we_vote_id) messages_on_stage = get_messages(request) template_values = { 'messages_on_stage': messages_on_stage, 'ballot_item_choices': KIND_OF_BALLOT_ITEM_CHOICES, 'language_choices': LANGUAGE_CHOICES, 'quick_info_master': quick_info_master, 'quick_info_list': quick_info_list, } return render(request, 'quick_info/quick_info_master_edit.html', template_values)
def organization_delete_existing_position_process_form_view(request, organization_id, position_id): """ :param request: :param organization_id: :param position_id: :return: """ # If person isn't signed in, we don't want to let them visit this page yet if not request.user.is_authenticated(): return redirect('/admin') organization_id = convert_to_int(organization_id) position_id = convert_to_int(position_id) # Get the existing position organization_position_on_stage_found = False if position_id > 0: organization_position_on_stage = PositionEntered() organization_position_on_stage_found = False position_entered_manager = PositionEnteredManager() results = position_entered_manager.retrieve_position_from_id(position_id) if results['position_found']: organization_position_on_stage_found = True organization_position_on_stage = results['position'] if not organization_position_on_stage_found: messages.add_message(request, messages.INFO, "Could not find this organization's position when trying to delete.") return HttpResponseRedirect(reverse('organization:organization_position_list', args=([organization_id]))) try: organization_position_on_stage.delete() except Exception as e: handle_record_not_deleted_exception(e) messages.add_message(request, messages.ERROR, 'Could not delete position.') return HttpResponseRedirect(reverse('organization:organization_position_list', args=([organization_id]))) messages.add_message(request, messages.INFO, 'Position deleted.') return HttpResponseRedirect(reverse('organization:organization_position_list', args=([organization_id])))
def get_maximum_number_to_retrieve_from_request(request): if 'maximum_number_to_retrieve' in request.GET: maximum_number_to_retrieve = request.GET['maximum_number_to_retrieve'] else: maximum_number_to_retrieve = 20 if maximum_number_to_retrieve is "": maximum_number_to_retrieve = 20 else: maximum_number_to_retrieve = convert_to_int(maximum_number_to_retrieve) return maximum_number_to_retrieve
def positions_display_list_related_to_candidate_campaign( request, candidate_campaign_id, stance_we_are_looking_for): show_only_followed_positions = convert_to_int(request.GET.get('f', 0)) show_only_not_followed_positions = convert_to_int(request.GET.get('nf', 0)) messages_on_stage = get_messages(request) candidate_campaign_id = convert_to_int(candidate_campaign_id) position_list_manager = PositionListForCandidateCampaign() all_positions_list_for_candidate_campaign = \ position_list_manager.retrieve_all_positions_for_candidate_campaign( candidate_campaign_id, stance_we_are_looking_for) voter_device_id = get_voter_device_id(request) voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) follow_organization_list_manager = FollowOrganizationList() organizations_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_info_for_voter_simple_array(voter_id) if show_only_followed_positions == 1: logger.debug("positions_display_list: show only followed positions") list_to_display = position_list_manager.calculate_positions_followed_by_voter( voter_id, all_positions_list_for_candidate_campaign, organizations_followed_by_voter) elif show_only_not_followed_positions == 1: logger.debug( "positions_display_list: show only NOT followed positions") list_to_display = position_list_manager.calculate_positions_not_followed_by_voter( all_positions_list_for_candidate_campaign, organizations_followed_by_voter) else: list_to_display = all_positions_list_for_candidate_campaign template_values = { 'error': True, 'messages_on_stage': messages_on_stage, 'position_list': list_to_display, 'organizations_followed_by_voter': organizations_followed_by_voter, } return render(request, 'position/position_list.html', template_values)
def special_interest_group_rating_list_view(request, special_interest_group_id): messages_on_stage = get_messages(request) special_interest_group_id = convert_to_int(special_interest_group_id) # google_civic_election_id = request.GET.get('google_civic_election_id', 0) special_interest_group_found = False try: special_interest_group_query = VoteSmartSpecialInterestGroup.objects.filter(sigId=special_interest_group_id) if special_interest_group_query.count(): special_interest_group = special_interest_group_query[0] special_interest_group_found = True except VotesmartApiError as error_instance: # Catch the error message coming back from Vote Smart and pass it in the status error_message = error_instance.args status = "EXCEPTION_RAISED: {error_message}".format(error_message=error_message) print_to_log(logger=logger, exception_message_optional=status) special_interest_group_found = False if not special_interest_group_found: messages.add_message(request, messages.ERROR, 'Could not find special_interest_group when trying to retrieve ratings.') return HttpResponseRedirect(reverse('import_export_vote_smart:vote_smart_special_interest_group_list', args=())) else: rating_list_found = False try: rating_list = VoteSmartRatingOneCandidate.objects.order_by('-timeSpan') rating_list = rating_list.filter(sigId=special_interest_group_id) if len(rating_list): rating_list_found = True except VotesmartApiError as error_instance: # Catch the error message coming back from Vote Smart and pass it in the status error_message = error_instance.args status = "EXCEPTION_RAISED: {error_message}".format(error_message=error_message) print_to_log(logger=logger, exception_message_optional=status) # election_list = Election.objects.order_by('-election_day_text') if rating_list_found: template_values = { 'messages_on_stage': messages_on_stage, 'special_interest_group': special_interest_group, 'rating_list': rating_list, # 'election_list': election_list, # 'google_civic_election_id': google_civic_election_id, } else: template_values = { 'messages_on_stage': messages_on_stage, 'special_interest_group': special_interest_group, # 'election_list': election_list, # 'google_civic_election_id': google_civic_election_id, } return render(request, 'import_export_vote_smart/group_rating_list.html', template_values)
def positions_display_list_related_to_candidate_campaign(request, candidate_campaign_id, stance_we_are_looking_for): show_only_followed_positions = convert_to_int(request.GET.get('f', 0)) show_only_not_followed_positions = convert_to_int(request.GET.get('nf', 0)) messages_on_stage = get_messages(request) candidate_campaign_id = convert_to_int(candidate_campaign_id) position_list_manager = PositionListForCandidateCampaign() all_positions_list_for_candidate_campaign = \ position_list_manager.retrieve_all_positions_for_candidate_campaign( candidate_campaign_id, stance_we_are_looking_for) voter_device_id = get_voter_device_id(request) voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) follow_organization_list_manager = FollowOrganizationList() organizations_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_info_for_voter_simple_array(voter_id) if show_only_followed_positions == 1: logger.debug("positions_display_list: show only followed positions") list_to_display = position_list_manager.calculate_positions_followed_by_voter( voter_id, all_positions_list_for_candidate_campaign, organizations_followed_by_voter) elif show_only_not_followed_positions == 1: logger.debug("positions_display_list: show only NOT followed positions") list_to_display = position_list_manager.calculate_positions_not_followed_by_voter( all_positions_list_for_candidate_campaign, organizations_followed_by_voter) else: list_to_display = all_positions_list_for_candidate_campaign template_values = { 'error': True, 'messages_on_stage': messages_on_stage, 'position_list': list_to_display, 'organizations_followed_by_voter': organizations_followed_by_voter, } return render(request, 'position/position_list.html', template_values)
def organization_edit_process_view(request): """ Process the new or edit organization forms :param request: :return: """ organization_id = convert_to_int(request.POST['organization_id']) organization_name = request.POST['organization_name'] organization_twitter_handle = request.POST['organization_twitter_handle'] organization_website = request.POST['organization_website'] # 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 organization_on_stage.organization_name = organization_name organization_on_stage.organization_twitter_handle = organization_twitter_handle organization_on_stage.organization_website = organization_website organization_on_stage.save() organization_id = organization_on_stage.id messages.add_message(request, messages.INFO, 'Organization updated.') return HttpResponseRedirect(reverse('organization:organization_position_list', args=(organization_id,))) else: # Create new organization_on_stage = Organization( organization_name=organization_name, organization_twitter_handle=organization_twitter_handle, organization_website=organization_website ) organization_on_stage.save() organization_id = organization_on_stage.id messages.add_message(request, messages.INFO, 'New organization saved.') return HttpResponseRedirect(reverse('organization:organization_position_list', args=(organization_id,))) except Exception as e: handle_record_not_saved_exception(e, logger=logger) messages.add_message(request, messages.ERROR, 'Could not save organization.' ' {error} [type: {error_type}]'.format(error=e.message, error_type=type(e))) return HttpResponseRedirect(reverse('organization:organization_list', args=()))
def organization_position_list_view(request, organization_id): messages_on_stage = get_messages(request) organization_id = convert_to_int(organization_id) google_civic_election_id = request.GET.get('google_civic_election_id', 0) 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) organization_on_stage_found = False if not organization_on_stage_found: messages.add_message(request, messages.ERROR, 'Could not find organization when trying to retrieve positions.') return HttpResponseRedirect(reverse('organization:organization_list', args=())) else: organization_position_list_found = False try: organization_position_list = PositionEntered.objects.order_by('stance') organization_position_list = organization_position_list.filter(organization_id=organization_id) if positive_value_exists(google_civic_election_id): organization_position_list = organization_position_list.filter( google_civic_election_id=google_civic_election_id) if len(organization_position_list): organization_position_list_found = True except Exception as e: handle_record_not_found_exception(e, logger=logger) election_list = Election.objects.order_by('-election_day_text') if organization_position_list_found: template_values = { 'messages_on_stage': messages_on_stage, 'organization': organization_on_stage, 'organization_position_list': organization_position_list, 'election_list': election_list, 'google_civic_election_id': google_civic_election_id, } else: template_values = { 'messages_on_stage': messages_on_stage, 'organization': organization_on_stage, 'election_list': election_list, 'google_civic_election_id': google_civic_election_id, } return render(request, 'organization/organization_position_list.html', template_values)
def candidate_edit_view(request, candidate_id): messages_on_stage = get_messages(request) candidate_id = convert_to_int(candidate_id) candidate_on_stage_found = False candidate_on_stage = CandidateCampaign() try: candidate_on_stage = CandidateCampaign.objects.get(id=candidate_id) candidate_on_stage_found = True except CandidateCampaign.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except CandidateCampaign.DoesNotExist: # This is fine, create new pass if candidate_on_stage_found: # Working with Vote Smart data try: vote_smart_candidate_id = candidate_on_stage.vote_smart_id rating_list_query = VoteSmartRatingOneCandidate.objects.order_by("-timeSpan") # Desc order rating_list = rating_list_query.filter(candidateId=vote_smart_candidate_id) except VotesmartApiError as error_instance: # Catch the error message coming back from Vote Smart and pass it in the status error_message = error_instance.args status = "EXCEPTION_RAISED: {error_message}".format(error_message=error_message) print_to_log(logger=logger, exception_message_optional=status) rating_list = [] # Working with We Vote Positions candidate_position_list_found = False try: candidate_position_list = PositionEntered.objects.order_by("stance") candidate_position_list = candidate_position_list.filter(candidate_campaign_id=candidate_id) # if positive_value_exists(google_civic_election_id): # organization_position_list = candidate_position_list.filter( # google_civic_election_id=google_civic_election_id) except Exception as e: handle_record_not_found_exception(e, logger=logger) candidate_position_list = [] template_values = { "messages_on_stage": messages_on_stage, "candidate": candidate_on_stage, "rating_list": rating_list, "candidate_position_list": candidate_position_list, } else: template_values = {"messages_on_stage": messages_on_stage} return render(request, "candidate/candidate_edit.html", template_values)
def retrieve_voter(self, voter_id, email='', voter_we_vote_id=''): voter_id = convert_to_int(voter_id) if not validate_email(email): # We do not want to search for an invalid email email = None if positive_value_exists(voter_we_vote_id): voter_we_vote_id = voter_we_vote_id.strip() error_result = False exception_does_not_exist = False exception_multiple_object_returned = False voter_on_stage = Voter() try: if positive_value_exists(voter_id): voter_on_stage = Voter.objects.get(id=voter_id) # If still here, we found an existing voter voter_id = voter_on_stage.id elif email is not '' and email is not None: voter_on_stage = Voter.objects.get( email=email) # If still here, we found an existing voter voter_id = voter_on_stage.id elif positive_value_exists(voter_we_vote_id): voter_on_stage = Voter.objects.get( we_vote_id=voter_we_vote_id) # If still here, we found an existing voter voter_id = voter_on_stage.id else: voter_id = 0 error_result = True except Voter.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) error_result = True exception_multiple_object_returned = True except Voter.DoesNotExist as e: error_result = True exception_does_not_exist = True results = { 'error_result': error_result, 'DoesNotExist': exception_does_not_exist, 'MultipleObjectsReturned': exception_multiple_object_returned, 'voter_found': True if voter_id > 0 else False, 'voter_id': voter_id, 'voter': voter_on_stage, } return results
def organization_position_list_view(request, organization_id): messages_on_stage = get_messages(request) organization_id = convert_to_int(organization_id) # election_id = 1 # TODO We will need to provide the election_id somehow, perhaps as a global variable? organization_on_stage_found = False try: organization_query = Organization.objects.filter(id=organization_id) if len(organization_query): organization_on_stage = organization_query[0] organization_on_stage_found = True except Exception as e: handle_record_not_found_exception(e) organization_on_stage_found = False if not organization_on_stage_found: messages.add_message( request, messages.ERROR, 'Could not find organization when trying to retrieve positions.') return HttpResponseRedirect( reverse('organization:organization_list', args=())) else: organization_position_list_found = False try: organization_position_list = PositionEntered.objects.order_by( 'stance') organization_position_list = organization_position_list.filter( organization_id=organization_id) # organization_position_list = organization_position_list.filter(election_id=election_id) if len(organization_position_list): organization_position_list_found = True except Exception as e: handle_record_not_found_exception(e) if organization_position_list_found: template_values = { 'messages_on_stage': messages_on_stage, 'organization': organization_on_stage, 'organization_position_list': organization_position_list, } else: template_values = { 'messages_on_stage': messages_on_stage, 'organization': organization_on_stage, } return render(request, 'organization/organization_position_list.html', template_values)
def organization_edit_process_view(request): """ Process the new or edit organization forms :param request: :return: """ # If person isn't signed in, we don't want to let them visit this page yet if not request.user.is_authenticated(): return redirect('/admin') organization_id = convert_to_int(request.POST['organization_id']) organization_name = request.POST['organization_name'] # Check to see if this organization is already being used anywhere organization_on_stage_found = False try: # organization_query = Organization.objects.all() # organization_query = organization_query.filter(id=organization_id) organization_query = Organization.objects.filter(id=organization_id) if len(organization_query): organization_on_stage = organization_query[0] organization_on_stage_found = True except Exception as e: handle_record_not_found_exception(e) try: if organization_on_stage_found: # Update organization_on_stage.name = organization_name organization_on_stage.save() messages.add_message(request, messages.INFO, 'Organization updated.') else: # Create new organization_on_stage = Organization(name=organization_name, ) organization_on_stage.save() messages.add_message(request, messages.INFO, 'New organization saved.') except Exception as e: handle_record_not_saved_exception(e) messages.add_message(request, messages.ERROR, 'Could not save organization.') return HttpResponseRedirect( reverse('organization:organization_list', args=()))
def update_or_create_organization_voter_guide_by_election_id( self, organization_we_vote_id, google_civic_election_id ): google_civic_election_id = convert_to_int(google_civic_election_id) new_voter_guide = VoterGuide() voter_guide_owner_type = ORGANIZATION exception_multiple_object_returned = False if not google_civic_election_id or not organization_we_vote_id: status = "ERROR_VARIABLES_MISSING_FOR_ORGANIZATION_VOTER_GUIDE" success = False new_voter_guide_created = False else: try: updated_values = { # Values we search against below "google_civic_election_id": google_civic_election_id, "voter_guide_owner_type": voter_guide_owner_type, "organization_we_vote_id": organization_we_vote_id, # The rest of the values } voter_guide_on_stage, new_voter_guide_created = VoterGuide.objects.update_or_create( google_civic_election_id__exact=google_civic_election_id, organization_we_vote_id__exact=organization_we_vote_id, defaults=updated_values, ) success = True if new_voter_guide_created: status = "VOTER_GUIDE_CREATED_FOR_ORGANIZATION" else: status = "VOTER_GUIDE_UPDATED_FOR_ORGANIZATION" except VoterGuide.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) success = False status = "MULTIPLE_MATCHING_VOTER_GUIDES_FOUND_FOR_ORGANIZATION" exception_multiple_object_returned = True new_voter_guide_created = False results = { "success": success, "status": status, "MultipleObjectsReturned": exception_multiple_object_returned, "voter_guide_saved": success, "new_voter_guide_created": new_voter_guide_created, } return results
def election_edit_view(request, election_id): messages_on_stage = get_messages(request) election_id = convert_to_int(election_id) election_on_stage_found = False try: election_on_stage = Election.objects.get(id=election_id) election_on_stage_found = True except Election.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except Election.DoesNotExist: # This is fine, create new pass if election_on_stage_found: template_values = {"messages_on_stage": messages_on_stage, "election": election_on_stage} else: template_values = {"messages_on_stage": messages_on_stage} return render(request, "election/election_edit.html", template_values)
def measure_edit_process_view(request): """ Process the new or edit measure forms :param request: :return: """ measure_id = convert_to_int(request.POST['measure_id']) measure_name = request.POST['measure_name'] twitter_handle = request.POST['twitter_handle'] measure_website = request.POST['measure_website'] # Check to see if this measure is already being used anywhere measure_on_stage_found = False measure_on_stage = ContestMeasure() try: measure_query = ContestMeasure.objects.filter(id=measure_id) if len(measure_query): measure_on_stage = measure_query[0] measure_on_stage_found = True except Exception as e: handle_record_not_found_exception(e, logger=logger) try: if measure_on_stage_found: # Update measure_on_stage.measure_name = measure_name measure_on_stage.twitter_handle = twitter_handle measure_on_stage.measure_website = measure_website measure_on_stage.save() messages.add_message(request, messages.INFO, 'ContestMeasure updated.') else: # Create new measure_on_stage = ContestMeasure( measure_name=measure_name, twitter_handle=twitter_handle, measure_website=measure_website, ) measure_on_stage.save() messages.add_message(request, messages.INFO, 'New measure saved.') except Exception as e: handle_record_not_saved_exception(e, logger=logger) messages.add_message(request, messages.ERROR, 'Could not save measure.') return HttpResponseRedirect(reverse('measure:measure_list', args=()))
def candidate_edit_process_view(request): """ Process the new or edit candidate forms :param request: :return: """ candidate_id = convert_to_int(request.POST['candidate_id']) candidate_name = request.POST['candidate_name'] twitter_handle = request.POST['twitter_handle'] candidate_website = request.POST['candidate_website'] # Check to see if this candidate is already being used anywhere candidate_on_stage_found = False candidate_on_stage = CandidateCampaign() try: candidate_query = CandidateCampaign.objects.filter(id=candidate_id) if len(candidate_query): candidate_on_stage = candidate_query[0] candidate_on_stage_found = True except Exception as e: handle_record_not_found_exception(e, logger=logger) try: if candidate_on_stage_found: # Update candidate_on_stage.candidate_name = candidate_name candidate_on_stage.twitter_handle = twitter_handle candidate_on_stage.candidate_website = candidate_website candidate_on_stage.save() messages.add_message(request, messages.INFO, 'CandidateCampaign updated.') else: # Create new candidate_on_stage = CandidateCampaign( candidate_name=candidate_name, twitter_handle=twitter_handle, candidate_website=candidate_website, ) candidate_on_stage.save() messages.add_message(request, messages.INFO, 'New candidate saved.') except Exception as e: handle_record_not_saved_exception(e, logger=logger) messages.add_message(request, messages.ERROR, 'Could not save candidate.') return HttpResponseRedirect(reverse('candidate:candidate_list', args=()))
def candidate_summary_view(request, candidate_id): messages_on_stage = get_messages(request) candidate_id = convert_to_int(candidate_id) candidate_on_stage_found = False candidate_on_stage = CandidateCampaign() try: candidate_on_stage = CandidateCampaign.objects.get(id=candidate_id) candidate_on_stage_found = True except CandidateCampaign.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except CandidateCampaign.DoesNotExist: # This is fine, create new pass if candidate_on_stage_found: template_values = {"messages_on_stage": messages_on_stage, "candidate": candidate_on_stage} else: template_values = {"messages_on_stage": messages_on_stage} return render(request, "candidate/candidate_summary.html", template_values)
def retrieve_voter(self, voter_id, email): voter_id = convert_to_int(voter_id) if not validate_email(email): # We do not want to search for an invalid email email = None error_result = False exception_does_not_exist = False exception_multiple_object_returned = False voter_on_stage = Voter() try: if voter_id > 0: voter_on_stage = Voter.objects.get(id=voter_id) # If still here, we found an existing voter voter_id = voter_on_stage.id elif email is not '' and email is not None: voter_on_stage = Voter.objects.get(email=email) # If still here, we found an existing voter voter_id = voter_on_stage.id else: voter_id = 0 error_result = True except Voter.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e) error_result = True exception_multiple_object_returned = True except Voter.DoesNotExist as e: handle_exception_silently(e) error_result = True exception_does_not_exist = True results = { 'error_result': error_result, 'DoesNotExist': exception_does_not_exist, 'MultipleObjectsReturned': exception_multiple_object_returned, 'voter_found': True if voter_id > 0 else False, 'voter_id': voter_id, 'voter': voter_on_stage, } return results
def organization_edit_process_view(request): """ Process the new or edit organization forms :param request: :return: """ organization_id = convert_to_int(request.POST['organization_id']) organization_name = request.POST['organization_name'] # 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 len(organization_query): 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 organization_on_stage.name = organization_name organization_on_stage.save() messages.add_message(request, messages.INFO, 'Organization updated.') else: # Create new organization_on_stage = Organization(name=organization_name, ) organization_on_stage.save() messages.add_message(request, messages.INFO, 'New organization saved.') except Exception as e: handle_record_not_saved_exception(e, logger=logger) messages.add_message(request, messages.ERROR, 'Could not save organization.') return HttpResponseRedirect( reverse('organization:organization_list', args=()))
def election_edit_view(request, election_id): messages_on_stage = get_messages(request) election_id = convert_to_int(election_id) election_on_stage_found = False try: election_on_stage = Election.objects.get(id=election_id) election_on_stage_found = True except Election.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except Election.DoesNotExist: # This is fine, create new pass if election_on_stage_found: template_values = { 'messages_on_stage': messages_on_stage, 'election': election_on_stage, } else: template_values = { 'messages_on_stage': messages_on_stage, } return render(request, 'election/election_edit.html', template_values)
def organization_edit_view(request, organization_id): messages_on_stage = get_messages(request) organization_id = convert_to_int(organization_id) organization_on_stage_found = False try: organization_on_stage = Organization.objects.get(id=organization_id) organization_on_stage_found = True except Organization.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e) except Organization.DoesNotExist as e: # This is fine, create new handle_exception_silently(e) if organization_on_stage_found: template_values = { 'messages_on_stage': messages_on_stage, 'organization': organization_on_stage, } else: template_values = { 'messages_on_stage': messages_on_stage, } return render(request, 'organization/organization_edit.html', template_values)
def organization_save_new_or_edit_existing_position_process_form_view(request): """ :param request: :return: """ organization_id = convert_to_int(request.POST['organization_id']) position_id = convert_to_int(request.POST['position_id']) candidate_campaign_id = convert_to_int( request.POST['candidate_campaign_id']) measure_campaign_id = convert_to_int(request.POST['measure_campaign_id']) stance = request.POST.get( 'stance', SUPPORT) # Set a default if stance comes in empty statement_text = request.POST.get( 'statement_text', '') # Set a default if stance comes in empty more_info_url = request.POST.get('more_info_url', '') # Make sure this is a valid organization before we try to save a position organization_on_stage_found = False try: organization_query = Organization.objects.filter(id=organization_id) if len(organization_query): # organization_on_stage = organization_query[0] organization_on_stage_found = True except Exception as e: # If we can't retrieve the organization, we cannot proceed handle_record_not_found_exception(e, logger=logger) if not organization_on_stage_found: messages.add_message( request, messages.ERROR, "Could not find the organization when trying to create or edit a new position." ) return HttpResponseRedirect( reverse('organization:organization_list', args=())) # Now retrieve the CandidateCampaign or the MeasureCampaign so we can save it with the Position # We need either candidate_campaign_id or measure_campaign_id if candidate_campaign_id: try: candidate_campaign_on_stage = CandidateCampaign.objects.get( id=candidate_campaign_id) candidate_campaign_on_stage_found = True except CandidateCampaign.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except CandidateCampaign.DoesNotExist as e: handle_record_not_found_exception(e, logger=logger) if not candidate_campaign_on_stage_found: messages.add_message( request, messages.ERROR, "Could not find Candidate's campaign when trying to create or edit a new position." ) if position_id: return HttpResponseRedirect( reverse('organization:organization_position_edit', args=([organization_id], [position_id]))) else: return HttpResponseRedirect( reverse('organization:organization_position_new', args=([organization_id]))) elif measure_campaign_id: logger.warn( "measure_campaign_id FOUND. Look for MeasureCampaign here.") else: logger.warn( "Neither candidate_campaign_id nor measure_campaign_id found") messages.add_message(request, messages.ERROR, "Unable to find either Candidate or Measure.") return HttpResponseRedirect( reverse('organization:organization_position_list', args=([organization_id]))) organization_position_on_stage_found = False logger.info("position_id: {position_id}".format(position_id=position_id)) # Retrieve position from position_id if it exists already if position_id > 0: position_entered_manager = PositionEnteredManager() results = position_entered_manager.retrieve_position_from_id( position_id) if results['position_found']: organization_position_on_stage_found = True organization_position_on_stage = results['position'] if not organization_position_on_stage_found: # If a position_id hasn't been passed in, then we are trying to create a new position. # Check to make sure a position for this org and candidate doesn't already exist position_entered_manager = PositionEnteredManager() results = position_entered_manager.retrieve_organization_candidate_campaign_position( organization_id, candidate_campaign_id) if results['MultipleObjectsReturned']: messages.add_message( request, messages.ERROR, "We found more than one existing positions for this candidate. Please delete all but one position." ) return HttpResponseRedirect( reverse('organization:organization_position_list', args=([organization_id]))) elif results['position_found']: organization_position_on_stage_found = True organization_position_on_stage = results['position'] # Now save existing, or create new try: if organization_position_on_stage_found: # Update the position organization_position_on_stage.stance = stance organization_position_on_stage.statement_text = statement_text organization_position_on_stage.more_info_url = more_info_url organization_position_on_stage.save() messages.add_message( request, messages.INFO, "Position on {candidate_name} updated.".format( candidate_name=candidate_campaign_on_stage.candidate_name)) else: # Create new organization_position_on_stage = PositionEntered( organization_id=organization_id, candidate_campaign_id=candidate_campaign_on_stage.id, stance=stance, statement_text=statement_text, more_info_url=more_info_url, ) organization_position_on_stage.save() messages.add_message( request, messages.INFO, "New position on {candidate_name} saved.".format( candidate_name=candidate_campaign_on_stage.candidate_name)) except Exception as e: handle_record_not_saved_exception(e, logger=logger) logger.error("Problem saving PositionEntered for CandidateCampaign") return HttpResponseRedirect( reverse('organization:organization_position_list', args=([organization_id])))