* Readonly document view.
[redakcja.git] / apps / wiki / models.py
1 # -*- coding: utf-8 -*-
2 #
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.
5 #
6 import re
7 import os
8 import vstorage
9 from vstorage import DocumentNotFound
10 from wiki import settings
11
12 from django.http import Http404
13
14 import logging
15 logger = logging.getLogger("fnp.wiki")
16
17
18 class DocumentStorage(object):
19     def __init__(self, path):
20         self.vstorage = vstorage.VersionedStorage(path)
21
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)
25
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)
29
30     def get_or_404(self, *args, **kwargs):
31         try:
32             return self.get(*args, **kwargs)
33         except DocumentNotFound:
34             raise Http404
35
36     def put(self, document, author, comment, parent):
37         self.vstorage.save_text(
38                 title=document.name,
39                 text=document.text,
40                 author=author,
41                 comment=comment,
42                 parent=parent)
43
44         return document
45
46     def create_document(self, id, text, title=None):
47         if title is None:
48             title = id.title()
49
50         if text is None:
51             text = u''
52
53         document = Document(self, name=id, text=text, title=title)
54         return self.put(document, u"<wiki>", u"Document created.", None)
55
56     def delete(self, name, author, comment):
57         self.vstorage.delete_page(name, author, comment)
58
59     def all(self):
60         return list(self.vstorage.all_pages())
61
62     def history(self, title):
63         return list(self.vstorage.page_history(title))
64
65
66 class Document(object):
67     META_REGEX = re.compile(r'\s*<!--\s(.*?)-->', re.DOTALL | re.MULTILINE)
68
69     def __init__(self, storage, **kwargs):
70         self.storage = storage
71         for attr, value in kwargs.iteritems():
72             setattr(self, attr, value)
73
74     def add_tag(self, tag, revision, author):
75         """ Add document specific tag """
76         logger.debug("Adding tag %s to doc %s version %d", tag, self.name, revision)
77         self.storage.vstorage.add_page_tag(self.name, revision, tag, user=author)
78
79     @property
80     def plain_text(self):
81         return re.sub(self.META_REGEX, '', self.text, 1)
82
83     def meta(self):
84         result = {}
85
86         m = re.match(self.META_REGEX, self.text)
87         if m:
88             for line in m.group(1).split('\n'):
89                 try:
90                     k, v = line.split(':', 1)
91                     result[k.strip()] = v.strip()
92                 except ValueError:
93                     continue
94
95         gallery = result.get('gallery', self.name.replace(' ', '_'))
96
97         if gallery.startswith('/'):
98             gallery = os.path.basename(gallery)
99
100         result['gallery'] = gallery
101
102         if 'title' not in result:
103             result['title'] = self.name.title()
104
105         return result
106
107     def info(self):
108         return self.storage.vstorage.page_meta(self.name, self.revision)
109
110
111 def getstorage():
112     return DocumentStorage(settings.REPOSITORY_PATH)