-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_security.py
284 lines (264 loc) · 12.5 KB
/
main_security.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
import pandas as pd
import mysql.connector
import PySimpleGUI as sg
import random
import aspose.words as aw
import ctypes, sys
# Подключение к БД
conn = mysql.connector.connect(user='root', password='root', host='127.0.0.1', database='token_task')
cursor = conn.cursor(buffered=True)
# Импрот
def import_token(result_out, name):
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
for i in range(0, len(result_out)):
builder.writeln("Тип работы: " + result_out[i][0])
builder.writeln("Тема работы: " + result_out[i][1])
builder.writeln("Задание: " + result_out[i][2])
doc.save(str(name)+'.docx')
def pars(file):
# Чтение excel
df = pd.read_excel(io=file, engine='openpyxl', sheet_name='Лист1')
# Парс excel
result = []
# Парс ТЕМ
for i in range(0, len(df['Темы'].tolist())):
add_ser = 'INSERT INTO `themas` (`name`) VALUES (%s)'
result.append(df['Темы'].tolist()[i])
check_input = 'SELECT * FROM `themas` WHERE `name` = %s'
cursor.execute(check_input, result)
if cursor.fetchone() == None:
cursor.execute(add_ser, result)
conn.commit()
result = []
else:
result = []
# Парс ЗАДАНИЙ
for i in range(0, len(df['Задание'].tolist())):
add_ser = 'INSERT INTO `tasks` (`name`) VALUES (%s)'
result.append(df['Задание'].tolist()[i])
check_input = 'SELECT * FROM `tasks` WHERE `name` = %s'
cursor.execute(check_input, result)
if cursor.fetchone() == None:
cursor.execute(add_ser, result)
conn.commit()
result = []
else:
result = []
# Парс ТИПОВ ЗАДАНИЙ
for i in range(0, len(df['Тип задания'].tolist())):
add_ser = 'INSERT INTO `type_tasks` (`name`) VALUES (%s)'
result.append(df['Тип задания'].tolist()[i])
check_input = 'SELECT * FROM `type_tasks` WHERE `name` = %s'
cursor.execute(check_input, result)
if cursor.fetchone() == None:
cursor.execute(add_ser, result)
conn.commit()
result = []
else:
result = []
# Парс БИЛЕТОВ
for i in range(0, len(df['Темы'].tolist())):
add_ser = 'INSERT INTO `tokens` (`themas_id`, `tasks_id`, `type_tasks_id`) VALUES (%s, %s, %s)'
for j in range(0, len(df.values.tolist()[i])):
data_db = []
if j == 0:
data_db.append(df.values.tolist()[i][j])
check_input = 'SELECT `id` FROM `themas` WHERE `name` = %s'
cursor.execute(check_input, data_db)
result.append(cursor.fetchone()[0])
elif j == 1:
data_db.append(df.values.tolist()[i][j])
check_input = 'SELECT `id` FROM `tasks` WHERE `name` = %s'
cursor.execute(check_input, data_db)
result.append(cursor.fetchone()[0])
elif j == 2:
data_db.append(df.values.tolist()[i][j])
check_input = 'SELECT `id` FROM `type_tasks` WHERE `name` = %s'
cursor.execute(check_input, data_db)
result.append(cursor.fetchone()[0])
check_input = 'SELECT * FROM `tokens` WHERE `themas_id` = %s AND `tasks_id` = %s AND `type_tasks_id` = %s'
cursor.execute(check_input, result)
if cursor.fetchone() == None:
cursor.execute(add_ser, result)
conn.commit()
result = []
else:
result = []
sg.theme('dark grey 9')
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
def pars_win():
if is_admin():
# Code of your program here
text = sg.popup_get_file('Please enter a file name')
pars(text)
else:
# Re-run the program with admin rights
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
def import_(result_out):
layout = [[sg.Text('Введите название документа'), sg.InputText()],
[sg.Button('Ок', key='ok')]]
window = sg.Window('Выгрузка билета', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Cancel': # if user closes window or clicks cancel
break
if event == 'ok':
import_token(result_out, values[0])
def main():
cursor.execute('SELECT COUNT(`name`) FROM `themas`')
N = cursor.fetchone()
dropdown = []
index = []
for i in range(1, N[0]+1):
sel_them = 'SELECT `name` FROM `themas` WHERE `id` = %s'
index.append(i)
cursor.execute(sel_them, index)
dropdown.append(cursor.fetchone())
index = []
layout = [
[sg.InputCombo(dropdown, key='thema'), sg.Button('Сгенерировать', key='gen')],
[sg.Output(size=(88, 20))],
[sg.Button('Загрузить', key='open_p'), sg.Button('Выгрузить', key='im')]
]
window = sg.Window('Генератор билетов', layout)
while True:
event, values = window.read()
if event in (None, 'Exit'):
break
if event == 'open_p':
pars_win()
if event == 'gen':
cursor.execute('SELECT COUNT(`name`) FROM `themas`')
N = cursor.fetchone()
result_out = []
for i in range(1, N[0] + 1):
result = []
val = []
right = False
sel_them = 'SELECT `id`, `name` FROM `themas` WHERE `id` = %s'
index.append(i)
cursor.execute(sel_them, index)
val.append(cursor.fetchone())
index = []
write = 0
if values['thema'][0] == str(val[0][1]):
sel_teor = "SELECT id FROM `type_tasks` WHERE `name` = 'теория'"
cursor.execute(sel_teor)
index.append(cursor.fetchone()[0])
sel_count = 'SELECT MAX(tasks_id) FROM `tokens` ' \
'WHERE type_tasks_id = %s '\
'AND themas_id = %s '
index.append(val[0][0])
result.append(index[0])
cursor.execute(sel_count, index)
tt_count = cursor.fetchone()[0]
t_count = []
t_count.append(index[0])
sel_count = 'SELECT COUNT(type_tasks_id) FROM `tokens`' \
'WHERE type_tasks_id = %s'
cursor.execute(sel_count, t_count)
control = 0
for j in range(0, cursor.fetchone()[0]):
task = random.randint(1, tt_count)
if write == 2:
break
for n in range(0, tt_count):
if task != control:
sel_token = 'SELECT ' \
'(SELECT `name` FROM `type_tasks` ' \
'WHERE type_tasks.id=%s), ' \
'(SELECT `name` FROM `themas` ' \
'WHERE themas.id=%s), ' \
'(SELECT `name` FROM `tasks` ' \
'WHERE tasks.id=%s) ' \
'FROM `tokens`'
result.append(val[0][0])
result.append(task)
control = task
check_sel = 'SELECT * FROM `tokens` ' \
'WHERE type_tasks_id = %s ' \
'AND themas_id = %s ' \
'AND tasks_id = %s'
cursor.execute(check_sel, result)
if cursor.fetchone() != None:
cursor.execute(sel_token, result)
result_out.append(cursor.fetchone())
result = []
result.append(index[0])
write += 1
break
else:
result = []
result.append(index[0])
j -= 1
break
else:
j -= 1
break
if write == 2:
result = []
index = []
if values['thema'][0] == str(val[0][1]):
sel_teor = "SELECT id FROM `type_tasks` WHERE `name` = 'практика'"
cursor.execute(sel_teor)
index.append(cursor.fetchone()[0])
sel_count = 'SELECT MAX(tasks_id) FROM `tokens` ' \
'WHERE type_tasks_id = %s ' \
'AND themas_id = %s '
index.append(val[0][0])
result.append(index[0])
cursor.execute(sel_count, index)
tt_count = cursor.fetchone()[0]
p_count = []
p_count.append(index[0])
sel_count = 'SELECT COUNT(type_tasks_id) FROM `tokens` WHERE type_tasks_id=%s'
cursor.execute(sel_count, p_count)
for p in range(0, cursor.fetchone()[0]):
task = random.randint(0, tt_count)
for n in range(0, tt_count):
if right == False:
sel_token = 'SELECT ' \
'(SELECT `name` FROM `type_tasks` ' \
'WHERE type_tasks.id=%s), ' \
'(SELECT `name` FROM `themas` ' \
'WHERE themas.id=%s), ' \
'(SELECT `name` FROM `tasks` ' \
'WHERE tasks.id=%s) ' \
'FROM `tokens`'
result.append(val[0][0])
result.append(task)
check_sel = 'SELECT * FROM `tokens` ' \
'WHERE type_tasks_id = %s ' \
'AND themas_id = %s ' \
'AND tasks_id = %s'
cursor.execute(check_sel, result)
if cursor.fetchone() != None:
cursor.execute(sel_token, result)
result_out.append(cursor.fetchone())
result = []
result.append(index[0])
right = True
break
else:
result = []
result.append(index[0])
break
else:
break
if right == True:
break
for m in range(0, len(result_out)):
print("Тип работы: "+result_out[m][0])
print("Тема работы: "+result_out[m][1])
print("Задание: "+result_out[m][2])
print('')
if event == 'im':
import_(result_out)
window.close()
if __name__ == "__main__":
main()