Coding style overhaul for Python files (PEP8 conformant). Removed buggy csstidy pytho...
[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         if revision is None:
24             text = self.vstorage.page_text(name)
25         else:
26             text = self.vstorage.revision_text(name, revision)
27         return Document(self, name=name, text=text)
28
29     def get_or_404(self, *args, **kwargs):
30         try:
31             return self.get(*args, **kwargs)
32         except DocumentNotFound:
33             raise Http404
34
35     def put(self, document, author, comment, parent):
36         self.vstorage.save_text(
37                 title=document.name,
38                 text=document.text,
39                 author=author,
40                 comment=comment,
41                 parent=parent)
42
43     def delete(self, name, author, comment):
44         self.vstorage.delete_page(name, author, comment)
45
46     def all(self):
47         return list(self.vstorage.all_pages())
48
49     def history(self, title):
50         return list(self.vstorage.page_history(title))
51
52     def _info(self, name):
53         return self.vstorage.page_meta(name)
54
55
56 class Document(object):
57     META_REGEX = re.compile(r'\s*<!--\s(.*?)-->', re.DOTALL | re.MULTILINE)
58
59     def __init__(self, storage, **kwargs):
60         self.storage = storage
61         for attr, value in kwargs.iteritems():
62             setattr(self, attr, value)
63
64     def revision(self):
65         try:
66             return self.storage._info(self.name)[0]
67         except DocumentNotFound:
68             return - 1
69
70     def add_tag(self, tag):
71         """ Add document specific tag """
72         logger.debug("Adding tag %s to doc %s version %d", tag, self.name, self.revision)
73         self.storage.vstorage.add_page_tag(self.name, self.revision, tag)
74
75     @property
76     def plain_text(self):
77         return re.sub(self.META_REGEX, '', self.text, 1)
78
79     def meta(self):
80         result = {}
81
82         m = re.match(self.META_REGEX, self.text)
83         if m:
84             for line in m.group(1).split('\n'):
85                 try:
86                     k, v = line.split(':', 1)
87                     result[k.strip()] = v.strip()
88                 except ValueError:
89                     continue
90
91         gallery = result.get('gallery', self.name.replace(' ', '_'))
92
93         if gallery.startswith('/'):
94             gallery = os.path.basename(gallery)
95
96         result['gallery'] = gallery
97
98         if 'title' not in result:
99             result['title'] = self.name.title()
100
101         return result
102
103     def info(self):
104         return dict(zip(
105             ('revision', 'last_update', 'last_comitter', 'commit_message'),
106             self.storage._info(self.name),
107         ))
108
109
110 def getstorage():
111     return DocumentStorage(settings.REPOSITORY_PATH)