Add folder name suggestions
[cds-indico.git] / indico / modules / attachments / forms.py
blobaea051bff13f737d86f05db32346be384f4aa2fe
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
20 from wtforms.ext.sqlalchemy.fields import QuerySelectField
21 from wtforms.ext.dateutil.fields import DateField
22 from wtforms.fields import BooleanField, TextAreaField
23 from wtforms.fields.html5 import URLField
24 from wtforms.fields.simple import StringField, HiddenField
25 from wtforms.validators import DataRequired, Optional
27 from indico.core.db import db
28 from indico.core.db.sqlalchemy.protection import ProtectionMode
29 from indico.modules.attachments.models.folders import AttachmentFolder
30 from indico.modules.attachments.util import get_default_folder_names
31 from indico.util.i18n import _
32 from indico.web.forms.base import IndicoForm, generated_data
33 from indico.web.forms.fields import PrincipalListField, IndicoSelectMultipleCheckboxField
34 from indico.web.forms.validators import UsedIf
35 from indico.web.forms.widgets import SwitchWidget, TypeaheadWidget
38 class AttachmentFormBase(IndicoForm):
39 protected = BooleanField(_("Protected"), widget=SwitchWidget())
40 folder = QuerySelectField(_("Folder"), allow_blank=True, blank_text=_("No folder selected"), get_label='title',
41 description=_("Adding materials to folders allow grouping and easier permission "
42 "management."))
44 def __init__(self, *args, **kwargs):
45 linked_object = kwargs.pop('linked_object')
46 super(AttachmentFormBase, self).__init__(*args, **kwargs)
47 self.folder.query = (AttachmentFolder
48 .find(linked_object=linked_object, is_default=False, is_deleted=False)
49 .order_by(db.func.lower(AttachmentFolder.title)))
51 @generated_data
52 def protection_mode(self):
53 return ProtectionMode.protected if self.protected.data else ProtectionMode.inheriting
56 class EditAttachmentFormBase(AttachmentFormBase):
57 title = StringField(_("Title"), [DataRequired()])
58 description = TextAreaField(_("Description"))
59 acl = PrincipalListField(_("Grant Access To"), [UsedIf(lambda form, field: form.protected.data)],
60 groups=True, serializable=False, allow_external=True,
61 description=_("The list of users and groups with access to the material"))
64 class AddAttachmentFilesForm(AttachmentFormBase):
65 pass
68 class EditAttachmentFileForm(EditAttachmentFormBase):
69 pass
72 class AttachmentLinkFormMixin(object):
73 title = StringField(_("Title"), [DataRequired()])
74 link_url = URLField(_("URL"), [DataRequired()])
77 class AddAttachmentLinkForm(AttachmentLinkFormMixin, AttachmentFormBase):
78 pass
81 class EditAttachmentLinkForm(AttachmentLinkFormMixin, EditAttachmentFormBase):
82 pass
85 class AttachmentFolderForm(IndicoForm):
86 title = HiddenField(_("Name"), [DataRequired()], description=_("The name of the folder."), widget=TypeaheadWidget())
87 description = TextAreaField(_("Description"), description=_("Description of the folder and its content"))
88 protected = BooleanField(_("Protected"), widget=SwitchWidget())
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 def __init__(self, *args, **kwargs):
98 super(AttachmentFolderForm, self).__init__(*args, **kwargs)
99 self.title.choices = [{'name': value} for value in get_default_folder_names()]
101 @generated_data
102 def protection_mode(self):
103 return ProtectionMode.protected if self.protected.data else ProtectionMode.inheriting
106 class AttachmentPackageForm(IndicoForm):
107 added_since = DateField(_('Added Since'), [Optional()], parse_kwargs={'dayfirst': True},
108 description=_('Include only attachments uploaded after this date'))
109 sessions = IndicoSelectMultipleCheckboxField(_('Sessions'),
110 description=_('Include attachments from selected sessions'))
111 contributions = IndicoSelectMultipleCheckboxField(_('Contributions'),
112 description=_('Include attachments from selected contributions'))