class EditUserLocalityHandler(webapp2.RequestHandler): importDao = DaoFactory.create_rw_importDao() def post(self): logging.info(self.request.body) id = self.request.get("pk") user = User.get_by_id(long(id)) user.locality = utils.stringify(self.request.get("value")) locality_exist = self.importDao.query_by_place_name(user.locality.title()) if locality_exist: user.locality_id = locality_exist.place_id logger.info('Id: %s Value: %s' % (id, user.locality)) user.put() logger.debug('User Data: ' + str(user)) #return json.dumps(result) #or, as it is an empty json, you can simply use return "{}" return
class PlaygroundSearchHandler(BaseHandler): playgroundDao = DaoFactory.create_rw_playgroundDao() mediaDao = DaoFactory.create_ro_mediaDao() importDao = DaoFactory.create_rw_importDao() def get(self, city_name=None, locality_name=None): return self.post(city_name, locality_name) def post(self, city_name=None, locality_name=None): city_str = str(self.request.get('city-name')) locality_nav = str(self.request.get('nav-locality')) locality_home = str(self.request.get('pg-locality')) locality_str = locality_nav if locality_nav != '' else locality_home locality_id_nav = str(self.request.get('nav-locality_id')) locality_id_home = str(self.request.get('pg-locality_id')) locality_id_str = locality_id_nav if locality_id_nav != '' else locality_id_home name = str(self.request.get('name')) sport = str(self.request.get('sport')) pg_sport = str(self.request.get('pg-sport')) sport = pg_sport if pg_sport != '' and pg_sport != 'None' else sport city = city_name if city_name is not None else city_str locality = locality_name if locality_name is not None else locality_str logger.debug('playground search :: city %s, sport %s, locality %s' % (city, sport, locality)) nav_type_str = str(self.request.get('nav')) nav_type = nav_type_str if nav_type_str != '' and nav_type_str != 'None' else None curs_str = str(self.request.get('page')) curs = curs_str if curs_str != '' and curs_str != 'None' else None remote_ip = self.request.remote_addr params = {} search_params = {} playgrounds = [] playground_list = [] playground_media = [] if name != '' and name != 'None': search_params['name'] = name if sport != '' and sport != 'None': search_params['sport'] = sport if city != '' and city != 'None': params['city_name'] = city search_params['address.city'] = city if locality != '' and locality != 'None' and locality != 'all': search_params['address.locality'] = locality #logger.debug('Search Params : ' + str(search_params)) page_id = 1 if curs is not None: page_id = int(curs) else: playground_search = self.playgroundDao.search_index( remote_ip, 'approved', **search_params) playground_keys = self.playgroundDao.get_search_keys( constants.PLAYGROUND + '_' + str(remote_ip)) total_entries = len(playground_keys) logger.debug('NO of playgrounds matched the search %s ' % total_entries) if total_entries > 0: total_pages = (total_entries / PAGE_SIZE) + 1 params['page_range'] = range(1, total_pages + 1) offset = (page_id * PAGE_SIZE) - PAGE_SIZE limit = offset + PAGE_SIZE playground_list = playground_keys[offset:limit] playgrounds = ndb.get_multi(playground_list) playground_media = self.mediaDao.get_primary_media( playgrounds, constants.PLAYGROUND) logger.debug('Displayed Page No.%s Playgrounds from %s to %s' % (page_id, offset + 1, limit)) suggest_params = {} if len(playground_keys) < PAGE_SIZE and (locality_id_str != '' or locality != '' or curs is not None): suggest_playground_keys = [] if curs is None: if locality_id_str != '': locality_data = self.importDao.query_by_place_id( locality_id_str) elif locality != '': locality_data = self.importDao.query_by_place_name( locality.title()) if locality_data: suggest_params['latlong'] = locality_data.latlong suggest_params['address.locality'] = locality_data.name suggest_playground_keys = self.playgroundDao.search_index_suggest( remote_ip, 'approved', **suggest_params) else: suggest_playground_keys = self.playgroundDao.get_suggest_keys( 'suggest_' + str(constants.PLAYGROUND) + '_' + str(remote_ip)) if suggest_playground_keys is not None: suggest_playgrounds = ndb.get_multi(suggest_playground_keys) params['suggest_playgrounds'] = suggest_playgrounds logger.debug('No of Suggested Search Result : %s' % len(suggest_playground_keys)) #logger.debug('Suggested Search Result keys : ' + str(suggest_playground_keys)) suggest_playground_media = self.mediaDao.get_primary_media( suggest_playgrounds, constants.PLAYGROUND) #logger.debug('Suggest Media: ' + str(suggest_playground_media)) if len(suggest_playground_media) > 0: if len(playground_media) > 0: playground_media.update(suggest_playground_media) else: playground_media = suggest_playground_media params['types'] = constants.PLAYGROUND params['sport'] = sport params['locality_name'] = locality params['playgrounds'] = playgrounds params['playground_media'] = playground_media params['title'] = constants.PLAYGROUND logger.debug('Param Results: ' + str(params)) return self.render_template('/app/search_results.html', **params)
class BaseHandler(webapp2.RequestHandler): importDao = DaoFactory.create_rw_importDao() profileDao = DaoFactory.create_rw_profileDao() """ BaseHandler for all requests Holds the auth and session properties so they are reachable for all requests """ def __init__(self, request, response): """ Override the initialiser in order to set the language. """ self.initialize(request, response) self.locale = i18n.set_locale(self, request) self.view = ViewClass() def dispatch(self): """ Get a session store for this request. """ self.session_store = sessions.get_store(request=self.request) try: # csrf protection if self.request.method == "POST" and not self.request.path.startswith( '/taskqueue'): token = self.session.get('_csrf_token') if not token or token != self.request.get('_csrf_token'): self.abort(403) # Dispatch the request. webapp2.RequestHandler.dispatch(self) finally: # Save all sessions. self.session_store.save_sessions(self.response) @webapp2.cached_property def user_model(self): """Returns the implementation of the user model. Keep consistency when config['webapp2_extras.auth']['user_model'] is set. """ return self.auth.store.user_model @webapp2.cached_property def auth(self): return auth.get_auth() @webapp2.cached_property def session_store(self): return sessions.get_store(request=self.request) @webapp2.cached_property def session(self): # Returns a session using the default cookie key. return self.session_store.get_session() @webapp2.cached_property def messages(self): return self.session.get_flashes(key='_messages') def add_message(self, message, level=None): self.session.add_flash(message, level, key='_messages') @webapp2.cached_property def auth_config(self): """ Dict to hold urls for login/logout """ return { 'login_url': self.uri_for('login'), 'logout_url': self.uri_for('logout') } @webapp2.cached_property def user(self): return self.auth.get_user_by_session() @webapp2.cached_property def user_id(self): return str(self.user['user_id']) if self.user else None @webapp2.cached_property def user_info(self): if self.user: return self.user_model.get_by_id(long(self.user_id)) return None @webapp2.cached_property def user_type(self): if self.user: user_info = self.user_model.get_by_id(long(self.user_id)) return user_info.auth_ids[0].split(':') return None @webapp2.cached_property def user_key(self): if self.user: user_info = self.user_model.get_by_id(long(self.user_id)) return user_info.key return None @webapp2.cached_property def email(self): if self.user: try: user_info = self.user_model.get_by_id(long(self.user_id)) return user_info.email except AttributeError, e: # avoid AttributeError when the session was delete from the server logging.error(e) self.auth.unset_session() self.redirect_to('home') return None
class SaveLocalityHandler(blobstore_handlers.BlobstoreUploadHandler, BaseHandler): importDao = DaoFactory.create_rw_importDao() @user_required def get(self): params = {} upload_url = self.uri_for('save-locality') params['title'] = 'Save Locality' params['media_upload_url'] = blobstore.create_upload_url(upload_url) return self.render_template('/cms/save_locality.html', **params) @user_required def post(self): params = {} locality_count = 0 if not self.form.validate(): return self.get() lat = self.request.get('lat') long = self.request.get('long') radius = self.request.get('radius') limit = self.request.get('limit') api_key = self.request.get('key') feed_url = 'http://www.geoplugin.net/extras/nearby.gp?lat=' + lat + '&long=' + long + '&limit=' + limit + '&radius=' + radius + '&format=xml' logging.info('feed url %s' % feed_url) root = self.parse(feed_url) for item in root.getElementsByTagName('geoPlugin_nearby'): try: place_name = unicodedata.normalize( 'NFKD', unicode( item.getElementsByTagName('geoplugin_place') [0].firstChild.data)).encode('ascii', 'ignore') logger.info('Place: %s' % place_name) newfeed_url = 'https://maps.googleapis.com/maps/api/place/autocomplete/xml?types=(regions)&input=' + urllib.quote( place_name) + '&key=' + api_key logging.info('newfeed url %s' % newfeed_url) newroot = self.parse(newfeed_url) auto_status = newroot.getElementsByTagName( 'status')[0].firstChild.data logger.info('Auto Status: %s ' % auto_status) if auto_status == 'OK': for items in newroot.getElementsByTagName('prediction'): try: place_id = items.getElementsByTagName( 'place_id')[0].firstChild.data place_name = items.getElementsByTagName( 'value')[0].firstChild.data #description logger.info('Place Name: %s Place Id: %s ' % (place_name, place_id)) locality_count = self.process_locality( place_name, place_id, api_key) except IndexError, ValueError: pass else: logger.error('Error: %s' % auto_status) except IndexError, ValueError: pass