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 django.utils.translation import ugettext_lazy as _
14 from django.http import Http404
17 logger = logging.getLogger("fnp.wiki")
20 # _PCHARS_DICT = dict(zip((ord(x) for x in u"ĄĆĘŁŃÓŚŻŹąćęłńóśżź "), u"ACELNOSZZacelnoszz_"))
21 _PCHARS_DICT = dict(zip((ord(x) for x in u" "), u"_"))
23 # I know this is barbaric, but I didn't find a better solution ;(
25 parts = name.translate(_PCHARS_DICT).split('__')
28 def join_name(*parts, **kwargs):
29 name = u'__'.join(p.translate(_PCHARS_DICT) for p in parts)
30 logger.info("JOIN %r -> %r", parts, name)
33 def normalize_name(name):
35 >>> normalize_name("gąska".decode('utf-8'))
38 return unicode(name).translate(_PCHARS_DICT)
40 STAGE_TAGS_RE = re.compile(r'^#stage-finished: (.*)$', re.MULTILINE)
43 class DocumentStorage(object):
44 def __init__(self, path):
45 self.vstorage = vstorage.VersionedStorage(path)
47 def get(self, name, revision=None):
48 text, rev = self.vstorage.page_text(name, revision)
49 return Document(self, name=name, text=text, revision=rev)
51 def get_by_tag(self, name, tag):
52 text, rev = self.vstorage.page_text_by_tag(name, tag)
53 return Document(self, name=name, text=text, revision=rev)
55 def revert(self, name, revision):
56 text, rev = self.vstorage.revert(name, revision)
57 return Document(self, name=name, text=text, revision=rev)
59 def get_or_404(self, *args, **kwargs):
61 return self.get(*args, **kwargs)
62 except DocumentNotFound:
65 def put(self, document, author, comment, parent=None):
66 self.vstorage.save_text(
75 def create_document(self, text, name):
76 title = u', '.join(p.title() for p in split_name(name))
81 document = Document(self, name=name, text=text, title=title)
82 return self.put(document, u"<wiki>", u"Document created.")
84 def delete(self, name, author, comment):
85 self.vstorage.delete_page(name, author, comment)
88 return list(self.vstorage.all_pages())
90 def history(self, title):
91 def stage_desc(match):
92 stage = match.group(1)
93 return _("Finished stage: %s") % constants.DOCUMENT_STAGES_DICT[stage]
95 for changeset in self.vstorage.page_history(title):
96 changeset['description'] = STAGE_TAGS_RE.sub(stage_desc, changeset['description'])
101 class Document(object):
102 META_REGEX = re.compile(r'\s*<!--\s(.*?)-->', re.DOTALL | re.MULTILINE)
104 def __init__(self, storage, **kwargs):
105 self.storage = storage
106 for attr, value in kwargs.iteritems():
107 setattr(self, attr, value)
109 def add_tag(self, tag, revision, author):
110 """ Add document specific tag """
111 logger.debug("Adding tag %s to doc %s version %d", tag, self.name, revision)
112 self.storage.vstorage.add_page_tag(self.name, revision, tag, user=author)
115 def plain_text(self):
116 return re.sub(self.META_REGEX, '', self.text, 1)
121 m = re.match(self.META_REGEX, self.text)
123 for line in m.group(1).split('\n'):
125 k, v = line.split(':', 1)
126 result[k.strip()] = v.strip()
130 gallery = result.get('gallery', self.name.replace(' ', '_'))
132 if gallery.startswith('/'):
133 gallery = os.path.basename(gallery)
135 result['gallery'] = gallery
139 return self.storage.vstorage.page_meta(self.name, self.revision)
142 return DocumentStorage(settings.REPOSITORY_PATH)
148 class Theme(models.Model):
149 name = models.CharField(_('name'), max_length=50, unique=True)
153 verbose_name = _('theme')
154 verbose_name_plural = _('themes')
156 def __unicode__(self):
160 return "Theme(name=%r)" % self.name