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
7 from django.utils.translation import ugettext_lazy as _
9 from dvcs import models as dvcs_models
13 logger = logging.getLogger("fnp.wiki")
16 class Book(models.Model):
17 """ A document edited on the wiki """
19 slug = models.SlugField(_('slug'), max_length=255, unique=True)
20 title = models.CharField(_('displayed title'), max_length=255, blank=True)
21 doc = models.ForeignKey(dvcs_models.Document, editable=False)
22 gallery = models.CharField(_('scan gallery name'), max_length=255, blank=True)
26 verbose_name = _('book')
27 verbose_name_plural = _('books')
29 def __unicode__(self):
33 def create(cls, creator=None, text=u'', *args, **kwargs):
34 instance = cls(*args, **kwargs)
35 instance.doc = dvcs_models.Document.create(creator=creator, text=text)
40 def listener_create(sender, instance, created, **kwargs):
41 if created and instance.doc is None:
42 instance.doc = dvcs_models.Document.objects.create()
45 models.signals.post_save.connect(Book.listener_create, sender=Book)
50 from wiki import settings, constants
51 from slughifi import slughifi
53 from django.http import Http404
58 class Document(object):
60 def add_tag(self, tag, revision, author):
61 """ Add document specific tag """
62 logger.debug("Adding tag %s to doc %s version %d", tag, self.name, revision)
63 self.storage.vstorage.add_page_tag(self.name, revision, tag, user=author)
67 return re.sub(self.META_REGEX, '', self.text, 1)
72 m = re.match(self.META_REGEX, self.text)
74 for line in m.group(1).split('\n'):
76 k, v = line.split(':', 1)
77 result[k.strip()] = v.strip()
81 gallery = result.get('gallery', slughifi(self.name.replace(' ', '_')))
83 if gallery.startswith('/'):
84 gallery = os.path.basename(gallery)
86 result['gallery'] = gallery
90 return self.storage.vstorage.page_meta(self.name, self.revision)
95 class Theme(models.Model):
96 name = models.CharField(_('name'), max_length=50, unique=True)
100 verbose_name = _('theme')
101 verbose_name_plural = _('themes')
103 def __unicode__(self):
107 return "Theme(name=%r)" % self.name