Skip to content

Commit

Permalink
Remove AdminBoundary and BoundaryAlias models, use django postgres de…
Browse files Browse the repository at this point in the history
…fault DB engine
  • Loading branch information
norkans7 committed Jan 10, 2025
1 parent 04fc1c0 commit bda0797
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 5.1.4 on 2025-01-09 20:18

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("locations", "0034_populate_locations"),
]

operations = [
migrations.RemoveField(
model_name="boundaryalias",
name="boundary",
),
migrations.RemoveField(
model_name="boundaryalias",
name="created_by",
),
migrations.RemoveField(
model_name="boundaryalias",
name="modified_by",
),
migrations.RemoveField(
model_name="boundaryalias",
name="org",
),
migrations.DeleteModel(
name="AdminBoundary",
),
migrations.DeleteModel(
name="BoundaryAlias",
),
]
64 changes: 3 additions & 61 deletions temba/locations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,23 @@
from mptt.models import MPTTModel, TreeForeignKey
from smartmin.models import SmartModel

from django.contrib.gis.db import models
from django.db import models
from django.db.models import F, Value
from django.db.models.functions import Concat, Lower, Upper


# default manager for AdminBoundary, doesn't load geometries
# default manager for Location, doesn't load geometries
class NoGeometryManager(models.Manager):
def get_queryset(self):
return super().get_queryset().defer("simplified_geometry")


# optional 'geometries' manager for AdminBoundary, loads everything
# optional 'geometries' manager for Location, loads everything
class GeometryManager(models.Manager):
def get_queryset(self):
return super().get_queryset()


class AdminBoundary(MPTTModel, models.Model):
"""
Represents a single administrative boundary (like a country, state or district)
"""

LEVEL_COUNTRY = 0
LEVEL_STATE = 1
LEVEL_DISTRICT = 2
LEVEL_WARD = 3

MAX_NAME_LEN = 128

# Used to separate segments in a hierarchy of boundaries. Has the advantage of being a character in GSM7 and
# being very unlikely to show up in an admin boundary name.
PATH_SEPARATOR = ">"
PADDED_PATH_SEPARATOR = " > "

osm_id = models.CharField(max_length=15, unique=True)
name = models.CharField(max_length=MAX_NAME_LEN)
level = models.IntegerField()
parent = TreeForeignKey("self", null=True, on_delete=models.PROTECT, related_name="children", db_index=True)
path = models.CharField(max_length=768) # e.g. Rwanda > Kigali
simplified_geometry = models.MultiPolygonField(null=True)

objects = NoGeometryManager()
geometries = GeometryManager()

def release(self):
for child_boundary in AdminBoundary.objects.filter(parent=self): # pragma: no cover
child_boundary.release()

self.aliases.all().delete()
self.delete()

def __str__(self):
return self.name

class Meta:
indexes = [models.Index(Upper("name"), name="adminboundaries_by_name")]


class BoundaryAlias(SmartModel):
"""
An org specific alias for a boundary name
"""

org = models.ForeignKey("orgs.Org", on_delete=models.PROTECT)
boundary = models.ForeignKey(AdminBoundary, on_delete=models.PROTECT, related_name="aliases")
name = models.CharField(max_length=AdminBoundary.MAX_NAME_LEN, help_text="The name for our alias")

@classmethod
def create(cls, org, user, boundary, name):
return cls.objects.create(org=org, boundary=boundary, name=name, created_by=user, modified_by=user)

class Meta:
indexes = [models.Index(Upper("name"), name="boundaryaliases_by_name")]


class Location(MPTTModel, models.Model):
"""
Represents a single administrative locations (like a country, state or district)
Expand Down
2 changes: 1 addition & 1 deletion temba/settings_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@
# Database
# -----------------------------------------------------------------------------------
_default_database_config = {
"ENGINE": "django.contrib.gis.db.backends.postgis",
"ENGINE": "django.db.backends.postgresql",
"NAME": "temba",
"USER": "temba",
"PASSWORD": "temba",
Expand Down

0 comments on commit bda0797

Please sign in to comment.