def get(self): try: namespace_manager.set_namespace(self.request.get('namespace')) blocks_data_version_client = \ self.request.get('blocksDataVersion') camera_data_version_client = \ self.request.get('cameraDataVersion') block_properties_data_version_client = \ self.request.get('blockPropertiesDataVersion') new_block_data_version_client = \ self.request.get('newBlockDataVersion') prerender_mode_data_version_client = \ self.request.get('prerenderModeDataVersion') callback = self.request.get('callback') data = db.run_in_transaction \ (self.transaction, blocks_data_version_client, camera_data_version_client, block_properties_data_version_client, new_block_data_version_client, prerender_mode_data_version_client) dump_jsonp(self, data, callback) except Exception, e: logging.error('Could not retrieve data: ' + str(e))
def get(self): try: namespace_manager.set_namespace(self.request.get('namespace')) data = { 'camera_pos': [ float(self.request.get('camera.x')), float(self.request.get('camera.y')), float(self.request.get('camera.z')) ], 'camera_a_x': float(self.request.get('camera.aX')), 'camera_a_y': float(self.request.get('camera.aY')), 'camera_a_z': float(self.request.get('camera.aZ')), 'camera_fl': float(self.request.get('camera.fl')), 'camera_sensor_resolution': float(self.request.get('camera.sensorResolution')) } callback = self.request.get('callback') db.run_in_transaction(self.transaction, data) dump_jsonp(self, {}, callback) except Exception, e: logging.error('Could not update camera data: ' + str(e))
def get_overrides(cls, force_update=False): """Returns current property overrides, maybe cached.""" now = long(time.time()) age = now - cls.last_update_time max_age = UPDATE_INTERVAL_SEC.get_value(cls.db_overrides) if force_update or age < 0 or age >= max_age: # Value of '0' disables all datastore overrides. if UPDATE_INTERVAL_SEC.get_value() == 0: cls.db_overrides = {} return cls.db_overrides # Load overrides from a datastore. try: old_namespace = namespace_manager.get_namespace() try: namespace_manager.set_namespace( appengine_config.DEFAULT_NAMESPACE_NAME) cls.load_from_db() finally: namespace_manager.set_namespace(old_namespace) except Exception as e: # pylint: disable-msg=broad-except logging.error('Failed to load properties from a database: %s.', str(e)) finally: # Avoid overload and update timestamp even if we failed. cls.last_update_time = now cls.update_index += 1 return cls.db_overrides
def post(self): """ CREATE a new Game entity """ self.record_usage() namespace_manager.set_namespace(users.get_current_user().user_id()) param_map = {} param_map['player_faction'] = self.request.get('player_faction') param_map['player_warcaster'] = self.request.get('player_warcaster') param_map['opponent_name'] = self.request.get('opponent_name', '') param_map['opponent_faction'] = self.request.get('opponent_faction') param_map['opponent_warcaster'] = self.request.get( 'opponent_warcaster') param_map['result'] = self.request.get('result') param_map['size'] = int(self.request.get('size', 0)) param_map['won'] = self.request.get('won') == 'true' param_map['draw'] = self.request.get('draw') == 'true' param_map['teaching'] = self.request.get('teaching') == 'true' param_map['location'] = self.request.get('location', '') param_map['game_type'] = self.request.get('game_type', '') date = self.request.get('date') try: real_date = datetime.datetime.strptime(date, '%m/%d/%Y') except (ValueError): real_date = datetime.datetime.strptime(date, '%Y-%m-%d') param_map['date'] = real_date param_map['created_at'] = datetime.datetime.now() logging.error(param_map) game = Game(**param_map) game.put() self.response.out.write(game.key.urlsafe())
def setUp(self): super(CertificateCriteriaTestCase, self).setUp() self.base = '/' + self.COURSE_NAME context = actions.simple_add_course( self.COURSE_NAME, '*****@*****.**', 'Certificate Criteria') self.old_namespace = namespace_manager.get_namespace() namespace_manager.set_namespace('ns_%s' % self.COURSE_NAME) self.course = courses.Course(None, context) self.course.save() actions.login(self.STUDENT_EMAIL) actions.register(self, self.STUDENT_EMAIL) self.student = ( models.StudentProfileDAO.get_enrolled_student_by_email_for( self.STUDENT_EMAIL, context)) # Override course.yaml settings by patching app_context. self.get_environ_old = sites.ApplicationContext.get_environ self.certificate_criteria = [] def get_environ_new(app_context): environ = self.get_environ_old(app_context) environ['certificate_criteria'] = self.certificate_criteria return environ sites.ApplicationContext.get_environ = get_environ_new
def delete(self, regexp): # NOTE(andrin): When a counter is deleted and used again afterwards # it will not be harvested in the following case: # - Instance A deletes counter C. # - Instance B has C still in self.known_counter_names_ # and increments C without updating the CounterNamesShards. # - Instance A generates the harvest report and wont find C. # To fix this issue, we reload the DefaultCounterNamesManager # once a day. # To fix this issue manually, you can just deploy a new # version of your app. namespace = namespace_manager.get_namespace() try: namespace_manager.set_namespace(self.namespace) to_delete = [] matcher = re.compile("(" + regexp + ")$") for name in self.all(): if matcher.match(name): to_delete += [name] for shard in CounterNamesShard.all(): dirty = False for name in to_delete: if name in shard.names: shard.names.remove(name) dirty = True if dirty: shard.put() for name in to_delete: if name in self.known_counter_names_: self.known_counter_names_.remove(name) return to_delete finally: namespace_manager.set_namespace(namespace)
def outputPLstats(keyname, namespace = ''): import processDesires originalNamespace = namespace_manager.get_namespace() namespace_manager.set_namespace(namespace) try: meta = meSchema.metaAlg.get_by_key_name(keyname) trades = eval(meta.CashDelta) if namespace == '': stockList = ['HBC','CME','GOOG','INTC'] else: stockList = [namespace] tradeDict = {} for stock in stockList: tradeDict[stock] = {'P':0.0,'L':0.0,'Ptrades':[],'Ltrades':[]} for trade in trades: stock = trade['Symbol'] if not (trade['PandL'] <= -10.0 and trade['PandL'] >= -14.0) and trade['PandL'] - 10.0 > 0.0: tradeDict[stock]['P'] += trade['PandL'] - 10.0 tradeDict[stock]['Ptrades'].append(trade['PandL'] - 10.0) elif not (trade['PandL'] <= -10.0 and trade['PandL'] >= -14.0) and trade['PandL'] - 10.0 < 0.0: tradeDict[stock]['L'] += trade['PandL'] - 10.0 tradeDict[stock]['Ltrades'].append(trade['PandL'] - 10.0) for stock in tradeDict: try: stdDevP, meanP = processDesires.getStandardDeviationMean(tradeDict[stock]['Ptrades']) stdDevL, meanL = processDesires.getStandardDeviationMean(tradeDict[stock]['Ltrades']) print stock, ':', tradeDict[stock]['P'], ':', max(tradeDict[stock]['Ptrades']), print ':', len(tradeDict[stock]['Ptrades']), ':', meanP, ' stdDev:', stdDevP print stock, ':', tradeDict[stock]['L'], ':', min(tradeDict[stock]['Ltrades']), print ':', len(tradeDict[stock]['Ltrades']), ':', meanL, ' stdDev:', stdDevL except: print 'Error Encountered!' finally: namespace_manager.set_namespace(originalNamespace)
def init_xsrf_secret_if_none(cls): """Verifies that non-default XSRF secret exists; creates one if not.""" # Any non-default value is fine. if XSRF_SECRET.value and XSRF_SECRET.value != XSRF_SECRET.default_value: return # All property manipulations must run in the default namespace. old_namespace = namespace_manager.get_namespace() try: namespace_manager.set_namespace( appengine_config.DEFAULT_NAMESPACE_NAME) # Look in the datastore directly. entity = ConfigPropertyEntity.get_by_key_name(XSRF_SECRET.name) if not entity: entity = ConfigPropertyEntity(key_name=XSRF_SECRET.name) # Any non-default non-None value is fine. if (entity.value and not entity.is_draft and (str(entity.value) != str(XSRF_SECRET.default_value))): return # Initialize to random value. entity.value = base64.urlsafe_b64encode( os.urandom(XSRF_SECRET_LENGTH)) entity.is_draft = False entity.put() finally: namespace_manager.set_namespace(old_namespace)
def post(self): userNamespace = namespace_manager.get_namespace() namespace_manager.set_namespace('') results = {'result': False} user = self.current_user action = self.request.get('action') if (action == 'createPrivateArea'): # Anyone can create a private area newName = self.request.get('privateAreaName') newDisplayName = self.request.get('privateAreaDisplayName') results = self.createPrivateArea(newName, newDisplayName) elif (action == 'saveUsers'): if user.isAdmin: users = json.loads(self.request.get('newUserValues')) results = self.saveUsers(users) else: results['error'] = "User must be admin." resultJSON = json.dumps(results) self.response.headers[ "Content-Type"] = 'application/json; charset=utf-8' self.response.out.write(resultJSON) namespace_manager.set_namespace(userNamespace)
def put_copy(self, src, dst): """Copy file from src to dst. Metadata is copied. Args: src: /bucket/filename. This file must exist. dst: /bucket/filename """ common.validate_file_path(src) common.validate_file_path(dst) ns = namespace_manager.get_namespace() try: namespace_manager.set_namespace('') src_blobkey = self._filename_to_blobkey(src) source = _AE_GCSFileInfo_.get_by_key_name(src_blobkey) token = self._filename_to_blobkey(dst) new_file = _AE_GCSFileInfo_(key_name=token, filename=dst, finalized=True) new_file.options = source.options new_file.etag = source.etag new_file.size = source.size new_file.creation = datetime.datetime.utcnow() new_file.put() finally: namespace_manager.set_namespace(ns) local_file = self.blob_storage.OpenBlob(src_blobkey) self.blob_storage.StoreBlob(token, local_file)
def post_start_creation(self, filename, options): """Start object creation with a POST. This implements the resumable upload XML API. Only major limitation of current implementation is that we don't support multiple upload sessions for the same GCS file. Previous _AE_GCSFileInfo (which represents either a finalized file, or an upload session) will be removed when a new upload session is created. Args: filename: gcs filename of form /bucket/filename. options: a dict containing all user specified request headers. e.g. {'content-type': 'foo', 'x-goog-meta-bar': 'bar'}. Returns: a token (blobkey) used for continuing upload. """ ns = namespace_manager.get_namespace() try: namespace_manager.set_namespace('') common.validate_file_path(filename) token = self._filename_to_blobkey(filename) gcs_file = _AE_GCSFileInfo_.get_by_key_name(token) self._cleanup_old_file(gcs_file) new_file = _AE_GCSFileInfo_(key_name=token, filename=filename, finalized=False) new_file.options = options new_file.put() return token finally: namespace_manager.set_namespace(ns)
def MakeFollows(self): """ # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ADD FOLLOWS FOR ADMIN USERS # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ """ nextURL = None firstURL = self.request.get('nexturl') query = PointRoot.query().order(PointRoot.url) if firstURL: query = query.filter(PointRoot.url >= firstURL) pointRoots = query.fetch(11) if len(pointRoots) == 11: nextURL = pointRoots[-1].url pointRootsToReview = pointRoots[:10] else: pointRootsToReview = pointRoots i = 0 for pointRoot in pointRootsToReview: pointRootKey = pointRoot.key followers = {} versions = pointRoot.getAllVersions() for point in versions: if point.version == 1: followers[point.authorURL] = 'created' elif not point.authorURL in followers: followers[point.authorURL] = 'edited' for comment in pointRoot.getComments(): if not comment.userUrl in followers: followers[comment.userUrl] = 'commented' logging.info('ROOT: %s FOLLOWERS: %s' % (pointRoot.url, str(followers))) for url in followers.iterkeys(): followType = followers[url] previousNamespace = namespace_manager.get_namespace() if previousNamespace and previousNamespace != '': namespace_manager.set_namespace('') # DEFAULT NAMESPACE usr = WhysaurusUser.getByUrl(url) namespace_manager.set_namespace(previousNamespace) else: usr = WhysaurusUser.getByUrl(url) logging.info('Trying to follow for U:%s, R:%s, T:%s' % (url, pointRoot.url, followType)) f = None f = Follow.createFollow(usr.key, pointRootKey, followType) if f: i = i + 1 logging.info('ADDED follow for U:%s, R:%s, T:%s' % (url, pointRoot.url, followType)) logging.info('Added %d follows' % i) if nextURL: t = Task(url="/MakeFollows", params={'nexturl': nextURL}) t.add(queue_name="notifications") logging.info('Requeing MakeFollows task to start at url %s ' % nextURL)
def setUp(self): unittest.TestCase.setUp(self) self.appid = "testapp" os.environ["APPLICATION_ID"] = self.appid os.environ["AUTH_DOMAIN"] = "gmail.com" self.resetDatastore() namespace_manager.set_namespace(None)
def post(self): ns = self.request.get('ns') namespace_manager.set_namespace(ns) greeting = Greeting(key_name = self.request.get('key')) greeting.content = self.request.get('value') key = greeting.put() self.response.out.write("success!")
def timeseries_query(timeseries): project_id = list(field.value for field in timeseries.fields if field.key == 'project_id') if not project_id: raise ndb.Return() project_id = project_id[0] namespace_manager.set_namespace('projects.%s' % project_id) query = TimeSeriesModel.query() fieldmodels = [] for field in timeseries.fields: fieldmodels.append(FieldModel(field_key=field.key, value=field.value)) for fieldmodel in fieldmodels: query = query.filter(TimeSeriesModel.fields == fieldmodel) query = query.filter(TimeSeriesModel.metric == timeseries.metric) ts = yield query.get_async() if ts == None: ts = TimeSeriesModel(points=[], fields=fieldmodels, metric=timeseries.metric) ts.update_timestamp() ts.points = [ PointModel(time=point.time, value=point.value) for point in timeseries.points ] yield ts.put_async() raise ndb.Return()
def outputPerStockHistoryForChart(keyname): from math import floor,ceil originalNamespace = namespace_manager.get_namespace() try: namespace_manager.set_namespace('') meta = meSchema.metaAlg.get_by_key_name(keyname) finally: namespace_manager.set_namespace(originalNamespace) stopSteps = [] history = eval(meta.history) for hist in history: stopSteps.append(hist['step']) stopSteps.sort() trades = eval(meta.CashDelta) returnDict = {'HBC':{},'CME':{},'GOOG':{},'INTC':{}} for stopStep in stopSteps: for key in returnDict: returnDict[key][stopStep] = 0.0 for trade in trades: tradeIndex = (trade['step'] - stopSteps[0])/400.0 stopStep = stopSteps[int(ceil(tradeIndex))] returnDict[trade['Symbol']][stopStep] += trade['PandL'] for stock in returnDict: print stock,':', for step in stopSteps: print returnDict[stock][step], print
def get(self, vendorid, deviceid): try: namespace_manager.set_namespace(vendorid) except: self.error(400) return if not validate_did(deviceid): self.error(400) return deviceid = deviceid.lower() node = DeviceNode.get_by_id(deviceid) last_update = None if node is not None: last_update = datetime.datetime.now() - node.last_seen logging.info('last_update: ' + str(last_update)) clientid = vendorid + deviceid token = channel.create_channel(clientid) # logging.info('token ' + token) template_values = { 'token': token, 'vendorid': vendorid, 'deviceid': deviceid, 'node': node, 'last_update': fuzzy_readable_time(last_update), } template = JINJA_ENVIRONMENT.get_template('index.html') self.response.write(template.render(template_values)) return
def test_config_visible_from_any_namespace(self): """Test that ConfigProperty is visible from any namespace.""" assert ( config.UPDATE_INTERVAL_SEC.value == config.UPDATE_INTERVAL_SEC.default_value) new_value = config.UPDATE_INTERVAL_SEC.default_value + 5 # Add datastore override for known property. prop = config.ConfigPropertyEntity( key_name=config.UPDATE_INTERVAL_SEC.name) prop.value = str(new_value) prop.is_draft = False prop.put() # Check visible from default namespace. config.Registry.last_update_time = 0 assert config.UPDATE_INTERVAL_SEC.value == new_value # Check visible from another namespace. old_namespace = namespace_manager.get_namespace() try: namespace_manager.set_namespace( 'ns-test_config_visible_from_any_namespace') config.Registry.last_update_time = 0 assert config.UPDATE_INTERVAL_SEC.value == new_value finally: namespace_manager.set_namespace(old_namespace)
def post(self): user = users.get_current_user() if not user: self.redirect(users.create_login_url(self.request.uri)) bandid = self.request.get('bandid') logging.info('bandid: ' + bandid) try: namespace_manager.set_namespace(bandid) except: self.error(400); return name = self.request.get('name') interpreter = self.request.get('interpreter') comment = self.request.get('comment') song = SongNode.get_by_id(name+interpreter) if not song: self.error(404) song.comments.append(comment + ' (' + user.nickname() + ')') song.put() song_url=urllib.quote('/' + bandid + '/song/' + song.name + '/' + song.interpreter) send_notifications(song,'http://' + self.request.host + song_url, 'comment', user) self.redirect(song_url)
def convertMetaAlgToMetaAlgStats(startStep, stopStep, technique, namespace): namespace_manager.set_namespace(namespace) try: query = meSchema.metaAlg.all().filter('startStep =', startStep).filter('stopStep =', stopStep).filter('technique =', technique) results = query.fetch(500) if len(results) == 0: return percentReturns = [] Positive = 0 for metaAlg in results: percentReturns.append(metaAlg.percentReturn) if metaAlg.percentReturn >= 0: Positive += 1 Min = min(percentReturns) Max = max(percentReturns) n = len(percentReturns) Mean = sum(percentReturns)/n percentReturns.sort() if n%2 == 0: mid = n/2 Median = (percentReturns[mid-1] + percentReturns[mid])/2 else: Median = percentReturns[n/2] PercentPositive = Positive/float(n) stepRange = stopStep - startStep key_name = str(stepRange).rjust(7,'0') + '-' + str(startStep).rjust(7,'0') + '-' + str(stopStep).rjust(7,'0') + '-' + technique metaAlgStat = meSchema.metaAlgStat(key_name = key_name, Min = Min, Median = Median, Mean = Mean, Max = Max, Positive = PercentPositive, stopStep = stopStep, startStep = startStep, stepRange = stepRange, technique = technique) db.put(metaAlgStat) finally: namespace_manager.set_namespace('')
def update_note_parents(entity): """Update note parents property""" namespace = namespace_manager.get_namespace() if namespace != 'newyear': namespace_manager.set_namespace('newyear') entity.parents = get_note_parents(entity) yield op.db.Put(entity)
def add_event(customer_id, content_id, time, user_ip, user_user_agent, user_location): """Store event and content name in Datastore""" log.info( "customer_id: {}, content_id {}, event_time {}, ip {}, user_agent {}, " "location {}".format( customer_id, content_id, time, user_ip, user_user_agent, user_location)) previous_namespace = namespace_manager.get_namespace() try: namespace_manager.set_namespace(customer_id) load_content_name_if_needed(content_id) ndb_model.Event( content_id=content_id, time=datetime.datetime.fromtimestamp(time / 1000.0), user_ip=user_ip, user_user_agent=user_user_agent, user_location=user_location).put() log.info("Stored Event entity") return True except Exception as ex: log.warning("Unable to store entity") log.warning(ex.message) return False finally: namespace_manager.set_namespace(previous_namespace)
def push_batch(): """Push all the events that have been caused by this request.""" batch = flask.g.get('events', None) setattr(flask.g, 'events', None) if batch is None: return logging.info('Sending %d events to proxy', len(batch)) pusher_shim = pusher_client.get_client() # Now figure out what channel to post these to. # Building id should always be in the namespace # for any request which triggers events. # So we use the namespace. Horrid. assert namespace_manager.get_namespace() != '' building_id = namespace_manager.get_namespace() try: namespace_manager.set_namespace(None) proxies = Proxy.query(Proxy.building_id == building_id).iter() for proxy in proxies: channel_id = 'private-%s' % proxy.key.string_id() logging.info('Pushing %d events to channel %s', len(batch), channel_id) pusher_shim.push(channel_id, batch) finally: namespace_manager.set_namespace(building_id)
def dashboard(self): status = [] cquery = CronUrlDB3.all() for cronUrlDB in cquery: namespace_manager.set_namespace(str(cronUrlDB.key())) query = StatDB3.all() if query.count() > 0: n_errors = 0 n_retrys = 0 for statDB in query: n_errors += statDB.n_errors n_retrys += statDB.n_retrys if n_errors > 4: image = 'level3.png' elif n_errors > 1: image = 'level2.png' elif n_retrys > 0: image = 'level1.png' else: image = 'level0.png' else: image = 'level.png' status.append((cronUrlDB, image)) namespace_manager.set_namespace('') # remove namespace template_values = { 'style': style, 'header': header(self), 'footer': footer(self), 'urlDBs': UrlDB.all(), 'status': status, } printHtml(self, 'checkurl.html', template_values)
def post_attendance(self, request): """Register whether or not a single student attended class. Arguments request: an AttendanceRecord message. Returns: An instance of message_types.VoidMessage. """ current_user = endpoints.get_current_user() app_id = os.environ['APPLICATION_ID'] if app_id.startswith('dev~') or app_id == 'testbed-test': email = '*****@*****.**' else: if current_user is None: raise endpoints.UnauthorizedException('Invalid token.') email = current_user.email() domain = email.split('@')[1] if domain == 'gmail.com': domain = '' namespace_manager.set_namespace(domain) student_key = ndb.Key('Student', request.student) student = student_key.get() if not student: raise endpoints.BadRequestException('No student: %s' % request.student) classname_key = ndb.Key('Class', request.classname) classname = classname_key.get() if not classname: raise endpoints.BadRequestException('No class: %s' % request.classname) if not AttendanceAPI.is_enrolled(classname, student_key): raise endpoints.BadRequestException( 'Student %s is not enrolled in class %s' % (student_key, classname.name)) today = AttendanceAPI.today_as_ordinal(classname.timezone) if request.date != today: raise endpoints.BadRequestException('Date must be %s, but is: %s' % (today, request.date)) logging.info('Updating class: %s student: %s date: %s user: %s attend: %s' % (classname.name, request.student, request.date, email, request.attended)) attendance_key = ndb.Key('Class', request.classname, 'Attendance', request.date) attendance = attendance_key.get() if not attendance: attendance = Attendance(key=attendance_key, attending=[]) if request.attended: if not AttendanceAPI.is_present(attendance, student_key): attendance.attending.append(StudentPresent(student=student_key)) attendance.put() else: try: attendance.attending.remove(student_present) attendance.put() except ValueError: pass return message_types.VoidMessage()
def get(self, bandid): user = users.get_current_user() if not user: self.redirect(users.create_login_url(self.request.uri)) logging.info('bandid: ' + bandid) try: namespace_manager.set_namespace(bandid) except: self.error(400); return conf = Configuration.get_by_id("singleton") if user not in conf.users: conf.users.append(user) # TODO: For now users are authenticated by default. This should # probably be a configuration parameter. conf.auth_users.append(user) conf.put() ranking_query = SongNode.query().order(-SongNode.vote_cnt) ranking_query = ranking_query.filter(SongNode.vote_cnt < conf.votes_to_graduate) songs = ranking_query.fetch(50) template_values = { 'is_admin': conf.admin == user, 'songs': songs, 'band_name': conf.name, 'bandid': bandid, 'logout_uri': users.create_login_url(self.request.uri), 'votes_to_graduate': conf.votes_to_graduate } template = JINJA_ENVIRONMENT.get_template('ranking.html') self.response.write(template.render(template_values))
def post(self): user = users.get_current_user() if not user: self.redirect(users.create_login_url(self.request.uri)) bandid = self.request.get('bandid') logging.info('bandid: ' + bandid) try: namespace_manager.set_namespace(bandid) except: self.error(400); return conf = Configuration.get_by_id("singleton") if user not in conf.auth_users: logging.info("letting an unauthed users vote... for now") votes = self.request.get_all('votes') for songid in votes: logging.info('songid:' + songid + 'len: ' + str(len(songid))) song = SongNode.get_by_id(songid) song_url=urllib.quote('/' + bandid + '/song/' + song.name + '/' + song.interpreter) if user not in song.votes: send_notifications(song,'http://' + self.request.host + song_url, 'vote', user) song.votes.append(user) song.vote_cnt += 1 song.graduated = song.vote_cnt >= VOTES_TO_GRADUATE song.put() self.redirect('/' + bandid + '/thanks')
def get(self, bandid, name, interpreter): user = users.get_current_user() if not user: self.redirect(users.create_login_url(self.request.uri)) logging.info('bandid: ' + bandid) try: namespace_manager.set_namespace(bandid) except: self.error(400); return song = SongNode.get_by_id(name+interpreter) if not song: logging.error("Cant find song id = " + name + interpreter) self.error(404); return template_values = { 'song': song, 'last_update': fuzzy_readable_time(datetime.datetime.now() - song.last_update), 'bandid': bandid, 'user_has_voted': user in song.votes } template = JINJA_ENVIRONMENT.get_template('song.html') self.response.write(template.render(template_values))
def get(self, bandid): user = users.get_current_user() if not user: self.redirect(users.create_login_url(self.request.uri)) logging.info('bandid: ' + bandid) try: namespace_manager.set_namespace(bandid) except: self.error(400); return conf = Configuration.get_by_id("singleton") if conf.admin == None: conf.admin = user conf.put() if conf.admin != user: self.error(401); return template_values = { 'admin': conf.admin, 'users': conf.users, 'auth_users': conf.auth_users, 'bandid': bandid, } template = JINJA_ENVIRONMENT.get_template('config.html') self.response.write(template.render(template_values))
def post(self): user = users.get_current_user() if not user: self.redirect(users.create_login_url(self.request.uri)) votes_to_graduate = self.request.get('votes_to_graduate') name = self.request.get('name') bandid = urllib.quote(name.replace(' ', '-').lower()) if not name or not votes_to_graduate or Bands.get_by_id(bandid): msg = "Requested group exists already" else: msg = "New band created successfully" Bands(id=bandid).put() namespace_manager.set_namespace(bandid) conf = Configuration(id="singleton") conf.name = name conf.votes_to_graduate = int(votes_to_graduate) conf.put() template_values = { 'bandid': bandid, 'name': name, 'votes_to_graduate': votes_to_graduate, 'msg': msg } template = JINJA_ENVIRONMENT.get_template('confirm.html') self.response.write(template.render(template_values))
def testGenerator(self): """Test generator functionality.""" expected_keys = [] for _ in range(0, 100): expected_keys.append(TestEntity().put()) namespace_manager.set_namespace("google") for _ in range(0, 100): expected_keys.append(TestEntity().put()) namespace_manager.set_namespace(None) kranges = [ key_range.KeyRange( key_start=key(25), key_end=key(50), direction="ASC", include_start=False, include_end=True ), key_range.KeyRange( key_start=key(110, namespace="google"), key_end=key(150, namespace="google"), direction="ASC", include_start=False, include_end=True, namespace="google", ), ] query_range = input_readers.DatastoreKeyInputReader(ENTITY_KIND, kranges, 50) keys = [] for k in query_range: keys.append(k) self.assertEquals(65, len(keys)) self.assertEquals(expected_keys[25:50] + expected_keys[110:150], keys)
def testNamespaces(self): """Tests namespace support.""" from google.appengine.api import namespace_manager namespace = namespace_manager.get_namespace() class Author(db.Model): name = db.StringProperty() class Book(db.Model): title = db.StringProperty() try: namespace_manager.set_namespace("testing") a = Author(name="Douglas Adams", key_name="douglasadams") a.put() b = Book(parent=a, title="Last Chance to See") b.put() query = Book.all().filter("title =", "Last Chance to See") self.assertEqual(query.get().title, b.title) finally: namespace_manager.set_namespace(namespace) query = Book.all().filter("title =", "Last Chance to See") self.assertEqual(query.get(), None)
def MakeLinksAllAreas(self): q = PrivateArea.query() for pa in q.iter(): logging.info("Kicking off the make links for private area: " + pa.name) namespace_manager.set_namespace(pa.name) self.MakeLinks()
def setUp(self): super(NewsTestBase, self).setUp() self.base = '/' + self.COURSE_NAME self.app_context = actions.simple_add_course( self.COURSE_NAME, self.ADMIN_EMAIL, 'Title') self.maxDiff = None # Show full text expansion of expect mismatches. # Simplify equivalence checks by supplying a deep comparator, rather # than getting object instance equality comparison. def news_items_are_equal(thing_one, thing_two): return (thing_one.resource_key == thing_two.resource_key and thing_one.when == thing_two.when and thing_one.url == thing_two.url and thing_one.labels == thing_two.labels) news.NewsItem.__eq__ = news_items_are_equal news.NewsItem.__repr__ = lambda x: x.__dict__.__repr__() def seen_items_are_equal(thing_one, thing_two): return (thing_one.resource_key == thing_two.resource_key and abs((thing_one.when - thing_two.when).total_seconds()) < 2) news.SeenItem.__eq__ = seen_items_are_equal news.SeenItem.__repr__ = lambda x: x.__dict__.__repr__() news.I18nTitleRegistry.register( 'test', lambda x: 'Test Item ' + x.split(':', 1)[1]) self.old_namespace = namespace_manager.get_namespace() namespace_manager.set_namespace(self.NAMESPACE) # News is always enabled during own-module tests so that we do not # accumulate any regressions within ourselves. Other modules are # free to simply disable if they wish. self.save_is_enabled = news.is_enabled news.is_enabled = lambda: True if not news.custom_module.enabled: news.custom_module.enable()
def get_overrides(cls, force_update=False): """Returns current property overrides, maybe cached.""" now = long(time.time()) age = now - cls.last_update_time max_age = UPDATE_INTERVAL_SEC.get_value(db_overrides=cls.db_overrides) if force_update or age < 0 or age >= max_age: # Value of '0' disables all datastore overrides. if UPDATE_INTERVAL_SEC.get_value() == 0: cls.db_overrides = {} return cls.db_overrides # Load overrides from a datastore. try: old_namespace = namespace_manager.get_namespace() try: namespace_manager.set_namespace( appengine_config.DEFAULT_NAMESPACE_NAME) cls.load_from_db() finally: namespace_manager.set_namespace(old_namespace) except Exception as e: # pylint: disable-msg=broad-except logging.error( 'Failed to load properties from a database: %s.', str(e)) finally: # Avoid overload and update timestamp even if we failed. cls.last_update_time = now cls.update_index += 1 return cls.db_overrides
def setUp(self): super(ExtraTabsTests, self).setUp() self.base = '/' + COURSE_NAME app_context = actions.simple_add_course( COURSE_NAME, ADMIN_EMAIL, 'Extra Tabs Course') self.old_namespace = namespace_manager.get_namespace() namespace_manager.set_namespace('ns_%s' % COURSE_NAME) self.course = courses.Course(None, app_context) courses.Course.ENVIRON_TEST_OVERRIDES = { 'course': { 'extra_tabs': [ { 'label': 'FAQ', 'position': 'left', 'visibility': 'all', 'url': '', 'content': 'Frequently asked questions'}, { 'label': 'Resources', 'position': 'right', 'visibility': 'student', 'url': 'http://www.example.com', 'content': 'Links to resources'}] } } self.faq_url = 'modules/extra_tabs/render?index=0' actions.login(STUDENT_EMAIL, is_admin=False) actions.register(self, STUDENT_NAME)
def post(self): logging.info('self.request') logging.info(self.request) token = self.request.get('token') version = self.request.get('appver') if not version: version = 0 path = self.request.path pathArray = path.split("/") apiVersion = int(pathArray[2]) aID = pathArray[3] cTime = int(time.time()) auInfo={} aInfo={} param={} allresult={} gdNamespace = namespace_manager.get_namespace() appNamespace = 'APP_'+aID namespace_manager.set_namespace(gdNamespace) #find app try: aInfo = DB_App.get_by_id(aID) except Exception, e: self.printError("dont find app1",100,aInfo.secretKey) return
def process(self, resource_id, *args, **kwargs): from voluptuous import Schema from google.appengine.api import namespace_manager namespace_manager.set_namespace('_x_') session_store = sessions.get_store(request=self.request) session = session_store.get_session() if not 'u' in session: self.about(403) return obj = json.loads(self.request.body) schema = Schema(self.schema, extra=True) try: obj = schema(obj) except: logging.exception('validation failed') session_store = sessions.get_store() session = session_store.get_session() school_key = ndb.Key(urlsafe = session.get('s')) obj['school'] = school_key entity = self.entity.from_dict(obj) entity.put() self.write_json_response(entity.to_dict())
def claim_proxy(): """Claim the given proxy id for the current user.""" body = flask.request.get_json() if body is None: flask.abort(400, 'JSON body and mime type required.') proxy_id = body.get('proxy_id', None) if proxy_id is None or proxy_id == '': flask.abort(400, 'proxy_id required.') # this will run as a user, so we don't need to authenticate # it (already done in main). Running in users namespace. assert namespace_manager.get_namespace() != '' building_id = namespace_manager.get_namespace() # We need to reset the namespace to access the proxies namespace_manager.set_namespace(None) proxy = Proxy.get_by_id(proxy_id) if proxy == None: logging.info('Proxy \'%s\' not found', proxy_id) flask.abort(404) if proxy.building_id is not None: flask.abort(400, 'Proxy already claimed') proxy.building_id = building_id proxy.put() return ('', 201)
def run_tasks(self, url=None, queue_name=None, method='POST', response_status_code=200, **kwargs): """ Helper method to execute a specific group of tasks :param user: :param queue_name: :param queue_url: :param kwargs: :return: """ from google.appengine.api import namespace_manager tasks = self.taskqueue_stub.get_filtered_tasks(url=url, queue_names=[queue_name]) for task in tasks: namespace = task.headers.get('X-AppEngine-Current-Namespace', '') previous_namespace = namespace_manager.get_namespace() try: namespace_manager.set_namespace(namespace) headers = { k: v for k, v in task.headers.iteritems() if k.startswith('X-AppEngine')} if method == 'PUT': response = self.testapp.put(url, task.payload, headers=headers, status='*') else: response = self.testapp.post(url, task.payload, headers=headers, status='*') finally: namespace_manager.set_namespace(previous_namespace)
def get(self,propkey): #memcache namespace changes UNTESTED FIXME propkey = sanitizeinputwords(propkey) major_ver, minor_ver = os.environ.get('CURRENT_VERSION_ID').rsplit('.', 1) namespace_manager.set_namespace(major_ver) logging.debug("namespace: " + major_ver) p = memcache.get(propkey) if p is not None: logging.debug("from memcache: " + propkey) self.response.headers["Content-Type"] = "application/json" self.response.out.write(json.dumps(p, sort_keys=True,indent=4, separators=(',', ': '))) else: detailurl = "http://my.appleton.org/Propdetail.aspx?PropKey=" + str(propkey) datagroups = [] try: docroot = lxml.html.fromstring(urllib2.urlopen(detailurl).read()) tables = docroot.xpath("//table[@class='t1']") for table in tables: ths = table.xpath("./tr/th") #assuming single <th> per table for th in ths: if th is not None: lxml.etree.strip_tags(th, 'a', 'b', 'br', 'span', 'strong') if th.text: thkey = re.sub('\W', '', th.text).lower() #nospaces all lower datagroups.append(thkey) if th.text.strip() == "Businesses": logging.debug("found Business <th>") tdkey = "businesses" businesslist = [] tds = table.xpath("./tr/td") if tds is not None: for td in tds: lxml.etree.strip_tags(td, 'a', 'b', 'br', 'span', 'strong') businesslist.append(td.text.strip()) logging.debug("businesslist: " + str(businesslist)) datadict = {} tdcounter = 0 tds = table.xpath("./tr/td") if tds is not None: for td in tds: lxml.etree.strip_tags(td, 'a', 'b', 'br', 'span', 'strong') if tdcounter == 0: tdkey = re.sub('\W', '', td.text).lower() if td.text else '' tdcounter += 1 else: tdvalue = td.text.strip().title() if td.text else '' tdvalue = " ".join(tdvalue.split()) #remove extra whitespace tdcounter = 0 #when the source tr + td are commented out lxml still sees them. PREVENT! if tdkey == '' and tdvalue == '': break else: datadict[tdkey] = tdvalue datagroups.append(datadict) logging.debug("setting memcache for key: " + propkey) memcache.add(str(propkey),datagroups) self.response.headers["Content-Type"] = "application/json" self.response.out.write(json.dumps(datagroups, sort_keys=True,indent=4, separators=(',', ': '))) except urllib2.HTTPError, response: self.response.out.write( 'error - Scrape: ' + str(response))
def wrapped_method(*args, **kwargs): original_namespace = namespace_manager.get_namespace() try: namespace_manager.set_namespace(DATASTORE_NAMESPACE) return original_method(*args, **kwargs) finally: namespace_manager.set_namespace(original_namespace)
def setUp(self): super(CertificateCriteriaTestCase, self).setUp() self.base = '/' + self.COURSE_NAME context = actions.simple_add_course( self.COURSE_NAME, self.ADMIN_EMAIL, 'Certificate Criteria') self.old_namespace = namespace_manager.get_namespace() namespace_manager.set_namespace('ns_%s' % self.COURSE_NAME) self.course = courses.Course(None, context) self.course.save() self.TEST_USER = actions.login('*****@*****.**') actions.register(self, self.TEST_USER.email()) self.student = ( models.StudentProfileDAO.get_enrolled_student_by_user_for( self.TEST_USER, context)) # Override course.yaml settings by patching app_context. self.get_environ_old = sites.ApplicationContext.get_environ self.certificate_criteria = [] def get_environ_new(app_context): environ = self.get_environ_old(app_context) environ['certificate_criteria'] = self.certificate_criteria return environ sites.ApplicationContext.get_environ = get_environ_new
def Projects(self, sub): if sub != None: namespace_manager.set_namespace(None) rootUser = UserProfile.all().filter("user", users.get_current_user()).get() if rootUser != None: return rootUser.projects return "" if self.projects is None else self.projects # should be blank...
def processOfRequest(self): self.response.headers['Content-Type'] = 'application/json' # if self.checkAccessIPAddress() == False: # return try: is_login, user_email = self.checkLogin(not_redirect=True) if is_login == False: self._code = 403 self._msg = self.getMsg('MSG_NOT_LOGINED') self.responseAjaxResult() return # Requestからvoにセット req = UcfVoInfo.setRequestToVo(self) start = int(req['start']) limit = int(req['limit']) namespace_manager.set_namespace('') sk_target_domain = UcfUtil.getHashStr(req, 'sk_target_domain').lower() q = sateraito_func.AddOnHistory.all() if sk_target_domain != '': q.filter('target_domain >= ', sk_target_domain) q.filter('target_domain < ', ''.join(sk_target_domain + u'\uFFE0')) count = 0 fetch_data = None count = q.count() fetch_data = q.fetch(limit, start) result_list = [] for entry in fetch_data: vo = {} vo['target_domain'] = UcfUtil.nvl(entry.target_domain) vo['application_id'] = UcfUtil.nvl(entry.application_id) vo['is_available'] = UcfUtil.nvl(entry.is_available) vo['reason'] = UcfUtil.nvl(entry.reason) vo['ip_address'] = UcfUtil.nvl(entry.ip_address) vo['user_id'] = UcfUtil.nvl(entry.user_id) vo['log_text'] = UcfUtil.nvl(entry.log_text) vo['user_agent'] = UcfUtil.nvl(entry.user_agent) #vo['result_code'] = UcfUtil.nvl(entry.result_code) #vo['result_message'] = UcfUtil.nvl(entry.result_message) vo['created_date'] = UcfUtil.nvl( UcfUtil.getLocalTime(entry.created_date, self._timezone)) vo['updated_date'] = UcfUtil.nvl( UcfUtil.getLocalTime(entry.updated_date, self._timezone)) result_list.append(vo) ret_value = {'all_count': str(count), 'records': result_list} self._code = 0 self.responseAjaxResult(ret_value) except BaseException, e: self.outputErrorLog(e) self._code = 999 self.responseAjaxResult()
def addIfNew(self, names): new_names = [] fresh = None for name in names: if name not in self.known_counter_names_: if fresh == None: fresh = self.all() if name not in fresh: new_names += [name] else: new_names += [name] if new_names: namespace = namespace_manager.get_namespace() try: namespace_manager.set_namespace(self.namespace) if self.last_shard_ == None: CounterNamesShard.get_or_insert("0") self.last_shard_ = 0 ADD_SUCCESS = 1 ADD_FULL = 2 ADD_FAIL = 3 def addNames(key, names): record = db.get(key) local_names = set(record.names) for name in names: if name not in local_names: local_names.add(name) record.names += [name] try: record.put() return ADD_SUCCESS except (RequestTooLargeError, ValueError): if len(names) == len(record.names): return ADD_FAIL else: return ADD_FULL result = None try: result = db.run_in_transaction( addNames, db.Key.from_path('CounterNamesShard', str(self.last_shard_)), new_names) except: result = ADD_FAIL if result == ADD_FULL: CounterNamesShard.get_or_insert(str(self.last_shard_ + 1)) self.addIfNew(names) if result == ADD_SUCCESS: logging.info("Registered new counter names: %s." % ",".join(new_names)) else: logging.warning( "Coudn't register counter names: %s. (Will retry next time)" % ",".join(new_names)) finally: namespace_manager.set_namespace(namespace) return (not not fresh, len(new_names))
def persist_namespace(self): forced_namespace = self.request.get("namespace") log.info('forced namespace'.format(forced_namespace)) namespace_manager.set_namespace(forced_namespace) self.response.set_cookie('namespace', forced_namespace, expires=datetime.datetime.now() + datetime.timedelta(days=365))
def __init__(self, request=None, response=None): from sosbeacon.group import Group from sosbeacon.group import group_schema from google.appengine.api import namespace_manager namespace_manager.set_namespace('_x_') super(GroupHandler, self).__init__(Group, group_schema, request, response)
def stats_v1(): namespace_manager.set_namespace('VER_2') data = { "version": namespace_manager.get_namespace(), "views": Version.get_by_id('v2').views, "likes": Version.get_by_id('v2').likes } return jsonify(data)
def put_multi_with_namespace(cls, words): user = users.get_current_user() previous_namespace = namespace_manager.get_namespace() try: namespace_manager.set_namespace(user.email().translate(NAMESPACE_TRANS)) ndb.put_multi(words) finally: namespace_manager.set_namespace(previous_namespace)
def put_with_namespace(cls, coll): user = users.get_current_user() previous_namespace = namespace_manager.get_namespace() try: namespace_manager.set_namespace(user.email().translate(NAMESPACE_TRANS)) coll.put() finally: namespace_manager.set_namespace(previous_namespace)
def update_memcache(server_name, host_item=None): namespace_manager.set_namespace("shared.host") memcache_key = "shared.host.info." + server_name if host_item is None: host_item = HostInformation.get_or_insert(host=server_name) memcache.set(key=memcache_key, value=host_item, time=3600) namespace_manager.set_namespace(host_item.namespace) return host_item
def non_transactional_submit(self): old_namespace = namespace_manager.get_namespace() try: namespace_manager.set_namespace(self._namespace) DurableJobEntity._create_job(self._job_name) deferred.defer(self.main) finally: namespace_manager.set_namespace(old_namespace)
def get_enrolled_student_by_email_for(cls, email, app_context): """Returns student for a specific course.""" old_namespace = namespace_manager.get_namespace() try: namespace_manager.set_namespace(app_context.get_namespace_name()) return Student.get_enrolled_student_by_email(email) finally: namespace_manager.set_namespace(old_namespace)
def Projects(self, sub): if sub != None: namespace_manager.set_namespace(None) rootUser = UserProfile.all().filter( 'user', users.get_current_user()).get() if rootUser != None: return rootUser.projects return '' if self.projects is None else self.projects # should be blank...
def CopyIntoNamespace(e, ns): namespace_manager.set_namespace(ns) np = e.__class__() for aen, aep in e.__dict__['_entity'].iteritems(): av = getattr(e, aen) setattr(np, aen, av) np.put() return np
def newfunc(*args, **kwargs): """Set proper namespace for each method call.""" old_namespace = namespace_manager.get_namespace() try: namespace_manager.set_namespace(self._ns) return attr(*args, **kwargs) finally: namespace_manager.set_namespace(old_namespace)
def CreateInNamespace(self, namespace): old_ns = namespace_manager.get_namespace() try: namespace_manager.set_namespace(namespace) pet = Pet() pet.put() finally: namespace_manager.set_namespace(old_ns)