Exemplo n.º 1
0
 def explore_in_venues(self, **kwargs):
     params = parameters.PlaceSearchParametersFactory.create(
         kwargs, self.city_service).get_db_params()
     latitude = params.get(params_names.LATITUDE)
     longitude = params.get(params_names.LONGITUDE)
     city_id = params.get(params_names.CITY_ID)
     query = params.get(params_names.QUERY)
     radius = params.get(params_names.RADIUS)
     creator = {
         'email':kwargs[u'creator'],
         'latitude':latitude,
         'longitude':longitude
     }
     #get place_list from 4qk        
     result = venue.search(
         {'latitude': latitude, 'longitude': longitude},
         query,
         radius
     )['venues']
     #filter only in radius
     result_filter_by_radius = filter(
         lambda p: gis_core.distance(
                 creator['longitude'], creator['latitude'],
                 p[u'location'].get('lng'), p[u'location'].get('lat')
             )<=radius,
         result
     )
     #take only not created in db places
     result_filter_by_created = filter(
         lambda p: 
             #place not created in db
             not models.Place.objects.filter(
                 name=p.get(u'name'),
                 position=gis_core.ll_to_point(
                     p[u'location'].get(u'lng'), p[u'location'].get(u'lat')
                 )
             ) 
             #or place created in db but not active
             or models.Place.objects.get(
                 name=p.get(u'name'),
                 position=gis_core.ll_to_point(
                     p[u'location'].get(u'lng'), p[u'location'].get(u'lat')
                 )
             ).is_active is False,                    
         result_filter_by_radius
     )
     place_and_distance_list = []
     for p in result_filter_by_created:
         new_place = dict(
             distance=gis_core.distance(
                 creator['longitude'], creator['latitude'],
                 p[u'location'].get('lng'), p[u'location'].get('lat')
             ),
             azimuth=gis_core.azimuth(longitude, latitude,
                 p[u'location'].get(u'lng'), p[u'location'].get(u'lat')),
             creator=creator,
             place=self.create_place(p,city_id),
         )
         place_and_distance_list.append(new_place)
     return place_and_distance_list
Exemplo n.º 2
0
 def explore_in_venues(self, **kwargs):
     params = parameters.PlaceSearchParametersFactory.create(
         kwargs, self.city_service).get_db_params()
     latitude = params.get(params_names.LATITUDE)
     longitude = params.get(params_names.LONGITUDE)
     city_id = params.get(params_names.CITY_ID)
     query = params.get(params_names.QUERY)
     radius = params.get(params_names.RADIUS)
     creator = {
         'email': kwargs[u'creator'],
         'latitude': latitude,
         'longitude': longitude
     }
     #get place_list from 4qk
     result = venue.search({
         'latitude': latitude,
         'longitude': longitude
     }, query, radius)['venues']
     #filter only in radius
     result_filter_by_radius = filter(
         lambda p: gis_core.distance(creator['longitude'], creator[
             'latitude'], p[u'location'].get('lng'), p[u'location'].get(
                 'lat')) <= radius, result)
     #take only not created in db places
     result_filter_by_created = filter(
         lambda p:
         #place not created in db
         not models.Place.objects.filter(name=p.get(u'name'),
                                         position=gis_core.ll_to_point(
                                             p[u'location'].get(u'lng'), p[
                                                 u'location'].get(u'lat')))
         #or place created in db but not active
         or models.Place.objects.get(
             name=p.get(u'name'),
             position=gis_core.ll_to_point(p[u'location'].get(u'lng'), p[
                 u'location'].get(u'lat'))).is_active is False,
         result_filter_by_radius)
     place_and_distance_list = []
     for p in result_filter_by_created:
         new_place = dict(
             distance=gis_core.distance(creator['longitude'],
                                        creator['latitude'],
                                        p[u'location'].get('lng'),
                                        p[u'location'].get('lat')),
             azimuth=gis_core.azimuth(longitude, latitude,
                                      p[u'location'].get(u'lng'),
                                      p[u'location'].get(u'lat')),
             creator=creator,
             place=self.create_place(p, city_id),
         )
         place_and_distance_list.append(new_place)
     return place_and_distance_list
Exemplo n.º 3
0
def get_place(latitude, longitude, name):
    try:
        return models.Place.objects.get(
            name=name,position = gis_core.ll_to_point(longitude,latitude))
    except ObjectDoesNotExist:
        raise serializers.ValidationError(_("Place with name %s, lng %f,\
         lat %f is not create in sz"%(name,longitude,latitude)))
Exemplo n.º 4
0
 def create_place(self, params, city_id):
     location = params.get(u'location', {})
     params['position'] = gis_core.ll_to_point(
         location.get(u'lng'), params[u'location'].get(u'lat'))
     params['address'] = location.get('address')
     params['crossStreet'] = location.get('crossStreet')
     params['contact'] = location.get('contact')
     if location:
         del params[u'location']
     foursquare_cat = params.get(u'categories')[0] \
         if len(params.get(u'categories',[])) else {}
     if foursquare_cat:
         foursquare_icon = foursquare_cat.get('icon', {})
         params['foursquare_icon_suffix'] = foursquare_icon.get(u'suffix')
         params['foursquare_icon_prefix'] = foursquare_icon.get(u'prefix')
         del params[u'categories']
     params['fsq_id'] = params.get(u'id')
     if params['fsq_id']:
         del params[u'id']
     params['city_id'] = city_id
     p, is_created = models.Place.objects.get_or_create(**params)
     if not is_created:
         p.is_active = True
         p.date_is_active = timezone.now()
         p.save()
     return p
Exemplo n.º 5
0
 def create_place(self,params, city_id):  
     location = params.get(u'location',{})
     params['position'] = gis_core.ll_to_point(
         location.get(u'lng'), params[u'location'].get(u'lat'))
     params['address'] = location.get('address')
     params['crossStreet'] = location.get('crossStreet')
     params['contact'] = location.get('contact')
     if location:
         del params[u'location']
     foursquare_cat = params.get(u'categories')[0] \
         if len(params.get(u'categories',[])) else {}        
     if foursquare_cat:
         foursquare_icon = foursquare_cat.get('icon',{})
         params['foursquare_icon_suffix'] = foursquare_icon.get(u'suffix')
         params['foursquare_icon_prefix'] = foursquare_icon.get(u'prefix')
         del params[u'categories']
     params['fsq_id'] = params.get(u'id')    
     if params['fsq_id']:
         del params[u'id']
     params['city_id'] = city_id
     p, is_created = models.Place.objects.get_or_create(**params)
     if not is_created:
         p.is_active = True
         p.date_is_active = timezone.now()
         p.save()        
     return p
Exemplo n.º 6
0
 def restore_object(self, instance=None):
     try:
         place = models.Place.objects.get(
             name=name,position = gis_core.ll_to_point(longitude,latitude))
     except:
         raise serializers.ValidationError(_("Place with name %s, lng %f,\
                 lat %f is not create in sz"%(name,longitude,latitude)))
     return place
Exemplo n.º 7
0
def create_place(name = "LebowskiStyle", latitude = 50.0, longitude = 127.0):
	place_param = {
			"name":name,
			"position":gis_core.ll_to_point(longitude,latitude),
			"city_id": 1
		}
	places = models.Place.objects
	place = places.filter(**place_param) and places.get(**place_param) \
		or places.create(**place_param)
	return place
Exemplo n.º 8
0
 def setUp(self):
     user = User()
     user.save()
     self.user = user
     place = Place(id='test', name='test', contact='', address='', crossStreet='',
                   position=gis_core.ll_to_point(128, 56), city_id=2026609,
                   foursquare_icon_suffix='', foursquare_icon_prefix='')
     place.save()
     self.place = place
     smile = Smile(emotion='lol')
     smile.save()
     self.smile = smile
Exemplo n.º 9
0
 def _filter_place(self, p, creator, radius):
     distance = gis_core.distance(
         creator['longitude'], creator['latitude'],
         p[u'location'].get('lng'), p[u'location'].get('lat'))
     #filter only in radius
     if distance > radius:
         return False
     position = gis_core.ll_to_point(
         p[u'location'].get(u'lng'), p[u'location'].get(u'lat'))
     place = modelPlace.objects.filter(name=p['name'], position=position)
     #take only not created or not active in db places
     is_create = place[0].is_active if place else False
     return not is_create
Exemplo n.º 10
0
 def validate(self, attrs):
     attrs = super(PlaceSerializer, self).validate(attrs) 
     name = attrs.get('name')
     longitude = attrs.get('longitude')
     latitude = attrs.get('latitude')
     try:
         # place = Place.objects.get(
         attrs['place'] = models.Place.objects.get(
             name=name,position = gis_core.ll_to_point(longitude,latitude))
     except:
         raise serializers.ValidationError(_("Place with name %s, lng %f,\
          lat %f is not create in sz"%(name,longitude,latitude)))
     return attrs
Exemplo n.º 11
0
 def validate(self, attrs):
     attrs = super(PlaceSerializer, self).validate(attrs)
     name = attrs.get('name')
     longitude = attrs.get('longitude')
     latitude = attrs.get('latitude')
     try:
         # place = Place.objects.get(
         attrs['place'] = models.Place.objects.get(
             name=name, position=gis_core.ll_to_point(longitude, latitude))
     except:
         raise serializers.ValidationError(
             _("Place with name %s, lng %f,\
          lat %f is not create in sz" % (name, longitude, latitude)))
     return attrs
Exemplo n.º 12
0
	def setUp(self, name="LebowskiStyle", latitude = 50.0, longitude = 127.0):		
		place_param = {
			"name":name,
			"position":gis_core.ll_to_point(longitude,latitude),
			"city_id": 1
		}
		if not models.Place.objects.filter(name=place_param["name"]):
			self.place = models.Place.objects.create(**place_param)
		else:
			self.place = models.Place.objects.get(
				name=place_param["name"], position = place_param["position"])
		self.place_attrs = {
			"name":self.place.name,
			"latitude":self.place.latitude(),
			"longitude":self.place.longitude()
		}
Exemplo n.º 13
0
 def search_in_venues(self, **kwargs):
     params = parameters.PlaceSearchParametersFactory.create(kwargs, self.city_service).get_db_params()
     latitude = params.get(params_names.LATITUDE)
     longitude = params.get(params_names.LONGITUDE)
     city_id = params.get(params_names.CITY_ID)
     query = params.get(params_names.QUERY)
     radius = params.get(params_names.RADIUS)
     result = venue.search({"latitude": latitude, "longitude": longitude}, query, radius)
     place_and_distance_list = map(
         lambda l: {
             "place": models.Place(
                 id=l[u"id"],
                 name=l[u"name"],
                 contact=l.get(u"contact"),
                 address=l[u"location"].get(u"address") and (u"%s" % l[u"location"].get(u"address")) or None,
                 crossStreet=l[u"location"].get(u"crossStreet")
                 and (u"%s" % l[u"location"].get(u"crossStreet"))
                 or None,
                 position=gis_core.ll_to_point(l[u"location"].get(u"lng"), l[u"location"].get(u"lat")),
                 city_id=None,
                 foursquare_icon_suffix=utils.safe_get(l, lambda el: el[u"categories"][0][u"icon"][u"suffix"]),
                 foursquare_icon_prefix=utils.safe_get(l, lambda el: el[u"categories"][0][u"icon"][u"prefix"]),
             ),
             "distance": l[u"location"].get(u"distance"),
             "azimuth": gis_core.azimuth(
                 longitude, latitude, l[u"location"].get(u"lng"), l[u"location"].get(u"lat")
             ),
         },
         result["venues"],
     )
     if len(place_and_distance_list) > 0:
         caching_manager = ModelCachingManager(
             [item["place"] for item in place_and_distance_list],
             lambda e: e.date,
             datetime.timedelta(seconds=60 * 60 * 24 * 3),
         )
         if len(caching_manager.for_insert) > 0:
             for e in caching_manager.for_insert:
                 e.city_id = city_id
         if len(caching_manager.cached) > 0:
             for e in caching_manager.cached:
                 stored_city = lists.first_match(lambda x: x.id == e.id, caching_manager.stored)
                 e.city_id = stored_city.city_id
         caching_manager.save()
     return place_and_distance_list
Exemplo n.º 14
0
 def create_place(self, params, city_id, creator, radius):
     distance = gis_core.distance(
         creator['longitude'], creator['latitude'],
         params[u'location'].get('lng'), params[u'location'].get('lat'))
     #filter only in radius
     if distance > radius:
         return False
     location = params.get(u'location', {})
     position = gis_core.ll_to_point(
         location.get(u'lng'), params[u'location'].get(u'lat'))
     place_params = dict(
         address=location.get('address'),
         crossStreet=location.get('crossStreet'),
         contact=location.get('contact'),
         fsq_id=params.get('id'),
         city_id=city_id,
         position=position,
         name=params.get('name')
     )
     foursquare_cat = params.get(u'categories')[0] \
         if len(params.get(u'categories', [])) else {}
     if foursquare_cat:
         foursquare_icon = foursquare_cat.get('icon', {})
         place_params['foursquare_icon_suffix'] = foursquare_icon.get(
             u'suffix')
         place_params['foursquare_icon_prefix'] = foursquare_icon.get(
             u'prefix')
     '''
     Зачем здесь экспешн ?
     Да потому что из-за уникальной географической позиции в
     городе тестирования city_id определяется поразному в разных
     точках города и с таким ид, name, position  место не находится,
     но и создавать не даст
     '''
     try:
         p, is_create = modelPlace.objects.get_or_create(**place_params)
     except IntegrityError:
         p = modelPlace.objects.filter(
             position=position, name=params.get('name'))
         p = p[0] if p else None
     return p if (p and not p.is_active) else False
Exemplo n.º 15
0
def get_params(p):
    return dict(name=p['name'], city_id=0,
        position=gis_core.ll_to_point(p['location']['lng'],p['location']['lat']))
Exemplo n.º 16
0
def get_params(p):
    return dict(name=p['name'],
                city_id=0,
                position=gis_core.ll_to_point(p['location']['lng'],
                                              p['location']['lat']))
Exemplo n.º 17
0
Arquivo: tests.py Projeto: sz-2013/sz
GENDERS = ['u', 'm', 'f']
RACES = ['futuri', 'amadeus', 'united']
FACES = [get_file('faces/3.png')]
EMAIL = "*****@*****.**"
PSWD = '123'
PHOTO = dict(file='photos/1.jpg', photo_height=1024, photo_width=768)
LATITUDE = 40.7755555
LONGITUDE = -73.9747221
RADIUS = 250

PLACE_NAME = 'CoolPlace'
PLACE_LAT = 40.76022
PLACE_LON = -73.98439
CITY_ID = 1
POSITION = gis_core.ll_to_point(PLACE_LON, PLACE_LAT)

api_root = '/api/'
API = {
    'user': {
        'create': api_root + 'users/register/',
    },
    'place': {
        'explore': api_root + 'places/explore-in-venues/',
        'search': api_root + 'search-in-venues',
    },
    'message': {
        'photopreview': api_root + 'messages/add/photopreviews/',
        'create': api_root + 'messages/add/'
    },
    'gamemap': {