Ejemplo n.º 1
0
    def get_last_version(self, filename):
        """Get a file from GridFS by ``"filename"``.

        Returns the most recently uploaded file in GridFS with the
        name `filename` as an instance of
        :class:`~gridfs.grid_file.GridOut`. Raises
        :class:`~gridfs.errors.NoFile` if no such file exists.

        An index on ``{filename: 1, uploadDate: -1}`` will
        automatically be created when this method is called the first
        time.

        :Parameters:
          - `filename`: ``"filename"`` of the file to get

        .. versionadded:: 1.6
        """
        self.__files.ensure_index(
            filter.sort(ASCENDING("filename") + DESCENDING("uploadDate")))

        doc = yield self.__files.find_one({"filename": filename},
                                          filter=filter.sort(
                                              DESCENDING("uploadDate")))
        if doc is None:
            raise NoFile("TxMongo: no file in gridfs with filename {0}".format(
                repr(filename)))

        defer.returnValue(GridOut(self.__collection, doc))
Ejemplo n.º 2
0
    def get_last_version(self, filename):
        """Get a file from GridFS by ``"filename"``.

        Returns the most recently uploaded file in GridFS with the
        name `filename` as an instance of
        :class:`~gridfs.grid_file.GridOut`. Raises
        :class:`~gridfs.errors.NoFile` if no such file exists.

        An index on ``{filename: 1, uploadDate: -1}`` will
        automatically be created when this method is called the first
        time.

        :Parameters:
          - `filename`: ``"filename"`` of the file to get

        .. versionadded:: 1.6
        """
        self.__files.ensure_index(filter.sort(ASCENDING("filename") + \
                                   DESCENDING("uploadDate")))

        d = self.__files.find({"filename": filename},
                                  filter=filter.sort(DESCENDING('uploadDate')))
        d.addCallback(self._cb_get_last_version, filename)
        return d
Ejemplo n.º 3
0
    def get_version(self, filename=None, version=-1):
        """Get a file from GridFS by ``"filename"``.
        Returns a version of the file in GridFS whose filename matches
        `filename` and whose metadata fields match the supplied keyword
        arguments, as an instance of :class:`~gridfs.grid_file.GridOut`.
        Version numbering is a convenience atop the GridFS API provided
        by MongoDB. If more than one file matches the query (either by
        `filename` alone, by metadata fields, or by a combination of
        both), then version ``-1`` will be the most recently uploaded
        matching file, ``-2`` the second most recently
        uploaded, etc. Version ``0`` will be the first version
        uploaded, ``1`` the second version, etc. So if three versions
        have been uploaded, then version ``0`` is the same as version
        ``-3``, version ``1`` is the same as version ``-2``, and
        version ``2`` is the same as version ``-1``. Note that searching by
        random (unindexed) meta data is not supported here.
        Raises :class:`~gridfs.errors.NoFile` if no such version of
        that file exists.
        :Parameters:
          - `filename`: ``"filename"`` of the file to get, or `None`
          - `version` (optional): version of the file to get (defaults
            to -1, the most recent version uploaded)
        """
        query = {"filename": filename}
        skip = abs(version)
        if version < 0:
            skip -= 1
            myorder = DESCENDING("uploadDate")
        else:
            myorder = ASCENDING("uploadDate")

        def ok(cursor):
            if cursor:
                return GridOut(self.__collection, cursor[0])

            raise NoFile("no version %d for filename %r" % (version, filename))

        return self.__files.find(query, filter=filter.sort(myorder), limit=1, skip=skip)\
            .addCallback(ok)
Ejemplo n.º 4
0
    def query(self, filter, limit=None):
        """
        Return matching results.
        """
        if limit == 0:
            return succeed([])

        # The txmongo API differs from pymongo with regard to sorting.
        # To sort results when making a query using txmongo, a query
        # filter needs to be created and passed to collection.find().
        sort_filter = orderby(DESCENDING('sort$timestamp'))

        # Do not include the '_id' and 'sort$timestamp' fields in the
        # results as these are not part of the original document.
        find_args = dict(filter=sort_filter,
                         fields={
                             '_id': False,
                             'sort$timestamp': False
                         })
        if limit:
            find_args['limit'] = limit

        return self.collection.find(filter, **find_args)
Ejemplo n.º 5
0
def sortdesc(field):
    return mongosort(DESCENDING(field))