1 # -*- coding: utf-8 -*-
3 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
9 from vstorage import DocumentNotFound
10 from wiki import settings
12 from django.http import Http404
15 logger = logging.getLogger("fnp.wiki")
17 class DocumentStorage(object):
18 def __init__(self, path):
19 self.vstorage = vstorage.VersionedStorage(path)
21 def get(self, name, revision = None):
23 text = self.vstorage.page_text(name)
25 text = self.vstorage.revision_text(name, revision)
26 return Document(self, name = name, text = text)
28 def get_or_404(self, *args, **kwargs):
30 return self.get(*args, **kwargs)
31 except DocumentNotFound:
34 def put(self, document, author, comment, parent):
35 self.vstorage.save_text(
36 title = document.name,
42 def delete(self, name, author, comment):
43 self.vstorage.delete_page(name, author, comment)
46 return list(self.vstorage.all_pages())
48 def history(self, title):
49 return list(self.vstorage.page_history(title))
51 def _info(self, name):
52 return self.vstorage.page_meta(name)
55 class Document(object):
56 META_REGEX = re.compile(r'\s*<!--\s(.*?)-->', re.DOTALL | re.MULTILINE)
58 def __init__(self, storage, **kwargs):
59 self.storage = storage
60 for attr, value in kwargs.iteritems():
61 setattr(self, attr, value)
65 return self.storage._info(self.name)[0]
66 except DocumentNotFound:
69 def add_tag(self, tag):
70 """ Add document specific tag """
71 logger.debug("Adding tag %s to doc %s version %d", tag, self.name, self.revision)
72 self.storage.vstorage.add_page_tag(self.name, self.revision, tag)
76 return re.sub(self.META_REGEX, '', self.text, 1)
81 m = re.match(self.META_REGEX, self.text)
83 for line in m.group(1).split('\n'):
85 k, v = line.split(':', 1)
86 result[k.strip()] = v.strip()
90 gallery = result.get('gallery', self.name.replace(' ', '_'))
92 if gallery.startswith('/'):
93 gallery = os.path.basename(gallery)
95 result['gallery'] = gallery
97 if 'title' not in result:
98 result['title'] = self.name.title()
104 ('revision', 'last_update', 'last_comitter', 'commit_message'),
105 self.storage._info(self.name)
109 return DocumentStorage(settings.REPOSITORY_PATH)