Add "material package" to the attachment module
[cds-indico.git] / indico / modules / attachments / forms.py
blob251852e8da64a322bb573e94b0b9a0a5fea8e308
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 description=_("By default, the materials will inherit the protection of the parent. "
39 "Checking this field will restrict all access. The protection can be "
40 "modified later on from the material settings."))
42 folder = QuerySelectField(_("Folder"), allow_blank=True, blank_text=_("No folder selected"), get_label='title',
43 description=_("Adding materials to folders allow grouping and easier permission "
44 "management."))
46 def __init__(self, *args, **kwargs):
47 linked_object = kwargs.pop('linked_object')
48 super(AttachmentFormBase, self).__init__(*args, **kwargs)
49 self.folder.query = (AttachmentFolder
50 .find(linked_object=linked_object, is_default=False, is_deleted=False)
51 .order_by(db.func.lower(AttachmentFolder.title)))
53 @generated_data
54 def protection_mode(self):
55 return ProtectionMode.protected if self.protected.data else ProtectionMode.inheriting
58 class EditAttachmentFormBase(AttachmentFormBase):
59 title = StringField(_("Title"), [DataRequired()], description=_("The title of the file."))
60 description = TextAreaField(_("Description"))
61 acl = PrincipalListField(_("Grant Access To"), [UsedIf(lambda form, field: form.protected.data)],
62 groups=True, serializable=False, allow_external=True,
63 description=_("The list of users and groups with access to the material"))
66 class AddAttachmentFilesForm(AttachmentFormBase):
67 pass
70 class EditAttachmentFileForm(EditAttachmentFormBase):
71 pass
74 class AttachmentLinkForm(EditAttachmentFormBase):
75 link_url = URLField(_("URL"), [DataRequired()])
77 def __init__(self, *args, **kwargs):
78 super(AttachmentLinkForm, self).__init__(*args, **kwargs)
79 self.title.description = _("The title of the link")
82 class AttachmentFolderForm(IndicoForm):
83 title = StringField(_("Name"), [DataRequired()], description=_("The name of the folder."))
84 description = TextAreaField(_("Description"), description=_("Description of the folder and its content"))
85 protected = BooleanField(_("Protected"), widget=SwitchWidget(),
86 description=_("By default, the folder will inherit the protection of the event. "
87 "Checking this field will restrict all access. The protection can be "
88 "modified later on from the folder settings."))
89 acl = PrincipalListField(_("Grant Access To"), [UsedIf(lambda form, field: form.protected.data)],
90 groups=True, serializable=False, allow_external=True,
91 description=_("The list of users and groups with access to the folder"))
92 is_always_visible = BooleanField(_("Always Visible"), widget=SwitchWidget(),
93 description=_("By default, folders are always visible, even if a user cannot "
94 "access them. You can disable this behavior here, hiding the folder "
95 "for anyone who does not have permission to access it."))
97 @generated_data
98 def protection_mode(self):
99 return ProtectionMode.protected if self.protected.data else ProtectionMode.inheriting
102 class MaterialsPackageForm(IndicoForm):
104 added_since = DateField(_('Added Since'), [Optional()], parse_kwargs={'dayfirst': True},
105 description=_('Include only attachments uploaded after this date'))
106 sessions = IndicoSelectMultipleCheckboxField(_('Sessions'),
107 description=_('Include attachments from selected sessions'))
108 contributions = IndicoSelectMultipleCheckboxField(_('Contributions'),
109 description=_('Include attachments from selected contributions'))