text: # This file is part of Indico.
  # Copyright (C) 2002 - 2022 CERN
  #
  # Indico is free software; you can redistribute it and/or
  # modify it under the terms of the MIT License; see the
  # LICENSE file for more details.
  
  from wtforms.widgets import CheckboxInput
  from wtforms_sqlalchemy.fields import QuerySelectMultipleField
  
  from indico.web.forms.widgets import JinjaWidget
  
  
  class IndicoQuerySelectMultipleField(QuerySelectMultipleField):
      """Like the parent, but with a callback that allows you to modify the list
  
      The callback can return a new list or yield items, and you can use it e.g. to sort the list.
      """
  
      def __init__(self, *args, **kwargs):
          self.modify_object_list = kwargs.pop('modify_object_list', None)
          self.collection_class = kwargs.pop('collection_class', list)
          super().__init__(*args, **kwargs)
  
      def _get_object_list(self):
          object_list = super()._get_object_list()
          if self.modify_object_list:
              object_list = list(self.modify_object_list(object_list))
          return object_list
  
      def _get_data(self):
          data = super()._get_data()
          return self.collection_class(data)
  
      data = property(_get_data, QuerySelectMultipleField._set_data)
  
  
  class IndicoQuerySelectMultipleCheckboxField(IndicoQuerySelectMultipleField):
      option_widget = CheckboxInput()
      widget = JinjaWidget('forms/checkbox_group_widget.html', single_kwargs=True)
  