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


Python PopItPerson.create_from_dict方法代码示例

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


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

示例1: handle

# 需要导入模块: from candidates.models import PopItPerson [as 别名]
# 或者: from candidates.models.PopItPerson import create_from_dict [as 别名]
 def handle(self, **options):
     for collection in ('organization', 'person'):
         api_collection = getattr(self.api, collection + 's')
         message = "{titled} {base_url}{plural}/{id}"
         for item in popit_unwrap_pagination(
                 api_collection,
                 embed='',
                 per_page=100
         ):
             print message.format(
                 titled=collection.title(),
                 base_url=get_base_url(),
                 plural=(collection + "s"),
                 id=item['id']
             )
             for image in item.get('images', []):
                 print "  Image with URL:", image['url']
                 fix_image(image)
                 # Some images have an empty 'created' field, which
                 # causes an Elasticsearch indexing error, so change it
                 # to null if that's the case:
                 if not image.get('created'):
                     image['created'] = None
             fix_dates(item)
             try:
                 api_collection(item['id']).put(item)
             except HttpClientError as e:
                 print "HttpClientError", e.content
                 sys.exit(1)
             # If this is a person, make sure that the
             # corresponding cache entries are invalidated:
             if collection == 'person':
                 person = PopItPerson.create_from_dict(item)
                 person.invalidate_cache_entries()
开发者ID:YoQuieroSaber,项目名称:yournextrepresentative,代码行数:36,代码来源:candidates_fix_image_metadata.py

示例2: handle

# 需要导入模块: from candidates.models import PopItPerson [as 别名]
# 或者: from candidates.models.PopItPerson import create_from_dict [as 别名]
 def handle(self, **options):
     all_people = []
     for person_dict in popit_unwrap_pagination(
             self.api.persons,
             embed="membership.organization",
             per_page=100,
     ):
         if person_dict.get('standing_in') \
             and person_dict['standing_in'].get(options['year']):
             person = PopItPerson.create_from_dict(person_dict)
             all_people.append(person.as_dict(year=options['year']))
     csv = list_to_csv(all_people)
     # Write to stdout if no output filename is specified, or if it
     # is '-'
     if options['output_filename'] in (None, '-'):
         with sys.stdout as f:
             f.write(csv)
     else:
         # Otherwise write to a temporary file and atomically
         # rename into place:
         ntf = NamedTemporaryFile(
             delete=False,
             dir=dirname(options['output_filename'])
         )
         ntf.write(csv)
         chmod(ntf.name, 0o644)
         rename(ntf.name, options['output_filename'])
开发者ID:EdwardBetts,项目名称:yournextmp-popit,代码行数:29,代码来源:candidates_create_csv.py

示例3: get_existing_popit_person

# 需要导入模块: from candidates.models import PopItPerson [as 别名]
# 或者: from candidates.models.PopItPerson import create_from_dict [as 别名]
def get_existing_popit_person(vi_person_id):
    from candidates.models import PopItPerson
    from candidates.popit import get_search_url
    # See if this person already exists by searching for the
    # ID they were imported with:
    query_format = \
        'identifiers.identifier:"{id}" AND ' + \
        'identifiers.scheme:"{scheme}"'
    search_url = get_search_url(
        'persons',
        query_format.format(
            id=vi_person_id, scheme='import-id'
        ),
        embed='membership.organization'
    )
    results = requests.get(search_url).json()

    total = results['total']
    if total > 1:
        message = "Multiple matches for CI ID {0}"
        raise Exception(message.format(vi_person_id))
    if total == 0:
        return None
    # Otherwise there was exactly one result:
    return PopItPerson.create_from_dict(results['result'][0])
开发者ID:ConocimientoAbierto,项目名称:policrowd,代码行数:27,代码来源:ar_elections_2015_import_candidates_csv.py

示例4: handle

# 需要导入模块: from candidates.models import PopItPerson [as 别名]
# 或者: from candidates.models.PopItPerson import create_from_dict [as 别名]
 def handle(self, **options):
     for person_data in popit_unwrap_pagination(
             self.api.persons,
             embed='',
             per_page=100
     ):
         msg = "Person {0}persons/{1}"
         print msg.format(get_base_url(), person_data['id'])
         strip_bogus_fields(
             person_data,
             [
                 'founding_date',
                 'dissolution_date',
                 'start_date',
                 'end_date'
             ]
         )
         for image in person_data.get('images', []):
             strip_bogus_fields(
                 image,
                 [
                     'birth_date',
                     'death_date',
                     'founding_date',
                     'dissolution_date',
                     'start_date',
                     'end_date'
                 ]
             )
         person = PopItPerson.create_from_dict(person_data)
         person.save_to_popit(self.api)
         person.invalidate_cache_entries()
开发者ID:YoQuieroSaber,项目名称:yournextrepresentative,代码行数:34,代码来源:candidates_remove_bogus_dates.py

示例5: parse_data

# 需要导入模块: from candidates.models import PopItPerson [as 别名]
# 或者: from candidates.models.PopItPerson import create_from_dict [as 别名]
 def parse_data(self, json_file):
     with open(json_file) as f:
         for ec_party in json.load(f):
             ec_party_id = ec_party['ECRef'].strip()
             # We're only interested in political parties:
             if not ec_party_id.startswith('PP'):
                 continue
             party_id = self.clean_id(ec_party_id)
             if ec_party['RegulatedEntityTypeName'] == 'Minor Party':
                 register = ec_party['RegisterNameMinorParty'].replace(
                     ' (minor party)', ''
                 )
             else:
                 register = ec_party['RegisterName']
             party_name, party_dissolved = self.clean_name(ec_party['RegulatedEntityName'])
             party_founded = self.clean_date(ec_party['ApprovedDate'])
             party_data = {
                 'id': party_id,
                 'name': party_name,
                 'slug': slugify(party_name),
                 'classification': 'Party',
                 'descriptions': get_descriptions(ec_party),
                 'founding_date': party_founded,
                 'dissolution_date': party_dissolved,
                 'register': register,
                 'identifiers': [
                     {
                         'identifier': ec_party_id,
                         'scheme': 'electoral-commission',
                     }
                 ]
             }
             try:
                 self.api.organizations.post(party_data)
                 self.upload_images(ec_party['PartyEmblems'], party_id)
             except HttpServerError as e:
                 if 'E11000' in e.content:
                     # Duplicate Party Found
                     self.api.organizations(party_id).put(party_data)
                     self.upload_images(ec_party['PartyEmblems'], party_id)
                 else:
                     raise
             organization_with_memberships = \
                 self.api.organizations(party_id).get(embed='membership.person')['result']
             # Make sure any members of these parties are
             # invalidated from the cache so that the embedded
             # party information when getting posts and persons is
             # up-to-date:
             for membership in organization_with_memberships.get(
                     'memberships', []
             ):
                 person = PopItPerson.create_from_dict(membership['person_id'])
                 person.invalidate_cache_entries()
开发者ID:YoQuieroSaber,项目名称:yournextrepresentative,代码行数:55,代码来源:candidates_update_popit_parties_from_ec_data.py

示例6: handle

# 需要导入模块: from candidates.models import PopItPerson [as 别名]
# 或者: from candidates.models.PopItPerson import create_from_dict [as 别名]
    def handle(self, *args, **options):
        from candidates.models import PopItPerson
        from candidates.popit import create_popit_api_object

        self.verbosity = int(options.get('verbosity', 1))
        api = create_popit_api_object()
        if len(args) != 1:
            raise CommandError("You must provide a person.js URL")
        person_js_url = args[0]
        people_data = requests.get(person_js_url).json()
        for person_data in people_data['persons']:
            twfy_person = PopItPerson.create_from_dict(person_data)
            ynmp_id = twfy_person.get_identifier('yournextmp')
            if not ynmp_id:
                continue
            parlparse_id = twfy_person.id
            ynmp_person = PopItPerson.create_from_popit(api, ynmp_id)
            existing_parlparse_id = ynmp_person.get_identifier('uk.org.publicwhip')
            if existing_parlparse_id:
                if existing_parlparse_id == parlparse_id:
                    # That's fine, there's already the right parlparse ID
                    pass
                else:
                    # Otherwise there's a mismatch, which needs investigation
                    msg = "Warning: parlparse ID mismatch between YNMP {0} "
                    msg += "and TWFY {1} for YNMP person {2}\n"
                    self.stderr.write(
                        msg.format(
                            existing_parlparse_id,
                            parlparse_id,
                            ynmp_id,
                        )
                    )
                continue
            msg = "Updating the YourNextMP person {0} with parlparse_id {1}\n"
            self.stdout.write(msg.format(ynmp_id, parlparse_id))
            ynmp_person.set_identifier(
                'uk.org.publicwhip',
                parlparse_id,
            )
            change_metadata = get_change_metadata(
                None, "Fetched a new parlparse ID"
            )
            ynmp_person.record_version(change_metadata)
            ynmp_person.save_to_popit(api)
            ynmp_person.invalidate_cache_entries()
开发者ID:ConocimientoAbierto,项目名称:policrowd,代码行数:48,代码来源:candidates_add_new_parlparse_ids.py

示例7: handle

# 需要导入模块: from candidates.models import PopItPerson [as 别名]
# 或者: from candidates.models.PopItPerson import create_from_dict [as 别名]
 def handle(self, **options):
     for o in popit_unwrap_pagination(
             self.api.organizations,
             per_page=100,
             embed='membership.person'
     ):
         if o['classification'] != 'Party':
             continue
         print o['name']
         for image in o.get('images', []):
             print "  DELETE", image['_id']
             self.api.organizations(o['id']).image(image['_id']).delete()
         # The person pages get party images via the
         # membership.organization embed, so invalidate the cache
         # entries for any person who's a member of this party:
         for membership in o.get('memberships', []):
             person = PopItPerson.create_from_dict(membership['person_id'])
             person.invalidate_cache_entries()
开发者ID:YoQuieroSaber,项目名称:yournextrepresentative,代码行数:20,代码来源:candidates_delete_party_images.py

示例8: handle

# 需要导入模块: from candidates.models import PopItPerson [as 别名]
# 或者: from candidates.models.PopItPerson import create_from_dict [as 别名]
 def handle(self, **options):
     for person_data in popit_unwrap_pagination(
             self.api.persons,
             embed='',
             per_page=100
     ):
         needs_update = False
         for version in person_data.get('versions', []):
             data = version['data']
             if data.get('last_party'):
                 needs_update = True
                 msg = "Fixing person {0}persons/{1}"
                 print msg.format(get_base_url(), person_data['id'])
                 del data['last_party']
         if not needs_update:
             continue
         person = PopItPerson.create_from_dict(person_data)
         person.save_to_popit(self.api)
         person.invalidate_cache_entries()
开发者ID:YoQuieroSaber,项目名称:yournextrepresentative,代码行数:21,代码来源:candidates_remove_last_party_from_versions.py

示例9: handle

# 需要导入模块: from candidates.models import PopItPerson [as 别名]
# 或者: from candidates.models.PopItPerson import create_from_dict [as 别名]
 def handle(self, *args, **options):
     if len(args) != 1:
         msg = "You must supply the prefix for output filenames"
         raise CommandError(msg)
     output_prefix = args[0]
     all_people = []
     election_to_people = defaultdict(list)
     for person_dict in popit_unwrap_pagination(
             self.api.persons,
             embed="membership.organization",
             per_page=100,
     ):
         standing_in = person_dict.get('standing_in')
         if not standing_in:
             continue
         for election in standing_in.keys():
             if not standing_in[election]:
                 continue
             person = PopItPerson.create_from_dict(person_dict)
             person_as_csv_dict = person.as_dict(election=election)
             all_people.append(person_as_csv_dict)
             election_to_people[election].append(person_as_csv_dict)
     elections = election_to_people.keys() + [None]
     for election in elections:
         if election is None:
             output_filename = output_prefix + '-all.csv'
             people_data = all_people
         else:
             output_filename = output_prefix + '-' + election + '.csv'
             people_data = election_to_people[election]
         csv = list_to_csv(people_data)
         # Otherwise write to a temporary file and atomically
         # rename into place:
         ntf = NamedTemporaryFile(
             delete=False,
             dir=dirname(output_filename)
         )
         ntf.write(csv)
         chmod(ntf.name, 0o644)
         rename(ntf.name, output_filename)
开发者ID:YoQuieroSaber,项目名称:yournextrepresentative,代码行数:42,代码来源:candidates_create_csv.py

示例10: test_age_year_ambiguous

# 需要导入模块: from candidates.models import PopItPerson [as 别名]
# 或者: from candidates.models.PopItPerson import create_from_dict [as 别名]
 def test_age_year_ambiguous(self, mock_date):
     mock_date.today.return_value = date(1977, 9, 10)
     mock_date.side_effect = \
         lambda *args, **kwargs: date(*args, **kwargs)
     p = PopItPerson.create_from_dict({'birth_date': '1975'})
     self.assertEqual(p.age, '1 or 2')
开发者ID:YoQuieroSaber,项目名称:yournextrepresentative,代码行数:8,代码来源:test_age.py

示例11: test_age_month_early_in_year

# 需要导入模块: from candidates.models import PopItPerson [as 别名]
# 或者: from candidates.models.PopItPerson import create_from_dict [as 别名]
 def test_age_month_early_in_year(self, mock_date):
     mock_date.today.return_value = date(1977, 8, 15)
     mock_date.side_effect = \
         lambda *args, **kwargs: date(*args, **kwargs)
     p = PopItPerson.create_from_dict({'birth_date': '1976-09'})
     self.assertEqual(p.age, '0')
开发者ID:YoQuieroSaber,项目名称:yournextrepresentative,代码行数:8,代码来源:test_age.py

示例12: test_age_full_obvious

# 需要导入模块: from candidates.models import PopItPerson [as 别名]
# 或者: from candidates.models.PopItPerson import create_from_dict [as 别名]
 def test_age_full_obvious(self, mock_date):
     mock_date.today.return_value = date(1977, 9, 3)
     mock_date.side_effect = \
         lambda *args, **kwargs: date(*args, **kwargs)
     p = PopItPerson.create_from_dict({'birth_date': '1976-09-01'})
     self.assertEqual(p.age, '1')
开发者ID:YoQuieroSaber,项目名称:yournextrepresentative,代码行数:8,代码来源:test_age.py


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