Skip to content

Instantly share code, notes, and snippets.

@tmtlbt
Created February 25, 2015 10:02
Show Gist options
  • Select an option

  • Save tmtlbt/ed3715f0047b4775f265 to your computer and use it in GitHub Desktop.

Select an option

Save tmtlbt/ed3715f0047b4775f265 to your computer and use it in GitHub Desktop.
Wagtail Map Locatable Mixin
class Locatable(models.Model):
location_name = models.CharField(
max_length=255,
blank=True)
location = models.ForeignKey(
'home.Location',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+',
editable=False
)
def save_location(self):
if self.location_name:
self.location = Location.objects.get_or_create(
# iexact doesn't work with get_or_create :(
name=self.location_name.upper()
)[0]
self.location.save()
if not self.location_name and self.location:
# If self.location_name has been set to None,
# make sure to set self.location to None
self.location = None
@staticmethod
def search_locations(q):
location = Location()
location.name = q
location.geocode()
return location.find_nearby_locations()
def search_children_locations(self, q, model):
return model.objects.filter(
location__in=self.search_locations(q)
).child_of(self).live()
def search_sibling_locations(self, q):
return self.__class__.objects.filter(
location__in=self.search_locations(q)
).sibling_of(self).live()
panels = [
FieldPanel('location_name')
]
class Meta:
abstract = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment