diff --git a/apps/geo/tasks.py b/apps/geo/tasks.py index b895565499..2305e02c86 100644 --- a/apps/geo/tasks.py +++ b/apps/geo/tasks.py @@ -181,34 +181,26 @@ def _load_geo_areas(region_id): Basically, it starts with root admin level and iterate through all the children. """ - region = Region.objects.filter(pk=region_id).first() - if not region: - logger.error("Region not found", exc_info=True) - return False - try: - with reversion.create_revision(): - if AdminLevel.objects.filter(region=region).count() == 0: - return True - parent_admin_levels = AdminLevel.objects.filter( - region=region, parent=None - ) - completed_levels = [] - _extract_from_admin_levels( - parent_admin_levels, - None, - completed_levels, - ) + with reversion.create_revision(): + region = Region.objects.get(pk=region_id) - region.calc_cache() - region.status = Region.Status.COMPLETED - region.save() - return True + if AdminLevel.objects.filter(region=region).count() == 0: + return True - except Exception: - logger.error('Load Geo Areas', exc_info=True) - region.status = Region.Status.FAILED - region.save() + parent_admin_levels = AdminLevel.objects.filter( + region=region, parent=None + ) + completed_levels = [] + _extract_from_admin_levels( + parent_admin_levels, + None, + completed_levels, + ) + + region.calc_cache() + + return True @shared_task @@ -218,14 +210,17 @@ def load_geo_areas(region_id): have_lock = lock.acquire(blocking=False) if not have_lock: return False - + region = Region.objects.filter(id=region_id).first() + region.status = Region.Status.INITIATED try: - region_id.status = Region.Status.INITIATED return_value = _load_geo_areas(region_id) + region.status = Region.Status.COMPLETED except Exception: logger.error('Load Geo Areas', exc_info=True) return_value = False + region.status = Region.Status.FAILED + region.save() lock.release() return return_value