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.
6 from django.db import models
10 from vstorage import DocumentNotFound
11 from wiki import settings, constants
12 from slughifi import slughifi
13 from django.utils.translation import ugettext_lazy as _
15 from django.http import Http404
18 logger = logging.getLogger("fnp.wiki")
21 # _PCHARS_DICT = dict(zip((ord(x) for x in u"ĄĆĘŁŃÓŚŻŹąćęłńóśżź "), u"ACELNOSZZacelnoszz_"))
22 _PCHARS_DICT = dict(zip((ord(x) for x in u" "), u"_"))
24 # I know this is barbaric, but I didn't find a better solution ;(
26 parts = name.translate(_PCHARS_DICT).split('__')
29 def join_name(*parts, **kwargs):
30 name = u'__'.join(p.translate(_PCHARS_DICT) for p in parts)
31 logger.info("JOIN %r -> %r", parts, name)
34 def normalize_name(name):
36 >>> normalize_name("gąska".decode('utf-8'))
39 return unicode(name).translate(_PCHARS_DICT)
41 STAGE_TAGS_RE = re.compile(r'^#stage-finished: (.*)$', re.MULTILINE)
44 class DocumentStorage(object):
45 def __init__(self, path):
46 self.vstorage = vstorage.VersionedStorage(path)
48 def get(self, name, revision=None):
49 text, rev = self.vstorage.page_text(name, revision)
50 return Document(self, name=name, text=text, revision=rev)
52 def get_by_tag(self, name, tag):
53 text, rev = self.vstorage.page_text_by_tag(name, tag)
54 return Document(self, name=name, text=text, revision=rev)
56 def revert(self, name, revision):
57 text, rev = self.vstorage.revert(name, revision)
58 return Document(self, name=name, text=text, revision=rev)
60 def get_or_404(self, *args, **kwargs):
62 return self.get(*args, **kwargs)
63 except DocumentNotFound:
66 def put(self, document, author, comment, parent=None):
67 self.vstorage.save_text(
76 def create_document(self, text, name):
77 title = u', '.join(p.title() for p in split_name(name))
82 document = Document(self, name=name, text=text, title=title)
83 return self.put(document, u"<wiki>", u"Document created.")
85 def delete(self, name, author, comment):
86 self.vstorage.delete_page(name, author, comment)
89 return list(self.vstorage.all_pages())
91 def history(self, title):
92 def stage_desc(match):
93 stage = match.group(1)
94 return _("Finished stage: %s") % constants.DOCUMENT_STAGES_DICT[stage]
96 for changeset in self.vstorage.page_history(title):
97 changeset['description'] = STAGE_TAGS_RE.sub(stage_desc, changeset['description'])
100 def doc_meta(self, title, revision=None):
101 return self.vstorage.page_meta(title, revision)
105 class Document(object):
106 META_REGEX = re.compile(r'\s*<!--\s(.*?)-->', re.DOTALL | re.MULTILINE)
108 def __init__(self, storage, **kwargs):
109 self.storage = storage
110 for attr, value in kwargs.iteritems():
111 setattr(self, attr, value)
113 def add_tag(self, tag, revision, author):
114 """ Add document specific tag """
115 logger.debug("Adding tag %s to doc %s version %d", tag, self.name, revision)
116 self.storage.vstorage.add_page_tag(self.name, revision, tag, user=author)
119 def plain_text(self):
120 return re.sub(self.META_REGEX, '', self.text, 1)
125 m = re.match(self.META_REGEX, self.text)
127 for line in m.group(1).split('\n'):
129 k, v = line.split(':', 1)
130 result[k.strip()] = v.strip()
134 gallery = result.get('gallery', slughifi(self.name.replace(' ', '_')))
136 if gallery.startswith('/'):
137 gallery = os.path.basename(gallery)
139 result['gallery'] = gallery
143 return self.storage.vstorage.page_meta(self.name, self.revision)
146 return DocumentStorage(settings.REPOSITORY_PATH)
152 class Theme(models.Model):
153 name = models.CharField(_('name'), max_length=50, unique=True)
157 verbose_name = _('theme')
158 verbose_name_plural = _('themes')
160 def __unicode__(self):
164 return "Theme(name=%r)" % self.name