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 import forms
7 from django.core.exceptions import ValidationError
8 from django.utils.translation import ugettext_lazy as _
10 from catalogue.constants import STAGES
11 from catalogue.models import Category
12 from catalogue.models.document import metadata_from_text
13 from librarian.document import Document
16 class DocumentTextSaveForm(forms.Form):
18 Form for saving document's text:
20 * parent_revision - revision which the modified text originated from.
21 * comment - user's verbose comment; will be used in commit.
22 * stage - change to this stage
26 parent_revision = forms.IntegerField(widget=forms.HiddenInput, required=False)
27 text = forms.CharField(widget=forms.HiddenInput)
29 author_name = forms.CharField(
32 help_text=_(u"Your name"),
35 author_email = forms.EmailField(
37 label=_(u"Author's email"),
38 help_text=_(u"Your email address, so we can show a gravatar :)"),
41 comment = forms.CharField(
43 widget=forms.Textarea,
44 label=_(u"Your comments"),
45 help_text=_(u"Describe changes you made."),
48 stage = forms.ChoiceField(
52 help_text=_(u"If completed a work stage, change to another one."),
55 def __init__(self, *args, **kwargs):
56 user = kwargs.pop('user')
57 super(DocumentTextSaveForm, self).__init__(*args, **kwargs)
58 if user and user.is_authenticated():
59 self.fields['author_name'].required = False
60 self.fields['author_email'].required = False
63 text = self.cleaned_data['text']
65 doc = Document.from_string(text)
66 except ValueError as e:
67 raise ValidationError(e.message)
69 from librarian import SSTNS, DCNS
70 root_elem = doc.edoc.getroot()
71 if len(root_elem) < 1 or root_elem[0].tag != SSTNS('metadata'):
72 raise ValidationError("The first tag in section should be metadata")
73 if len(root_elem) < 2 or root_elem[1].tag != SSTNS('header'):
74 raise ValidationError("The first tag after metadata should be header")
76 if not getattr(header, 'text', None) or not header.text.strip():
77 raise ValidationError(
78 "The first header should contain the title in plain text (no links, emphasis etc.) and cannot be empty")
80 cover_url = doc.meta.get_one(DCNS('relation.coverimage.url'))
82 ext = cover_url.rsplit('.', 1)[-1].lower()
83 if ext not in ('jpg', 'jpeg', 'png', 'gif', 'svg'):
84 raise ValidationError('Invalid cover image format, should be an image file (jpg, png, gif, svg). '
85 'Change it in Metadata.')
86 metadata = metadata_from_text(text)
87 if 'multiple_values' in metadata:
88 raise ValidationError('Category "%s" does not allow multiple values.' % metadata['multiple_values'])
89 for category in Category.objects.all():
90 values = metadata.get(category.dc_tag)
93 if not category.multiple:
98 if not category.tag_set.filter(dc_value=value):
99 raise ValidationError('Invalid value for dc:%s: %s' % (category.dc_tag, value))
103 class DocumentTextRevertForm(forms.Form):
105 Form for reverting document's text:
107 * revision - revision to revert to.
108 * comment - user's verbose comment; will be used in commit.
112 revision = forms.IntegerField(widget=forms.HiddenInput)
114 author_name = forms.CharField(
117 help_text=_(u"Your name"),
120 author_email = forms.EmailField(
122 label=_(u"Author's email"),
123 help_text=_(u"Your email address, so we can show a gravatar :)"),
126 comment = forms.CharField(
128 widget=forms.Textarea,
129 label=_(u"Your comments"),
130 help_text=_(u"Describe the reason for reverting."),
134 class DocumentTextPublishForm(forms.Form):
135 revision = forms.IntegerField(widget=forms.HiddenInput)