예제 #1
0
    def ac(self, **params):
        """Autocomplete API.

        """
        field = params['field']
        term = params['term']
        colls = params['colls'].split(",")
        # FIXME - allow restricting search to a set of collections

        if field == '*':
            return jsonresp([])

        fieldname, fieldtype = field.rsplit('_', 1)
        fieldconfigs = SearchCollection.config['types'] \
            .get('record', {}) \
            .get('fields', {})

        if '*' in colls:
            target = SearchCollection.all()
        else:
            target = SearchCollection.field.coll.is_or_is_descendant(colls)

        return jsonresp(
            field_item_types[fieldtype]
            .autocomplete(target, fieldconfigs,
                          fieldname, term)
        )
예제 #2
0
    def mediainfo(self, path="", mimepattern=None, hidden=0, exactpath=0):
        """Return information on the media at a given path.

        `path` is treated as a path prefix: information on all files in the
        specified directory which begin with the specified prefix is returned.

        Returned information is:
         - `path`: The value of path supplied (useful to tie the response to a
           request in an async environment).
         - `sep`: The path separator to use.
         - `dirpath`: The path of the directory holding `path`, with a
           separator at the end.
         - `parent`: The path of the parent directory of this path.
         - `dirs`: A list of the directories in dirpath with the appropriate
           prefix.  Entries in this are dicts containing:
            - `name`: The filename.
            - `mtime`: The modification time of the file
            - `hidden`: True iff the file is hidden.
         - `files`: A list of the files in dirpath with the appropriate
           prefix.  Entries in this are dicts containing:
            - `name`: The filename.
            - `type`: The type of the file.
            - `mtime`: The modification time of the file
            - `hidden`: True iff the file is hidden.

        If the path which would be contained in `dirpath` is not a directory,
        this will instead return a 404 error.

        """
        if exactpath:
            hidden = False
        if mimepattern:
            mimepattern = mimepattern.split("/", 1)
            if len(mimepattern) == 1 or mimepattern[1] == "*":
                mimepattern = [mimepattern[0], None]

        tmp = path.rsplit("/", 1)
        if len(tmp) == 1:
            tmp = (tmp[0], "")
        dirpath, fileprefix = tmp
        parentpath = dirpath.rsplit("/", 1)
        if len(parentpath) == 1:
            parentpath = ""
        else:
            parentpath = parentpath[0]
        if not parentpath.endswith("/"):
            parentpath += "/"
        if not dirpath.endswith("/"):
            dirpath += "/"

        files = []
        dirs = []
        if mapper.isdir(dirpath):
            user = get_user()
            user.set_info("media_lastdir", dirpath)
            user.objects.flush()
            for name in mapper.listdir(dirpath):
                if exactpath:
                    if name != fileprefix:
                        continue
                else:
                    if not name.startswith(fileprefix):
                        continue
                subpath = "/".join((dirpath.rstrip("/"), name))
                if not hidden and mapper.is_hidden(subpath):
                    continue
                if mapper.isdir(subpath):
                    dirs.append(mapper.get_dir_info(subpath))
                elif mapper.isfile(subpath):
                    info = mapper.get_file_info(subpath)
                    if mimepattern is not None:
                        infomime = info["mimetype"]
                        if infomime is None:
                            continue
                        infomime = infomime.split("/", 1)
                        if len(infomime) == 1:
                            infomime = [infomime[0], None]
                        if infomime[0] != mimepattern[0]:
                            continue
                        if mimepattern[1] is not None and infomime[1] != mimepattern[1]:
                            continue
                    files.append(info)
            dirs.sort(key=lambda x: x.get("name"))
            files.sort(key=lambda x: x.get("name"))

        response = dict(path=path, sep="/", dirpath=dirpath, parent=parentpath, dirs=dirs, files=files)

        return jsonresp(response)