Browse Source

get_versions method added

master
Eugen Ciur 2 years ago
parent
commit
e341100e69
  1. 6
      changelog.md
  2. 25
      mglib/storage.py
  3. 46
      test/test_storage.py

6
changelog.md

@ -1,6 +1,12 @@
# Changelog
## [1.3.4] - 14 December 2020
### Changed
- mglib.storage.get_versions(self, doc_path) method added
## [1.3.3] - 14 December 2020
### Changed

25
mglib/storage.py

@ -35,6 +35,31 @@ class Storage:
exist_ok=True
)
def get_versions(self, doc_path):
"""
Returns a list of (all) ordered versions
of specific doc_path. Versions
start with 0. Examples of return values:
- [0, 1, 2, 3] = 4 versions of the document
- [ 0 ] = only one version (original)
To count versions it just counts number of subfolders
in specific document folder. Versions are
stored in subfolders named v1, v2, v3, ...
"""
abs_dirname_docs = self.path(
doc_path.dirname_docs
)
only_dirs = [
fi for fi in listdir(abs_dirname_docs) if isdir(
join(abs_dirname_docs, fi)
)
]
dirs_count = len(only_dirs)
return list(range(0, dirs_count + 1))
def get_pagecount(self, doc_path):
"""
Returns total number of pages for this doc_path.

46
test/test_storage.py

@ -41,3 +41,49 @@ class TestStorage(unittest.TestCase):
f1.exists()
)
def test_get_versions_1(self):
storage = FileSystemStorage(location=MEDIA_ROOT)
with TemporaryNode(MEDIA_ROOT) as media_root:
docs = media_root.add_folder("docs")
res = media_root.add_folder("results")
f1 = docs.add_folder("user_1/document_2")
f1.add_file("doku.pdf")
# simulate 2 versions of the document.
f1.add_folder("v1")
f1.add_folder("v2")
res.add_folder("user_1/document_2/pages")
doc_path = DocumentPath(
user_id=1,
document_id=2,
file_name='doku.pdf',
version=2
)
versions = storage.get_versions(doc_path)
self.assertEqual(
versions, [0, 1, 2]
)
def test_get_versions_2(self):
storage = FileSystemStorage(location=MEDIA_ROOT)
with TemporaryNode(MEDIA_ROOT) as media_root:
docs = media_root.add_folder("docs")
f1 = docs.add_folder("user_1/document_2")
f1.add_file("doku.pdf")
doc_path = DocumentPath(
user_id=1,
document_id=2,
file_name='doku.pdf',
version=2
)
versions = storage.get_versions(doc_path)
# document has only one version - the latest
self.assertEqual(
versions, [0]
)

Loading…
Cancel
Save