This repository has been archived by the owner on May 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Table.py
177 lines (146 loc) · 9.26 KB
/
Table.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
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import traceback
import sqlite3 as sqlite
from Employee import Employee
class Table:
"""
Клас Table створено для будь-якої взаємодій, що пов'язана з табелями:
додання/видалення/редагування, що можна побачити з однойменних методів,
які мають доступ до відповідних таблиць всередині баз даних, де зберігається вся інформація
"""
def __init__(self, table="Tables", database="templates/db/database.db"):
self.table = table
self.database = database
def add_table(self, employee_name, month, year, hours):
self.add_to_db(employee_name, month, year, hours)
def remove_table(self, id):
try:
db = sqlite.connect(self.database)
with db:
conn = db.cursor()
conn.execute("DELETE FROM {} WHERE id={}".format(self.table, id))
except Exception as e:
print(e.args)
def edit_table(self, id, employee_name, month, year, hours):
try:
db = sqlite.connect(self.database)
with db:
conn = db.cursor()
employees = Employee().get_all_db_data()
conn.execute("SELECT * FROM {} WHERE id={}".format(self.table, id))
responce = conn.fetchall()
if (len(employee_name) == 0):
employee_name = responce[0][1]
if (len(str(month)) == 0):
salary = responce[0][2]
if (len(str(year)) == 0):
year = responce[0][3]
if (len(str(hours)) == 0):
hours = responce[0][4]
employee_dict = {}
for employee in employees:
employee_dict[employee["name"]] = employee["id"]
employee_id = employee_dict[employee_name]
conn.execute(
"UPDATE {} SET employeeId = '{}', month = '{}', year = '{}', hours='{}' WHERE id = {}"
.format(self.table, employee_id, month, year, hours, id)
)
except Exception as e:
print("Troubles with edit_table: " + e.args[0])
print(traceback.format_exc())
def get_all_db_data(self):
db = sqlite.connect(self.database)
db.row_factory = sqlite.Row
with db:
conn = db.cursor()
conn.execute("SELECT * FROM {}".format(self.table))
data = conn.fetchall()
return data
# функція init_table в проекті не використовується, але вона дозволяє перезаписати таблицю з БД з деякими початковими записами
def init_table(self):
db = sqlite.connect(self.database)
with db:
conn = db.cursor()
conn.execute("DROP TABLE {}".format(self.table))
# звідси видно, які поля має таблиця для збереження табелів працівників
conn.execute("CREATE TABLE {} (id INTEGER, employeeId INTEGER, month INTEGER, year INTEGER, hours TEXT)".format(self.table))
db.commit()
tables = (
(1, 1, 1, 2016, "v v 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"),
(2, 1, 2, 2016, "v v 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"),
(3, 1, 3, 2015, "v v 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"),
(4, 1, 4, 2015, "v v 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"),
(5, 1, 5, 2015, "8 v 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"),
(6, 1, 6, 2015, "v v 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"),
(7, 1, 7, 2015, "v v 8 8 9 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"),
(8, 1, 8, 2015, "v v 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8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 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"),
(37, 4, 1, 2017, "v v 8 8 8 8 3 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"),
(38, 4, 2, 2017, "v v 8 8 8 8 8 8 8 8 8 8 8 8 8 8 9 8 8 8 8 8 8 8 8 8 8 8 8 8"),
(39, 4, 3, 2017, "v v 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 0 8 8 8 8 8 8 8 8 8 8 8 8"),
(40, 4, 4, 2017, "v v 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 1 8 8 8 8 8 8 8 8 8 8 8 8"),
(41, 4, 5, 2017, "v v 8 8 8 8 8 8 8 8 8 8 8 7 8 8 3 8 8 8 8 8 8 8 8 8 8 8 8 8"),
(42, 4, 6, 2017, "v v 8 8 8 8 8 8 8 8 8 6 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"),
(43, 4, 7, 2017, "v v 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"),
(44, 4, 8, 2017, "v v 8 8 8 8 8 8 8 h 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"),
(45, 4, 9, 2016, "v v v v v v v v 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"),
(46, 4, 10, 2016, "h h 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"),
(47, 4, 11, 2016, "h h 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8"),
(48, 4, 12, 2016, "h h 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8")
)
conn.executemany("INSERT INTO {} (id, employeeId, month, year, hours) VALUES (?, ?, ?, ?, ?)"
.format(self.table), tables)
db.commit()
def add_to_db(self, name, month, year, hours):
try:
db = sqlite.connect(self.database)
with db:
conn = db.cursor()
id = 1
try:
conn.execute("SELECT MAX(id) FROM {}".format(self.table))
db.commit()
max_id = conn.fetchall()
id = max_id[0][0] + 1
except Exception as e:
print("Inserting into empty table: " + self.table + " new index equals " + str(id))
employees = Employee().get_all_db_data()
employeeId = 0
for employee in employees:
if name == employee["name"]:
employeeId = employee["id"]
new_table = (id, employeeId, month, year, hours)
conn.execute(
"INSERT INTO {} (id, employeeId, month, year, hours) VALUES (?,?,?,?,?)"
.format(self.table), new_table
)
except Exception as e:
print("Troubles with add_commodity_to_db: " + e.args[0])