Skip to content

Commit

Permalink
Fixing some errors from Splunk logs. Added more read_only options.
Browse files Browse the repository at this point in the history
  • Loading branch information
DaleMcGrew committed Oct 15, 2018
1 parent 0ddf2f9 commit 96c4b0a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 10 deletions.
2 changes: 1 addition & 1 deletion apis_v1/controllers.py
Expand Up @@ -53,7 +53,7 @@ def organization_follow(voter_device_id, organization_id=0, organization_we_vote
if positive_value_exists(organization_twitter_handle):
organization_manager = OrganizationManager()
organization_results = organization_manager.retrieve_organization_from_twitter_handle(
organization_twitter_handle)
organization_twitter_handle, read_only=True)
if organization_results['organization_found']:
organization_we_vote_id = organization_results['organization'].we_vote_id

Expand Down
42 changes: 40 additions & 2 deletions follow/models.py
Expand Up @@ -5,8 +5,8 @@
from datetime import datetime, timedelta
from django.db import models
from election.models import ElectionManager
from exception.models import handle_record_found_more_than_one_exception,\
handle_record_not_found_exception, handle_record_not_saved_exception
from exception.models import handle_exception, handle_record_found_more_than_one_exception,\
handle_record_not_found_exception, handle_record_not_saved_exception, print_to_log
from issue.models import IssueManager
from organization.models import OrganizationManager
import pytz
Expand Down Expand Up @@ -809,6 +809,44 @@ def retrieve_follow_organization(self, follow_organization_id, voter_id, organiz
exception_multiple_object_returned = True
success = False
status = 'FOLLOW_ORGANIZATION_NOT_FOUND_MultipleObjectsReturned'
follow_organization_list_found = False
follow_organization_list = []

# Delete the oldest values and retrieve the correct one
try:
if positive_value_exists(voter_id) and positive_value_exists(organization_id):
follow_organization_query = FollowOrganization.objects.all()
follow_organization_query = follow_organization_query.filter(
voter_id=voter_id, organization_id=organization_id)
follow_organization_query = follow_organization_query.order_by('id')
follow_organization_list = list(follow_organization_query)

follow_organization_list_found = positive_value_exists(len(follow_organization_list))

success = True
status = 'FOLLOW_ORGANIZATION_FOUND_WITH_VOTER_ID_AND_ORGANIZATION_ID'
elif positive_value_exists(voter_id) and positive_value_exists(organization_we_vote_id):
follow_organization_query = FollowOrganization.objects.all()
follow_organization_query = follow_organization_query.filter(
voter_id=voter_id, organization_we_vote_id=organization_we_vote_id)
follow_organization_query = follow_organization_query.order_by('id')
follow_organization_list = list(follow_organization_query)

follow_organization_list_found = positive_value_exists(len(follow_organization_list))

success = True
status = 'FOLLOW_ORGANIZATION_FOUND_WITH_VOTER_ID_AND_ORGANIZATION_WE_VOTE_ID'

if follow_organization_list_found:
follow_organization_on_stage = follow_organization_list.pop()
follow_organization_on_stage_id = follow_organization_on_stage.id
# Now cycle through remaining list and delete
for one_follow_organization in follow_organization_list:
one_follow_organization.delete()
print_to_log(logger, exception_message_optional="FollowOrganization duplicates removed.")
except Exception as e:
handle_exception(e, logger,
exception_message="Error trying to delete duplicate FollowOrganization entries.")
except FollowOrganization.DoesNotExist:
error_result = False
exception_does_not_exist = True
Expand Down
6 changes: 3 additions & 3 deletions organization/models.py
Expand Up @@ -284,18 +284,18 @@ def retrieve_organization_from_we_vote_id(self, organization_we_vote_id, read_on
def retrieve_organization_from_vote_smart_id(self, vote_smart_id, read_only=False):
return self.retrieve_organization(0, '', vote_smart_id, read_only=read_only)

def retrieve_organization_from_twitter_handle(self, twitter_handle):
def retrieve_organization_from_twitter_handle(self, twitter_handle, read_only=False):
organization_id = 0
organization_we_vote_id = ""

twitter_user_manager = TwitterUserManager()
twitter_retrieve_results = twitter_user_manager.retrieve_twitter_link_to_organization_from_twitter_handle(
twitter_handle)
twitter_handle, read_only=True) # Always read_only
if twitter_retrieve_results['twitter_link_to_organization_found']:
twitter_link_to_organization = twitter_retrieve_results['twitter_link_to_organization']
organization_we_vote_id = twitter_link_to_organization.organization_we_vote_id

return self.retrieve_organization(organization_id, organization_we_vote_id)
return self.retrieve_organization(organization_id, organization_we_vote_id, read_only=read_only)

def retrieve_organization_from_twitter_user_id(self, twitter_user_id):
organization_we_vote_id = ''
Expand Down
4 changes: 2 additions & 2 deletions twitter/models.py
Expand Up @@ -335,9 +335,9 @@ def delete_twitter_link_possibilities(self, candidate_campaign_we_vote_id):
def retrieve_twitter_link_to_organization_from_twitter_user_id(self, twitter_user_id):
return self.retrieve_twitter_link_to_organization(twitter_user_id)

def retrieve_twitter_link_to_organization_from_twitter_handle(self, twitter_handle):
def retrieve_twitter_link_to_organization_from_twitter_handle(self, twitter_handle, read_only=False):
twitter_user_id = 0
results = self.retrieve_twitter_user_locally_or_remotely(twitter_user_id, twitter_handle)
results = self.retrieve_twitter_user_locally_or_remotely(twitter_user_id, twitter_handle, read_only=read_only)
if results['twitter_user_found']:
twitter_user = results['twitter_user']
twitter_user_id = twitter_user.twitter_id
Expand Down
9 changes: 7 additions & 2 deletions voter/models.py
Expand Up @@ -2049,9 +2049,14 @@ def retrieve_voter_device_link(self, voter_device_id, voter_id=0, voter_device_l
elif positive_value_exists(voter_id):
status += " RETRIEVE_VOTER_DEVICE_LINK-GET_BY_VOTER_ID"
if read_only:
voter_device_link_on_stage = VoterDeviceLink.objects.using('readonly').get(voter_id=voter_id)
voter_device_link_query = VoterDeviceLink.objects.using('readonly').all()
else:
voter_device_link_on_stage = VoterDeviceLink.objects.get(voter_id=voter_id)
voter_device_link_query = VoterDeviceLink.objects.all()
voter_device_link_query = voter_device_link_query.filter(voter_id=voter_id)
voter_device_link_query = voter_device_link_query.order_by('id')
voter_device_link_list = list(voter_device_link_query)
voter_device_link_on_stage = voter_device_link_list.pop() # Pop the last created

# If still here, we found an existing position
voter_device_link_id = voter_device_link_on_stage.id
elif positive_value_exists(voter_device_link_id):
Expand Down

0 comments on commit 96c4b0a

Please sign in to comment.