forked from redu/qt-example
/
api_consumer.py
393 lines (331 loc) · 16.5 KB
/
api_consumer.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtCore, QtGui
from main_window import Ui_MainWindow
import urlRequest
import simplejson
# OBS: todos os ENVIRONMENTS estao escritos sem o n
class APIWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setupUi(self)
self.connect(self.enviroment, QtCore.SIGNAL('clicked()'),self.related)
self.connect(self.course, QtCore.SIGNAL('clicked()'),self.course_related)
self.connect(self.discipline, QtCore.SIGNAL('clicked()'),self.discipline_related)
self.connect(self.gang, QtCore.SIGNAL('clicked()'),self.gang_related)
self.connect(self.abort, QtCore.SIGNAL('clicked()'),self.clean_form)
self.connect(self.submit_ok, QtCore.SIGNAL('clicked()'),self.new)
self.connect(self.submit, QtCore.SIGNAL('clicked()'),self.nothing)
self.connect(self.edit, QtCore.SIGNAL('clicked()'),self.update)
self.connect(self.remove, QtCore.SIGNAL('clicked()'),self.kill)
self.name_label.setText('Nome')
# Funcao lista Coligado
def related(self):
self.clean_form()
global event
event = False
self.listWidget.clear()
self.comboBox.clear()
if self.checkBox_form.checkState() == 0:
self.checkBox_form.animateClick()
self.lineEdit_2.setText('Coligado')
if self.checkBox_name.checkState() == 0:
self.checkBox_name.animateClick()
if self.checkBox_path.checkState() == 0:
self.checkBox_path.animateClick()
if self.checkBox_initials.checkState() == 0:
self.checkBox_initials.animateClick()
if self.checkBox_workload.checkState() == 2:
self.checkBox_workload.animateClick()
if self.checkBox_description.checkState() == 0:
self.checkBox_description.animateClick()
self.textEdit.setText('Optional')
content = urlRequest.list_related()
for iterate in content:
self.comboBox.addItem(iterate['name'])
self.comboBox.activated[str].connect(self.onActivated_coligado)
for iterate in content:
self.listWidget.addItem(iterate['name'])
def onActivated_coligado(self, text):
self.listWidget.clear()
self.listWidget.addItem(text)
global current
current = text
# Funcao lista cursos depende do Coligado selecionado
def course_related(self):
self.clean_form()
if self.checkBox_form.checkState() == 0:
self.checkBox_form.animateClick()
self.lineEdit_2.setText('Curso')
if self.checkBox_name.checkState() == 0:
self.checkBox_name.animateClick()
if self.checkBox_path.checkState() == 0:
self.checkBox_path.animateClick()
if self.checkBox_initials.checkState() == 2:
self.checkBox_initials.animateClick()
if self.checkBox_workload.checkState() == 0:
self.checkBox_workload.animateClick()
if self.checkBox_description.checkState() == 0:
self.checkBox_description.animateClick()
self.textEdit.setText('Optional')
self.workload.setText('Optional')
if current:
content = urlRequest.get_courses(current)
if content == []:
self.listWidget.addItem('Nao ha curso cadastrado')
else:
for iterate in content:
self.comboBox.addItem(iterate['name'])
self.listWidget.addItem(iterate['name'])
self.comboBox.activated[str].connect(self.onActivated_courses)
# else:
# self.listWidget.addItem('Click em Coligados e escolha um Coligado no ComboBox')
def onActivated_courses(self, text):
self.listWidget.clear()
self.listWidget.addItem(text)
content = urlRequest.get_courses(current)
for iterate in content:
if text == iterate['name']:
global course_current
course_current = iterate['path']
def discipline_related(self):
self.clean_form()
if self.checkBox_form.checkState() == 0:
self.checkBox_form.animateClick()
self.lineEdit_2.setText('Disciplina')
if self.checkBox_name.checkState() == 0:
self.checkBox_name.animateClick()
if self.checkBox_path.checkState() == 2:
self.checkBox_path.animateClick()
if self.checkBox_initials.checkState() == 2:
self.checkBox_initials.animateClick()
if self.checkBox_workload.checkState() == 2:
self.checkBox_workload.animateClick()
if self.checkBox_description.checkState() == 0:
self.checkBox_description.animateClick()
if current:
discipline = urlRequest.get_spaces(course_current)
if discipline != []:
for iterate in discipline:
self.comboBox.addItem(iterate['name'])
self.listWidget.addItem(iterate['name'])
else:
self.listWidget.addItem('Nao ha disciplinas cadastradas')
self.comboBox.activated[str].connect(self.onActivated_discipline)
def onActivated_discipline(self, text):
self.listWidget.clear()
self.listWidget.addItem(text)
def gang_related(self):
self.clean_form()
if self.checkBox_form.checkState() == 0:
self.checkBox_form.animateClick()
self.lineEdit_2.setText('Turma')
if self.checkBox_name.checkState() == 2:
self.checkBox_name.animateClick()
if self.checkBox_path.checkState() == 2:
self.checkBox_path.animateClick()
if self.checkBox_initials.checkState() == 2:
self.checkBox_initials.animateClick()
if self.checkBox_workload.checkState() == 2:
self.checkBox_workload.animateClick()
if self.checkBox_description.checkState() == 2:
self.checkBox_description.animateClick()
if course_current:
result = urlRequest.get_user_gang(course_current)
if result != []:
for iterate in result:
self.comboBox.addItem(iterate['first_name'])
self.listWidget.addItem(iterate['first_name'])
self.comboBox.activated[str].connect(self.onActivated_gang)
def onActivated_gang(self, text):
global user_name_current
result = urlRequest.get_user_gang(course_current)
if result != []:
for iterate in result:
if iterate['first_name'] == text:
user_name_current = iterate
def nothing(self):
global event
event = False
form_name = str(self.lineEdit_2.text())
self.clean_form()
if form_name == 'Turma':
self.name_label.setText('Email')
self.listWidget.addItem('Para matricular um novo aluno preencha o campo email no formulario acima')
if self.checkBox_name.checkState() == 0:
self.checkBox_name.animateClick()
def clean_form(self):
self.comboBox.clear()
self.listWidget.clear()
self.name.setText('')
self.workload.setText('')
self.textEdit.setText('')
self.path.setText('')
self.initials.setText('')
# ============================================================
# metodo update
#
def update(self):
global event
event = True
form_name = str(self.lineEdit_2.text())
if form_name == 'Coligado':
content = urlRequest.list_related()
for iterate in content:
if iterate['name'] == current:
self.name.setText(iterate['name'])
self.path.setText(iterate['path'])
self.initials.setText(iterate['initials'])
if iterate['description'] and iterate['description'] != []:
self.description.setText(iterate['description'])
elif form_name == 'Curso':
content = urlRequest.get_courses(current)
for iterate in content:
if iterate['name'] == current:
self.name.setText(iterate['name'])
self.path.setText(iterate['path'])
if iterate['workload'] and iterate['workload'] != []:
self.workload.setText(str(iterate['workload']))
if iterate['description'] and iterate['description'] != []:
self.description.setText(iterate['description'])
elif form_name == 'Disciplina':
content = urlRequest.get_spaces(course_current)
for iterate in content:
if iterate['name'] == current:
self.name.setText(iterate['name'])
if iterate['description'] and iterate['description'] != []:
self.description.setText(iterate['description'])
def new(self):
# Butao Ok para o metodo update
# =============================================================================
if event:
form_name = str(self.lineEdit_2.text())
if form_name == 'Coligado':
dict_environment = {'name':'','path':'','initials':'','description':''}
dict_environment['name'] = str(self.name.text())
dict_environment['path'] = str(self.path.text())
dict_environment['initials'] = str(self.initials.text())
if str(self.textEdit.toPlainText()) != 'Optional':
dict_environment['description'] = str(self.textEdit.toPlainText())
self.clean_form()
self.listWidget.clear()
result = urlRequest.update_form(dict_environment, current, form_name)
if result.ok:
self.listWidget.addItem('O coligado foi atualizado com sucesso')
else:
self.listWidget.addItem('Erro')
self.name.setText(simplejson.loads(result.content)['name'][0])
self.path.setText(simplejson.loads(result.content)['path'][0])
elif form_name == 'Curso':
dict_course = {'name':'','path':'','workload':'','description':''}
dict_course['name'] = str(self.name.text())
dict_course['path'] = str(self.path.text())
if str(self.workload.text()) != 'Optional':
dict_course['workload'] = str(self.workload.text())
if str(self.textEdit.toPlainText()) != 'Optional':
dict_course['description'] = str(self.textEdit.toPlainText())
self.clean_form()
self.listWidget.clear()
result = urlRequest.update_form(dict_course,current, form_name)
if result.ok:
self.listWidget.addItem('O curso foi atualizado com sucesso')
else:
self.listWidget.addItem('Erro')
self.name.setText(simplejson.loads(result.content)['name'][0])
self.path.setText(simplejson.loads(result.content)['path'][0])
elif form_name == 'Disciplina':
dict_discipline = {'name':'','description':''}
dict_discipline['name'] = str(self.name.text())
dict_discipline['description'] = str(self.textEdit.toPlainText())
self.clean_form()
self.listWidget.clear()
result = urlRequest.update_form(dict_discipline,current, course_current)
if result.ok:
self.listWidget.addItem('O curso foi atualizado com sucesso')
else:
self.listWidget.addItem('Erro')
self.name.setText(simplejson.loads(result.content)['name'][0])
self.path.setText(simplejson.loads(result.content)['path'][0])
#
# =========================================================================
# Butao Ok para o metodo new
else:
form_name = str(self.lineEdit_2.text())
if form_name == 'Coligado':
# Cadastra coligado
dict_enviroment = {'name':'','path':'','initials':'','description':''}
dict_enviroment['name'] = str(self.name.text())
dict_enviroment['path'] = str(self.path.text())
dict_enviroment['initials'] = str(self.initials.text())
dict_enviroment['description'] = str(self.textEdit.toPlainText())
self.clean_form()
self.listWidget.clear()
result = urlRequest.new_enviroment(dict_enviroment)
if result.ok:
self.listWidget.addItem('Novo coligado adicionado')
else:
self.listWidget.addItem('Erro')
self.name.setText(simplejson.loads(result.content)['name'][0])
self.path.setText(simplejson.loads(result.content)['path'][0])
elif form_name == 'Curso':
# Cadastra curso
dict_course = {'name':'','path':'','workload':'','description':''}
dict_course['name'] = str(self.name.text())
dict_course['path'] = str(self.path.text())
dict_course['workload'] = str(self.workload.text())
if str(self.textEdit.toPlainText()) != 'Optional':
dict_course['description'] = str(self.textEdit.toPlainText())
self.clean_form()
self.listWidget.clear()
result = urlRequest.new_form(dict_course,current,form_name)
if result.ok:
self.listWidget.addItem('Novo curso cadastrado')
else:
self.listWidget.addItem('Erro')
self.name.setText(simplejson.loads(result.content)['name'][0])
self.path.setText(simplejson.loads(result.content)['path'][0])
elif form_name == 'Disciplina':
# Cadastra disciplina
dict_discipline = {'name':'','description':''}
dict_discipline['name'] = str(self.name.text())
dict_discipline['description'] = str(self.textEdit.toPlainText())
self.clean_form()
self.listWidget.clear()
result = urlRequest.new_form(dict_discipline,current,form_name)
if result.ok:
self.listWidget.addItem('Nova disciplina cadastrada')
else:
self.listWidget.addItem('Erro')
self.name.setText(simplejson.loads(result.content)['name'][0])
else:
# Matricular no curso
dict_enrollment_user = {'email':''}
dict_enrollment_user['email'] = str(self.name.text())
self.clean_form()
self.listWidget.clear()
result = urlRequest.new_form(dict_discipline,current,form_name)
if result.ok:
self.listWidget.addItem('Novo aluno matriculado')
else:
self.listWidget.addItem('Erro')
self.name.setText(simplejson.loads(result.content)['name'][0])
self.name_label.setText('Nome')
def kill(self):
name_form = str(self.lineEdit_2.text())
self.clean_form()
self.listWidget.clear()
if name_form != 'Coligado' and name_form != 'Curso' and name_form != 'Turma':
result = urlRequest.kill_current(course_current, current)
elif name_form == 'Turma':
result = urlRequest.kill_current(name_form, user_name_current)
else:
result = urlRequest.kill_current(name_form,current)
if result.ok:
self.listWidget.addItem('Remocao efetuada com sucesso')
else:
self.listWidget.addItem('Erro: Nao encontrado')
app = QtGui.QApplication(sys.argv)
form = APIWindow()
form.show()
app.exec_()