def get(self, request, region_id, version=None): if not Region.objects.filter(id=region_id).exists(): raise exceptions.NotFound() if not Region.objects.get(id=region_id).can_get(request.user): raise exceptions.PermissionDenied() if not settings.TESTING: load_geo_areas.delay(region_id) return response.Response({ 'load_triggered': region_id, })
def import_country(self, country): print('------------') print('Migrating country') code = country['reference_code'] modified_code = country['code'] title = country['name'] print('{} - {}'.format(code, title)) public = (code == modified_code) migration, _ = CountryMigration.objects.get_or_create( code=modified_code, ) if not migration.region: region = Region.objects.create( code=code, title=title, ) migration.region = region migration.save() else: return migration.region region = migration.region region.code = code region.title = title region.public = public region.regional_groups = country['regions'] region.key_figures = country['key_figures'] region.media_sources = country['media_sources'] region.save() admin_levels = country['admin_levels'] admin_levels.sort(key=lambda a: a['level']) parent = None for admin_level in admin_levels: parent = self.import_admin_level(region, parent, admin_level) load_geo_areas.delay(region.id) return region
def create(self, validated_data): admin_level = super().create(validated_data) admin_level.stale_geo_areas = True admin_level.save() region = admin_level.region region.modified_by = self.context['request'].user region.save() if not settings.TESTING: transaction.on_commit(lambda: load_geo_areas.delay(region.id)) return admin_level
def update(self, instance, validated_data): admin_level = super(AdminLevelSerializer, self).update( instance, validated_data, ) admin_level.stale_geo_areas = True admin_level.save() region = admin_level.region region.modified_by = self.context['request'].user region.save() if not settings.TESTING: transaction.on_commit(lambda: load_geo_areas.delay(region.id)) return admin_level