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.encoding import force_unicode
14 from django.utils.translation import ugettext_lazy as _
15 from dvcs.models import Ref
16 from organizations.models import Organization
17 from catalogue.constants import STAGES
18 from .tag import Tag, Category
21 def metadata_from_text(text):
22 from lxml import etree
24 text = text.replace(u'\ufeff', '')
25 # This is bad. The editor shouldn't spew unknown HTML entities.
26 text = text.replace(u' ', u'\u00a0')
29 t = etree.fromstring(text)
31 return {'title': '<<Resource invalid>>'}
32 header = t.find('.//header')
34 header = etree.fromstring(text).find('.//{http://nowoczesnapolska.org.pl/sst#}header')
35 metadata['title'] = getattr(header, 'text', ' ') or ' '
36 # print 'meta', d['title']
38 m = t.find('metadata')
40 m = t.find('{http://nowoczesnapolska.org.pl/sst#}metadata')
42 c = m.find('{http://purl.org/dc/elements/1.1/}relation.coverimage.url')
44 metadata['cover_url'] = c.text
45 for category in Category.objects.all():
46 for elem in m.findall('{http://purl.org/dc/elements/1.1/}' + category.dc_tag):
47 if elem.text is not None:
49 if category.dc_tag not in metadata:
50 metadata[category.dc_tag] = []
51 metadata[category.dc_tag].append(elem.text)
53 if category.dc_tag in metadata:
54 metadata['multiple_values'] = category.dc_tag
55 metadata[category.dc_tag] = elem.text
60 """ An editable chunk of text."""
62 owner_user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True)
63 owner_organization = models.ForeignKey(Organization, null=True)
64 stage = models.CharField(_('stage'), max_length=128, blank=True, default=STAGES[0][0])
65 assigned_to = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, related_name='assignments')
66 deleted = models.BooleanField(default=False)
67 tags = models.ManyToManyField(Tag, blank=True)
68 # we need to know if it were ever published (for notifications)
69 published = models.BooleanField(default=False)
71 # Where to cache searchable stuff from metadata?
72 # Probably in some kind of search index.
75 verbose_name = _('document')
76 verbose_name_plural = _('documents')
77 ordering = ['-revision__created_at']
80 return render_to_string('catalogue/book_list/book.html', {'book': self})
83 return metadata_from_text(self.materialize())
85 def can_edit(self, user):
89 return self.owner_user == user
91 return self.owner_organization.is_member(user)
93 def set_stage(self, stage):
95 plan = self.get_plan()
97 self.assigned_to = plan.user
99 self.assigned_to = None
102 def stage_name(self):
103 return force_unicode(dict(STAGES)[self.stage]) if self.stage else None
107 plan = self.plan_set.get(stage=self.stage)
108 except (ObjectDoesNotExist, MultipleObjectsReturned):
112 def is_overdue(self):
113 plan = self.get_plan()
114 return plan is not None and plan.deadline and plan.deadline < date.today()
116 def commit(self, *args, **kwargs):
117 super(Document, self).commit(*args, **kwargs)
119 for category in Category.objects.all():
120 values = m.get(category.dc_tag)
121 if not category.multiple:
125 tags = category.tag_set.filter(dc_value__in=values)
126 category.set_tags_for(self, tags)