from flask.ext.wtf import Form from wtforms import StringField, HiddenField, PasswordField, BooleanField, \ validators, ValidationError from wtforms.ext.sqlalchemy.orm import model_form, ModelConverter from app import db from models import Group, Pad from utils.forms import Unique, RedirectMixin from utils.widgets import TextArea, Static, PasswordInput CreateGroup = model_form( Group, base_class=Form, only=['name', 'description', 'public', 'browsable'], field_args={ 'name': {'validators': [ validators.Required(), validators.Regexp('^[a-zA-Z1-9_-]+$', message=u'Invalid group name ' '(only simple characters, numbers, - and _).'), validators.Regexp('^[a-zA-Z1-9]', message=u'Group name should not ' 'start with a special character.'), Unique(Group, Group.name, message=u'A group with this name ' 'already exists.')] }, 'description': {'widget': TextArea(rows=7)}, 'public': {'validators': []}, 'browsable': {'validators': []}, }, db_session=db.session) ChangeGroup = model_form( Group, base_class=Form, only=['name', 'description', 'public', 'browsable'], field_args={ 'name': {'widget': Static()}, 'description': {'widget': TextArea(rows=7)}, 'public': {'validators': []}, 'browsable': {'validators': []}, }, db_session=db.session) _CreatePad = model_form( Pad, base_class=Form, exclude=['api_id', 'created', 'group'], field_args={ 'name': {'validators': [ validators.Required(), validators.Regexp('^[a-zA-Z1-9_-]+$', message=u'Invalid pad name ' '(only simple characters, numbers, - and _).'), validators.Regexp('^[a-zA-Z1-9]', message=u'Pad name should not ' 'start with a special character.')]}, 'public': {'validators': []}, 'password': {'widget': PasswordInput(autocomplete='off')}, }, db_session=db.session) class CreatePad(_CreatePad): def __init__(self, formdata=None, obj=None, group=None, prefix='', **kwarg): self.group = group super(CreatePad, self).__init__(formdata, obj, prefix, **kwarg) def validate_name(self, field): if self.group is not None: pad_query = Pad.query.filter_by(name=field.data, group=self.group) if pad_query.count() > 0: raise ValidationError(u'A pad with this name already ' 'exists in this group.') ChangePad = model_form( Pad, base_class=Form, exclude=['api_id', 'created', 'group'], field_args={ 'name': {'widget': Static()}, 'public': {'validators': []}, 'password': {'widget': PasswordInput(autocomplete='off')}, }, db_session=db.session) class LoginForm(RedirectMixin, Form): user = StringField('login', [validators.Required()]) password = PasswordField('password', [validators.Required()]) class DeleteForm(Form): sure = HiddenField('are you sure', default='yes')