-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforms.py
More file actions
67 lines (56 loc) · 2.06 KB
/
forms.py
File metadata and controls
67 lines (56 loc) · 2.06 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
import json
from flask_wtf import FlaskForm
from flask_wtf.file import FileAllowed, FileField, FileRequired
# Schema
from schema import And, Schema
from wtforms import StringField, TextAreaField
from wtforms.validators import DataRequired, ValidationError
textCords_Schema = Schema({"x": int, "y": int, "w": int, "h": int})
textConfig_Schema = Schema(
{
"textSize": And(str, lambda n: 8 <= int(n) <= 256 and n.isdigit()),
"textColor": str,
"textDir": lambda i: i in ["rtl", "ltr"],
"textAlign": lambda i: i in ["left", "center", "right"],
}
)
text_Schema = Schema(And({"sentences": [str]}, lambda a: len(a.get("sentences")) > 0))
class ThumbnailDesignForm(FlaskForm):
# Image upload
imageInput = FileField(
"Design Template",
validators=[
FileAllowed(["png", "jpg", "jpeg"], "Images only!"),
],
)
fontFile = FileField(
"Font file (Optional)",
validators=[
FileAllowed(["ttf"], "TTF fonts only!"),
],
)
# Json object with x,y and w
textCords = TextAreaField(validators=[DataRequired()])
# A JSON object for text config
textConfig = TextAreaField(validators=[DataRequired()])
# A JSON objects {"sentences": []}
text = TextAreaField(validators=[DataRequired()])
def validate_textCords(form, field):
cords = json.loads(field.data)
if textCords_Schema.is_valid(cords):
textCords_Schema.validate(cords)
return 123
else:
raise ValidationError("Text cords field has invalid data.")
def validate_textConfig(form, field):
config = json.loads(field.data)
if textConfig_Schema.is_valid(config):
textConfig_Schema.validate(config)
else:
raise ValidationError("Text Config field has invalid data.")
# def validate_text(form, field):
# text = json.loads(field.data)
# if text_Schema.is_valid(text):
# text_Schema.validate(text)
# else:
# raise ValidationError("Text field has invalid data.")