forms.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. """Contains the logic to create cohesive forms on the explore view"""
  18. from typing import List # pylint: disable=unused-import
  19. from flask_appbuilder.fieldwidgets import BS3TextFieldWidget
  20. from wtforms import Field
  21. class CommaSeparatedListField(Field):
  22. widget = BS3TextFieldWidget()
  23. data = [] # type: List[str]
  24. def _value(self):
  25. if self.data:
  26. return u", ".join(self.data)
  27. return u""
  28. def process_formdata(self, valuelist):
  29. if valuelist:
  30. self.data = [x.strip() for x in valuelist[0].split(",")]
  31. else:
  32. self.data = []
  33. def filter_not_empty_values(value):
  34. """Returns a list of non empty values or None"""
  35. if not value:
  36. return None
  37. data = [x for x in value if x]
  38. if not data:
  39. return None
  40. return data