当前位置: 首页>>代码示例>>Python>>正文


Python CandidateCampaignManager.update_or_create_candidate_campaign方法代码示例

本文整理汇总了Python中candidate.models.CandidateCampaignManager.update_or_create_candidate_campaign方法的典型用法代码示例。如果您正苦于以下问题:Python CandidateCampaignManager.update_or_create_candidate_campaign方法的具体用法?Python CandidateCampaignManager.update_or_create_candidate_campaign怎么用?Python CandidateCampaignManager.update_or_create_candidate_campaign使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在candidate.models.CandidateCampaignManager的用法示例。


在下文中一共展示了CandidateCampaignManager.update_or_create_candidate_campaign方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: process_candidates_from_structured_json

# 需要导入模块: from candidate.models import CandidateCampaignManager [as 别名]
# 或者: from candidate.models.CandidateCampaignManager import update_or_create_candidate_campaign [as 别名]
def process_candidates_from_structured_json(
        candidates_structured_json, google_civic_election_id, ocd_division_id, state_code, contest_office_id,
        contest_office_we_vote_id):
    """
    "candidates": [
        {
         "name": "Nancy Pelosi",
         "party": "Democratic"
        },
        {
         "name": "John Dennis",
         "party": "Republican",
         "candidateUrl": "http://www.johndennisforcongress.com/",
         "channels": [
          {
           "type": "Facebook",
           "id": "https://www.facebook.com/johndennis2010"
          },
          {
           "type": "Twitter",
           "id": "https://twitter.com/johndennis2012"
    """
    results = {}
    for one_candidate in candidates_structured_json:
        candidate_name = one_candidate['name'] if 'name' in one_candidate else ''
        # For some reason Google Civic API violates the JSON standard and uses a / in front of '
        candidate_name = candidate_name.replace('/', "'")
        # We want to save the name exactly as it comes from the Google Civic API
        google_civic_candidate_name = one_candidate['name'] if 'name' in one_candidate else ''
        party = one_candidate['party'] if 'party' in one_candidate else ''
        order_on_ballot = one_candidate['orderOnBallot'] if 'orderOnBallot' in one_candidate else 0
        candidate_url = one_candidate['candidateUrl'] if 'candidateUrl' in one_candidate else ''
        photo_url = one_candidate['photoUrl'] if 'photoUrl' in one_candidate else ''
        email = one_candidate['email'] if 'email' in one_candidate else ''
        phone = one_candidate['phone'] if 'phone' in one_candidate else ''

        # Make sure we start with empty channel values
        facebook_url = ''
        twitter_url = ''
        google_plus_url = ''
        youtube_url = ''
        if 'channels' in one_candidate:
            channels = one_candidate['channels']
            for one_channel in channels:
                if 'type' in one_channel:
                    if one_channel['type'] == 'Facebook':
                        facebook_url = one_channel['id'] if 'id' in one_channel else ''
                    if one_channel['type'] == 'Twitter':
                        twitter_url = one_channel['id'] if 'id' in one_channel else ''
                    if one_channel['type'] == 'GooglePlus':
                        google_plus_url = one_channel['id'] if 'id' in one_channel else ''
                    if one_channel['type'] == 'YouTube':
                        youtube_url = one_channel['id'] if 'id' in one_channel else ''

        # DALE 2016-02-20 It would be helpful to call a service here that disambiguated the candidate
        # ...and linked to a politician
        # ...and looked to see if there were any other candidate_campaign entries for this election (in case the
        #   Google Civic contest_office name changed so we generated another contest)

        we_vote_id = ''
        # Make sure we have the minimum variables required to uniquely identify a candidate
        if google_civic_election_id and contest_office_id and candidate_name:
            # NOT using " and ocd_division_id"

            # Make sure there isn't an alternate entry for this election and contest_office (under a similar but
            # slightly different name TODO
            # Note: This doesn't deal with duplicate Presidential candidates. These duplicates are caused because
            # candidates are tied to a particular google_civic_election_id, so there is a different candidate entry
            # for each Presidential candidate for each state.

            updated_candidate_campaign_values = {
                # Values we search against
                'google_civic_election_id': google_civic_election_id,
                'ocd_division_id': ocd_division_id,
                # Note: When we decide to start updating candidate_name elsewhere within We Vote, we should stop
                #  updating candidate_name via subsequent Google Civic imports
                'candidate_name': candidate_name,
                # The rest of the values
                'state_code': state_code,  # Not required due to federal candidates
                'party': party,
                'candidate_email': email,
                'candidate_phone': phone,
                'order_on_ballot': order_on_ballot,
                'candidate_url': candidate_url,
                'photo_url': photo_url,
                'facebook_url': facebook_url,
                'twitter_url': twitter_url,
                'google_plus_url': google_plus_url,
                'youtube_url': youtube_url,
                'google_civic_candidate_name': google_civic_candidate_name,
                # 2016-02-20 Google Civic sometimes changes the name of contests, which can create a new contest
                #  so we may need to update the candidate to a new contest_office_id
                'contest_office_id': contest_office_id,
                'contest_office_we_vote_id': contest_office_we_vote_id,
            }
            candidate_campaign_manager = CandidateCampaignManager()
            results = candidate_campaign_manager.update_or_create_candidate_campaign(
                we_vote_id, google_civic_election_id, ocd_division_id, contest_office_id, contest_office_we_vote_id,
                google_civic_candidate_name, updated_candidate_campaign_values)

#.........这里部分代码省略.........
开发者ID:eternal44,项目名称:WeVoteServer,代码行数:103,代码来源:controllers.py

示例2: process_candidates_from_structured_json

# 需要导入模块: from candidate.models import CandidateCampaignManager [as 别名]
# 或者: from candidate.models.CandidateCampaignManager import update_or_create_candidate_campaign [as 别名]
def process_candidates_from_structured_json(
        candidates_structured_json, google_civic_election_id, ocd_division_id, state_code, contest_office_id,
        contest_office_we_vote_id):
    """
    "candidates": [
        {
         "name": "Nancy Pelosi",
         "party": "Democratic"
        },
        {
         "name": "John Dennis",
         "party": "Republican",
         "candidateUrl": "http://www.johndennisforcongress.com/",
         "channels": [
          {
           "type": "Facebook",
           "id": "https://www.facebook.com/johndennis2010"
          },
          {
           "type": "Twitter",
           "id": "https://twitter.com/johndennis2012"
    """
    results = {}
    for one_candidate in candidates_structured_json:
        candidate_name = one_candidate['name'] if 'name' in one_candidate else ''
        # For some reason Google Civic API violates the JSON standard and uses a
        candidate_name = candidate_name.replace('/', "'")
        # We want to save the name exactly as it comes from the Google Civic API
        google_civic_candidate_name = one_candidate['name'] if 'name' in one_candidate else ''
        party = one_candidate['party'] if 'party' in one_candidate else ''
        order_on_ballot = one_candidate['orderOnBallot'] if 'orderOnBallot' in one_candidate else 0
        candidate_url = one_candidate['candidateUrl'] if 'candidateUrl' in one_candidate else ''
        photo_url = one_candidate['photoUrl'] if 'photoUrl' in one_candidate else ''
        email = one_candidate['email'] if 'email' in one_candidate else ''
        phone = one_candidate['phone'] if 'phone' in one_candidate else ''

        # set them to channel values to empty
        facebook_url = ''
        twitter_url = ''
        google_plus_url = ''
        youtube_url = ''
        if 'channels' in one_candidate:
            channels = one_candidate['channels']
            for one_channel in channels:
                if 'type' in one_channel:
                    if one_channel['type'] == 'Facebook':
                        facebook_url = one_channel['id'] if 'id' in one_channel else ''
                    if one_channel['type'] == 'Twitter':
                        twitter_url = one_channel['id'] if 'id' in one_channel else ''
                    if one_channel['type'] == 'GooglePlus':
                        google_plus_url = one_channel['id'] if 'id' in one_channel else ''
                    if one_channel['type'] == 'YouTube':
                        youtube_url = one_channel['id'] if 'id' in one_channel else ''

        we_vote_id = ''
        if google_civic_election_id and ocd_division_id and contest_office_id and candidate_name:
            updated_candidate_campaign_values = {
                # Values we search against
                'google_civic_election_id': google_civic_election_id,
                'ocd_division_id': ocd_division_id,
                'contest_office_id': contest_office_id,
                'contest_office_we_vote_id': contest_office_we_vote_id,
                # Note: When we decide to start updating candidate_name elsewhere within We Vote, we should stop
                #  updating candidate_name via subsequent Google Civic imports
                'candidate_name': candidate_name,
                # The rest of the values
                'state_code': state_code,  # Not required due to federal candidates
                'party': party,
                'candidate_email': email,
                'candidate_phone': phone,
                'order_on_ballot': order_on_ballot,
                'candidate_url': candidate_url,
                'photo_url': photo_url,
                'facebook_url': facebook_url,
                'twitter_url': twitter_url,
                'google_plus_url': google_plus_url,
                'youtube_url': youtube_url,
                'google_civic_candidate_name': google_civic_candidate_name,
            }
            candidate_campaign_manager = CandidateCampaignManager()
            results = candidate_campaign_manager.update_or_create_candidate_campaign(
                we_vote_id, google_civic_election_id, ocd_division_id, contest_office_id, contest_office_we_vote_id,
                google_civic_candidate_name, updated_candidate_campaign_values)

    return results
开发者ID:JesseAldridge,项目名称:WeVoteServer,代码行数:87,代码来源:controllers.py


注:本文中的candidate.models.CandidateCampaignManager.update_or_create_candidate_campaign方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。