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")
18 class DocumentStorage(object):
19 def __init__(self, path):
20 self.vstorage = vstorage.VersionedStorage(path)
22 def get(self, name, revision=None):
23 text, rev = self.vstorage.page_text(name, revision)
24 return Document(self, name=name, text=text, revision=rev)
26 def get_by_tag(self, name, tag):
27 text, rev = self.vstorage.page_text_by_tag(name, tag)
28 return Document(self, name=name, text=text, revision=rev)
30 def get_or_404(self, *args, **kwargs):
32 return self.get(*args, **kwargs)
33 except DocumentNotFound:
36 def put(self, document, author, comment, parent):
37 self.vstorage.save_text(
46 def create_document(self, id, text, title=None):
53 document = Document(self, name=id, text=text, title=title)
54 return self.put(document, u"<wiki>", u"Document created.", None)
56 def delete(self, name, author, comment):
57 self.vstorage.delete_page(name, author, comment)
60 return list(self.vstorage.all_pages())
62 def history(self, title):
63 return list(self.vstorage.page_history(title))
65 def _info(self, name):
66 return self.vstorage.page_meta(name)
69 class Document(object):
70 META_REGEX = re.compile(r'\s*<!--\s(.*?)-->', re.DOTALL | re.MULTILINE)
72 def __init__(self, storage, **kwargs):
73 self.storage = storage
74 for attr, value in kwargs.iteritems():
75 setattr(self, attr, value)
77 def add_tag(self, tag, revision, author):
78 """ Add document specific tag """
79 logger.debug("Adding tag %s to doc %s version %d", tag, self.name, revision)
80 self.storage.vstorage.add_page_tag(self.name, revision, tag, user=author)
84 return re.sub(self.META_REGEX, '', self.text, 1)
89 m = re.match(self.META_REGEX, self.text)
91 for line in m.group(1).split('\n'):
93 k, v = line.split(':', 1)
94 result[k.strip()] = v.strip()
98 gallery = result.get('gallery', self.name.replace(' ', '_'))
100 if gallery.startswith('/'):
101 gallery = os.path.basename(gallery)
103 result['gallery'] = gallery
105 if 'title' not in result:
106 result['title'] = self.name.title()
112 ('revision', 'last_update', 'last_comitter', 'commit_message'),
113 self.storage._info(self.name),
118 return DocumentStorage(settings.REPOSITORY_PATH)