Skip to content

Instantly share code, notes, and snippets.

@scabbiaza
Last active August 29, 2015 14:06
Show Gist options
  • Save scabbiaza/538ff6a682743d49de15 to your computer and use it in GitHub Desktop.
Save scabbiaza/538ff6a682743d49de15 to your computer and use it in GitHub Desktop.
WTForms sketches
# Caching results in SELECT choices
# http://stackoverflow.com/questions/10368900/how-to-make-wtforms-read-from-the-datastore
# Choices will be cached
def get_clients_list_choices():
choices = [(0, '')]
clients = Client.query.order_by('company')
for client in clients:
choices.append((client.id, client.fullname))
return choices
class JobForm(Jorm):
client_id = SelectField('Client', choices=get_clients_list_choices(), coerce=int)
# Choices will NOT be cached
class ClientField(SelectField):
def __init__(self, *args, **kwargs):
super(ClientField, self).__init__(*args, **kwargs)
self.choices = [(0, '')]
clients = Client.query.order_by('company').all()
for client in clients:
self.choices.append((client.id, client.company_fullname))
class JobForm(Jorm):
client = ClientField('Client', coerce=int)
################################################################################
# WysiWygLength & WysiWygRequired Validators
from bs4 import BeautifulSoup
def strip_tags(value):
soup = BeautifulSoup(value)
return ''.join(soup.findAll(text=True))
def sanitize_html(value):
if value is None: return value
VALID_TAGS = ['em', 'p', 'ul', 'ol', 'li', 'br', 'a', 'span', 'div', 'strong']
soup = BeautifulSoup(value)
for tag in soup.findAll(True):
if tag.name not in VALID_TAGS:
tag.hidden = True
return unicode(soup.renderContents(), 'utf-8')
def WysiWygLength(min=None, max=None):
# Need to count only text, without tags
def _WysiWygLength(form, field):
if min is not None and len(strip_tags(field.data)) < min:
raise ValidationError(messages.field_min_length_check % dict(min=min))
if max is not None and len(strip_tags(field.data)) > max:
raise ValidationError(messages.field_max_length_check % dict(max=max))
return _WysiWygLength
def WysiWygRequired(form, field):
# When the field is empty summernote wysiwig sends <p><br></p>.
# The same behavior we could expect form other text editors.
# Because of this DataRequired validation is not working properly.
# Solution is to check not the whole content, but content without html tags.
# Before checking, we should sanitize content on not valid tags.
# Some tags are significant, and if they exist in content, content is not empty.
if field.data is None:
raise ValidationError(messages.field_required)
SIGNIFICANT_TAGS = ['img']
field.data = sanitize_html(field.data)
soup = BeautifulSoup(field.data)
for tag in soup.findAll(True):
if tag.name in SIGNIFICANT_TAGS:
return
if not soup.get_text(strip=True):
raise ValidationError(messages.field_required)
################################################################################
# USING GET method
filter_form = FilterForm(formdata=werkzeug.datastructures.MultiDict(request.values))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment