Exemplo n.º 1
0
    def get(self):
        import pipeline  # Break circular dependency
        if pipeline._ENFORCE_AUTH:
            if not users.is_current_user_admin():
                self.response.out.write('Forbidden')
                self.response.set_status(403)
                return

        # XSRF protection
        if (not pipeline._DEBUG
                and self.request.headers.get('X-Requested-With') !=
                'XMLHttpRequest'):
            self.response.out.write('Request missing X-Requested-With header')
            self.response.set_status(403)
            return

        self.json_response = {}
        try:
            self.handle()
            output = simplejson.dumps(self.json_response)
        except Exception, e:
            self.json_response.clear()
            self.json_response['error_class'] = e.__class__.__name__
            self.json_response['error_message'] = str(e)
            self.json_response['error_traceback'] = traceback.format_exc()
            output = simplejson.dumps(self.json_response)
Exemplo n.º 2
0
  def get(self):
    import pipeline  # Break circular dependency
    if pipeline._ENFORCE_AUTH:
      if not users.is_current_user_admin():
        self.response.out.write('Forbidden')
        self.response.set_status(403)
        return

    # XSRF protection
    if (not pipeline._DEBUG and
        self.request.headers.get('X-Requested-With') != 'XMLHttpRequest'):
      self.response.out.write('Request missing X-Requested-With header')
      self.response.set_status(403)
      return

    self.json_response = {}
    try:
      self.handle()
      output = simplejson.dumps(self.json_response)
    except Exception, e:
      self.json_response.clear()
      self.json_response['error_class'] = e.__class__.__name__
      self.json_response['error_message'] = str(e)
      self.json_response['error_traceback'] = traceback.format_exc()
      output = simplejson.dumps(self.json_response)
Exemplo n.º 3
0
  def to_json_str(self):
    """Convert data to json string representation.

    Returns:
      json representation as string.
    """
    return simplejson.dumps(self.to_json(), sort_keys=True)
Exemplo n.º 4
0
    def to_json_str(self):
        """Convert data to json string representation.

    Returns:
      json representation as string.
    """
        return simplejson.dumps(self.to_json(), sort_keys=True)
Exemplo n.º 5
0
  def to_json(self):
    """Serialize KeyRange to json.

    Returns:
      string with KeyRange json representation.
    """
    if simplejson is None:
      raise SimplejsonUnavailableError(
          "JSON functionality requires simplejson to be available")

    def key_to_str(key):
      if key:
        return str(key)
      else:
        return None

    obj_dict = {
        "direction": self.direction,
        "key_start": key_to_str(self.key_start),
        "key_end": key_to_str(self.key_end),
        "include_start": self.include_start,
        "include_end": self.include_end,
        "namespace": self.namespace,
        }
    if self._app:
      obj_dict["_app"] = self._app

    return simplejson.dumps(obj_dict, sort_keys=True)
Exemplo n.º 6
0
    def to_json(self):
        """Serialize KeyRange to json.

    Returns:
      string with KeyRange json representation.
    """
        if simplejson is None:
            raise SimplejsonUnavailableError(
                "JSON functionality requires simplejson to be available")

        def key_to_str(key):
            if key:
                return str(key)
            else:
                return None

        obj_dict = {
            "direction": self.direction,
            "key_start": key_to_str(self.key_start),
            "key_end": key_to_str(self.key_end),
            "include_start": self.include_start,
            "include_end": self.include_end,
            "namespace": self.namespace,
        }
        if self._app:
            obj_dict["_app"] = self._app

        return simplejson.dumps(obj_dict, sort_keys=True)
Exemplo n.º 7
0
class JsonHandler(BaseHandler):
  """Base class for JSON handlers for user interface.

  Sub-classes should implement the 'handle' method. They should put their
  response data in the 'self.json_response' dictionary. Any exceptions raised
  by the sub-class implementation will be sent in a JSON response with the
  name of the error_class and the error_message.
  """

  def __init__(self, *args):
    """Initializer."""
    super(BaseHandler, self).__init__(*args)
    self.json_response = {}

  def base_path(self):
    """Base path for all mapreduce-related urls.

    JSON handlers are mapped to /base_path/command/command_name thus they
    require special treatment.
    """
    path = self.request.path
    base_path = path[:path.rfind("/")]
    if not base_path.endswith("/command"):
      raise BadRequestPathError(
          "Json handlers should have /command path prefix")
    return base_path[:base_path.rfind("/")]

  def _handle_wrapper(self):
    if self.request.headers.get("X-Requested-With") != "XMLHttpRequest":
      logging.error(self.request.headers)
      logging.error("Got JSON request with no X-Requested-With header")
      self.response.set_status(
          403, message="Got JSON request with no X-Requested-With header")
      return

    self.json_response.clear()
    try:
      self.handle()
    except errors.MissingYamlError:
      logging.debug("Could not find 'mapreduce.yaml' file.")
      self.json_response.clear()
      self.json_response["error_class"] = "Notice"
      self.json_response["error_message"] = "Could not find 'mapreduce.yaml'"
    except Exception, e:
      logging.exception("Error in JsonHandler, returning exception.")
      # TODO(user): Include full traceback here for the end-user.
      self.json_response.clear()
      self.json_response["error_class"] = e.__class__.__name__
      self.json_response["error_message"] = str(e)

    self.response.headers["Content-Type"] = "text/javascript"
    try:
      output = simplejson.dumps(self.json_response)
    except:
      logging.exception("Could not serialize to JSON")
      self.response.set_status(500, message="Could not serialize to JSON")
      return
    else:
      self.response.out.write(output)
Exemplo n.º 8
0
 def schedule_mapreduce(state, mapper_input_readers, eta, countdown):
   state.save()
   readers_json = [reader.to_json_str() for reader in mapper_input_readers]
   taskqueue.Task(
       url=base_path + "/kickoffjob_callback",
       params={"mapreduce_spec": state.mapreduce_spec.to_json_str(),
               "input_readers": simplejson.dumps(readers_json)},
       eta=eta, countdown=countdown).add(queue_name, transactional=True)
Exemplo n.º 9
0
 def schedule_mapreduce(state, mapper_input_readers, eta, countdown):
   state.put()
   readers_json = [reader.to_json_str() for reader in mapper_input_readers]
   taskqueue.Task(
       url=base_path + "/kickoffjob_callback",
       params={"mapreduce_spec": state.mapreduce_spec.to_json_str(),
               "input_readers": simplejson.dumps(readers_json)},
       eta=eta, countdown=countdown).add(queue_name, transactional=True)
Exemplo n.º 10
0
  def to_json_str(self):
    """Convert data to json string representation.

    Returns:
      json representation as string.
    """
    json = self.to_json()
    try:
      return simplejson.dumps(json, sort_keys=True, cls=JsonEncoder)
    except:
      logging.exception("Could not serialize JSON: %r", json)
      raise
Exemplo n.º 11
0
    def to_json_str(self):
        """Convert data to json string representation.

    Returns:
      json representation as string.
    """
        json = self.to_json()
        try:
            return simplejson.dumps(json, sort_keys=True)
        except:
            logging.exception("Could not serialize JSON: %r", json)
            raise
Exemplo n.º 12
0
  def get_value_for_datastore(self, model_instance):
    """Gets a value to store in the Datastore.

    Args:
      model_instance: instance of the model class.

    Returns:
      datastore-compatible value.
    """
    value = super(JsonProperty, self).get_value_for_datastore(model_instance)
    if value is None:
      return None
    return db.Text(simplejson.dumps(value, sort_keys=True))
Exemplo n.º 13
0
  def get_value_for_datastore(self, model_instance):
    """Gets value for datastore.

    Args:
      model_instance: instance of the model class.

    Returns:
      datastore-compatible value.
    """
    value = super(JsonProperty, self).get_value_for_datastore(model_instance)
    if not value:
      return None
    return datastore_types.Text(simplejson.dumps(
        value.to_json(), sort_keys=True))
Exemplo n.º 14
0
  def process_data(description, stats_data):
    def sorted_unique_list_from_itr(i):
      return natsort.natsorted(set(itertools.chain.from_iterable(i)))

    dimensions = sorted_unique_list_from_itr(
        (i.dimensions for i in line.values.buckets) for line in stats_data)

    table = _stats_data_to_summary(stats_data)
    # TODO(maruel): 'dimensions' should be updated when the user changes the
    # resolution at which the data is displayed.
    return {
      'dimensions': simplejson.dumps(dimensions),
      'initial_data': gviz_api.DataTable(description, table).ToJSon(
          columns_order=_Summary.ORDER),
    }
Exemplo n.º 15
0
    def process_data(description, stats_data):
        def sorted_unique_list_from_itr(i):
            return natsort.natsorted(set(itertools.chain.from_iterable(i)))

        dimensions = sorted_unique_list_from_itr(
            (i.dimensions for i in line.values.buckets) for line in stats_data)

        table = _stats_data_to_summary(stats_data)
        # TODO(maruel): 'dimensions' should be updated when the user changes the
        # resolution at which the data is displayed.
        return {
            'dimensions':
            simplejson.dumps(dimensions),
            'initial_data':
            gviz_api.DataTable(description,
                               table).ToJSon(columns_order=_Summary.ORDER),
        }
Exemplo n.º 16
0
class JsonHandler(BaseHandler):
    """Base class for JSON handlers for user interface.

  Sub-classes should implement the 'handle' method. They should put their
  response data in the 'self.json_response' dictionary. Any exceptions raised
  by the sub-class implementation will be sent in a JSON response with the
  name of the error_class and the error_message.
  """
    def __init__(self):
        """Initializer."""
        super(BaseHandler, self).__init__()
        self.json_response = {}

    def get(self):
        self.post()

    def post(self):
        self.json_response.clear()
        try:
            self.handle()
        except Exception, e:
            logging.exception("Error in JsonHandler, returning exception.")
            # TODO(user): Include full traceback here for the end-user.
            self.json_response.clear()
            self.json_response["error_class"] = e.__class__.__name__
            self.json_response["error_message"] = str(e)

        self.response.headers["Content-Type"] = "text/javascript"
        self.response.headers['Access-Control-Allow-Origin'] = '*'
        try:
            output = simplejson.dumps(self.json_response)
        except:
            logging.exception("Could not serialize to JSON")
            self.response.set_status(500,
                                     message="Could not serialize to JSON")
            return
        else:
            self.response.out.write(output)