<s> from django . db import models <EOL> from exception . models import handle_record_found_more_than_one_exception , handle_record_not_found_exception , handle_record_not_saved_exception <EOL> from organization . models import OrganizationManager <EOL> import wevote_functions . admin <EOL> FOLLOWING = '<STR_LIT>' <EOL> STOP_FOLLOWING = '<STR_LIT>' <EOL> FOLLOW_IGNORE = '<STR_LIT>' <EOL> FOLLOWING_CHOICES = ( <EOL> ( FOLLOWING , '<STR_LIT>' ) , <EOL> ( STOP_FOLLOWING , '<STR_LIT>' ) , <EOL> ( FOLLOW_IGNORE , '<STR_LIT>' ) , <EOL> ) <EOL> logger = wevote_functions . admin . get_logger ( __name__ ) <EOL> class FollowOrganization ( models . Model ) : <EOL> voter_id = models . BigIntegerField ( null = True , blank = True ) <EOL> organization_id = models . BigIntegerField ( null = True , blank = True ) <EOL> following_status = models . CharField ( max_length = <NUM_LIT:15> , choices = FOLLOWING_CHOICES , default = FOLLOWING ) <EOL> date_last_changed = models . DateTimeField ( verbose_name = '<STR_LIT>' , null = True , auto_now = True ) <EOL> def __unicode__ ( self ) : <EOL> return self . organization_id <EOL> def is_following ( self ) : <EOL> if self . following_status == FOLLOWING : <EOL> return True <EOL> return False <EOL> def is_not_following ( self ) : <EOL> if self . following_status == STOP_FOLLOWING : <EOL> return True <EOL> return False <EOL> def is_ignoring ( self ) : <EOL> if self . following_status == FOLLOW_IGNORE : <EOL> return True <EOL> return False <EOL> class FollowOrganizationManager ( models . Model ) : <EOL> def __unicode__ ( self ) : <EOL> return "<STR_LIT>" <EOL> def toggle_on_voter_following_organization ( self , voter_id , organization_id ) : <EOL> following_status = FOLLOWING <EOL> follow_organization_manager = FollowOrganizationManager ( ) <EOL> return follow_organization_manager . toggle_voter_following_organization ( <EOL> voter_id , organization_id , following_status ) <EOL> def toggle_off_voter_following_organization ( self , voter_id , organization_id ) : <EOL> following_status = STOP_FOLLOWING <EOL> follow_organization_manager = FollowOrganizationManager ( ) <EOL> return follow_organization_manager . toggle_voter_following_organization ( <EOL> voter_id , organization_id , following_status ) <EOL> def toggle_voter_following_organization ( self , voter_id , organization_id , following_status ) : <EOL> follow_organization_manager = FollowOrganizationManager ( ) <EOL> results = follow_organization_manager . retrieve_follow_organization ( <NUM_LIT:0> , voter_id , organization_id ) <EOL> follow_organization_on_stage_found = False <EOL> follow_organization_on_stage_id = <NUM_LIT:0> <EOL> follow_organization_on_stage = FollowOrganization ( ) <EOL> if results [ '<STR_LIT>' ] : <EOL> follow_organization_on_stage = results [ '<STR_LIT>' ] <EOL> try : <EOL> follow_organization_on_stage . following_status = following_status <EOL> follow_organization_on_stage . save ( ) <EOL> follow_organization_on_stage_id = follow_organization_on_stage . id <EOL> follow_organization_on_stage_found = True <EOL> except Exception as e : <EOL> handle_record_not_saved_exception ( e , logger = logger ) <EOL> elif results [ '<STR_LIT>' ] : <EOL> logger . warn ( "<STR_LIT>" ) <EOL> elif results [ '<STR_LIT>' ] : <EOL> try : <EOL> organization_manager = OrganizationManager ( ) <EOL> results = organization_manager . retrieve_organization ( organization_id ) <EOL> if results [ '<STR_LIT>' ] : <EOL> follow_organization_on_stage = FollowOrganization ( <EOL> voter_id = voter_id , <EOL> organization_id = organization_id , <EOL> following_status = following_status , <EOL> ) <EOL> follow_organization_on_stage . save ( ) <EOL> follow_organization_on_stage_id = follow_organization_on_stage . id <EOL> follow_organization_on_stage_found = True <EOL> except Exception as e : <EOL> handle_record_not_saved_exception ( e , logger = logger ) <EOL> results = { <EOL> '<STR_LIT:success>' : True if follow_organization_on_stage_found else False , <EOL> '<STR_LIT>' : follow_organization_on_stage_found , <EOL> '<STR_LIT>' : follow_organization_on_stage_id , <EOL> '<STR_LIT>' : follow_organization_on_stage , <EOL> } <EOL> return results <EOL> def retrieve_follow_organization ( self , follow_organization_id , voter_id , organization_id ) : <EOL> error_result = False <EOL> exception_does_not_exist = False <EOL> exception_multiple_object_returned = False <EOL> follow_organization_on_stage = FollowOrganization ( ) <EOL> follow_organization_on_stage_id = <NUM_LIT:0> <EOL> try : <EOL> if follow_organization_id > <NUM_LIT:0> : <EOL> follow_organization_on_stage = FollowOrganization . objects . get ( id = follow_organization_id ) <EOL> follow_organization_on_stage_id = organization_id . id <EOL> elif voter_id > <NUM_LIT:0> and organization_id > <NUM_LIT:0> : <EOL> follow_organization_on_stage = FollowOrganization . objects . get ( <EOL> voter_id = voter_id , organization_id = organization_id ) <EOL> follow_organization_on_stage_id = follow_organization_on_stage . id <EOL> except FollowOrganization . MultipleObjectsReturned as e : <EOL> handle_record_found_more_than_one_exception ( e , logger = logger ) <EOL> error_result = True <EOL> exception_multiple_object_returned = True <EOL> except FollowOrganization . DoesNotExist : <EOL> error_result = True <EOL> exception_does_not_exist = True <EOL> follow_organization_on_stage_found = True if follow_organization_on_stage_id > <NUM_LIT:0> else False <EOL> results = { <EOL> '<STR_LIT:success>' : True if follow_organization_on_stage_found else False , <EOL> '<STR_LIT>' : follow_organization_on_stage_found , <EOL> '<STR_LIT>' : follow_organization_on_stage_id , <EOL> '<STR_LIT>' : follow_organization_on_stage , <EOL> '<STR_LIT>' : follow_organization_on_stage . is_following ( ) , <EOL> '<STR_LIT>' : follow_organization_on_stage . is_not_following ( ) , <EOL> '<STR_LIT>' : follow_organization_on_stage . is_ignoring ( ) , <EOL> '<STR_LIT>' : error_result , <EOL> '<STR_LIT>' : exception_does_not_exist , <EOL> '<STR_LIT>' : exception_multiple_object_returned , <EOL> } <EOL> return results <EOL> class FollowOrganizationList ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> def retrieve_follow_organization_info_for_voter ( self , voter_id ) : <EOL> follow_organization_list_found = False <EOL> following_status = FOLLOWING <EOL> follow_organization_list = { } <EOL> try : <EOL> follow_organization_list = FollowOrganization . objects . all ( ) <EOL> follow_organization_list = follow_organization_list . filter ( voter_id = voter_id ) <EOL> follow_organization_list = follow_organization_list . filter ( following_status = following_status ) <EOL> if len ( follow_organization_list ) : <EOL> follow_organization_list_found = True <EOL> except Exception as e : <EOL> handle_record_not_found_exception ( e , logger = logger ) <EOL> if follow_organization_list_found : <EOL> return follow_organization_list <EOL> else : <EOL> follow_organization_list = { } <EOL> return follow_organization_list <EOL> def retrieve_follow_organization_info_for_voter_simple_array ( self , voter_id ) : <EOL> follow_organization_list_manager = FollowOrganizationList ( ) <EOL> follow_organization_list = follow_organization_list_manager . retrieve_follow_organization_info_for_voter ( voter_id ) <EOL> follow_organization_list_simple_array = [ ] <EOL> if len ( follow_organization_list ) : <EOL> for follow_organization in follow_organization_list : <EOL> follow_organization_list_simple_array . append ( follow_organization . organization_id ) <EOL> return follow_organization_list_simple_array <EOL> def retrieve_follow_organization_info_for_organization ( self , organization_id ) : <EOL> follow_organization_list_found = False <EOL> following_status = FOLLOWING <EOL> follow_organization_list = { } <EOL> try : <EOL> follow_organization_list = FollowOrganization . objects . all ( ) <EOL> follow_organization_list = follow_organization_list . filter ( organization_id = organization_id ) <EOL> follow_organization_list = follow_organization_list . filter ( following_status = following_status ) <EOL> if len ( follow_organization_list ) : <EOL> follow_organization_list_found = True <EOL> except Exception as e : <EOL> handle_record_not_found_exception ( e , logger = logger ) <EOL> if follow_organization_list_found : <EOL> return follow_organization_list <EOL> else : <EOL> follow_organization_list = { } <EOL> return follow_organization_list </s>
