📄 pyforms.py
字号:
#! python
#
# $Workfile: pyforms.py $ $Revision: 10 $
# $Date: 9/13/01 11:22p $ $Author: Sholden $
#
#
"""Forms generation module - the quirky nature of the code and form
representation is due to its translation from the original VBScript!
Ultimately it will be much more satisfactory to use a specialised object as
a field descriptor, but time presses so I have hacked this into submission.
"""
from string import rstrip
from dtuple import TupleDescriptor, DatabaseTuple
# Describe the form description entries
DT = TupleDescriptor((['FieldName'],['Description'],['Type'],['Size'],['Options']))
def Descriptor():
return DT
def FieldVal(nm, R):
if R == None:
return ""
else:
return rstrip(str(R[nm]))
def Button(name, confirm):
if confirm:
OnClick = "confirm('Are you SURE?')"
else:
OnClick = "true"
return """<input TYPE="submit" NAME="Submit" VALUE="%s" OnClick="doit=%s">""" \
% (name, OnClick)
def TextIn(name, size, value, width):
return """<INPUT TYPE="TEXT" NAME="%s" SIZE="%d" MAXLENGTH="%s" VALUE="%s">""" \
% (name, width, size, value)
def PwdIn(name, size, value, width):
return """<INPUT TYPE="PASSWORD" NAME="%s" SIZE="%d" MAXLENGTH="%s" VALUE="%s">""" \
% (name, width, size, value)
def HiddenIn(name, size, value):
return """<INPUT TYPE="HIDDEN" NAME="%s" VALUE="%s">""" \
% (name, value)
def TextArea(name, size, value):
rows, cols = size
return """<TEXTAREA NAME="%s" ROWS="%d" cols="%d" WRAP="VIRTUAL">%s</TEXTAREA>""" \
% (name, rows, cols, value)
def DropDown(name, tbl, valcol, descol, cursor,
default="Please make a selection",
selected=None, ordercol=None, autosubmit=None):
"""Create a dropdown selector from two columns of a database table.
Note that column aliasing is used to give fixed attribute names to
the columns retrieved, so this algorithm works with two different
database columns or the same one for both value and description."""
td = TupleDescriptor([("ValCol",), ("DesCol",)])
if autosubmit:
auto = """ OnChange="submit(this.parent)\""""
else:
auto = ""
result = ["""<SELECT NAME="%s"%s>""" % (name, auto)]
result.append(Option("", default))
stmt = "SELECT %s, %s FROM %s" % (valcol, descol, tbl)
if ordercol:
stmt += " ORDER BY %s" % ordercol
# print stmt
cursor.execute(stmt)
rows = cursor.fetchall()
for row in rows:
row = DatabaseTuple(td, row)
result.append(Option(row.ValCol, row.DesCol))
result.append("</SELECT>\n")
return "\n".join(result)
def Option(val, dsc):
return """<OPTION VALUE="%s">%s""" % (val, dsc)
def FormBuild(Flist, Name, Action, R=None, Width=40,
Buttons=None, cursor=None):
"""Build a form from a form description.
Can be used "live", in which case the R argument is a database tuple
or other object that can be used to populate the form. Also
useful for generating static forms that can be served up by HTML.py.
Note that some fields do not require data values. The database tuple is
always accessed by field name. It therefore need not contain an entry
for each row in the form description, only the ones describing data
fields, which wil expect values when R is not the default None.
"""
result = '''
<form name="%s" action="%s" method="POST" OnSubmit="return doit">
<input type="hidden" name="_Flist_" value="%d">
<table border="0" cellpadding="0" cellspacing="2">
<tr>
''' % (Name, Action, id(Flist))
for ff in Flist:
f = DatabaseTuple(DT, ff)
if f.Type == "REM":
result = """%s
<td> </td><td> </td><td width="350"><font size="%s"><p>%s</font></td>
""" % (result, f.Size, f.Description)
elif f.Type[0] == "H":
result = """%s
<td align="right" valign="top"><font size="-2"><p><b>
%s</b></font></td>
<td valign="top"> </td>
<td>
<p><b>%s</b>%s</td>
""" % (result, f.Description, FieldVal(f.FieldName, R), HiddenIn(f.FieldName, f.Size, FieldVal(f.FieldName, R)))
else:
result = """%s<td align="right" valign="top"><font size="-2"><p><b>
%s</b></font></td>
<td valign="top">""" % (result, f.Description)
if "R" in f.Options:
result = """%s<font color="red">*</font>""" % result
else:
result = """%s """ % result
result = """%s</td>
<td>
""" % result
if f.Type in ["T","N"]:
result = """%s%s
""" % (result, TextIn(f.FieldName, f.Size, FieldVal(f.FieldName, R), Width))
elif f.Type in ["P", "PP", "PPP"]:
if f.Type == "PPP":
fval = FieldVal(f.FieldName, R)
else:
fval = ""
result = """%s%s
</td>
</tr>""" % (result, PwdIn(f.FieldName, f.Size, fval, Width))
if f.Type in ["PP", "PPP"]:
result = """%s
<tr>
<td align="right" valign="top"><font size="-2"><b>Confirm %s</b></font></td>
<td> </td>
<td>%s
""" % (result, f.Description,
PwdIn(f.FieldName+"+", f.Size, fval, Width))
elif f.Type == "Y":
if FieldVal(f.FieldName, R) == "True":
c1, c2 = "CHECKED", ""
else:
c2, c1 = "CHECKED", ""
result = """%s<P><input TYPE="radio" NAME="%s" %s value="ON"> Yes
<input TYPE="radio" NAME="%s" %s value="OFF"> No
""" % (result, f.FieldName, c1, f.FieldName, c2)
elif f.Type == "M":
result = """%s%s
""" % (result, TextArea(f.FieldName, f.Size, FieldVal(f.FieldName, R)))
elif f.Type in ["KSA","KNA"]:
if not R:
result = """%s%s
""" % (result, TextIn(f.FieldName, f.Size, FieldVal(f.FieldName, R), Width))
else:
result = """%s<p><font size="+1"><b>%s</b></font></p>
""" % (result, FieldVal(f.FieldName, R))
elif f.Type in ["KSS", "KNS"]:
if R == None:
result = """%s<p><font size="+1"><b>To be assigned</b></font></p>
""" % result
else:
result = """%s<p><font size="+1"><b><%s></b></font></p>
""" % (result, FieldVal(f.FieldName, R),)
elif f.Type in ["DD", "DDA"]:
tbl, valcol, descol = f.Size.split(":")
result += DropDown(f.FieldName, tbl, valcol, descol, cursor,
autosubmit=(f.Type == "DDA"))
else:
result = """%s<b>This field not editable: Its type is unknown</b>
""" % result
result = """%s</td>
""" % result
result = """%s</tr>
<tr>""" % result
result = """%s<td> </td><td> </td>
<td>
""" % result
if not Buttons:
if R:
result = """%s%s
%s
""" % (result, Button("Submit", 0), Button("Delete", 1))
else:
result = """%s<input TYPE="submit" NAME="Submit" VALUE="Create" OnClick="doit=true">
""" % result
else:
for name, confirm in Buttons:
result = """%s
%s""" % (result, Button(name, confirm))
result = """%s </td>
</tr>
</table>
</form>
""" % (result, )
return result
if __name__ == '__main__':
Flist = (
["Comment1", "Web Page Details", "REM", "+2", ""],
["Name", "Page Name", "KSA", 20, "R"],
["Fixed", "Fixed Value", "H", 20, ""],
["Password", "Your Password", "PP", 12, "R"],
["PageSet", "Page Set Name", "T", 20, "R"],
["Num", "Page Number", "N", 5, ""],
["LinkText", "Link Text", "T", 30, ""],
["Answer", "Does it Work?", "Y", None, "R"],
["Content", "Page Content", "M", (12, 25), "R"]
)
FT = TupleDescriptor(tuple([[nm[0]] for nm in Flist]))
R = DatabaseTuple(FT, ("One","Two","Three","Four","Five","Six","Seven","Eight","Nine"))
print FormBuild(Flist, "TestForm", "/UserLogin/", R=R,
Width=25,
Buttons=(("Yes", 1), ("No", 0), ("Cancel", 0))
)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -