Skip to content
This repository has been archived by the owner on Jul 14, 2020. It is now read-only.

Add versions() method #67

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified MANIFEST.in
100755 → 100644
Empty file.
Empty file modified debian/rules
100755 → 100644
Empty file.
27 changes: 27 additions & 0 deletions mongokit/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,33 @@ def get_version(self, filename, version=-1, **kwargs):
except StopIteration:
raise NoFile("no version %d for filename %r" % (version, filename))

def versions(self, filename, **kwargs):
"""Return number of version for specified filename
This is a rapid hack, which as not been add to tests
TODO : Ajouter le test de version

class Doc(Document):
structure = {
'title':unicode,
}
gridfs = {'files': ['source']}
con = Connection()
col = con['test']['mongokit']
con.register([Doc])
doc = col.Doc()
doc['title'] = u'Hello'
doc.fs.source = "Hello World !"
doc.fs.versions() --> 1 attendu
doc.fs.source = "Hello World 2 !"
doc.fs.versions() --> 2 attendu
"""
# This is took from pymongo source. We need to go a little deeper here
self._GridFS__files.ensure_index([("filename", ASCENDING),
("uploadDate", DESCENDING)])
########## Begin of MongoKit hack ##########
return self._GridFS__files.find(self._get_spec(filename=filename, **kwargs)).count()
########## end of MongoKit hack ############

class FSContainer(FS):
def __init__(self, container_name, obj):
self._container_name = container_name
Expand Down
25 changes: 24 additions & 1 deletion tests/test_gridfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,27 @@ class Doc(Document):
new_id = doc.fs.get_last_version("source")._id
doc.fs.delete(new_id)
assert doc.fs.source == 'Hello World', doc.fs.source


def test_gridfs_versions(self):

class Doc(Document):
structure = {
'title':unicode,
}
gridfs = {'files': ['source']}
self.connection.register([Doc])
doc = self.col.Doc()
doc['title'] = u'Hello'
doc.save()

# Version 1
doc.fs.source = "Hello World !"
assert doc.fs.source == "Hello World !"
self.assertEqual(doc.fs.versions("source"), 1)

# Version 2
doc.fs.source = "Salut !"
assert doc.fs.source == "Salut !"

self.assertEqual(doc.fs.versions("source"), 2)