Skip to content

Commit

Permalink
feat: code sends boundaries and their corresponding coordinates to wi…
Browse files Browse the repository at this point in the history
…dget template
  • Loading branch information
lc-hd committed Dec 2, 2023
1 parent 355a0c6 commit f608ec1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
9 changes: 9 additions & 0 deletions communities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,19 @@ class Coordinate(models.Model):
latitude = models.DecimalField(max_digits=9, decimal_places=6)
longitude = models.DecimalField(max_digits=9, decimal_places=6)

def get_coordinate(self):
return self.latitude, self.longitude


class Boundary(models.Model):
coordinates = models.ManyToManyField(Coordinate, related_name="coordinates")

def get_coordinates(self):
return [
(float(c.latitude), float(c.longitude))
for c in self.coordinates.all()
]


class Community(models.Model):
community_creator = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
Expand Down
14 changes: 7 additions & 7 deletions communities/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
from django.template import loader
from django.utils.safestring import mark_safe

from communities.models import Boundary


class BoundaryWidget(Widget):
template_name = 'widget_forms/community/boundary_widget.html'

def get_context(self, name, value, attrs=None):
return {'widget': {
'name': name,
'value': value,
}}

def render(self, name, value, attrs=None, renderer=None):
context = self.get_context(name, value, attrs)
boundaries = {}
for boundary_id in value:
boundaries[boundary_id] = Boundary.objects.get(id=boundary_id).get_coordinates()

context = {'boundaries': boundaries}
template = loader.get_template(self.template_name).render(context)
return mark_safe(template)

0 comments on commit f608ec1

Please sign in to comment.