def get(self, request, *args, **kwargs): # default sort is "end_date" self.ordering = self.request.GET.get("ordering") if not self.ordering: self.ordering = self.request.session.get("event_sort", "end_date") else: self.request.session["event_sort"] = self.ordering bounds = self.request.GET.get("bounds", None) self.zoom = self.request.GET.get("zoom", 8) if bounds: try: bounds_list = bounds.split(",") sw = (bounds_list[0], bounds_list[1]) ne = (bounds_list[2], bounds_list[3]) xmin = sw[1] ymin = sw[0] xmax = ne[1] ymax = ne[0] bbox = (xmin, ymin, xmax, ymax) self.bounds = Polygon.from_bbox(bbox) if self.request.user.is_authenticated(): profile = self.request.user.profile profile.last_known_location_bounds = self.bounds profile.last_map_zoom = self.zoom profile.save() except IndexError: pass if not self.bounds and self.request.user.is_authenticated(): self.bounds = self.request.user.profile.last_known_location_bounds self.zoom = self.request.user.profile.last_map_zoom if self.bounds: centroid = self.bounds.centroid self.latitude = centroid.y self.longitude = centroid.x neighborhood = rev_geocode_location_component(self.latitude, self.longitude, "neighborhood") if not neighborhood: # If not succeed in first try, try again (with a bigger net) neighborhood = rev_geocode_location_component(self.latitude, self.longitude, "political") city = rev_geocode_location_component(self.latitude, self.longitude, "locality") if not city: # If not succeed in first try, try again (with a bigger net) city = rev_geocode_location_component(self.latitude, self.longitude, "administrative_area") self.location_name = u"{}, {}".format(neighborhood, city) self.category_slug = kwargs.get("cat_slug", None) self.category_list = Category.objects.filter(parent_category__isnull=False) return super(Events, self).get(request, *args, **kwargs)
def save(self, *args, **kwargs): if not self.slug or self.name != self.__original_name: unique_slugify(self, self.name) super(Venue, self).save(*args, **kwargs) # This is a bit hacky, need to do this twice because latitude/longitude are plugged in during # call to super's save if self.latitude and self.longitude: changed = False if not self.neighborhood: self.neighborhood = rev_geocode_location_component(self.latitude, self.longitude, 'neighborhood') changed = True if not self.neighborhood: #If not succeed in first try, try again (with a bigger net) self.neighborhood = rev_geocode_location_component(self.latitude, self.longitude, 'political') changed = True if not self.city: self.city = rev_geocode_location_component(self.latitude, self.longitude, 'locality') changed = True if not self.state: self.state = rev_geocode_location_component(self.latitude, self.longitude, 'administrative_area_level_1') changed = True if not self.country: self.country = rev_geocode_location_component(self.latitude, self.longitude, 'country') changed = True if not self.zipcode: self.zipcode = rev_geocode_location_component(self.latitude, self.longitude, 'postal_code') changed = True if self.neighborhood: self.neighborhood = self.fix_neighborhood(self.neighborhood) # If any of the parameters were changed, save the model again if changed: super(Venue, self).save(*args, **kwargs) self.__original_name = self.name