class VCRoomField(HiddenField): widget = SelectizeWidget(min_trigger_length=3) def process_formdata(self, valuelist): if valuelist and valuelist[0].isdigit(): self.data = VCRoom.get(valuelist[0]) def _value(self): return self.data.id if self.data is not None else None
class AbstractField(QuerySelectField): """A selectize-based field to select an abstract from an event.""" widget = SelectizeWidget(allow_by_id=True, search_field='title', label_field='full_title', preload=True, search_method='POST', inline_js=True) def __init__(self, *args, **kwargs): kwargs.setdefault('allow_blank', True) kwargs.setdefault('render_kw', {}).setdefault('placeholder', _('Enter abstract title or #id')) kwargs['query_factory'] = self._get_query kwargs['get_label'] = lambda a: f'#{a.friendly_id}: {a.title}' self.ajax_endpoint = kwargs.pop('ajax_endpoint') self.excluded_abstract_ids = set() super().__init__(*args, **kwargs) @classmethod def _serialize_abstract(cls, abstract): return {'id': abstract.id, 'friendly_id': abstract.friendly_id, 'title': abstract.title, 'full_title': f'#{abstract.friendly_id}: {abstract.title}'} def _get_query(self): query = Abstract.query.with_parent(self.event).options(joinedload('submitter').lazyload('*')) excluded = set(map(int, request.form.getlist('excluded_abstract_id'))) if excluded: query = query.filter(Abstract.id.notin_(excluded)) return query.order_by(Abstract.friendly_id) def _get_object_list(self): return [(key, abstract) for key, abstract in super(AbstractField, self)._get_object_list() if abstract.can_access(session.user)] def _value(self): return self._serialize_abstract(self.data) if self.data else None def pre_validate(self, form): super().pre_validate(form) if self.data is not None and self.data.id in self.excluded_abstract_ids: raise ValueError(_('This abstract cannot be selected.')) @property def event(self): # This cannot be accessed in __init__ since `get_form` is set # afterwards (when the field gets bound to its form) so we # need to access it through a property instead. return self.get_form().event @property def search_url(self): return url_for(self.ajax_endpoint, self.event) @property def search_payload(self): return {'excluded_abstract_id': list(self.excluded_abstract_ids)}
class VCRoomField(HiddenField): widget = SelectizeWidget() def _convert_principal(self, principal): if principal['_type'] == 'Avatar': return u'Avatar', principal['id'] else: return u'Group', principal['id'] def process_formdata(self, valuelist): if valuelist and valuelist[0].isdigit(): self.data = VCRoom.get(valuelist[0]) def _value(self): return self.data.id if self.data is not None else None
class LinkedObjectField(Field): widget = SelectizeWidget(allow_by_id=True, search_field='title', label_field='full_title', preload=True, inline_js=True) def __init__(self, *args, **kwargs): self.ajax_endpoint = kwargs.pop('ajax_endpoint') super().__init__(*args, **kwargs) def _value(self): pass @property def event(self): # This cannot be accessed in __init__ since `get_form` is set # afterwards (when the field gets bound to its form) so we # need to access it through a property instead. return self.get_form().event @property def search_url(self): return url_for(self.ajax_endpoint, self.event)