Repository navigation
Expand file tree
/
Copy pathforms.py
More file actions
82 lines (59 loc) · 2.38 KB
/
Copy pathforms.py
File metadata and controls
82 lines (59 loc) · 2.38 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from flask_wtf import Form
from wtforms import StringField, TextAreaField, FieldList, FormField, BooleanField
from wtforms.ext.sqlalchemy.fields import QuerySelectField
from wtforms.validators import DataRequired, Length, Required, Optional
from models import Character
# Set your classes here.
class RequiredIf(Required):
# a validator which makes a field required if
# another field is set and has a truthy value
def __init__(self, other_field_name, *args, **kwargs):
self.other_field_name = other_field_name
super(RequiredIf, self).__init__(*args, **kwargs)
def __call__(self, form, field):
other_field = form._fields.get(self.other_field_name)
if other_field is None:
raise Exception('no field named "%s" in form' % self.other_field_name)
if bool(other_field.data):
super(RequiredIf, self).__call__(form, field)
class RelationshipForm(Form):
related_to = QuerySelectField(get_label='name', validators=[Optional()])
related_to.query = Character.query
relationship_type = StringField(
'Relationship Type', validators=[RequiredIf('related_to')]
)
relationship_description = TextAreaField(
'Relationship Description', validators=[RequiredIf('related_to')]
)
bidirectional = BooleanField(
'Bidirectional?'
)
other_relationship_type = StringField(
'Relationship Type', validators=[RequiredIf('related_to')]
)
other_relationship_description = StringField(
'Relationship Description', validators=[RequiredIf('related_to')]
)
class CharacterForm(Form):
character = None
name = StringField(
'Character Name', validators=[DataRequired(), Length(min=1, max=120)]
)
short_description = StringField(
'Short Description', validators=[DataRequired(), Length(min=1, max=256)]
)
description = TextAreaField(
'Character Description', validators=[DataRequired(), Length(min=10, max=10000)]
)
relationships = FieldList(FormField(RelationshipForm), validators=[Optional()], min_entries=1)
class SearchCharacterForm(Form):
name = StringField(
'Character Name'
)
description_snippet = StringField(
'Description'
)
# class NewRelationshipTypeForm(Form):
# description = StringField(
# 'Relationship Type', validators=[DataRequired(), Length(min=1, max=120)]
# )