Make protection messages more user friendly
[cds-indico.git] / indico / modules / attachments / forms.py
blobddb854b97bbdc12b31a16251c75d15320e95fad5
1 # This file is part of Indico.
2 # Copyright (C) 2002 - 2015 European Organization for Nuclear Research (CERN).
4 # Indico is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 3 of the
7 # License, or (at your option) any later version.
9 # Indico is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with Indico; if not, see <http://www.gnu.org/licenses/>.
17 from __future__ import unicode_literals
19 from wtforms.ext.sqlalchemy.fields import QuerySelectField
20 from wtforms.ext.dateutil.fields import DateField
21 from wtforms.fields import BooleanField, TextAreaField
22 from wtforms.fields.html5 import URLField
23 from wtforms.fields.simple import StringField
24 from wtforms.validators import DataRequired, Optional
26 from indico.core.db import db
27 from indico.core.db.sqlalchemy.protection import ProtectionMode
28 from indico.modules.attachments.models.folders import AttachmentFolder
29 from indico.util.i18n import _
30 from indico.web.forms.base import IndicoForm, generated_data
31 from indico.web.forms.fields import PrincipalListField, IndicoSelectMultipleCheckboxField
32 from indico.web.forms.validators import UsedIf
33 from indico.web.forms.widgets import SwitchWidget
36 class AttachmentFormBase(IndicoForm):
37 protected = BooleanField(_("Protected"), widget=SwitchWidget())
38 folder = QuerySelectField(_("Folder"), allow_blank=True, blank_text=_("No folder selected"), get_label='title',
39 description=_("Adding materials to folders allow grouping and easier permission "
40 "management."))
42 def __init__(self, *args, **kwargs):
43 linked_object = kwargs.pop('linked_object')
44 super(AttachmentFormBase, self).__init__(*args, **kwargs)
45 self.folder.query = (AttachmentFolder
46 .find(linked_object=linked_object, is_default=False, is_deleted=False)
47 .order_by(db.func.lower(AttachmentFolder.title)))
49 @generated_data
50 def protection_mode(self):
51 return ProtectionMode.protected if self.protected.data else ProtectionMode.inheriting
54 class EditAttachmentFormBase(AttachmentFormBase):
55 title = StringField(_("Title"), [DataRequired()])
56 description = TextAreaField(_("Description"))
57 acl = PrincipalListField(_("Grant Access To"), [UsedIf(lambda form, field: form.protected.data)],
58 groups=True, serializable=False, allow_external=True,
59 description=_("The list of users and groups with access to the material"))
62 class AddAttachmentFilesForm(AttachmentFormBase):
63 pass
66 class EditAttachmentFileForm(EditAttachmentFormBase):
67 pass
70 class AttachmentLinkFormMixin(object):
71 title = StringField(_("Title"), [DataRequired()])
72 link_url = URLField(_("URL"), [DataRequired()])
75 class AddAttachmentLinkForm(AttachmentLinkFormMixin, AttachmentFormBase):
76 pass
79 class EditAttachmentLinkForm(AttachmentLinkFormMixin, EditAttachmentFormBase):
80 pass
83 class AttachmentFolderForm(IndicoForm):
84 title = StringField(_("Name"), [DataRequired()], description=_("The name of the folder."))
85 description = TextAreaField(_("Description"), description=_("Description of the folder and its content"))
86 protected = BooleanField(_("Protected"), widget=SwitchWidget())
87 acl = PrincipalListField(_("Grant Access To"), [UsedIf(lambda form, field: form.protected.data)],
88 groups=True, serializable=False, allow_external=True,
89 description=_("The list of users and groups with access to the folder"))
90 is_always_visible = BooleanField(_("Always Visible"), widget=SwitchWidget(),
91 description=_("By default, folders are always visible, even if a user cannot "
92 "access them. You can disable this behavior here, hiding the folder "
93 "for anyone who does not have permission to access it."))
95 @generated_data
96 def protection_mode(self):
97 return ProtectionMode.protected if self.protected.data else ProtectionMode.inheriting
100 class AttachmentPackageForm(IndicoForm):
101 added_since = DateField(_('Added Since'), [Optional()], parse_kwargs={'dayfirst': True},
102 description=_('Include only attachments uploaded after this date'))
103 sessions = IndicoSelectMultipleCheckboxField(_('Sessions'),
104 description=_('Include attachments from selected sessions'))
105 contributions = IndicoSelectMultipleCheckboxField(_('Contributions'),
106 description=_('Include attachments from selected contributions'))