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])
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)
30 # Where to cache searchable stuff from metadata?
31 # Probably in some kind of search index.
34 verbose_name = _('document')
35 verbose_name_plural = _('documents')
38 return render_to_string('catalogue/book_list/book.html', {'book': self})
41 from lxml import etree
44 data = self.materialize()
45 data = data.replace(u'\ufeff', '')
46 # This is bad. The editor shouldn't spew unknown HTML entities.
47 data = data.replace(u' ', u'\u00a0')
50 t = etree.fromstring(data)
52 return {'title': '<<Resource invalid>>'}
53 header = t.find('.//header')
55 header = etree.fromstring(data).find('.//{http://nowoczesnapolska.org.pl/sst#}header')
56 metadata['title'] = getattr(header, 'text', ' ') or ' '
57 # print 'meta', d['title']
59 m = t.find('metadata')
61 m = t.find('{http://nowoczesnapolska.org.pl/sst#}metadata')
63 c = m.find('{http://purl.org/dc/elements/1.1/}relation.coverimage.url')
65 metadata['cover_url'] = c.text
66 c = m.find('{http://purl.org/dc/elements/1.1/}audience')
68 metadata['audience'] = c.text
72 def can_edit(self, user):
74 return self.owner_user == user
76 return self.owner_organization.is_member(user)
78 def set_stage(self, stage):
80 plan = self.get_plan()
82 self.assigned_to = plan.user
84 self.assigned_to = None
89 plan = self.plan_set.get(stage=self.stage)
90 except (ObjectDoesNotExist, MultipleObjectsReturned):
95 plan = self.get_plan()
96 return plan is not None and plan.deadline and plan.deadline < date.today()