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 for category in Category.objects.all():
88 values = metadata.get(category.dc_tag)
89 if not category.multiple:
94 if not category.tag_set.filter(dc_value=value):
95 raise ValidationError('Invalid value for dc:%s: %s' % (category.dc_tag, value))
99 class DocumentTextRevertForm(forms.Form):
101 Form for reverting document's text:
103 * revision - revision to revert to.
104 * comment - user's verbose comment; will be used in commit.
108 revision = forms.IntegerField(widget=forms.HiddenInput)
110 author_name = forms.CharField(
113 help_text=_(u"Your name"),
116 author_email = forms.EmailField(
118 label=_(u"Author's email"),
119 help_text=_(u"Your email address, so we can show a gravatar :)"),
122 comment = forms.CharField(
124 widget=forms.Textarea,
125 label=_(u"Your comments"),
126 help_text=_(u"Describe the reason for reverting."),
130 class DocumentTextPublishForm(forms.Form):
131 revision = forms.IntegerField(widget=forms.HiddenInput)