Last active
October 6, 2023 06:32
-
-
Save M0r13n/71655c53b2fbf41dc1db8412978bcbf9 to your computer and use it in GitHub Desktop.
A sample Wtforms field for creating a list of tags with custom separators, e.g. "," or " ".
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form action="" method="POST" role="form" class="form"> | |
{{ form.hidden_tag() }} | |
<!--Other fields--> | |
{{ wtf.form_field(form.tags, placeholder='audio, hardware, chip') }} | |
<button class="btn btn-success" type="submit">submit</button> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from wtforms.fields import StringField | |
class TagListField(StringField): | |
"""Stringfield for a list of separated tags""" | |
def __init__(self, label='', validators=None, remove_duplicates=True, to_lowercase=True, separator=' ', **kwargs): | |
""" | |
Construct a new field. | |
:param label: The label of the field. | |
:param validators: A sequence of validators to call when validate is called. | |
:param remove_duplicates: Remove duplicates in a case insensitive manner. | |
:param to_lowercase: Cast all values to lowercase. | |
:param separator: The separator that splits the individual tags. | |
""" | |
super(TagListField, self).__init__(label, validators, **kwargs) | |
self.remove_duplicates = remove_duplicates | |
self.to_lowercase = to_lowercase | |
self.separator = separator | |
self.data = [] | |
def _value(self): | |
if self.data: | |
return u', '.join(self.data) | |
else: | |
return u'' | |
def process_formdata(self, valuelist): | |
if valuelist: | |
self.data = [x.strip() for x in valuelist[0].split(self.separator)] | |
if self.remove_duplicates: | |
self.data = list(self._remove_duplicates(self.data)) | |
if self.to_lowercase: | |
self.data = [x.lower() for x in self.data] | |
@classmethod | |
def _remove_duplicates(cls, seq): | |
"""Remove duplicates in a case insensitive, but case preserving manner""" | |
d = {} | |
for item in seq: | |
if item.lower() not in d: | |
d[item.lower()] = True | |
yield item |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask_wtf import FlaskForm | |
from tag_list_field import TagListField | |
class Post(FlaskForm): | |
# other fields | |
tags = TagListField( | |
"Tags", | |
separator=",", | |
validators=[Length(max=8, message="You can only use up to 8 tags.")] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment