summaryrefslogtreecommitdiffstats
path: root/forms.py
blob: 969ea73a758ff07f5dc491cff7f6c393a7476242 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from wtforms import HiddenField, PasswordField, validators, ValidationError
from wtfpeewee.orm import model_form, ModelConverter
from flask.ext.wtf import Form
from utils import Unique, ReadonlyField
from models import Group, Pad
from widgets import TextArea


CreateGroup = model_form(Group, base_class=Form, exclude=['api_id'], 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)}})


ChangeGroup = model_form(Group, base_class=Form, exclude=['api_id'], field_args={
    'description': {'widget': TextArea(rows=7)}},
    converter=ModelConverter(overrides={'name': ReadonlyField}))


_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.')]}},
    converter=ModelConverter(overrides={'password': PasswordField}))


ChangePad = model_form(
    Pad, base_class=Form, exclude=['api_id', 'created', 'group'],
    converter=ModelConverter(overrides={'password': PasswordField, 'name': ReadonlyField}))


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:
            try:
                Pad.get(Pad.name == field.data, Pad.group == self.group)
                raise ValidationError(u'A pad with this name already '
                                      'exists in this group.')
            except Pad.DoesNotExist:
                pass


class DeleteForm(Form):
    sure = HiddenField('are you sure', default='yes')