-
Notifications
You must be signed in to change notification settings - Fork 1
/
forms.py
132 lines (106 loc) · 3.4 KB
/
forms.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from datetime import datetime
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms.fields.core import BooleanField, DateField, RadioField, TimeField
from wtforms.fields.simple import PasswordField
from wtforms.validators import InputRequired
class CustomFlaskForm(FlaskForm):
name = ''
QRFormat = '{}'
@classmethod
def to_qr_string(cls, dict) -> str:
print(cls, cls.QRFormat, dict)
return cls.QRFormat.format(*dict.values())
class BasicQRCodeForm(CustomFlaskForm):
"""
User input for the str content of the desired QR code
"""
name = 'Basic QR'
content = StringField(
label='QRCode content',
description='Link, text, email, phone number... anything!',
validators=[InputRequired()],
)
class WifiQRCodeForm(CustomFlaskForm):
"""
Wifi QR Code generator form
Example:
`WIFI:T:WPA;S:mynetwork;P:mypass;;`
"""
name = 'Wi-Fi QR'
QRFormat = 'WIFI:T:{crypt};S:{ssid};P:{passwd};H:{hidden};;'
@classmethod
def to_qr_string(cls, dict) -> str:
if hidden := dict.get('hidden', 'n') == 'y':
hidden = 'true'
else:
hidden = 'false'
return cls.QRFormat.format(
crypt=dict['crypt'],
ssid=dict['ssid'],
passwd=dict['passwd'],
hidden=hidden
)
ssid = StringField(
label='SSID',
validators=[InputRequired()],
render_kw={'placeholder': 'My Wi-Fi network'}
)
ssid_hidden = BooleanField(
label='Hidden network',
default=False
)
crypt = RadioField(
label='Network cryptography',
choices=[
('WEP', 'WEP'),
('WPA', 'WPA (default)'),
# ('WPA2-EAP', 'WPA2-EAP'),
('nopass', 'None')],
default='WPA',
)
passwd = PasswordField(
label='Password',
render_kw={'placeholder': 'MyPass123 (optional)'},
)
class EventQRCodeForm(CustomFlaskForm):
name = 'Event QR'
QRFormat = \
"""
BEGIN:VEVENT\r\n
SUMMARY:{summary}\r\n
DTSTART:{start}\r\n
DTEND:{end}\r\n
END:VEVENT\r\n
"""
# DTSTART:19980118T073000Z
DATE_FORMAT = '%Y%m%d'
TIME_FORMAT = 'T%H%M00Z'
@classmethod
def to_qr_string(cls, dict) -> str:
try:
dtstart = datetime.strptime(
f'{dict["dtpicker_start_date"]} {dict["start_hour"]}', '%d/%m/%Y %H:%M')
dtstart = dtstart.strftime(cls.DATE_FORMAT + cls.TIME_FORMAT)
except:
dtstart = datetime.now().strftime(cls.DATE_FORMAT + cls.TIME_FORMAT)
try:
dtend = datetime.strptime(
f'{dict["dtpicker_end_date"]} {dict["end_hour"]}', '%d/%m/%Y %H:%M')
dtend = dtend.strftime(cls.DATE_FORMAT + cls.TIME_FORMAT)
except:
dtend = datetime.now().strftime(cls.DATE_FORMAT + cls.TIME_FORMAT)
return cls.QRFormat.format(
summary=dict['event_name'],
start=dtstart,
end=dtend
)
event_name = StringField(
label='Event',
validators=[InputRequired()],
render_kw={'placeholder': 'My birthday! Please come, I\'m lonely'}
)
start_hour = TimeField(label='Starts at')
end_hour = TimeField(label='Ends at')
dtpicker_start_date = DateField(label='Begins')
dtpicker_end_date = DateField(label='Ends')