示例#1
0
 def pid(self):
     """Place ID as used by ebpub.db.views
     """
     from ebpub.utils.view_utils import make_pid
     if self.block_id:
         return make_pid(self.block, 8)
     else:
         return make_pid(self.location)
示例#2
0
 def pid(self):
     """Place ID as used by ebpub.db.views
     """
     from ebpub.utils.view_utils import make_pid
     if self.block_id:
         return make_pid(self.block, 8)
     else:
         return make_pid(self.location)
示例#3
0
 def pid(self):
     """Place ID as used by ebpub.db.views
     """
     from ebpub.utils.view_utils import make_pid
     if self.block_center:
         block = self._get_block()
         return make_pid(block, 8)
     else:
         return make_pid(self.location)
示例#4
0
文件: utils.py 项目: slinkp/openblock
def get_place_info_for_request(request, *args, **kwargs):
    """
    A utility function that abstracts getting some commonly used
    location-related information: a place (Location or Block), its type,
    a bbox, a list of nearby locations, etc.
    """
    info = dict(
        bbox=None,
        nearby_locations=[],
        location=None,
        place_type=None,
        is_block=False,
        block_radius=None,
        is_saved=False,
        pid='',
        cookies_to_set={},
    )

    if 'place' in kwargs:
        info['place'] = place = kwargs['place']
    else:
        info['place'] = place = url_to_place(*args, **kwargs)

    if isinstance(place, Block):
        info['is_block'] = True
        xy_radius, block_radius, cookies_to_set = block_radius_value(request)
        block_radius = kwargs.get('block_radius') or block_radius
        nearby, search_buf = get_locations_near_place(place, block_radius)
        info['nearby_locations'] = nearby
        info['bbox'] = search_buf.extent
        saved_place_lookup = {'block_center': place.geom.centroid}
        info['block_radius'] = block_radius
        info['cookies_to_set'] = cookies_to_set
        info['pid'] = make_pid(place, block_radius)
        info['place_type'] = 'block'
    else:
        info['location'] = place
        info['place_type'] = place.location_type.slug
        saved_place_lookup = {'location__id': place.id}
        info['pid'] = make_pid(place)
        if place.location is None:
            # No geometry.
            info['bbox'] = get_metro()['extent']
        else:
            nearby, search_buf = get_locations_near_place(place)
            info['bbox'] = search_buf.extent
            info['nearby_locations'] = nearby

    # Determine whether this is a saved place.
    if not request.user.is_anonymous():
        saved_place_lookup[
            'user_id'] = request.user.id  # TODO: request.user.id should not do a DB lookup
        info['is_saved'] = SavedPlace.objects.filter(
            **saved_place_lookup).count()

    return info
示例#5
0
文件: utils.py 项目: mesrut/openblock
def get_place_info_for_request(request, *args, **kwargs):
    """
    A utility function that abstracts getting some commonly used
    location-related information: a place (Location or Block), its type,
    a bbox, a list of nearby locations, etc.
    """
    info = dict(bbox=None,
                nearby_locations=[],
                location=None,
                place_type=None,
                is_block=False,
                block_radius=None,
                is_saved=False,
                pid='',
                cookies_to_set={},
                )

    if 'place' in kwargs:
        info['place'] = place = kwargs['place']
    else:
        info['place'] = place = url_to_place(*args, **kwargs)

    if isinstance(place, Block):
        info['is_block'] = True
        xy_radius, block_radius, cookies_to_set = block_radius_value(request)
        block_radius = kwargs.get('block_radius') or block_radius
        nearby, search_buf = get_locations_near_place(place, block_radius)
        info['nearby_locations'] = nearby
        info['bbox'] = search_buf.extent
        saved_place_lookup = {'block__id': place.id}
        info['block_radius'] = block_radius
        info['cookies_to_set'] = cookies_to_set
        info['pid'] = make_pid(place, block_radius)
        info['place_type'] = 'block'
    else:
        info['location'] = place
        info['place_type'] = place.location_type.slug
        saved_place_lookup = {'location__id': place.id}
        info['pid'] = make_pid(place)
        if place.location is None:
            # No geometry.
            info['bbox'] = get_metro()['extent']
        else:
            nearby, search_buf = get_locations_near_place(place)
            info['bbox'] = search_buf.extent
            info['nearby_locations'] = nearby

    # Determine whether this is a saved place.
    if not request.user.is_anonymous():
        saved_place_lookup['user_id'] = request.user.id # TODO: request.user.id should not do a DB lookup
        info['is_saved'] = SavedPlace.objects.filter(**saved_place_lookup).count()

    return info
示例#6
0
 def test_make_and_parse_pid__location(self):
     loc = self._makeLocation()
     self.assertEqual(parse_pid(make_pid(loc)),
                      (loc, None, None))
示例#7
0
 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']))
示例#8
0
 def test_make_pid__location(self):
     loc = self._makeLocation()
     self.assertEqual(make_pid(loc), 'l:%d' % loc.id)
示例#9
0
 def test_make_pid__block__default_radius(self):
     b = self._makeBlock()
     self.assertEqual(make_pid(b), 'b:%d.8' % b.id)
示例#10
0
 def test_make_pid__block(self):
     b = self._makeBlock()
     self.assertEqual(make_pid(b, 1), 'b:%d.1' % b.id)
示例#11
0
 def test_make_and_parse_pid__block(self):
     block = self._makeBlock()
     self.assertEqual(parse_pid(make_pid(block, 1)),
                      (block, '1'))
示例#12
0
 def test_make_pid__location(self):
     loc = self._makeLocation()
     self.assertEqual(make_pid(loc), 'l:%d' % loc.id)
示例#13
0
 def test_make_pid__block__default_radius(self):
     b = self._makeBlock()
     self.assertEqual(make_pid(b), 'b:%d.8' % b.id)
示例#14
0
 def test_make_pid__block(self):
     b = self._makeBlock()
     self.assertEqual(make_pid(b, 1), 'b:%d.1' % b.id)
示例#15
0
 def test_make_and_parse_pid__location(self):
     loc = self._makeLocation()
     self.assertEqual(parse_pid(make_pid(loc)), (loc, None, None))
示例#16
0
 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']))