Esempio n. 1
0
  def __init__(self, key=None, *args, **kwargs):
    if not key:
      raise KeyError("You need to supply a key to the File model!")

    self._content = None
    self._is_directory = key.endswith("/")
    Document.__init__(self, key=key, *args, **kwargs)
Esempio n. 2
0
    def __init__(self, key=None, *args, **kwargs):
        if not key:
            raise KeyError("You need to supply a key to the File model!")

        self._content = None
        self._is_directory = key.endswith("/")
        Document.__init__(self, key=key, *args, **kwargs)
Esempio n. 3
0
    def delete(self, *args, **kwargs):
        """ Deletes from the file system too.

    If you delete a directory, be careful that you don't use any file object
    that's under that directory. That would cause a lot of errors.
    """
        # This is for moving only. In that we already removed that path.
        db_only = kwargs.pop("db_only", False)
        fspath = self.fspath
        if not db_only and not os.path.exists(fspath):
            try:
                Document.delete(self, *args, **kwargs)
            except:
                pass

            raise NotFoundError("{} not found!".format(fspath))

        if self.is_directory:
            base_dir = os.path.join(File.FILES_FOLDER, self.project.key)
            l = len(base_dir)
            for root, subdirs, filenames in os.walk(fspath, topdown=False):
                for fname in filenames:
                    p = os.path.join(root, fname)
                    p = p[l:]
                    key = File.keygen(self.project, p)
                    File.get(key).delete(db_only=db_only)

                if root != fspath:
                    p = root[
                        l:] + "/"  # os walk does not have the trailing slash
                    key = File.keygen(self.project, p)
                    File.get(key).delete(db_only=db_only)

            if not db_only:
                os.rmdir(fspath)  # suppose to fail if it is not empty.
        else:
            if not db_only:
                os.unlink(fspath)

        return Document.delete(self, *args, **kwargs)
Esempio n. 4
0
  def delete(self, *args, **kwargs):
    """ Deletes from the file system too.

    If you delete a directory, be careful that you don't use any file object
    that's under that directory. That would cause a lot of errors.
    """
    # This is for moving only. In that we already removed that path.
    db_only = kwargs.pop("db_only", False)
    fspath = self.fspath
    if not db_only and not os.path.exists(fspath):
      try:
        Document.delete(self, *args, **kwargs)
      except:
        pass

      raise NotFoundError("{} not found!".format(fspath))

    if self.is_directory:
      base_dir = os.path.join(File.FILES_FOLDER, self.project.key)
      l = len(base_dir)
      for root, subdirs, filenames in os.walk(fspath, topdown=False):
        for fname in filenames:
          p = os.path.join(root, fname)
          p = p[l:]
          key = File.keygen(self.project, p)
          File.get(key).delete(db_only=db_only)

        if root != fspath:
          p = root[l:] + "/" # os walk does not have the trailing slash
          key = File.keygen(self.project, p)
          File.get(key).delete(db_only=db_only)

      if not db_only:
        os.rmdir(fspath) # suppose to fail if it is not empty.
    else:
      if not db_only:
        os.unlink(fspath)

    return Document.delete(self, *args, **kwargs)
Esempio n. 5
0
  def save(self, *args, **kwargs):
    self.date = datetime.now()
    fspath = self.fspath

    if not self._ensure_base_dir_exists(fspath):
      raise NotFoundError("Base dir is not found for {}".format(fspath))

    if not os.path.exists(fspath):
      # We have to do this.. Should PROBABLY move this to new_project
      # TODO: move this to new project
      safe_mkdirs(self.base_dir)

      if self.is_directory:
        safe_mkdirs(fspath)
      else:
        # TODO: we need to worry about race conditions here as well.
        if self._content:
          self._content.save(fspath)

    return Document.save(self, *args, **kwargs)
Esempio n. 6
0
    def save(self, *args, **kwargs):
        self.date = datetime.now()
        fspath = self.fspath

        if not self._ensure_base_dir_exists(fspath):
            raise NotFoundError("Base dir is not found for {}".format(fspath))

        if not os.path.exists(fspath):
            # We have to do this.. Should PROBABLY move this to new_project
            # TODO: move this to new project
            safe_mkdirs(self.base_dir)

            if self.is_directory:
                safe_mkdirs(fspath)
            else:
                # TODO: we need to worry about race conditions here as well.
                if self._content:
                    self._content.save(fspath)

        return Document.save(self, *args, **kwargs)
Esempio n. 7
0
    def delete(self, *args, **kwargs):
        for comment in Comment.index("parent", self.key):
            comment.delete()

        return Document.delete(self, *args, **kwargs)
Esempio n. 8
0
  def delete(self, *args, **kwargs):
    for comment in Comment.index("parent", self.key):
      comment.delete()

    return Document.delete(self, *args, **kwargs)
Esempio n. 9
0
  def delete(self, *args, **kwargs):
    """Overriden as we need to delete the children"""
    for comment in Comment.index("parent", self.key):
      comment.delete()

    return Document.delete(self, *args, **kwargs)