forked from aquarimeter/aquarimeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
initialsetup.py
288 lines (237 loc) · 9.84 KB
/
initialsetup.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk
import json
import requests
import twisted
import sys
import os
import collections
url = "http://aquarimeter.rocks/api/v1/"
class Aquarium_Setup:
#checks fields are filled in on form
def check_register(self,widget):
fill = self.validate_fields()
long_pwd = self.check_length(self.valid_Password.get_text())
temp_valid = self.check_temp()
if(not fill):
self.error_fill()
elif(not long_pwd):
self.error_password()
elif(not temp_valid):
self.error_temp_range()
else:
self.register()
#checks required fields is filled before registering
def validate_fields(self):
if(not self.valid_Email.get_text() or
not self.valid_Password.get_text() or
not self.first_Name.get_text() or
not self.last_Name.get_text()):
return False
else:
return True
#checks password is long enough
def check_length(self,password):
if(len(password) < 10):
return False
else:
return True
#checks temperature values are valid
def check_temp(self):
idealT = self.ideal_spin.get_value()
minT = self.min_spin.get_value()
maxT = self.max_spin.get_value()
if(minT <= idealT and idealT<= maxT):
return True
else:
return False
#pop up message box asking to fill all fields
def error_fill(self):
info = gtk.MessageDialog(type=gtk.MESSAGE_WARNING, buttons=gtk.BUTTONS_OK)
info.set_property('title', 'Missing information')
info.set_property('text', 'Please fill in all fields')
info.run()
info.destroy()
#pop up message for short password
def error_password(self):
info = gtk.MessageDialog(type=gtk.MESSAGE_WARNING, buttons=gtk.BUTTONS_OK)
info.set_property('title', 'Password too short')
info.set_property('text', 'Password must be at least 10 characters long')
info.run()
info.destroy()
#pop up message for bad temperature set
def error_temp_range(self):
info = gtk.MessageDialog(type=gtk.MESSAGE_WARNING, buttons=gtk.BUTTONS_OK)
info.set_property('title', 'Wrong temperature range')
info.set_property('text', 'Ideal temperature must be between Min and Max temperature.\n'
'Min temperature must be less than or equal to Max temperature')
info.run()
info.destroy()
#registers with entered information from gui
def register(self):
#json payload send happens here
#storing email and password so no need to get again
email = self.valid_Email.get_text()
password = self.valid_Password.get_text()
firstnm = self.first_Name.get_text()
lastnm = self.last_Name.get_text()
register_data = {"user":{"email":email,
"password":password,
"first_name":self.first_Name.get_text(),
"last_name":self.last_Name.get_text()}}
reg_data = json.dumps(register_data)
url_reg = "http://aquarimeter.rocks:5000/api/v1/register"
reg = requests.post(url_reg,reg_data)
if reg.status_code == 200:
self.login(email,password)
elif reg.status_code == 422:
self.error_register_422()
else:
self.error_register()
#pop up message for registeration error code == 422
def error_register_422(self):
info = gtk.MessageDialog(type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_CLOSE)
info.set_property('title', 'Error 422')
info.set_property('text', 'Something went wrong with validation')
info.run()
info.destroy()
#pop up message for any other status code except 200 and 422
def error_register(self):
info = gtk.MessageDialog(type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_CLOSE)
info.set_property('title', 'Error with validation')
info.set_property('text', 'Site is suffering internel '
'conflict or url is no longer valid')
info.run()
info.destroy()
#login with password and email
def login(self,email,password):
#login happens here
login_data = {"user":{"email":email,
"password":password}}
log_data = json.dumps(login_data)
url_log = "http://aquarimeter.rocks:5000/api/v1/login"
log = requests.post(url_log, data = url_log)
if log.status_code == 200:
#auth_token, ideal_temp, min_temp and max_temp will be passed
#to aquarimete program
auth_token = log[auth_token]
idealT = self.ideal_spin.get_value()
minT = self.min_spin.get_value()
maxT = self.max_spin.get_value()
comand = "sudo aquarimeter.py" + auth_token + idealT + minT + maxT
os.system(comand)
self.successful_login()
elif log.status_code == 401:
self.error_login_401()
else:
self.error_login()
#successful login starts aquarimeter and closes initial setup
def successful_login(self):
info = gtk.MessageDialog(type=gtk.MESSAGE_INFO, buttons=gtk.BUTTONS_CLOSE)
info.set_property('title', 'Error with login')
info.set_property('text', 'Unknown error has occured url might be bad')
info.run()
info.destroy()
#login is invalid. status code 401
def error_login_401(self):
info = gtk.MessageDialog(type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_CLOSE)
info.set_property('title', 'Error 401')
info.set_property('text', 'username/password is incorrect. ')
info.run()
info.destroy()
#any other status code covered here
def error_login(self):
info = gtk.MessageDialog(type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_CLOSE)
info.set_property('title', 'Error with login')
info.set_property('text', 'Unknown error has occured url might be bad')
info.run()
info.destroy()
#main gui window
def __init__(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title("Aquarimeter Intial Setup")
self.window.connect("destroy", lambda wid: gtk.main_quit())
main_Box = gtk.VBox(False, 5) #all items will be arranged vertically in here
self.window.add(main_Box)
self.window.set_border_width(10)
label = gtk.Label("Please enter all intial values as indicated. Your"
"settings will be saved in\n Aquarimeter Web, the"
" web application which helps you view the status "
"of your\n aquarium. It can be at http://aquarium."
"oconnor.ninja/")
main_Box.pack_start(label)
#create fields for registeration information to be entered
reg_frame = gtk.Frame("Login Details for Aquarimeter Web")
#labels for information needed
info_Box = gtk.HBox(False,5)
vbox1 = gtk.VBox(False,5)
info_Box.add(vbox1)
label = gtk.Label("E-mail")
vbox1.add(label)
label = gtk.Label("Password")
vbox1.add(label)
label = gtk.Label("First Name")
vbox1.add(label)
label = gtk.Label("Last Name")
vbox1.add(label)
#fields to enter information
vbox2 = gtk.VBox(False,5)
info_Box.add(vbox2)
self.valid_Email = gtk.Entry()
vbox2.add(self.valid_Email)
self.valid_Password = gtk.Entry()
vbox2.add(self.valid_Password)
self.first_Name = gtk.Entry()
vbox2.add(self.first_Name)
self.last_Name = gtk.Entry()
vbox2.add(self.last_Name)
reg_frame.add(info_Box)
main_Box.pack_start(reg_frame, False, False, 0)
#labels for varias temperature settings
temp_frame = gtk.Frame("Temperature set")
vbox3 = gtk.VBox(False,5)
hbox3 = gtk.HBox(False,5)
hbox3.add(vbox3)
label = gtk.Label("Intial Temperature")
vbox3.add(label)
label = gtk.Label("Min Temperature")
vbox3.add(label)
label = gtk.Label("Max Temperature")
vbox3.add(label)
#temperature selection setting
vbox4 = gtk.VBox(False,5)
hbox3.add(vbox4)
adj1 = gtk.Adjustment(70.0, 55.0, 85.0, 1.0, 5.0, 0.0)
self.ideal_spin = gtk.SpinButton(adj1, 0, 0)
vbox4.add(self.ideal_spin)
adj2 = gtk.Adjustment(70.0, 55.0, 85.0, 1.0, 5.0, 0.0)
self.min_spin = gtk.SpinButton(adj2, 0, 0)
vbox4.add(self.min_spin)
adj3 = gtk.Adjustment(70.0, 55.0, 85.0, 1.0, 5.0, 0.0)
self.max_spin = gtk.SpinButton(adj3, 0, 0)
vbox4.add(self.max_spin)
#temperature units setting
vbox5 = gtk.VBox(False,0)
hbox3.add(vbox5)
label = gtk.Label("Fahrenheit")
vbox5.add(label)
temp_frame.add(hbox3)
main_Box.pack_start(temp_frame, False, False, 0)
#cancel and submit button section
hbox10 = gtk.HBox(False,2)
main_Box.pack_start(hbox10, False,False,0)
button = gtk.Button("Submit and Register")
button.connect("clicked", self.check_register)
hbox10.pack_end(button,False,False,2)
button = gtk.Button("cancel")
button.connect_object("clicked",gtk.Widget.destroy, self.window)
hbox10.pack_end(button,False,False,2)
#show everything in a window
self.window.show_all()
def main(self):
gtk.main()
if __name__ == "__main__":
Aqua_Setup = Aquarium_Setup()
Aqua_Setup.main()