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: """ authority_required = {'verified_volunteer'} # admin, verified_volunteer if not voter_has_authority(request, authority_required): return redirect_to_sign_in_page(request, authority_required) 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 election_list = Election.objects.all() 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, 'election_list': election_list, } return render(request, 'organization/organization_position_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 organization_delete_existing_position_process_form_view(request, organization_id, position_id): """ :param request: :param organization_id: :param position_id: :return: """ authority_required = {'admin'} # 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(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, logger=logger) 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_edit', args=([organization_id])))
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 organization_delete_existing_position_process_form_view(request, organization_id, position_id): """ :param request: :param organization_id: :param position_id: :return: """ authority_required = {'admin'} # 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(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, logger=logger) 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 organization_position_edit_process_view(request): """ :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) google_civic_election_id = convert_to_int(request.POST.get('google_civic_election_id', 0)) organization_id = convert_to_int(request.POST.get('organization_id', 0)) position_id = convert_to_int(request.POST.get('position_id', 0)) candidate_campaign_id = convert_to_int(request.POST.get('candidate_campaign_id', 0)) contest_measure_id = convert_to_int(request.POST.get('contest_measure_id', 0)) 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', '') go_back_to_add_new = False # 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 organization_query.count(): 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 ContestMeasure so we can save it with the Position # We need either candidate_campaign_id or contest_measure_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 contest_measure_id: logger.warn("contest_measure_id FOUND. Look for ContestMeasure here.") else: logger.warn("Neither candidate_campaign_id nor contest_measure_id found") messages.add_message( request, messages.ERROR, "Unable to find either Candidate or Measure.") return HttpResponseRedirect( reverse('organization:organization_position_new', args=([organization_id])) + "?google_civic_election_id=" + str(google_civic_election_id) + "&stance=" + stance + "&statement_text=" + statement_text + "&more_info_url=" + more_info_url + "&candidate_not_found=1" ) 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'] organization_position_found_from_new_form = False if not organization_position_on_stage_found: # If not found from position_id # 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, candidate and election doesn't already exist position_entered_manager = PositionEnteredManager() results = position_entered_manager.retrieve_organization_candidate_campaign_position( organization_id, candidate_campaign_id, google_civic_election_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'] organization_position_found_from_new_form = True # Now save existing, or create new success = False try: if organization_position_on_stage_found: # Update the position organization_position_on_stage.stance = stance organization_position_on_stage.google_civic_election_id = google_civic_election_id if not organization_position_found_from_new_form or positive_value_exists(more_info_url): # Only update this if we came from update form, or there is a value in the incoming variable organization_position_on_stage.more_info_url = more_info_url if not organization_position_found_from_new_form or positive_value_exists(statement_text): # Only update this if we came from update form, or there is a value in the incoming variable organization_position_on_stage.statement_text = statement_text if not positive_value_exists(organization_position_on_stage.organization_we_vote_id): organization_position_on_stage.organization_we_vote_id = organization_on_stage.we_vote_id if not positive_value_exists(organization_position_on_stage.candidate_campaign_we_vote_id): organization_position_on_stage.candidate_campaign_we_vote_id = candidate_campaign_on_stage.we_vote_id if not positive_value_exists(organization_position_on_stage.google_civic_candidate_name): organization_position_on_stage.google_civic_candidate_name = \ candidate_campaign_on_stage.google_civic_candidate_name organization_position_on_stage.save() success = True messages.add_message( request, messages.INFO, "Position on {candidate_name} updated.".format( candidate_name=candidate_campaign_on_stage.display_candidate_name())) else: # Create new organization_position_on_stage = PositionEntered( organization_id=organization_id, organization_we_vote_id=organization_on_stage.we_vote_id, candidate_campaign_id=candidate_campaign_on_stage.id, candidate_campaign_we_vote_id=candidate_campaign_on_stage.we_vote_id, # Save candidate_campaign_on_stage so we can re-link candidates to positions if we_vote_id is lost google_civic_candidate_name=candidate_campaign_on_stage.google_civic_candidate_name, google_civic_election_id=google_civic_election_id, stance=stance, statement_text=statement_text, more_info_url=more_info_url, ) organization_position_on_stage.save() success = True messages.add_message( request, messages.INFO, "New position on {candidate_name} saved.".format( candidate_name=candidate_campaign_on_stage.display_candidate_name())) go_back_to_add_new = True except Exception as e: handle_record_not_saved_exception(e, logger=logger) logger.error("Problem saving PositionEntered for CandidateCampaign") # If the position was saved, then update the voter_guide entry if success: voter_guide_manager = VoterGuideManager() results = voter_guide_manager.update_or_create_organization_voter_guide_by_election_id( organization_on_stage.we_vote_id, google_civic_election_id) # if results['success']: if go_back_to_add_new: return HttpResponseRedirect( reverse('organization:organization_position_new', args=(organization_on_stage.id,)) + "?google_civic_election_id=" + str(google_civic_election_id)) else: return HttpResponseRedirect( reverse('organization:organization_position_list', args=(organization_on_stage.id,)))
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])))
def organization_save_new_or_edit_existing_position_process_form_view(request): """ :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']) 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) 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) except CandidateCampaign.DoesNotExist as e: handle_record_not_found_exception(e) 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: print "measure_campaign_id FOUND. Look for MeasureCampaign here." else: print "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 print "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) print "Problem saving PositionEntered for CandidateCampaign" return HttpResponseRedirect(reverse('organization:organization_position_list', args=([organization_id])))
def organization_save_new_or_edit_existing_position_process_form_view(request): """ :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) google_civic_election_id = convert_to_int(request.POST['google_civic_election_id']) 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']) contest_measure_id = convert_to_int(request.POST['contest_measure_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 organization_query.count(): 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 ContestMeasure so we can save it with the Position # We need either candidate_campaign_id or contest_measure_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 contest_measure_id: logger.warn("contest_measure_id FOUND. Look for ContestMeasure here.") else: logger.warn("Neither candidate_campaign_id nor contest_measure_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 success = False try: if organization_position_on_stage_found: # Update the position organization_position_on_stage.stance = stance organization_position_on_stage.google_civic_election_id = google_civic_election_id organization_position_on_stage.more_info_url = more_info_url organization_position_on_stage.statement_text = statement_text if not positive_value_exists(organization_position_on_stage.organization_we_vote_id): organization_position_on_stage.organization_we_vote_id = organization_on_stage.we_vote_id if not positive_value_exists(organization_position_on_stage.candidate_campaign_we_vote_id): organization_position_on_stage.candidate_campaign_we_vote_id = candidate_campaign_on_stage.we_vote_id if not positive_value_exists(organization_position_on_stage.google_civic_candidate_name): organization_position_on_stage.google_civic_candidate_name = \ candidate_campaign_on_stage.google_civic_candidate_name organization_position_on_stage.save() success = True 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, organization_we_vote_id=organization_on_stage.we_vote_id, candidate_campaign_id=candidate_campaign_on_stage.id, candidate_campaign_we_vote_id=candidate_campaign_on_stage.we_vote_id, # Save candidate_campaign_on_stage so we can re-link candidates to positions if we_vote_id is lost google_civic_candidate_name=candidate_campaign_on_stage.google_civic_candidate_name, google_civic_election_id=google_civic_election_id, stance=stance, statement_text=statement_text, more_info_url=more_info_url, ) organization_position_on_stage.save() success = True 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") # If the position was saved, then update the voter_guide entry if success: voter_guide_manager = VoterGuideManager() results = voter_guide_manager.update_or_create_organization_voter_guide_by_election_id( organization_on_stage.we_vote_id, google_civic_election_id) # if results['success']: return HttpResponseRedirect(reverse('organization:organization_position_list', args=(organization_on_stage.id,)))