def get(self): """Request handler for GET operations.""" urlfetch.set_default_fetch_deadline(URLFETCH_TIMEOUT) config = explorer_config.ExplorerConfigModel.Get() client = DataHandlerUtil.GetDataClient(self.env) client.project_id = config.default_project query = product_labels.ProductLabelsQuery( data_client=client, dataset_name=config.default_dataset) filters = http_util.GetJsonParam(self.request, 'filters') start_date = None if 'start_date' in filters and filters['start_date']: start_date = filters['start_date'] end_date = None if 'end_date' in filters and filters['end_date']: end_date = filters['end_date'] response = query.Execute( start_date=start_date, end_date=end_date, product_name=filters['product_name'], test=filters['test'], metric=filters['metric']) self.RenderJson({'labels': response['labels']})
def post(self): """Request handler for POST operations.""" try: owner_email = None data = http_util.GetJsonParam(self.request, fields.DATA) if fields.OWNER in data: owner_email = data[fields.OWNER] title = dashboard_model.Dashboard.GetDashboardTitle(data) created_by = dashboard_model.Dashboard.GetDashboardOwner( owner_email) dashboard = dashboard_model.Dashboard() dashboard.created_by = created_by dashboard.title = title dashboard_id = dashboard.put().integer_id() # Now that we have an ID, save the data with an ID attached. data[fields.ID] = str(dashboard_id) data[fields.OWNER] = created_by.email() dashboard.data = json.dumps(data) dashboard.put() self.RenderJson(data) except (base.InitializeError, dashboard_model.InitializeError, http_util.ParameterError) as err: self.RenderJson(data={error_fields.MESSAGE: err.message}, status=400)
def testGetJSON(self): providedValue = '{"name": "value"}' expected_value = {'name': 'value'} params = MockRequest({'data': providedValue}) self.assertEquals(expected_value, http_util.GetJsonParam(params, 'data'))
def get(self): """Request handler for GET operations.""" urlfetch.set_default_fetch_deadline(URLFETCH_TIMEOUT) filters = http_util.GetJsonParam(self.request, 'filters') start_date = filters['start_date'] end_date = filters['end_date'] product_name = filters['product_name'] test = filters['test'] metric = filters['metric'] field_name = self.request.GET.get('field_name') config = explorer_config.ExplorerConfigModel.Get() client = DataHandlerUtil.GetDataClient(self.env) client.project_id = config.default_project query = explorer_method.ExplorerQueryBase( data_client=client, dataset_name=config.default_dataset) query.fields = [field_name + ' AS name'] query.tables = ['lookup_field_cube'] query.wheres = [] if start_date: query.wheres.append( 'day_timestamp >= %s' % (explorer_method.ExplorerQueryBase .GetTimestampFromFilterExpression( start_date))) if end_date: query.wheres.append( 'day_timestamp <= %s' % (explorer_method.ExplorerQueryBase .GetTimestampFromFilterExpression( end_date))) if product_name and field_name != 'product_name': query.wheres.append('product_name = "%s"' % product_name) if test and field_name not in ['test', 'product_name']: query.wheres.append('test = "%s"' % test) if metric and field_name not in ['metric', 'test', 'product_name']: query.wheres.append('metric = "%s"' % metric) query.groups = ['name'] query.orders = ['name'] response = query.Execute() data = {'rows': response['rows']} self.RenderJson(data)
def post(self): """Request handler for POST operations.""" current_owner_email = None try: dashboard_id = http_util.GetIntegerParam(self.request, fields.ID) row = dashboard_model.Dashboard.GetDashboard(dashboard_id) if (not row.canEdit()): msg = ('You are not an owner or writer for this dashboard, ' 'and cannot modify it. Contact {owner} for access.'. format(owner=row.created_by.email())) self.RenderJson(data={error_fields.MESSAGE: msg}, status=403) return data = http_util.GetJsonParam(self.request, fields.DATA) title = dashboard_model.Dashboard.GetDashboardTitle(data) if fields.OWNER in data: current_owner_email = data[fields.OWNER] try: new_owner = dashboard_model.Dashboard.GetDashboardOwner( current_owner_email) except users.UserNotFoundError: new_owner = users.get_current_user() data[fields.OWNER] = new_owner.email() warning = ( 'The user {current} does not exist. Owner set to {new}.'. format(current=current_owner_email, new=new_owner.email())) logging.error(warning) data[error_fields.WARNINGS] = [warning] if 'writers' not in data: data['writers'] = [] new_writers = data.get('writers') writers_changed = row.writersChanged(data.get('writers')) owners_changed = (data[fields.OWNER] != new_owner.email()) if owners_changed or writers_changed: if (not row.isOwner()): msg = ('You are not an owner of this dashboard, and cannot' 'transfer ownership. Contact {owner} for ' 'reassignment if this is in error.'.format( owner=row.created_by.email())) self.RenderJson(data={error_fields.MESSAGE: msg}, status=403) return if owners_changed: data[fields.OWNER] = new_owner.email() if writers_changed: row.writers = [writer['email'] for writer in new_writers] row.data = json.dumps(data) row.title = title row.owner = new_owner row.modified_by = users.get_current_user() row.put() self.RenderJson(data) except (base.InitializeError, dashboard_model.InitializeError, http_util.ParameterError) as err: self.RenderJson(data={error_fields.MESSAGE: err.message}, status=400)