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, constants
11 from django.utils.translation import ugettext_lazy as _
13 from django.http import Http404
16 logger = logging.getLogger("fnp.wiki")
19 STAGE_TAGS_RE = re.compile(r'^#stage-finished: (.*)$', re.MULTILINE)
22 class DocumentStorage(object):
23 def __init__(self, path):
24 self.vstorage = vstorage.VersionedStorage(path)
26 def get(self, name, revision=None):
27 text, rev = self.vstorage.page_text(name, revision)
28 return Document(self, name=name, text=text, revision=rev)
30 def get_by_tag(self, name, tag):
31 text, rev = self.vstorage.page_text_by_tag(name, tag)
32 return Document(self, name=name, text=text, revision=rev)
34 def get_or_404(self, *args, **kwargs):
36 return self.get(*args, **kwargs)
37 except DocumentNotFound:
40 def put(self, document, author, comment, parent):
41 self.vstorage.save_text(
50 def create_document(self, id, text, title=None):
57 document = Document(self, name=id, text=text, title=title)
58 return self.put(document, u"<wiki>", u"Document created.", None)
60 def delete(self, name, author, comment):
61 self.vstorage.delete_page(name, author, comment)
64 return list(self.vstorage.all_pages())
66 def history(self, title):
67 def stage_desc(match):
68 stage = match.group(1)
69 return _("Finished stage: %s") % constants.DOCUMENT_STAGES_DICT[stage]
71 for changeset in self.vstorage.page_history(title):
72 changeset['description'] = STAGE_TAGS_RE.sub(stage_desc, changeset['description'])
77 class Document(object):
78 META_REGEX = re.compile(r'\s*<!--\s(.*?)-->', re.DOTALL | re.MULTILINE)
80 def __init__(self, storage, **kwargs):
81 self.storage = storage
82 for attr, value in kwargs.iteritems():
83 setattr(self, attr, value)
85 def add_tag(self, tag, revision, author):
86 """ Add document specific tag """
87 logger.debug("Adding tag %s to doc %s version %d", tag, self.name, revision)
88 self.storage.vstorage.add_page_tag(self.name, revision, tag, user=author)
92 return re.sub(self.META_REGEX, '', self.text, 1)
97 m = re.match(self.META_REGEX, self.text)
99 for line in m.group(1).split('\n'):
101 k, v = line.split(':', 1)
102 result[k.strip()] = v.strip()
106 gallery = result.get('gallery', self.name.replace(' ', '_'))
108 if gallery.startswith('/'):
109 gallery = os.path.basename(gallery)
111 result['gallery'] = gallery
113 if 'title' not in result:
114 result['title'] = self.name.title()
119 return self.storage.vstorage.page_meta(self.name, self.revision)
123 return DocumentStorage(settings.REPOSITORY_PATH)