From c2f8da256bb685329cad32dd48f8e6a8976d71e0 Mon Sep 17 00:00:00 2001 From: Kyle Roux Date: Tue, 16 May 2017 10:52:53 -0700 Subject: [PATCH 1/4] base form class and custom field classes --- forms.py | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/forms.py b/forms.py index 164e00b..33544f5 100644 --- a/forms.py +++ b/forms.py @@ -1,10 +1,38 @@ from flask_wtf import FlaskForm -from wtforms import StringField +from wtforms import StringField as _StringField, SubmitField as _SubmitField from wtforms.validators import DataRequired, Email, Length +from wtforms.fields.html5 import TelField as _TelField, EmailField as _EmailField +class AddArgsMixin(object): + _field_args = {} + + def __init__(self, name, field_args, *args, **kwargs): + self._field_args = field_args + super(AddArgsMixin, self).__init__(name, *args, **kwargs) + +class StringField(AddArgsMixin, _StringField): + pass + + +class SubmitField(AddArgsMixin, _SubmitField): + pass + + +class TelField(AddArgsMixin, _TelField): + pass + + +class EmailField(AddArgsMixin, _EmailField): + pass class ContactForm(FlaskForm): - name = StringField('Name', validators=[DataRequired(), Length(min=-1, max=80, message='You cannot have more than 80 characters')]) - surname = StringField('Surname', validators=[Length(min=-1, max=100, message='You cannot have more than 100 characters')]) - email = StringField('E-Mail', validators=[Email(), Length(min=-1, max=200, message='You cannot have more than 200 characters')]) - phone = StringField('Phone', validators=[Length(min=-1, max=20, message='You cannot have more than 20 characters')]) + name = StringField('Name', dict(class_='form-control'), validators=[DataRequired(), Length(min=-1, max=80, message='You cannot have more than 80 characters')]) + surname = StringField('Surname', field_args=dict(class_='form-control'), validators=[ + Length(min=-1, max=100, message='You cannot have more than 100 characters')]) + email = EmailField('E-Mail', field_args=dict(class_='form-control'), validators=[ + Email(), Length(min=-1, max=200, message='You cannot have more than 200 characters')]) + phone = TelField('Phone', field_args=dict(class_='form-control'), validators=[ + Length(min=-1, max=20, message='You cannot have more than 20 characters')]) + add = SubmitField('Add', field_args=dict(class_='btn btn-success')) + + From 19abb0749df4b621f33613103bac76d9c9a7f00e Mon Sep 17 00:00:00 2001 From: Kyle Roux Date: Tue, 16 May 2017 11:23:36 -0700 Subject: [PATCH 2/4] simplified form stuff --- app.py | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/app.py b/app.py index c7fb3e6..562a45a 100644 --- a/app.py +++ b/app.py @@ -56,25 +56,11 @@ def edit_contact(id): :param id: Id from contact ''' my_contact = Contact.query.filter_by(id=id).first() - form = ContactForm( - name=my_contact.name, - surname=my_contact.surname, - email=my_contact.email, - phone=my_contact.phone - ) + form = ContactForm(obj=my_contact) if form.validate_on_submit(): - # Get form - name = form.name.data - surname = form.surname.data - email = form.email.data - phone = form.phone.data - try: # Update contact - my_contact.name = name - my_contact.surname = surname - my_contact.email = email - my_contact.phone = phone + form.populate_obj(my_contact) db.session.add(my_contact) db.session.commit() # User info From a722f4267832ce86e8a86942b50d636c258cfd5e Mon Sep 17 00:00:00 2001 From: Kyle Roux Date: Tue, 16 May 2017 11:23:43 -0700 Subject: [PATCH 3/4] simplified more form stuff --- app.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/app.py b/app.py index 562a45a..58901c6 100644 --- a/app.py +++ b/app.py @@ -27,15 +27,10 @@ def new_contact(): Create new contact ''' form = ContactForm() - if form.validate_on_submit(): - # Get form - name = form.name.data - surname = form.surname.data - email = form.email.data - phone = form.phone.data - # Save in database + if form.validate_on_submit(): try: - my_contact = Contact(name, surname, email, phone) + my_contact = Contact() + form.populate_obj(my_contact) db.session.add(my_contact) db.session.commit() # User info From 979e8914daaf264fd59c5ae3f95afa8faf69b329 Mon Sep 17 00:00:00 2001 From: "Kyle J. Roux" Date: Tue, 16 May 2017 13:51:58 -0700 Subject: [PATCH 4/4] Update forms.py added sane default for non standard parameter `field_args` --- forms.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/forms.py b/forms.py index 33544f5..ba1715d 100644 --- a/forms.py +++ b/forms.py @@ -6,8 +6,8 @@ from wtforms.fields.html5 import TelField as _TelField, EmailField as _EmailFiel class AddArgsMixin(object): _field_args = {} - def __init__(self, name, field_args, *args, **kwargs): - self._field_args = field_args + def __init__(self, name, field_args=None, *args, **kwargs): + self._field_args = field_args or {} super(AddArgsMixin, self).__init__(name, *args, **kwargs) class StringField(AddArgsMixin, _StringField):