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