-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_test.py
executable file
·97 lines (82 loc) · 2.7 KB
/
add_test.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
#!/usr/bin/python
# IMPORTS:
import cgitb
cgitb.enable()
import cgi
import os
import base, settings, module
from connect import connect
import module
# :IMPORTS
# FUNCTIONS:
def add_test_basic(db, person_id, test_type, card_id, success, comments):
# Connect to the DB:
con = connect(True, db)
cur = con.cursor()
# Insert:
cmd_sql = "INSERT INTO Test (person_id, test_type_id, card_id, successful, comments, day) VALUES (%s, %s, %s, %s, %s, NOW())"
## This is safer because Python takes care of escaping any illegal/invalid text
items = (person_id, test_type, card_id, int(success), comments)
#if comments:
cur.execute(cmd_sql, items)
test_id = cur.lastrowid
con.commit()
# else: test_id = 0
return test_id
def add_test_attachment(db, test_id, afile, desc, comments):
if afile.filename:
# Connect to DB:
con = connect(True, db)
cur = con.cursor()
# Insert:
originalname = os.path.basename(afile.filename)
cur.execute("INSERT INTO Attachments (test_id, attachmime, attachdesc, comments, originalname) VALUES (%s,%s,%s,%s,%s)", (test_id, afile.type, desc, comments, originalname))
att_id = cur.lastrowid
con.commit()
# Deal with path:
ofn = settings.get_attachment_path(db, int(test_id), int(att_id))
sub_path = os.path.dirname(ofn)
if not os.path.exists(sub_path):
os.makedirs(sub_path)
open(ofn, 'wb').write(afile.file.read())
# print '<div> The file %s was uploaded successfully. </div>' % (originalname)
def main():
# Arguments:
form = cgi.FieldStorage()
person_id = base.cleanCGInumber(form.getvalue("person_id"))
test_type = base.cleanCGInumber(form.getvalue("test_type"))
card_id = base.cleanCGInumber(form.getvalue("card_id"))
success = (False, True)[bool(form.getvalue("success"))]
db = settings.get_db()
## Comments:
comments = form.getvalue("comments")
if comments:
comments = cgi.escape(comments)
if person_id != 0:
# Basic:
base.begin()
base.header_redirect("module.py?db={0}&card_id={1}".format(db, card_id))
base.top(db)
# Add the test to the DB:
test_id = add_test_basic(db, person_id, test_type, card_id, success, comments)
## Attachments:
for i in range(1, 4):
afile = form['attach{0}'.format(i)]
if (afile.filename):
adesc= form.getvalue("attachdesc{0}".format(i))
if adesc:
adesc = cgi.escape(adesc)
acomment= form.getvalue("attachcomment{0}".format(i))
if acomment:
acomment = cgi.escape(acomment)
add_test_attachment(db, test_id, afile, adesc, acomment)
base.bottom()
else:
base.error(db, "You must select a tester from the available list.")
else:
base.error(db, "You must input comments before the test can be added.")
# :FUNCTIONS
# MAIN:
if __name__ == "__main__":
main()
# :MAIN