def testGetFeaturesFromPlacesLayer(self): self.AssertGetFeaturesFromPlacesLayer(GOOGLE_PLACES_SEARCH_JSON_STR, PLACES_FEATURES) # Try the same request again and make sure the result comes from cache # (i.e. there are no calls to the urlfetch) self.mox.StubOutWithMock(urlfetch, 'fetch') self.mox.ReplayAll() self.assertEquals( PLACES_FEATURES, card.GetFeaturesFromPlacesLayer( MAP_ROOT.get('layers')[3], ndb.GeoPt(20, 50)))
def get(self): user = users.get_current_user() my_photo = Photo(image='\x00\x00\x00', created=datetime.datetime.now(), owner=user, location=ndb.GeoPt('52.37, 4.88'), views=0, caption='This is a photo.', camera_type='Pixel') key = my_photo.put() self.response.headers['Content-Type'] = 'text/plain' self.response.write(key.id())
def form_to_dao_locality(self, **datas): try: locality = Locality() locality.name = datas['name'] locality.ref_id = datas['ref_id'] locality.place_id = datas['place_id'] locality.address = datas['address'] locality.latlong = ndb.GeoPt(datas['lat'], datas['long']) except StandardError as e: logger.error('Error occured for %s:%s' % (str(e), datas['name'])) raise return locality
def post(self): venue = Venue() venue.title = self.request.get('title') venue.description = self.request.get('description') venue.phones = self.request.get('phones').split(',') venue.emails = self.request.get('emails').split(',') venue.coordinates = ndb.GeoPt(float(self.request.get('lat')), float(self.request.get('lon'))) venue.legal = LegalInfo.get_by_id(self.request.get_range('legal')).key venue.update_address() venue.put() self.redirect('/company/venues')
def create_location(pl_key, triangle): """ Creates a triangle database object. :param pl_key: key object of the parent location :param triangle: Triangle object :return: key of the database object """ triangle_location = TriangleLocation( private_location_fk=pl_key, max_latitude=triangle.get_max_lat(), min_latitude=triangle.get_min_lat(), max_longitude=triangle.get_max_lng(), min_longitude=triangle.get_min_lng(), point_a=ndb.GeoPt(triangle.point_a.get_lat(), triangle.point_a.get_lng()), point_b=ndb.GeoPt(triangle.point_b.get_lat(), triangle.point_b.get_lng()), point_c=ndb.GeoPt(triangle.point_c.get_lat(), triangle.point_c.get_lng()), triangle=triangle) return triangle_location.put()
def __init__(self, vessel): GoogleMap.__init__(self) self.wpts = datamodel.Waypoint.query(ancestor=vessel.key).order( datamodel.Waypoint.report_date, datamodel.Waypoint.received_date).fetch(configdata.MAX_WAYPOINTS) if self.wpts: self.current_wpt = self.wpts.pop() self.centre = self.current_wpt.position else: self.current_wpt = None self.zoom = self.WORLD_ZOOM self.centre = ndb.GeoPt(0, 0)
def post_place(self, request): store = PlaceStore( name = request.name, location = ndb.GeoPt(request.latitude, request.longitude), #latitude = request.latitude, #longitude = request.longitude, address = request.address, phone = request.phone ) key = store.put() return store.toMessage()
def _create_test_data(self): instance = SearchTestModel(string='123', repeated_string=['1', '2', '3'], datetime=datetime.datetime.utcnow(), person=users.User(email='*****@*****.**'), date=datetime.date.today(), time=datetime.time(4), ref=ndb.Key('Test', '123'), geopt=ndb.GeoPt(1, 2), repeated_number=[1, 2, 3]) instance.put() return instance
def copy_from_message(self, src): # копирует свои элементы из объект Message for attr in House.attrs_for_message: if hasattr(src, attr): v = getattr(src, attr) if v is not None: setattr(self, attr, v) # нестрандартные поля if src.gps is not None and len(src.gps) == 2: pt = ndb.GeoPt(src.gps[0], src.gps[1]) self.gps = pt
class Firms(ndb.Model): admin = ndb.KeyProperty(kind='User') admin_id = ndb.IntegerProperty() members = ndb.StringProperty(repeated=True) picture = ndb.StringProperty() link = ndb.StringProperty() desc = ndb.StringProperty() logo = ndb.BlobKeyProperty() title = ndb.StringProperty() pos = ndb.GeoPtProperty(default=ndb.GeoPt(28.635308, 77.224960)) privacy = ndb.IntegerProperty(default=0) date = ndb.DateTimeProperty(auto_now_add=True)
def get_roads(self): url = self.base_url + '[highway=*]' logging.info(url) root = self.get_data(url) #empty node dict nodes = {} roads = [] for child in root: if child.tag == 'node': #nodes should be at the top geo = ndb.GeoPt(child.attrib['lat'], child.attrib['lon']) node = classes.Node(geo_point=geo) nodes.update({child.attrib["id"]: node}) elif child.tag == 'way': way_nodes = [] subtype = "" subname = "" for way_child in child: # logging.info(child.tag) if way_child.tag == 'nd': #save node rederence in order way_nodes.append( copy.copy(nodes[way_child.attrib['ref']])) # logging.info(nodes[child.attrib['ref']]) elif way_child.attrib['k'] == 'highway': subtype = way_child.attrib['v'] logging.info(subtype) elif way_child.attrib['k'] == 'name': subname = way_child.attrib['v'] logging.info(subname) #grab the required nodes and create the entity for idx, way_node in enumerate(way_nodes): way_node.idx = idx #create the road road = classes.Road(nodes=way_nodes, subtype=subtype, subname=subname, parent=self.ghash_entity.key, id=child.attrib["id"]) #push the road onto the array roads.append(road) #store the array in the db ndb.put_multi(roads) #return the count return len(roads)
def post(self): #checking if user is logged in or not. If not, request is not served and # instead redirected to login page, then req sent again. # Generally this shouldnt be the case since the guy was asked to # sign in when visiting the complaint page. # user = users.get_current_user(); # if not user: # self.redirect(users.create_login_url(self.request.uri)) # i don't think we need to user_id from the frontend anymore. google_id = self.request.get('user_id') # this is google id # checking if user already exists q = ndb.gql('SELECT * FROM User WHERE google_id = :1', google_id) user = q.get() # returns first result or None if user is None: #need to create new user name = self.request.get('name') email = self.request.get('email') pic = self.request.get('pic') newuser = models.User(id=google_id, name=name, email=email, google_id=google_id, pic=pic) newuser.put() userKey = newuser.key print ">>>>> New user created." else: userKey = user.key print ">>>>> Old user." title = self.request.get('title') lat = float(self.request.get('lat')) lon = float(self.request.get('lon')) subtitle = self.request.get('subtitle') content = self.request.get('content') tags = json.loads(self.request.get('tags')) img_links = json.loads(self.request.get('img_links')) smallAdd = self.request.get('smallAdd') bigAdd = self.request.get('bigAdd') #### Added New location = ndb.GeoPt(lat=lat, lon=lon) newcomplaint = models.Complaint(user_id=userKey, title=title, location=location, subtitle=subtitle, content=content, tags=tags, img_links=img_links, bigAdd=bigAdd, smallAdd=smallAdd) newcomplaint.put()
def form_to_dao_address(form, entity): address = form.address.data if address is not None: entity.address = Address() entity.address.line1 = address['line1'].lower() entity.address.line2 = address['line2'].lower() entity.address.locality = address['locality'].lower() entity.address.city = address['city'].lower() entity.address.pin = address['pin'] latitude, longitude = get_latlong_from_address(entity.address) if latitude is not None and longitude is not None: entity.address.latlong = ndb.GeoPt(latitude, longitude) return entity
def get_coords(ip): url = IP_URL + ip content = None try: content = urllib2.urlopen(url).read() except URLError: return if content: d = minidom.parseString(content) coords = d.getElementsByTagName("gml:coordinates") if coords and coords[0].childNodes[0].nodeValue: lon, lat = coords[0].childNodes[0].nodeValue.split(',') return ndb.GeoPt(lat, lon)
def testGetFeatures(self): # Try getting features for a topic with two layers. self.SetForTest(kmlify, 'FetchData', lambda url, host: 'data from ' + url) self.SetForTest( card, 'GetFeaturesFromXml', lambda data, layer: ['parsed ' + data + ' for ' + layer]) self.assertEquals([ 'parsed data from http://example.com/one.kml for layer1', 'parsed data from http://example.com/three.kml for layer3' ], card.GetFeatures(MAP_ROOT, 'm1', 't1', self.request, ndb.GeoPt(20, 50)))
def _pre_put_hook(self): if 'preferences' not in self.statuses: if self.address and self.moods and self.interests: self.statuses.append('preferences') if 'vote' not in self.statuses: if self.vote: self.statuses.append('vote') if self.address and not self.location: response = GeoCoding.get_location(address=self.address) location = response['results'][0]['geometry']['location'] lat = location['lat'] lng = location['lng'] self.location = ndb.GeoPt(lat=lat, lon=lng) # lat, lon
def testGetFeaturesWithFailedFetches(self): # Even if some fetches fail, we should get features from the others. def FetchButSometimesFail(url, unused_host): if 'one.kml' in url: raise urlfetch.DownloadError return 'data from ' + url self.SetForTest(kmlify, 'FetchData', FetchButSometimesFail) self.SetForTest(card, 'GetFeaturesFromXml', lambda data, layer: ['parsed ' + data]) self.assertEquals(['parsed data from http://example.com/three.kml'], card.GetFeatures(MAP_ROOT, 'm1', 't1', self.request, ndb.GeoPt(20, 50)))
def get(self): user = users.get_current_user() my_photo = Photo(image='dajslkdjaslkd', created=datetime.datetime.now(), owner=user, location=ndb.GeoPt("52.37, 4.88"), views=0, caption="This is a photo.", camera_type="Pixel", people_in_photo=[]) key = my_photo.put() self.response.headers['Content-Type'] = 'text/plain' self.response.write(key.id())
def createGame(cls, name, description, private, lat, lon): game = Game() game.name = name game.description = description game.private = private game.center = ndb.GeoPt(lat, lon) gameController = BasicGameController() gameController.put() game.gameControllerId = gameController.key.id() game.put() return game
def test_to_json_dict(self): entity = TestEntity(test_string='Hello', test_geopt=ndb.GeoPt(50, 100)) entity.test_structuredprop = TestSubEntity(test_substring='foo') entity.put() entity.test_keyproperty = entity.key entity.put() entity_dict = entity.to_json_dict() self.assertIsInstance(entity_dict.get('test_datetime'), int) self.assertIsInstance( entity_dict['test_structuredprop']['test_subdatetime'], int) self.assertIsInstance(entity_dict['test_repeatedprop'], list)
def _value_to_property(self, value, prop): """Converts raw data value into an appropriate NDB property""" if isinstance(prop, ndb.KeyProperty): if value is None: return None try: return ndb.Key(urlsafe=value) except ProtocolBufferDecodeError as e: if prop._kind is not None: model_class = ndb.Model._kind_map.get(prop._kind) if getattr(model_class, 'RESTMeta', None) and getattr( model_class.RESTMeta, 'use_input_id', False): return ndb.Key(model_class, value) raise RESTException('invalid key: {}'.format(value)) elif isinstance(prop, ndb.TimeProperty): if dateutil is None: try: return datetime.strptime(value, "%H:%M:%S").time() except ValueError as e: raise RESTException( "Invalid time. Must be in ISO 8601 format.") else: return dateutil.parser.parse(value).time() elif isinstance(prop, ndb.DateProperty): if dateutil is None: try: return datetime.strptime(value, "%Y-%m-%d").date() except ValueError as e: raise RESTException( "Invalid date. Must be in ISO 8601 format.") else: return dateutil.parser.parse(value).date() elif isinstance(prop, ndb.DateTimeProperty): if dateutil is None: try: return datetime.strptime(value, "%Y-%m-%dT%H:%M:%S") except ValueError as e: raise RESTException( "Invalid datetime. Must be in ISO 8601 format.") else: return dateutil.parser.parse(value) elif isinstance(prop, ndb.GeoPtProperty): # Convert from string (formatted as '52.37, 4.88') to GeoPt return ndb.GeoPt(value) elif isinstance(prop, ndb.StructuredProperty): # It's a structured property - the input data is a dict - # recursively parse it as well return self._build_model_from_data(value, prop._modelclass) else: # Return as-is (no need for further manipulation) return value
def update_challenge(short_name, args_obj): # Get the contract to be updateds contract = Contract.query(Contract.short_name == short_name).get() # Update all fields represented in args_obj #contract.challenge_name = args_obj["description"] contract.objective_type = str(args_obj["objective"]) contract.time_unit = str(args_obj["unit"]) contract.time_period = int(args_obj["length"]) contract.start_date = datetime.date(int(args_obj["year"]), int(args_obj["month"]), int(args_obj["day"])) if not contract.stakes_id: curr_stakes = [0, 0, 0, 0] else: curr_stakes = contract.stakes_id stakes_ids = enter_stakes(args_obj["stakes"], curr_stakes) contract.stakes_id = stakes_ids contract.put() # Enter in Objective models if contract.objective_type == "highest-occurrence": # Get current highest GeneralObjective id top_objective = GeneralObjective.query().order( -GeneralObjective.gen_objective_id).get() if top_objective: new_id = top_objective.gen_objective_id + 1 else: new_id = 1 # Make new GeneralObjective model GeneralObjective(objective_name=str(args_obj["objective-name"]), contract_id=contract.contract_id, gen_objective_id=new_id).put() elif contract.objective_type == "location": # Get current highest GeolocationObjective id top_objective = GeolocationObjective.query().order( -GeolocationObjective.geo_objective_id).get() if top_objective: new_id = top_objective.geo_objective_id + 1 else: new_id = 1 # Make new GeolocationObjective model GeolocationObjective(checkin_loc=ndb.GeoPt(args_obj["checkin-loc"]), checkin_radius=int(args_obj["radius"]), loc_name=args_obj["checkin-loc-name"], geo_objective_id=new_id, contract_id=contract.contract_id).put()
def post(self, company_id, terminal_id): dt = DeliveryTerminal.get_by_id(terminal_id) assert dt.company_id == int(company_id) dt.name = self.request.get('name') dt.address = self.request.get('address') dt.location = ndb.GeoPt(self.request.get('location')) dt.phone = self.request.get('phone') dt.active = bool(self.request.get('active')) dt.schedule = ast.literal_eval(self.request.get('schedule')) dt.alpha_login = self.request.get('alpha_login') dt.alpha_pass = self.request.get('alpha_pass') dt.platius_payment_code = self.request.get('platius_payment_code') dt.put() self.redirect('/mt/company/%s/terminals' % company_id)
def update_event_location(cls, event): if not event.location: return if event.normalized_location: # Only set normalized_location once return location_info, score = cls.get_event_location_info(event) # Log performance text = "Event {} location score: {}".format(event.key.id(), score) if score < 0.8: logging.warning(text) else: logging.info(text) # Fallback to location only if not location_info: logging.warning( "Falling back to location only for event {}".format( event.key.id())) geocode_result = cls.google_maps_geocode_async( event.location).get_result() if geocode_result: location_info = cls.construct_location_info_async( geocode_result[0]).get_result() else: logging.warning("Event {} location failed!".format( event.key.id())) # Update event if 'lat' in location_info and 'lng' in location_info: lat_lng = ndb.GeoPt(location_info['lat'], location_info['lng']) else: lat_lng = None event.normalized_location = Location( name=location_info.get('name'), formatted_address=location_info.get('formatted_address'), lat_lng=lat_lng, street_number=location_info.get('street_number'), street=location_info.get('street'), city=location_info.get('city'), state_prov=location_info.get('state_prov'), state_prov_short=location_info.get('state_prov_short'), country=location_info.get('country'), country_short=location_info.get('country_short'), postal_code=location_info.get('postal_code'), place_id=location_info.get('place_id'), place_details=location_info.get('place_details'), )
def _load_delivery_terminals(company): iiko_delivery_terminals = get_delivery_terminals(company) dts = map(lambda iiko_delivery_terminal: DeliveryTerminal.get_or_insert( iiko_delivery_terminal['deliveryTerminalId'], company_id=company.key.id(), iiko_organization_id=company.iiko_org_id, active=True, name=iiko_delivery_terminal['deliveryRestaurantName'], phone=company.phone, address=iiko_delivery_terminal['address'], location=ndb.GeoPt(company.latitude, company.longitude) ), iiko_delivery_terminals) ndb.put_multi(dts) return dts
def post(self): logging.info("GeocodeTaskHandler :: Geocoding {0}".format(self.request.get('vendor_key'))) vendor_key = ndb.Key(urlsafe=self.request.get('vendor_key')) logging.info("GeocodeTaskHandler :: vendor_key = {0}".format(repr(vendor_key))) vendor = vendor_key.get() logging.info("GeocodeTaskHandler :: vendor = {0}".format(repr(vendor))) results = Geocoder.geocode(vendor.address_for_geocoding) (lat, lng) = results[0].coordinates vendor.new_geo_point = ndb.GeoPt(lat, lng) vendor.put() self.respond_ok()
def add_random_bookmark(url): bm = Bookmark() bm.url = url.key bm.user = '******' bm.title = "Title - {0}".format(random.randint(100, 999)) bm.object_type = random.randint(1, 3) bm.price = random.randint(100, 7999) * 1000 bm.gps = ndb.GeoPt(51.0 + random.randint(1,100) / 50.0, 37.0 + random.randint(1,100) / 50.0) bm.put() add_random_bookmark_tag(bm, 101) add_random_bookmark_tag(bm, 102) add_random_bookmark_tag(bm, 201) add_random_bookmark_tag(bm, 202) add_random_bookmark_tag(bm, random.randint(1, 5))
def edit_venue(venue, data, operator=None): """ Edit an event """ # Operator prep operator_key = None if operator: operator_key = operator.key venue_key = venue.key # Prep go data... if data.get('geo', None): if isinstance(data['geo'], list): data['geo'] = [ndb.GeoPt(lat=float(pt.lat), lon=float(pt.lon)) for pt in data['geo']] else: if isinstance(data['geo'], ndb.GeoPt): # Lazy... lat = data['geo'].lat lon = data['geo'].lon elif isinstance(data['geo'], dict): # Maybe this isn't the best solution to be flexible? lat = data['geo']['lat'] lon = data['geo']['lon'] else: geo_data = data['geo'].split(',') lat = geo_data[0].strip() lon = geo_data[1].strip() data['geo'] = ndb.GeoPt(lat=float(lat), lon=float(lon)) else: data['geo'] = None return _edit_venue_txn(venue_key, data, operator_key)
def _get_location(self, img_fh): # If the location has been specified, use that: lat = self.request.get('lat') lng = self.request.get('lng') # If it hasn't, but there's something in the search field, try that: if lat is None or lng is None or lat == '' or lng == '': geo_search_term = self.request.get('searchfield') geo_url = 'http://maps.googleapis.com/maps/api/geocode/' url = geo_url + 'json?address=' + geo_search_term geo_fh = urllib.urlopen(url) geo_json = json.load(geo_fh) if geo_json['results']: loc_json = geo_json['results'][0]['geometry']['location'] lat = loc_json['lat'] lng = loc_json['lng'] # If that doesn't work, try to get the location from the image's EXIF # info: try: location = ndb.GeoPt(lat, lng) except BadValueError as bve: logging.error(bve) try: lat, lng = self._get_latlng_exif(img_fh) location = ndb.GeoPt(lat, lng) except Exception as err2: logging.error(err2) err = SubmitError( "The plaque location wasn't specified. Please click the " "back button, select a location, and click 'Add your " "Plaque' again. Error (%s)" % err2) raise err return location
def post(self): # paramters, assume failure, response type params = {} params['response'] = "error" self.response.headers['Content-Type'] = "application/json" # get appliance variables try: packet = json.loads(self.request.body) apitoken = packet['appliance']['apitoken'] except: params['message'] = "You must submit a valid JSON object with a token." self.response.set_status(401) return self.render_template('api/response.json', **params) # load the appliance appliance = Appliance.get_by_token(apitoken) if not appliance: params['message'] = "Token is not valid." self.response.set_status(401) return self.render_template('api/response.json', **params) if appliance.activated == False: # appliance not activated params['message'] = "Appliance has been disabled by pool controller. Please contact support." self.response.set_status(409) return self.render_template('api/response.json', **params) # update appliance info latitude = float(packet['appliance']['location']['latitude']) longitude = float(packet['appliance']['location']['longitude']) appliance.location = ndb.GeoPt(latitude, longitude) appliance.dynamicimages = bool(packet['appliance']['dynamicimages']) appliance.put() # loop through instances being advertised for sale for appliance_instance in packet['instances']: # pass in appliance_instance and appliance object #logging.info("instance: %s" % appliance_instance['name']) instance = Instance.push(appliance_instance, appliance) # build parameter list params = {} params['response'] = "success" params['message'] = "Instances accepted for sale." self.response.headers['Content-Type'] = 'application/json' return self.render_template('api/response.json', **params)