def migrate_country(entry): """ Migration of a JSON-represented country. """ try: corresp_map = { 'code': entry['fields']['code'], 'wikiname': entry['fields']['wikiname'], 'currency': entry['fields']['currency'], 'slug': entry['fields']['slug'], 'name': entry['fields']['name'], } additional_corresp_map = { 'old_pk': entry['pk'], 'old_capitalcity_pk': entry['fields']['capital_city'], } except KeyError: raise RuntimeError(u"Invalid JSON configuration: %s" % str(entry)) country = Country.find_one({'code': entry['fields']['code']}) if not country: country = Country() country.set_lang("en") for k, v in corresp_map.iteritems(): setattr(country, k, v) for k, v in additional_corresp_map.iteritems(): country[k] = v country.save() return country