本文整理匯總了Python中organization.models.OrganizationManager.fetch_organization_id方法的典型用法代碼示例。如果您正苦於以下問題:Python OrganizationManager.fetch_organization_id方法的具體用法?Python OrganizationManager.fetch_organization_id怎麽用?Python OrganizationManager.fetch_organization_id使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類organization.models.OrganizationManager
的用法示例。
在下文中一共展示了OrganizationManager.fetch_organization_id方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: import_we_vote_positions_from_json
# 需要導入模塊: from organization.models import OrganizationManager [as 別名]
# 或者: from organization.models.OrganizationManager import fetch_organization_id [as 別名]
def import_we_vote_positions_from_json(request, load_from_uri=False):
"""
Get the json data, and either create new entries or update existing
:return:
"""
if load_from_uri:
# Request json file from We Vote servers
messages.add_message(request, messages.INFO, "Loading positions from We Vote Master servers")
request = requests.get(POSITIONS_URL, params={
"key": WE_VOTE_API_KEY, # This comes from an environment variable
})
structured_json = json.loads(request.text)
else:
# Load saved json from local file
messages.add_message(request, messages.INFO, "Loading positions from local file")
with open(POSITIONS_JSON_FILE) as json_data:
structured_json = json.load(json_data)
for one_position in structured_json:
# Make sure we have the minimum required variables
if len(one_position["id_we_vote"]) == 0 \
or len(one_position["organization_id_we_vote"]) == 0\
or len(one_position["candidate_campaign_id_we_vote"]) == 0:
continue
# Check to see if this position is already being used anywhere
position_on_stage_found = False
try:
if len(one_position["id_we_vote"]) > 0:
position_query = PositionEntered.objects.filter(id_we_vote=one_position["id_we_vote"])
if len(position_query):
position_on_stage = position_query[0]
position_on_stage_found = True
except PositionEntered.DoesNotExist as e:
handle_exception_silently(e)
except Exception as e:
handle_record_not_found_exception(e)
# We need to look up the local organization_id based on the newly saved we_vote_id
organization_manager = OrganizationManager()
organization_id = organization_manager.fetch_organization_id(one_position["organization_id_we_vote"])
# We need to look up the local candidate_campaign_id
candidate_campaign_manager = CandidateCampaignManager()
candidate_campaign_id = candidate_campaign_manager.fetch_candidate_campaign_id_from_id_we_vote(
one_position["candidate_campaign_id_we_vote"])
# TODO We need to look up measure_campaign_id
measure_campaign_id = 0
try:
if position_on_stage_found:
# Update
position_on_stage.id_we_vote = one_position["id_we_vote"]
position_on_stage.organization_id = organization_id
position_on_stage.candidate_campaign_id = candidate_campaign_id
position_on_stage.measure_campaign_id = measure_campaign_id
position_on_stage.date_entered = one_position["date_entered"]
position_on_stage.election_id = one_position["election_id"]
position_on_stage.stance = one_position["stance"]
position_on_stage.more_info_url = one_position["more_info_url"]
position_on_stage.statement_text = one_position["statement_text"]
position_on_stage.statement_html = one_position["statement_html"]
position_on_stage.save()
messages.add_message(request, messages.INFO, "Position updated: {id_we_vote}".format(
id_we_vote=one_position["id_we_vote"]))
else:
# Create new
position_on_stage = PositionEntered(
id_we_vote=one_position["id_we_vote"],
organization_id=organization_id,
candidate_campaign_id=candidate_campaign_id,
measure_campaign_id=measure_campaign_id,
date_entered=one_position["date_entered"],
election_id=one_position["election_id"],
stance=one_position["stance"],
more_info_url=one_position["more_info_url"],
statement_text=one_position["statement_text"],
statement_html=one_position["statement_html"],
)
position_on_stage.save()
messages.add_message(request, messages.INFO, "New position imported: {id_we_vote}".format(
id_we_vote=one_position["id_we_vote"]))
except Exception as e:
handle_record_not_saved_exception(e)
messages.add_message(request, messages.ERROR,
"Could not save position, id_we_vote: {id_we_vote}, "
"organization_id_we_vote: {organization_id_we_vote}, "
"candidate_campaign_id_we_vote: {candidate_campaign_id_we_vote}".format(
id_we_vote=one_position["id_we_vote"],
organization_id_we_vote=one_position["organization_id_we_vote"],
candidate_campaign_id_we_vote=one_position["candidate_campaign_id_we_vote"],
))
示例2: voter_guides_to_follow_retrieve_for_api
# 需要導入模塊: from organization.models import OrganizationManager [as 別名]
# 或者: from organization.models.OrganizationManager import fetch_organization_id [as 別名]
#.........這裏部分代碼省略.........
linked_organization_we_vote_id = ""
if results['voter_found']:
voter = results['voter']
linked_organization_we_vote_id = voter.linked_organization_we_vote_id
number_added_to_list = 0
position_manager = PositionEnteredManager()
position = PositionEntered()
for voter_guide in voter_guide_list:
if positive_value_exists(voter_guide.organization_we_vote_id) \
and linked_organization_we_vote_id == voter_guide.organization_we_vote_id:
# Do not return your own voter guide to follow
continue
position_found = False
one_voter_guide = {
'we_vote_id': voter_guide.we_vote_id,
'google_civic_election_id': voter_guide.google_civic_election_id,
'time_span': voter_guide.vote_smart_time_span,
'voter_guide_display_name': voter_guide.voter_guide_display_name(),
'voter_guide_image_url': voter_guide.voter_guide_image_url(),
'voter_guide_owner_type': voter_guide.voter_guide_owner_type,
'organization_we_vote_id': voter_guide.organization_we_vote_id,
'public_figure_we_vote_id': voter_guide.public_figure_we_vote_id,
'twitter_description': voter_guide.twitter_description,
'twitter_followers_count': voter_guide.twitter_followers_count,
'twitter_handle': voter_guide.twitter_handle,
'owner_voter_id': voter_guide.owner_voter_id,
'last_updated': voter_guide.last_updated.strftime('%Y-%m-%d %H:%M'),
}
if positive_value_exists(ballot_item_we_vote_id):
if kind_of_ballot_item == CANDIDATE:
organization_manager = OrganizationManager()
organization_id = organization_manager.fetch_organization_id(
voter_guide.organization_we_vote_id)
results = position_manager.retrieve_organization_candidate_campaign_position_with_we_vote_id(
organization_id, ballot_item_we_vote_id)
if results['position_found']:
position = results['position']
position_found = True
elif kind_of_ballot_item == MEASURE:
organization_manager = OrganizationManager()
organization_id = organization_manager.fetch_organization_id(
voter_guide.organization_we_vote_id)
results = position_manager.retrieve_organization_contest_measure_position_with_we_vote_id(
organization_id, ballot_item_we_vote_id)
if results['position_found']:
position = results['position']
position_found = True
if position_found:
one_voter_guide['is_support'] = position.is_support()
one_voter_guide['is_positive_rating'] = position.is_positive_rating()
one_voter_guide['is_support_or_positive_rating'] = position.is_support_or_positive_rating()
one_voter_guide['is_oppose'] = position.is_oppose()
one_voter_guide['is_negative_rating'] = position.is_negative_rating()
one_voter_guide['is_oppose_or_negative_rating'] = position.is_oppose_or_negative_rating()
one_voter_guide['is_information_only'] = position.is_information_only()
one_voter_guide['ballot_item_display_name'] = position.ballot_item_display_name
one_voter_guide['speaker_display_name'] = position.speaker_display_name
one_voter_guide['statement_text'] = position.statement_text
one_voter_guide['more_info_url'] = position.more_info_url
one_voter_guide['vote_smart_rating'] = position.vote_smart_rating
one_voter_guide['vote_smart_time_span'] = position.vote_smart_time_span
voter_guides.append(one_voter_guide.copy())
示例3: quick_info_import_from_sample_file
# 需要導入模塊: from organization.models import OrganizationManager [as 別名]
# 或者: from organization.models.OrganizationManager import fetch_organization_id [as 別名]
def quick_info_import_from_sample_file(request=None): # , load_from_uri=False # TODO to be converted
"""
Get the json data, and either create new entries or update existing
:return:
"""
# if load_from_uri:
# # Request json file from We Vote servers
# messages.add_message(request, messages.INFO, "Loading quick_info from We Vote Master servers")
# request = requests.get(QUICK_INFO_URL, params={
# "key": WE_VOTE_API_KEY, # This comes from an environment variable
# })
# structured_json = json.loads(request.text)
# else:
# Load saved json from local file
with open("quick_info/import_data/quick_info_sample.json") as json_data:
structured_json = json.load(json_data)
quick_info_saved = 0
quick_info_updated = 0
quick_info_not_processed = 0
for one_quick_info in structured_json:
# Make sure we have the minimum required variables
if not positive_value_exists(one_quick_info["we_vote_id"]) \
or not positive_value_exists(one_quick_info["organization_we_vote_id"])\
or not positive_value_exists(one_quick_info["candidate_campaign_we_vote_id"]):
quick_info_not_processed += 1
continue
# Check to see if this quick_info is already being used anywhere
quick_info_found = False
try:
if len(one_quick_info["we_vote_id"]) > 0:
quick_info_query = QuickInfo.objects.filter(we_vote_id=one_quick_info["we_vote_id"])
if len(quick_info_query):
quick_info = quick_info_query[0]
quick_info_found = True
except QuickInfo.DoesNotExist as e:
handle_record_not_found_exception(e, logger=logger)
pass
except Exception as e:
handle_record_not_found_exception(e, logger=logger)
# We need to look up the local organization_id based on the newly saved we_vote_id
organization_manager = OrganizationManager()
organization_id = organization_manager.fetch_organization_id(one_quick_info["organization_we_vote_id"])
# We need to look up the local candidate_campaign_id
candidate_campaign_manager = CandidateCampaignManager()
candidate_campaign_id = candidate_campaign_manager.fetch_candidate_campaign_id_from_we_vote_id(
one_quick_info["candidate_campaign_we_vote_id"])
# Find the google_civic_candidate_name so we have a backup way to link quick_info if the we_vote_id is lost
google_civic_candidate_name = one_quick_info["google_civic_candidate_name"] if \
"google_civic_candidate_name" in one_quick_info else ''
if not positive_value_exists(google_civic_candidate_name):
google_civic_candidate_name = candidate_campaign_manager.fetch_google_civic_candidate_name_from_we_vote_id(
one_quick_info["candidate_campaign_we_vote_id"])
# TODO We need to look up contest_measure_id
contest_measure_id = 0
try:
if quick_info_found:
# Update
quick_info.we_vote_id = one_quick_info["we_vote_id"]
quick_info.organization_id = organization_id
quick_info.organization_we_vote_id = one_quick_info["organization_we_vote_id"]
quick_info.candidate_campaign_id = candidate_campaign_id
quick_info.candidate_campaign_we_vote_id = one_quick_info["candidate_campaign_we_vote_id"]
quick_info.google_civic_candidate_name = google_civic_candidate_name
quick_info.contest_measure_id = contest_measure_id
quick_info.date_entered = one_quick_info["date_entered"]
quick_info.google_civic_election_id = one_quick_info["google_civic_election_id"]
quick_info.stance = one_quick_info["stance"]
quick_info.more_info_url = one_quick_info["more_info_url"]
quick_info.statement_text = one_quick_info["statement_text"]
quick_info.statement_html = one_quick_info["statement_html"]
quick_info.save()
quick_info_updated += 1
# messages.add_message(request, messages.INFO, u"QuickInfo updated: {we_vote_id}".format(
# we_vote_id=one_quick_info["we_vote_id"]))
else:
# Create new
quick_info = QuickInfo(
we_vote_id=one_quick_info["we_vote_id"],
organization_id=organization_id,
organization_we_vote_id=one_quick_info["organization_we_vote_id"],
candidate_campaign_id=candidate_campaign_id,
candidate_campaign_we_vote_id=one_quick_info["candidate_campaign_we_vote_id"],
google_civic_candidate_name=google_civic_candidate_name,
contest_measure_id=contest_measure_id,
date_entered=one_quick_info["date_entered"],
google_civic_election_id=one_quick_info["google_civic_election_id"],
stance=one_quick_info["stance"],
more_info_url=one_quick_info["more_info_url"],
statement_text=one_quick_info["statement_text"],
statement_html=one_quick_info["statement_html"],
)
quick_info.save()
quick_info_saved += 1
#.........這裏部分代碼省略.........
示例4: positions_import_from_sample_file
# 需要導入模塊: from organization.models import OrganizationManager [as 別名]
# 或者: from organization.models.OrganizationManager import fetch_organization_id [as 別名]
def positions_import_from_sample_file(request=None, load_from_uri=False):
"""
Get the json data, and either create new entries or update existing
:return:
"""
# if load_from_uri:
# # Request json file from We Vote servers
# messages.add_message(request, messages.INFO, "Loading positions from We Vote Master servers")
# request = requests.get(POSITIONS_URL, params={
# "key": WE_VOTE_API_KEY, # This comes from an environment variable
# })
# structured_json = json.loads(request.text)
# else:
# Load saved json from local file
with open("position/import_data/positions_sample.json") as json_data:
structured_json = json.load(json_data)
positions_saved = 0
positions_updated = 0
positions_not_processed = 0
for one_position in structured_json:
# Make sure we have the minimum required variables
if not positive_value_exists(one_position["we_vote_id"]) \
or not positive_value_exists(one_position["organization_we_vote_id"])\
or not positive_value_exists(one_position["candidate_campaign_we_vote_id"]):
positions_not_processed += 1
continue
# Check to see if this position is already being used anywhere
position_on_stage_found = False
try:
if len(one_position["we_vote_id"]) > 0:
position_query = PositionEntered.objects.filter(we_vote_id=one_position["we_vote_id"])
if len(position_query):
position_on_stage = position_query[0]
position_on_stage_found = True
except PositionEntered.DoesNotExist as e:
handle_record_not_found_exception(e, logger=logger)
pass
except Exception as e:
handle_record_not_found_exception(e, logger=logger)
# We need to look up the local organization_id based on the newly saved we_vote_id
organization_manager = OrganizationManager()
organization_id = organization_manager.fetch_organization_id(one_position["organization_we_vote_id"])
# We need to look up the local candidate_campaign_id
candidate_campaign_manager = CandidateCampaignManager()
candidate_campaign_id = candidate_campaign_manager.fetch_candidate_campaign_id_from_we_vote_id(
one_position["candidate_campaign_we_vote_id"])
# Find the google_civic_candidate_name so we have a backup way to link position if the we_vote_id is lost
google_civic_candidate_name = one_position["google_civic_candidate_name"] if \
"google_civic_candidate_name" in one_position else ''
if not positive_value_exists(google_civic_candidate_name):
google_civic_candidate_name = candidate_campaign_manager.fetch_google_civic_candidate_name_from_we_vote_id(
one_position["candidate_campaign_we_vote_id"])
# TODO We need to look up measure_campaign_id
measure_campaign_id = 0
try:
if position_on_stage_found:
# Update
position_on_stage.we_vote_id = one_position["we_vote_id"]
position_on_stage.organization_id = organization_id
position_on_stage.organization_we_vote_id = one_position["organization_we_vote_id"]
position_on_stage.candidate_campaign_id = candidate_campaign_id
position_on_stage.candidate_campaign_we_vote_id = one_position["candidate_campaign_we_vote_id"]
position_on_stage.google_civic_candidate_name = google_civic_candidate_name
position_on_stage.measure_campaign_id = measure_campaign_id
position_on_stage.date_entered = one_position["date_entered"]
position_on_stage.google_civic_election_id = one_position["google_civic_election_id"]
position_on_stage.stance = one_position["stance"]
position_on_stage.more_info_url = one_position["more_info_url"]
position_on_stage.statement_text = one_position["statement_text"]
position_on_stage.statement_html = one_position["statement_html"]
position_on_stage.save()
positions_updated += 1
# messages.add_message(request, messages.INFO, u"Position updated: {we_vote_id}".format(
# we_vote_id=one_position["we_vote_id"]))
else:
# Create new
position_on_stage = PositionEntered(
we_vote_id=one_position["we_vote_id"],
organization_id=organization_id,
organization_we_vote_id=one_position["organization_we_vote_id"],
candidate_campaign_id=candidate_campaign_id,
candidate_campaign_we_vote_id=one_position["candidate_campaign_we_vote_id"],
google_civic_candidate_name=google_civic_candidate_name,
measure_campaign_id=measure_campaign_id,
date_entered=one_position["date_entered"],
google_civic_election_id=one_position["google_civic_election_id"],
stance=one_position["stance"],
more_info_url=one_position["more_info_url"],
statement_text=one_position["statement_text"],
statement_html=one_position["statement_html"],
)
position_on_stage.save()
positions_saved += 1
#.........這裏部分代碼省略.........