-
Notifications
You must be signed in to change notification settings - Fork 15
/
htmlform.py
41 lines (37 loc) · 1.62 KB
/
htmlform.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
# automatically generate a htmlform
class HtmlForm():
fields = [] # list of (fieldname, value, type, description)
name = ""
descriptionText = ""
method="POST"
submittext=""
buttonType=""
def __init__(self, formname, submittext="Spara", method="POST", descriptionText="", buttonType="btn-primary"):
self.fields = []
self.name = formname
self.method=method
self.submittext = submittext
self.descriptionText = descriptionText
self.buttonType=buttonType
def AddField(self, name, value, description, type="text", required=True, readonly=False):
self.fields.append((name, value, description, type, required, readonly))
def __str__(self):
s = ""
if self.descriptionText != "":
s += '<p>' + self.descriptionText.replace('\n', '<br/>') + '</p>'
s += '<form role="form" name="' + self.name + '" method="' + self.method + '" action="./">'
for field in self.fields:
s += '<div class="form-group">'
s += '<label for="' + field[0] + '">' + field[2] + '</label>'
s += '<input type="' + field[3] + '" class="form-control" size="50"'
if field[4]:
s += ' required'
if field[5]:
s += ' readonly'
s += ' name="' + field[0] + '" id="' + field[0] + '" value="' + str(field[1]) + '"/>'
s += '</div>'
s += '<div class="btn-toolbar">'
s += '<button type="submit" name="submit" class="btn btn-lg ' + self.buttonType +'">' + self.submittext + '</button>'
s += '</div>'
s += '</form>'
return s