-
Notifications
You must be signed in to change notification settings - Fork 3
/
addUnitWidget.py
166 lines (159 loc) · 7.51 KB
/
addUnitWidget.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import connectDb
from AddUnit import Ui_NewUnit
from PyQt4 import QtGui, QtCore
def isNumber(s):
try:
float(s)
return True
except ValueError:
return False
class NewUnit(QtGui.QWidget):
def __init__(self, parent):
QtGui.QWidget.__init__(self)
self.ui = Ui_NewUnit()
self.ui.setupUi(self)
self.ui.add_cashier.clicked.connect(self.addCashier)
self.ui.add_pdu.clicked.connect(self.addPdu)
self.parent = parent
def addCashier(self):
self.ui.error_Id.clear()
self.ui.error_Pwd.clear()
cid = self.ui.person_id.text()
pwd = self.ui.person_password.text()
if (len(str(cid))>0) & (len(str(pwd))>0):
if isNumber(cid) & isNumber(pwd) & (len(str(pwd)) <= 6):
cid = int(cid)
pwd = int(pwd)
if cid<65536:
conn, cur = connectDb.connectToDatabase()
checkForExistence = "SELECT count(*) FROM cashier WHERE id = %d ;" % cid
cur.execute(checkForExistence)
count = cur.fetchone()
count = count[0]
checkForExistence = "SELECT count(*) FROM pdumap WHERE id = %d ;" % cid
cur.execute(checkForExistence)
countOther = cur.fetchone()
countOther = countOther[0]
if (count == 0) & (countOther == 0):
insertQuery = "INSERT INTO cashier VALUES(%d, %d, 1)" % (cid, pwd)
cur.execute(insertQuery)
cur.execute("UPDATE flag SET flag=1 WHERE 1")
conn.commit()
self.parent.viewUnits()
self.close()
else:
self.ui.person_id.clear()
self.ui.person_password.clear()
self.ui.error_Id.setText("*device exists")
connectDb.closeDatabaseConnection(conn, cur)
self.parent.viewUnits()
else:
self.ui.person_id.clear()
self.ui.person_password.clear()
self.ui.error_Id.setText("*id must be <65536")
else:
if not isNumber(cid):
self.ui.person_id.clear()
self.ui.person_password.clear()
self.ui.error_Id.setText("*incorrect input")
if not isNumber(pwd) | (len(str(pwd)) > 6):
self.ui.person_id.clear()
self.ui.person_password.clear()
self.ui.error_Pwd.setText("*incorrect input")
else:
if len(str(cid)) == 0:
self.ui.person_id.clear()
self.ui.person_password.clear()
self.ui.error_Id.setText("*required")
if len(str(pwd)) == 0:
self.ui.person_password.clear()
self.ui.error_Pwd.setText("*required")
def addPdu(self):
self.ui.error_Dev.clear()
self.ui.error_Bar.clear()
self.ui.error_Port.clear()
did = self.ui.device_id.text()
port = self.ui.device_port.text()
barcode = self.ui.device_barcode.text()
if (len(str(did))>0) & (len(str(barcode))>0) & (len(str(port))>0):
if isNumber(did) & isNumber(barcode) & (len(str(barcode)) == 8) & isNumber(port):
did = int(did)
barcode = int(barcode)
port = int(port)
if (did<=65536) & (did>=0) & (port>=0) & (port<8):
conn, cur = connectDb.connectToDatabase()
checkForExistence = "SELECT count(*) FROM pdumap WHERE pdumap.id = %d AND pdumap.port = %d;" % (did, port)
cur.execute(checkForExistence)
count = cur.fetchone()
count = count[0]
checkForExistence = "SELECT count(*) FROM cashier WHERE cashier.id = %d ;" % did
cur.execute(checkForExistence)
countOther = cur.fetchone()
countOther = countOther[0]
if (count == 0) & (countOther == 0):
checkForExistence1 = "SELECT count(*) FROM product WHERE product.barcode = %d ;" % barcode
cur.execute(checkForExistence1)
countProd = cur.fetchone()
countProd = countProd[0]
if countProd == 1:
insertQuery = "INSERT INTO pdumap(id, port, barcode) VALUES(%d, %d, %d)" % (did, port, barcode)
cur.execute(insertQuery)
cur.execute("UPDATE flag SET flag=1 WHERE 1")
conn.commit()
self.parent.viewUnits()
self.close()
else:
self.ui.device_barcode.clear()
self.ui.error_Bar.setText("*barcode does not exist")
else:
self.ui.device_id.clear()
self.ui.device_port.clear()
self.ui.device_barcode.clear()
self.ui.error_Dev.setText("*device exists")
connectDb.closeDatabaseConnection(conn, cur)
self.parent.viewUnits()
else:
if not ((did<65536) & (did>0)):
self.ui.device_id.clear()
self.ui.device_port.clear()
self.ui.device_barcode.clear()
self.ui.error_Dev.setText("*id must be <65536")
if not ((port<8) & (port>=0)):
self.ui.device_id.clear()
self.ui.device_port.clear()
self.ui.device_barcode.clear()
self.ui.error_Port.setText("*port must be btw 0-7")
else:
if not isNumber(did):
self.ui.device_id.clear()
self.ui.device_port.clear()
self.ui.device_barcode.clear()
self.ui.error_Dev.setText("*incorrect input")
if not isNumber(port):
self.ui.device_id.clear()
self.ui.device_port.clear()
self.ui.device_barcode.clear()
self.ui.error_Dev.setText("*incorrect input")
if not isNumber(barcode):
self.ui.device_id.clear()
self.ui.device_port.clear()
self.ui.device_barcode.clear()
self.ui.error_Bar.setText("*incorrect input")
if (len(str(barcode)) != 8):
self.ui.device_id.clear()
self.ui.device_port.clear()
self.ui.device_barcode.clear()
self.ui.error_Bar.setText("*incorrect input")
else:
if len(str(did)) == 0:
self.ui.device_id.clear()
self.ui.device_port.clear()
self.ui.device_barcode.clear()
self.ui.error_Dev.setText("*required")
if len(str(port)) == 0:
self.ui.device_port.clear()
self.ui.device_barcode.clear()
self.ui.error_Dev.setText("*required")
if len(str(barcode)) == 0:
self.ui.device_barcode.clear()
self.ui.error_Bar.setText("*required")