1 # -*- coding: utf-8 -*-
3 # This file is part of MIL/PEER, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 from __future__ import unicode_literals
8 from datetime import date
9 from django.conf import settings
10 from django.db import models
11 from django.template.loader import render_to_string
12 from django.utils.translation import ugettext_lazy as _
13 from dvcs.models import Ref
14 from organizations.models import Organization
15 from catalogue.constants import STAGES
20 """ An editable chunk of text."""
22 owner_user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True)
23 owner_organization = models.ForeignKey(Organization, null=True)
24 stage = models.CharField(_('stage'), max_length=128, blank=True, default=STAGES[0])
25 assigned_to = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, related_name='assignments')
26 deleted = models.BooleanField(default=False)
27 tags = models.ManyToManyField(Tag)
29 # Where to cache searchable stuff from metadata?
30 # Probably in some kind of search index.
33 verbose_name = _('document')
34 verbose_name_plural = _('documents')
37 return render_to_string('catalogue/book_list/book.html', {'book': self})
40 from lxml import etree
43 data = self.materialize()
44 data = data.replace(u'\ufeff', '')
45 # This is bad. The editor shouldn't spew unknown HTML entities.
46 data = data.replace(u' ', u'\u00a0')
49 t = etree.fromstring(data)
51 return {'title': '<<Resource invalid>>'}
52 header = t.find('.//header')
54 header = etree.fromstring(data).find('.//{http://nowoczesnapolska.org.pl/sst#}header')
55 metadata['title'] = getattr(header, 'text', ' ') or ' '
56 #print 'meta', d['title']
58 m = t.find('metadata')
60 m = t.find('{http://nowoczesnapolska.org.pl/sst#}metadata')
62 c = m.find('{http://purl.org/dc/elements/1.1/}relation.coverimage.url')
64 metadata['cover_url'] = c.text
65 c = m.find('{http://purl.org/dc/elements/1.1/}audience')
67 metadata['audience'] = c.text
71 def can_edit(self, user):
73 return self.owner_user == user
75 return self.owner_organization.is_member(user)
77 def set_stage(self, stage):
79 plan = self.get_plan()
81 self.assigned_to = plan.user
83 self.assigned_to = None
88 plan = self.plan_set.get(stage=self.stage)
94 plan = self.get_plan()
95 return plan is not None and plan.deadline and plan.deadline < date.today()