-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex_staff.pyw
287 lines (245 loc) · 12.2 KB
/
ex_staff.pyw
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
import tkinter as tk
from tkinter import filedialog
from tkinter import ttk
import datetime as dt
import csv
def staff_complete(file_name):
staff = {}
with open(file_name, newline='', encoding='utf-8') as f:
data = csv.reader(f, delimiter=";")
for row in data:
login = row[0]
data_employee = {}
if "Login" not in login:
data_employee['name'] = row[1]
data_employee['last_name'] = row[2]
data_employee['position'] = row[4]
data_employee['department'] = row[6]
staff[login] = data_employee
return staff
def employees_complete(staff):
tmp_dict = {}
sorted_dict = {}
for login, data in staff.items():
tmp_dict[login] = data['position']
sorted_list = sorted(tmp_dict.items(), key=lambda x: x[1])
for el in sorted_list:
sorted_dict[el[0]] = el[1]
return sorted_dict
def positions_complete(staff):
positions_list = []
for login, data in staff.items():
positions_list.append(data["position"])
return sorted(set(positions_list))
def departments_complete(staff):
departments_list = []
for login, data in staff.items():
departments_list.append(data["department"])
return sorted(set(departments_list))
class Common():
def __init__(self):
self.root = tk.Tk()
self.root.geometry('1000x600')
self.root.minsize(1200, 600)
self.root.title('ex_staff')
self.rad_var = tk.StringVar()
self.rad_var.set('position')
self.date_var = tk.StringVar()
self.date_var.set(str(dt.date.today()))
self.ticket_var = tk.StringVar()
self.ticket_var.set('SECALERTS-XXXXXX')
self.data_employees = {}
self.positions = {}
self.departments = {}
def init_frames(self):
self.main_frame = ttk.Frame(self.root)
self.btn_open_file_frame = ttk.Frame(self.main_frame)
self.radio_btn_frame = ttk.Frame(self.main_frame)
self.canvas_frame = ttk.Frame(self.main_frame)
self.canvas = tk.Canvas(self.canvas_frame, borderwidth=0, highlightthickness=0)
self.canvas_scroll_y = ttk.Scrollbar(self.canvas_frame, orient='vertical', command=self.canvas.yview)
self.scrollable_frame = ttk.Frame(self.canvas)
self.canvas.config(yscrollcommand=self.canvas_scroll_y.set)
self.canvas.bind("<Enter>", lambda event: self.canvas.bind_all("<MouseWheel>", self.on_mouse_wheel))
self.canvas.bind("<Leave>", lambda event: self.canvas.unbind_all("<MouseWheel>"))
self.scrollable_frame.bind('<Configure>', lambda e: self.frame_configure())
self.wrap_frame = self.canvas.create_window((0, 0), window=self.scrollable_frame, anchor='nw')
self.btn_save_frame = ttk.Frame(self.main_frame)
self.text_frame = ttk.Frame(self.root)
self.entry_frame = ttk.Frame(self.text_frame)
self.entry_frame_date = ttk.LabelFrame(self.entry_frame, text='Date')
self.entry_frame_ticket = ttk.LabelFrame(self.entry_frame, text='Ticket')
def frame_configure(self):
canvas = self.canvas
canvas.configure(scrollregion=canvas.bbox("all"))
canvas.itemconfigure(self.wrap_frame, width=canvas.winfo_width())
def on_mouse_wheel(self, event, scale=3):
canvas = self.canvas
if event.delta<0:
canvas.yview_scroll(scale, "units")
else:
canvas.yview_scroll(-scale, "units")
def init_buttons(self):
self.btn_open_file = ttk.Button(self.btn_open_file_frame, text='Open File', width=70, command=self.open_file)
self.btn_generate = ttk.Button(self.btn_save_frame, text='Generate Data', command=self.get_data_check)
self.btn_save_txt = ttk.Button(self.btn_save_frame, text='Save to TXT', command=self.save_txt)
self.btn_save_csv = ttk.Button(self.btn_save_frame, text='Save to CSV', command=self.save_csv)
def init_radio_buttons(self):
self.rad_pos = ttk.Radiobutton(self.radio_btn_frame, text='Position',
variable=self.rad_var, value='position', command=self.get_type_data)
self.rad_dep = ttk.Radiobutton(self.radio_btn_frame, text='Department',
variable=self.rad_var, value='department', command=self.get_type_data)
self.rad_emp = ttk.Radiobutton(self.radio_btn_frame, text='Employees',
variable=self.rad_var, value='employees', command=self.get_type_data)
def init_text_data(self):
self.text_date = tk.Entry(self.entry_frame_date, textvariable=self.date_var)
def init_text_ticket(self):
self.text_ticket = tk.Entry(self.entry_frame_ticket, textvariable=self.ticket_var)
def init_text_field(self):
self.text_box = tk.Text(self.text_frame, wrap='none')
self.scroll_y = ttk.Scrollbar(self.text_frame, orient='vertical', command=self.text_box.yview)
self.scroll_x = ttk.Scrollbar(self.text_frame, orient='horizontal', command=self.text_box.xview)
self.text_box.config(yscrollcommand=self.scroll_y.set)
self.text_box.config(xscrollcommand=self.scroll_x.set)
def pack_widgets(self):
self.main_frame.pack(anchor='nw', side='left', fill='both', padx=5, pady=5)
self.text_frame.pack(anchor='nw', fill='both', expand=True, padx=5, pady=5)
self.entry_frame.pack(anchor='nw', fill='x', padx=5, pady=5)
self.entry_frame_date.pack(side='left', fill='x', expand=True)
self.entry_frame_ticket.pack(side='left', fill='x', expand=True)
self.btn_open_file_frame.pack(anchor='nw', side='top', fill='x', padx=5, pady=5)
self.btn_save_frame.pack(side='bottom', fill='x', padx=5)
self.radio_btn_frame.pack()
self.canvas_frame.pack(fill='both', expand=True, padx=5)
self.canvas.pack(side='left', fill='both', expand=True)
self.canvas_scroll_y.pack(side='right', fill='y')
self.rad_pos.pack(side='left')
self.rad_dep.pack(side='left')
self.rad_emp.pack(side='left')
self.text_date.pack(side='top', fill='x', expand=True, padx=5, pady=5)
self.text_ticket.pack(side='top', fill='x', expand=True, padx=5, pady=5)
self.scroll_y.pack(side='right', fill='y')
self.scroll_x.pack(side='bottom', fill='x')
self.text_box.pack(side='bottom', fill='both', expand=True, padx=5, pady=5)
self.btn_open_file.pack(side='left', fill='x', expand=True)
self.btn_generate.pack(side='top', fill='x', expand=True, pady=5)
self.btn_save_txt.pack(side='left', fill='x', expand=True)
self.btn_save_csv.pack(side='left', fill='x', expand=True)
def open_file(self):
self.file = filedialog.askopenfilename(filetypes=[('CSV files', '*.csv')])
self.file_name = self.file
self.data_employees = staff_complete(self.file_name)
self.positions = positions_complete(self.data_employees)
self.departments = departments_complete(self.data_employees)
self.employees = employees_complete(self.data_employees)
self.update_data()
self.root.update()
def get_data_check(self):
try:
data_check = {}
for el in self.checks:
data_check[el] = self.checks[el].get()
if self.rad_var.get() == 'position':
generate_data = ''
for key, value in data_check.items():
if value == 1:
for login, data in self.data_employees.items():
for el in data:
if key == data[el]:
generate_data += f'{self.text_date.get()},{login},,"{self.text_ticket.get()}"\n'
self.text_box.delete('1.0', 'end-1c')
self.text_box.insert('1.0', generate_data)
if self.rad_var.get() == 'department':
data_list = []
generate_data = ''
for key, value in data_check.items():
if value == 1:
for login, data in self.data_employees.items():
for el in data:
if key == data[el]:
temp = data[el]
temp = temp.replace('"', '""')
line = f'{self.text_date.get()},,"{temp}","{self.text_ticket.get()}"\n'
if line not in data_list:
data_list.append(line)
for el in data_list:
generate_data += el
self.text_box.delete('1.0', 'end-1c')
self.text_box.insert('1.0', generate_data)
if self.rad_var.get() == 'employees':
generate_data = ''
for login, value in data_check.items():
if value == 1:
generate_data += f'{self.text_date.get()},{login},,"{self.text_ticket.get()}"\n'
self.text_box.delete('1.0', 'end-1c')
self.text_box.insert('1.0', generate_data)
except:
generate_data = ''
logins = self.text_box.get('1.0', 'end-1c')
for login in logins.split():
generate_data += f'{self.text_date.get()},{login.rstrip("@")},,"{self.text_ticket.get()}"\n'
self.text_box.delete('1.0', 'end-1c')
self.text_box.insert('1.0', generate_data)
def get_type_data(self):
self.rad_var.set(self.rad_var.get())
self.update_data()
def complete_check_data(self):
self.checks = {}
if self.rad_var.get() == 'position':
temp_data = self.positions
for el in temp_data:
self.checks[el] = tk.IntVar()
for el in temp_data:
self.checkbox_frame = ttk.Frame(self.scrollable_frame)
check_box = ttk.Checkbutton(self.checkbox_frame, text=el, variable=self.checks[el], onvalue=1, offvalue=0)
check_box.pack(anchor='nw')
self.checkbox_frame.pack(anchor='nw', padx=5)
elif self.rad_var.get() == 'department':
temp_data = self.departments
for el in temp_data:
self.checks[el] = tk.IntVar()
for el in temp_data:
self.checkbox_frame = ttk.Frame(self.scrollable_frame)
check_box = ttk.Checkbutton(self.checkbox_frame, text=el, variable=self.checks[el], onvalue=1, offvalue=0)
check_box.pack(anchor='nw')
self.checkbox_frame.pack(anchor='nw', padx=5)
else:
temp_data = self.employees
for el in temp_data:
self.checks[el] = tk.IntVar()
for key, value in temp_data.items():
self.checkbox_frame = ttk.Frame(self.scrollable_frame)
text = f'{key}@ - {value}'
check_box = ttk.Checkbutton(self.checkbox_frame, text=text, variable=self.checks[key], onvalue=1, offvalue=0)
check_box.pack(anchor='nw')
self.checkbox_frame.pack(anchor='nw', padx=5)
return self.checks
def save_txt(self):
date = self.date_var.get().split('-')
with open(f'{date[-1]}.{date[-2]}.txt', 'w', encoding='utf-8') as f:
f.write(self.text_box.get("1.0", 'end-1c'))
def save_csv(self):
date = self.date_var.get().split('-')
with open(f'{date[-1]}.{date[-2]}.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
for line in self.text_box.get("1.0", 'end-1c').split('\n'):
writer.writerow(line.split(','))
def init_widgets(self):
self.init_frames()
self.init_buttons()
self.init_radio_buttons()
self.init_text_data()
self.init_text_ticket()
self.init_text_field()
self.pack_widgets()
def update_data(self):
self.main_frame.destroy()
self.text_frame.destroy()
self.init_widgets()
self.complete_check_data()
def run_build(self):
self.init_widgets()
self.root.mainloop()
if __name__ == '__main__':
common = Common()
common.run_build()