Skip to content

Commit

Permalink
Add status in geo area
Browse files Browse the repository at this point in the history
  • Loading branch information
sudan45 committed Oct 23, 2024
1 parent 0e189e2 commit 3646af8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
18 changes: 18 additions & 0 deletions apps/geo/migrations/0044_region_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.25 on 2024-10-21 08:59

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('geo', '0043_create-unaccent_extension'),
]

operations = [
migrations.AddField(
model_name='region',
name='status',
field=models.CharField(choices=[('initiated', 'Initiated'), ('pending', 'Pending'), ('completed', 'Completed'), ('failed', 'Failed')], default='pending', max_length=30),
),
]
6 changes: 6 additions & 0 deletions apps/geo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@


class Region(UserResource):
class Status(models.TextChoices):
INITIATED = 'initiated', 'Initiated'
PENDING = 'pending', 'Pending'
COMPLETED = 'completed', 'Completed'
FAILED = 'failed', 'Failed'
"""
Region model
Expand All @@ -35,6 +40,7 @@ class Region(UserResource):
cache_index = models.SmallIntegerField(default=0) # Used to track cache update.
centroid = models.PointField(blank=True, null=True) # Admin level 0 centroid
geo_options = models.JSONField(default=None, blank=True, null=True)
status = models.CharField(max_length=30, choices=Status.choices, default=Status.PENDING)

def __init__(self, *args, **kwargs):
self.id: Union[int, None]
Expand Down
1 change: 1 addition & 0 deletions apps/geo/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def create(self, validated_data):
admin_level.save()

region = admin_level.region
region.status = Region.Status.INITIATED
region.modified_by = self.context['request'].user
region.save()

Expand Down
39 changes: 22 additions & 17 deletions apps/geo/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,26 +181,31 @@ def _load_geo_areas(region_id):
Basically, it starts with root admin level and iterate through all the
children.
"""
region = Region.objects.get(pk=region_id)
try:
with reversion.create_revision():
if AdminLevel.objects.filter(region=region).count() == 0:
return True

with reversion.create_revision():
region = Region.objects.get(pk=region_id)

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,
)
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()
region.calc_cache()
region.status = Region.Status.COMPLETED
region.save()
return True

return True
except Exception:
logger.error('Load Geo Areas', exc_info=True)
region.status = Region.Status.FAILED
region.save()


@shared_task
Expand Down

0 comments on commit 3646af8

Please sign in to comment.