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, **commit_args):
57 self.vstorage.revert(name, revision, **commit_args)
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'])
99 def doc_meta(self, title, revision=None):
100 return self.vstorage.page_meta(title, revision)
104 class Document(object):
105 META_REGEX = re.compile(r'\s*<!--\s(.*?)-->', re.DOTALL | re.MULTILINE)
107 def __init__(self, storage, **kwargs):
108 self.storage = storage
109 for attr, value in kwargs.iteritems():
110 setattr(self, attr, value)
112 def add_tag(self, tag, revision, author):
113 """ Add document specific tag """
114 logger.debug("Adding tag %s to doc %s version %d", tag, self.name, revision)
115 self.storage.vstorage.add_page_tag(self.name, revision, tag, user=author)
118 def plain_text(self):
119 return re.sub(self.META_REGEX, '', self.text, 1)
124 m = re.match(self.META_REGEX, self.text)
126 for line in m.group(1).split('\n'):
128 k, v = line.split(':', 1)
129 result[k.strip()] = v.strip()
133 gallery = result.get('gallery', slughifi(self.name.replace(' ', '_')))
135 if gallery.startswith('/'):
136 gallery = os.path.basename(gallery)
138 result['gallery'] = gallery
142 return self.storage.vstorage.page_meta(self.name, self.revision)
145 return DocumentStorage(settings.REPOSITORY_PATH)
151 class Theme(models.Model):
152 name = models.CharField(_('name'), max_length=50, unique=True)
156 verbose_name = _('theme')
157 verbose_name_plural = _('themes')
159 def __unicode__(self):
163 return "Theme(name=%r)" % self.name