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.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
11 from django.db import models
12 from django.template.loader import render_to_string
13 from django.utils.translation import ugettext_lazy as _
14 from dvcs.models import Ref
15 from organizations.models import Organization
16 from catalogue.constants import STAGES
21 """ An editable chunk of text."""
23 owner_user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True)
24 owner_organization = models.ForeignKey(Organization, null=True)
25 stage = models.CharField(_('stage'), max_length=128, blank=True, default=STAGES[0][0])
26 assigned_to = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, related_name='assignments')
27 deleted = models.BooleanField(default=False)
28 tags = models.ManyToManyField(Tag, blank=True)
29 # we need to know if it were ever published (for notifications)
30 published = models.BooleanField(default=False)
32 # Where to cache searchable stuff from metadata?
33 # Probably in some kind of search index.
36 verbose_name = _('document')
37 verbose_name_plural = _('documents')
40 return render_to_string('catalogue/book_list/book.html', {'book': self})
43 from lxml import etree
46 data = self.materialize()
47 data = data.replace(u'\ufeff', '')
48 # This is bad. The editor shouldn't spew unknown HTML entities.
49 data = data.replace(u' ', u'\u00a0')
52 t = etree.fromstring(data)
54 return {'title': '<<Resource invalid>>'}
55 header = t.find('.//header')
57 header = etree.fromstring(data).find('.//{http://nowoczesnapolska.org.pl/sst#}header')
58 metadata['title'] = getattr(header, 'text', ' ') or ' '
59 # print 'meta', d['title']
61 m = t.find('metadata')
63 m = t.find('{http://nowoczesnapolska.org.pl/sst#}metadata')
65 c = m.find('{http://purl.org/dc/elements/1.1/}relation.coverimage.url')
67 metadata['cover_url'] = c.text
68 c = m.find('{http://purl.org/dc/elements/1.1/}audience')
70 metadata['audience'] = c.text
74 def can_edit(self, user):
76 return self.owner_user == user
78 return self.owner_organization.is_member(user)
80 def set_stage(self, stage):
82 plan = self.get_plan()
84 self.assigned_to = plan.user
86 self.assigned_to = None
91 plan = self.plan_set.get(stage=self.stage)
92 except (ObjectDoesNotExist, MultipleObjectsReturned):
97 plan = self.get_plan()
98 return plan is not None and plan.deadline and plan.deadline < date.today()