def ajax_save_place(request): """ Creates a SavedPlace for request.POST['pid'] and request.user. """ if request.method != "POST": raise http.Http404() if "pid" not in request.POST: raise http.Http404("Missing pid") if request.user.is_anonymous(): raise http.Http404("Not logged in") place, block_radius, xy_radius = parse_pid(request.POST["pid"]) kwargs = {"user_id": request.user.id} if isinstance(place, Block): block, location = place, None kwargs["block__id"] = place.id else: block, location = None, place kwargs["location__id"] = place.id # Validate that the SavedPlace hasn't already been created for this user, # to avoid duplicates. try: SavedPlace.objects.get(**kwargs) except SavedPlace.DoesNotExist: pass else: return http.HttpResponse("0") # Already exists. savedplace = SavedPlace( user_id=request.user.id, block=block, location=location, nickname=request.POST.get("nickname", "").strip() ) savedplace.full_clean() savedplace.save() return http.HttpResponse("1")
def add_by_place_id(self, pid): """ ``pid`` is a place id string as used by parse_pid and make_pid, identifying a location or block (and if a block, a radius). """ place, block_radius, xy_radius = parse_pid(pid) if isinstance(place, ebpub.streets.models.Block): self["location"] = BlockFilter(self.request, self.context, self.qs, block_radius, block=place) else: self["location"] = LocationFilter(self.request, self.context, self.qs, location=place)
def newsitems_geojson(request): # Copy-pasted code from ajax_place_newsitems. Refactoring target: # Seems like there are a number of similar code blocks in # ebpub.db.views? pid = request.GET.get("pid", "") if pid: place, block_radius, xy_radius = parse_pid(pid) if isinstance(place, Block): search_buffer = make_search_buffer(place.location.centroid, block_radius) newsitem_qs = NewsItem.objects.filter(location__bboverlaps=search_buffer) else: # This depends on the trigger in newsitemlocation.sql newsitem_qs = NewsItem.objects.filter(newsitemlocation__location__id=place.id) else: # Whole city! newsitem_qs = NewsItem.objects.all() # Ordering by schema__id is an optimization for map_popups() newsitem_list = list(newsitem_qs.select_related().order_by("schema__id")) popup_list = map_popups(newsitem_list) features = {"type": "FeatureCollection", "features": []} for newsitem, popup_info in zip(newsitem_list, popup_list): if newsitem.location is None: # Can happen, see NewsItem docstring. # TODO: We should probably allow for newsitems that have a # location_object too? continue features["features"].append( { "type": "Feature", "geometry": { "type": "Point", "coordinates": [newsitem.location.centroid.x, newsitem.location.centroid.y], }, "properties": { "title": newsitem.title, "id": popup_info[0], "popup_html": popup_info[1], "schema": popup_info[2], }, } ) output = simplejson.dumps(features, indent=2) return HttpResponse(output, mimetype="application/javascript")
def add_by_place_id(self, pid): """ ``pid`` is a place id string as used by parse_pid and make_pid, identifying a location or block (and if a block, a radius). """ place, block_radius, xy_radius = parse_pid(pid) if isinstance(place, ebpub.streets.models.Block): self['location'] = BlockFilter(self.request, self.context, self.qs, block_radius, block=place) else: self['location'] = LocationFilter(self.request, self.context, self.qs, location=place)
def ajax_remove_place(request): """ Removes the SavedPlace for request.POST['pid'] and request.user. """ if request.method != "POST": raise http.Http404() if "pid" not in request.POST: raise http.Http404("Missing pid") if request.user.is_anonymous(): raise http.Http404("Not logged in") place, block_radius, xy_radius = parse_pid(request.POST["pid"]) kwargs = {"user_id": request.user.id} if isinstance(place, Block): block, location = place, None kwargs["block__id"] = place.id else: block, location = None, place kwargs["location__id"] = place.id SavedPlace.objects.filter(**kwargs).delete() return http.HttpResponse("1")
def ajax_remove_place(request): """ Removes the SavedPlace for request.POST['pid'] and request.user. """ if request.method != 'POST': raise http.Http404() if 'pid' not in request.POST: raise http.Http404('Missing pid') if request.user.is_anonymous(): raise http.Http404('Not logged in') place, block_radius, xy_radius = parse_pid(request.POST['pid']) kwargs = {'user_id': request.user.id} if isinstance(place, Block): block_center, location = place.geom.centroid, None kwargs['block_center'] = block_center else: block_center, location = None, place kwargs['location__id'] = place.id SavedPlace.objects.filter(**kwargs).delete() return http.HttpResponse('1')
def ajax_save_place(request): """ Creates a SavedPlace for request.POST['pid'] and request.user. """ if request.method != 'POST': raise http.Http404() if 'pid' not in request.POST: raise http.Http404('Missing pid') if request.user.is_anonymous(): raise http.Http404('Not logged in') place, block_radius, xy_radius = parse_pid(request.POST['pid']) kwargs = {'user_id': request.user.id} if isinstance(place, Block): block_center, location = place.geom.centroid, None kwargs['block_center'] = block_center else: block_center, location = None, place kwargs['location__id'] = place.id # Validate that the SavedPlace hasn't already been created for this user, # to avoid duplicates. try: SavedPlace.objects.get(**kwargs) except SavedPlace.DoesNotExist: pass else: return http.HttpResponse('0') # Already exists. savedplace = SavedPlace( user_id=request.user.id, block_center=block_center, location=location, nickname=request.POST.get('nickname', '').strip(), ) savedplace.full_clean() savedplace.save() return http.HttpResponse('1')
def test_parse_pid__block(self): b = self._makeBlock() result = parse_pid("b:%d.1" % b.id) self.assertEqual(result, (b, "1", BLOCK_RADIUS_CHOICES["1"]))
def test_make_and_parse_pid__location(self): loc = self._makeLocation() self.assertEqual(parse_pid(make_pid(loc)), (loc, None, None))
def test_make_and_parse_pid__block(self): block = self._makeBlock() self.assertEqual(parse_pid(make_pid(block, 1)), (block, '1', BLOCK_RADIUS_CHOICES['1']))
def test_parse_pid__location(self): loc = self._makeLocation() result = parse_pid('l:%d' % loc.id) self.assertEqual(result, (loc, None, None))
def test_parse_pid__block(self): b = self._makeBlock() result = parse_pid('b:%d.1' % b.id) self.assertEqual(result, (b, '1', BLOCK_RADIUS_CHOICES['1']))
def test_make_and_parse_pid__block(self): block = self._makeBlock() self.assertEqual(parse_pid(make_pid(block, 1)), (block, '1'))
def test_parse_pid__block(self): b = self._makeBlock() result = parse_pid('b:%d.1' % b.id) self.assertEqual(result, (b, '1'))