Ejemplo n.º 1
0
 def set_or_create(self, model, id):
   u = current_user(required=True)
   if model == "users":
     if not (id == "me" or id == "" or id == u):
       raise AppError("Id must be the current " +
           "user_id or me. User {} tried to modify user {}.".format(u,id))
     id = u
     cls = users
   else:
     cls = type(model.lower(), (ScopedExpando,), {})
   data = parse_body(self)
   id = parse_id(id, data.get("Id"))
   clean_data(data)
   validate(cls.__name__, data)
   if id and model != "users":
     old_model = cls.get_by_id(id)
     if old_model and not old_model.can_write(u):
       raise AppError("You do not have sufficient privileges.")
   m = reflective_create(cls, data)
   if id:
     m.key = ndb.Key(model, id)
   if model != "users":
     if len(m.owners) == 0:
       m.owners.append(u)
   m.put()
   # update indexes
   search.put(m)
   redirect = self.request.get("redirect")
   if redirect:
     self.redirect(redirect)
     raise BreakError()
     return
   return m.to_dict()
Ejemplo n.º 2
0
 def _post_put_hook(self, future):
   future.wait()
   search.put(self)
   if _config.METADATA and self._previous is None:
     counter.increment(self.__class__.__name__)
   if _config.post_put_hook:
     _config.post_put_hook(self)
  def set_or_create(self, model, id, parent_key=None):
    model = model.lower()
    u = current_user(required=True)
    if model == "users":
      if not (id == "me" or id == "" or id == u):
        raise AppError("Id must be the current " +
                       "user_id or me. User {} tried to modify user {}.".format(u, id))
      id = u
      cls = users
    else:
      cls = None
      if _config.DEFINED_MODELS:
        cls = _config.DEFINED_MODELS.get(model)
        if not cls and _config.RESTRICT_TO_DEFINED_MODELS:
          raise RestrictedModelError
        if cls:
          model = cls.__name__
      if not cls:
        validate_modelname(model)
        cls = type(model, (ScopedExpando,), {})
    data = parse_body(self)
    key = parse_id(id, model, data.get("Id"))
    clean_data(data)
    validate(cls.__name__, data)
    already_exists = False
    if key:
      old_model = key.get()
      if old_model:
        if model != "users" and not old_model.can_write(u):
          raise AppError("You do not have sufficient privileges.")
        already_exists = True

    # TODO: might want to add this post creation since you already have the key
    if parent_key:
      data[parent_key.kind()] = parent_key.urlsafe()

    m = reflective_create(cls, data)
    if key:
      m.key = key
    if model != "users":
      if len(m.owners) == 0:
        m.owners.append(u)
    m.put()
    # increment count
    if not already_exists and _config.METADATA:
      counter.increment(model)
    # update indexes
    search.put(m)
    redirect = self.request.get("redirect")
    if redirect:
      self.redirect(redirect)
      # Raising break error to avoid header and body writes from @as_json decorator since we override as a redirect
      raise BreakError()
    return m.to_dict()
Ejemplo n.º 4
0
  def set_or_create(self, model, id, parent_model=None, parent_id=None):
    u = current_user(required=True)
    if model == "users":
      if not (id == "me" or id == "" or id == u):
        raise AppError("Id must be the current " +
            "user_id or me. User {} tried to modify user {}.".format(u,id))
      id = u
      cls = users
    else:
      cls = type(model.lower(), (ScopedExpando,), {})
    data = parse_body(self)
    id = parse_id(id, data.get("Id"))
    clean_data(data)
    validate(cls.__name__, data)
    if id and model != "users":
      old_model = cls.get_by_id(id)
      if old_model and not old_model.can_write(u):
        raise AppError("You do not have sufficient privileges.")

    if parent_model and parent_id:
      # generated IDs are LONGs of we store them as floats we mis precision for later reference
      # therefore we force the datatype to be a STRING
      data[format_as_model_field(parent_model)] = format_as_model_reference(parent_id)

    m = reflective_create(cls, data)
    if id:
      m.key = ndb.Key(model, id)
    if model != "users":
      if len(m.owners) == 0:
        m.owners.append(u)
    m.put()
    # update indexes
    search.put(m)
    redirect = self.request.get("redirect")
    if redirect:
      self.redirect(redirect)
      # raise BreakError() # why is this here ??
      return
    return m.to_dict()