Exemplo n.º 1
0
class AttachmentFolderForm(IndicoForm):
    title = HiddenField(_("Name"), [DataRequired()], widget=TypeaheadWidget(),
                        description=_("The name of the folder."))
    description = TextAreaField(_("Description"), description=_("Description of the folder and its content"))
    protected = BooleanField(_("Protected"), widget=SwitchWidget())
    acl = PrincipalListField(_("Grant Access To"), [UsedIf(lambda form, field: form.protected.data)],
                             groups=True, serializable=False, allow_external=True,
                             description=_("The list of users and groups with access to the folder"))
    is_always_visible = BooleanField(_("Always Visible"), widget=SwitchWidget(),
                                     description=_("By default, folders are always visible, even if a user cannot "
                                                   "access them. You can disable this behavior here, hiding the folder "
                                                   "for anyone who does not have permission to access it."))

    def __init__(self, *args, **kwargs):
        self.linked_object = kwargs.pop('linked_object')
        super(AttachmentFolderForm, self).__init__(*args, **kwargs)
        self.title.choices = self._get_title_suggestions()

    def _get_title_suggestions(self):
        query = db.session.query(AttachmentFolder.title).filter_by(is_deleted=False, is_default=False,
                                                                   linked_object=self.linked_object)
        existing = set(x[0] for x in query)
        suggestions = set(get_default_folder_names()) - existing
        if self.title.data:
            suggestions.add(self.title.data)
        return sorted(suggestions)

    @generated_data
    def protection_mode(self):
        return ProtectionMode.protected if self.protected.data else ProtectionMode.inheriting
Exemplo n.º 2
0
class AttachmentFolderForm(IndicoForm):
    title = HiddenField(_('Name'), [DataRequired()],
                        widget=TypeaheadWidget(),
                        description=_('The name of the folder.'))
    description = TextAreaField(
        _('Description'),
        description=_('Description of the folder and its content'))
    protected = BooleanField(_('Protected'), widget=SwitchWidget())
    acl = AccessControlListField(
        _('Access control list'),
        [UsedIf(lambda form, field: form.protected.data)],
        allow_groups=True,
        allow_external_users=True,
        allow_event_roles=True,
        allow_category_roles=True,
        allow_registration_forms=True,
        event=lambda form: form.event,
        description=_(
            'The list of users and groups allowed to access the folder'))
    is_always_visible = BooleanField(
        _('Always Visible'), [HiddenUnless('is_hidden', value=False)],
        widget=SwitchWidget(),
        description=_(
            'By default, folders are always visible, even if a user cannot '
            'access them. You can disable this behavior here, hiding the folder '
            'for anyone who does not have permission to access it.'))
    is_hidden = BooleanField(
        _('Always hidden'), [HiddenUnless('is_always_visible', value=False)],
        widget=SwitchWidget(),
        description=
        _('Always hide the folder and its contents from public display areas of '
          'the event. You can use this for folders to store non-image files used '
          'e.g. in download links. The access permissions still apply.'))

    def __init__(self, *args, **kwargs):
        self.linked_object = kwargs.pop('linked_object')
        self.event = getattr(self.linked_object, 'event',
                             None)  # not present in categories
        super().__init__(*args, **kwargs)
        self.title.choices = self._get_title_suggestions()

    def _get_title_suggestions(self):
        query = db.session.query(AttachmentFolder.title).filter_by(
            is_deleted=False, is_default=False, object=self.linked_object)
        existing = {x[0] for x in query}
        suggestions = set(get_default_folder_names()) - existing
        if self.title.data:
            suggestions.add(self.title.data)
        return sorted(suggestions)

    def validate_is_always_visible(self, field):
        if self.is_always_visible.data and self.is_hidden.data:
            raise ValidationError(
                'These two options cannot be used at the same time')

    validate_is_hidden = validate_is_always_visible

    @generated_data
    def protection_mode(self):
        return ProtectionMode.protected if self.protected.data else ProtectionMode.inheriting