當前位置: 首頁>>代碼示例>>Python>>正文


Python CandidateCampaignManager.retrieve_candidate_campaign_from_candidate_name方法代碼示例

本文整理匯總了Python中candidate.models.CandidateCampaignManager.retrieve_candidate_campaign_from_candidate_name方法的典型用法代碼示例。如果您正苦於以下問題:Python CandidateCampaignManager.retrieve_candidate_campaign_from_candidate_name方法的具體用法?Python CandidateCampaignManager.retrieve_candidate_campaign_from_candidate_name怎麽用?Python CandidateCampaignManager.retrieve_candidate_campaign_from_candidate_name使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在candidate.models.CandidateCampaignManager的用法示例。


在下文中一共展示了CandidateCampaignManager.retrieve_candidate_campaign_from_candidate_name方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: transfer_maplight_data_to_we_vote_tables

# 需要導入模塊: from candidate.models import CandidateCampaignManager [as 別名]
# 或者: from candidate.models.CandidateCampaignManager import retrieve_candidate_campaign_from_candidate_name [as 別名]
def transfer_maplight_data_to_we_vote_tables(request):
    # TODO We need to perhaps set up a table for these mappings that volunteers can add to?
    #  We need a plan for how volunteers can help us add to these mappings
    # One possibility -- ask volunteers to update this Google Sheet, then write a csv importer:
    #  https://docs.google.com/spreadsheets/d/1havD7GCxmBhi-zLLMdOpSJlU_DtBjvb5IJNiXgno9Bk/edit#gid=0
    politician_name_mapping_list = []
    one_mapping = {
        "google_civic_name": "Betty T. Yee",
        "maplight_display_name": "Betty Yee",
        "maplight_original_name": "Betty T Yee",
    }
    politician_name_mapping_list.append(one_mapping)
    one_mapping = {
        "google_civic_name": "Edmund G. \"Jerry\" Brown",
        "maplight_display_name": "Jerry Brown",
        "maplight_original_name": "",
    }
    politician_name_mapping_list.append(one_mapping)

    candidate_campaign_manager = CandidateCampaignManager()

    maplight_candidates_current_query = MapLightCandidate.objects.all()

    for one_candidate_from_maplight_table in maplight_candidates_current_query:
        found_by_id = False
        # Try to find a matching candidate
        results = candidate_campaign_manager.retrieve_candidate_campaign_from_id_maplight(
            one_candidate_from_maplight_table.candidate_id)

        if not results['success']:
            logger.warn(u"Candidate NOT found by MapLight id: {name}".format(
                name=one_candidate_from_maplight_table.candidate_id
            ))
            results = candidate_campaign_manager.retrieve_candidate_campaign_from_candidate_name(
                one_candidate_from_maplight_table.display_name)

            if not results['success']:
                logger.warn(u"Candidate NOT found by display_name: {name}".format(
                    name=one_candidate_from_maplight_table.display_name
                ))
                results = candidate_campaign_manager.retrieve_candidate_campaign_from_candidate_name(
                    one_candidate_from_maplight_table.original_name)

                if not results['success']:
                    logger.warn(u"Candidate NOT found by original_name: {name}".format(
                        name=one_candidate_from_maplight_table.original_name
                    ))

                    one_mapping_google_civic_name = ''
                    for one_mapping_found in politician_name_mapping_list:
                        if positive_value_exists(one_mapping_found['maplight_display_name']) \
                                and one_mapping_found['maplight_display_name'] == \
                                one_candidate_from_maplight_table.display_name:
                            one_mapping_google_civic_name = one_mapping_found['google_civic_name']
                            break
                    if positive_value_exists(one_mapping_google_civic_name):
                        results = candidate_campaign_manager.retrieve_candidate_campaign_from_candidate_name(
                            one_mapping_google_civic_name)
                    if not results['success'] or not positive_value_exists(one_mapping_google_civic_name):
                        logger.warn(u"Candidate NOT found by mapping to google_civic name: {name}".format(
                            name=one_mapping_google_civic_name
                        ))

                        continue  # Go to the next candidate

        candidate_campaign_on_stage = results['candidate_campaign']

        # Just in case the logic above let us through to here accidentally without a candidate_name value, don't proceed
        if not positive_value_exists(candidate_campaign_on_stage.candidate_name):
            continue

        logger.debug(u"Candidate {name} found".format(
            name=candidate_campaign_on_stage.candidate_name
        ))

        try:
            # Tie the maplight id to our record
            if not found_by_id:
                candidate_campaign_on_stage.id_maplight = one_candidate_from_maplight_table.candidate_id

            # Bring over the photo
            candidate_campaign_on_stage.photo_url_from_maplight = one_candidate_from_maplight_table.photo

            # We can bring over other data as needed, like gender for example
            candidate_campaign_on_stage.save()
        except Exception as e:
            handle_record_not_saved_exception(e, logger=logger)

    messages.add_message(request, messages.INFO, 'MapLight data woven into We Vote tables.')

    return HttpResponseRedirect(reverse('import_export:import_export_index', args=()))
開發者ID:josephevans,項目名稱:WeVoteServer,代碼行數:93,代碼來源:views_admin.py


注:本文中的candidate.models.CandidateCampaignManager.retrieve_candidate_campaign_from_candidate_name方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。