-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWin.py
More file actions
155 lines (140 loc) · 6.27 KB
/
MainWin.py
File metadata and controls
155 lines (140 loc) · 6.27 KB
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
import psycopg2
import PySimpleGUI as sg
class MainWin:
def MainLoop(self ):
while True: # Event Loop
window, event, values = sg.read_all_windows()
# print(window, event, values, end = "\n\n") # testing info TODO delete befor lunch
if event == sg.WIN_CLOSED or event == 'Exit':
if (window == self.windowMain):
break
else:
for i in range(len(self.windows)):
if self.windows[i] == window:
self.windows[i].close()
del self.windows[i]
break
elif event == 'Dodaj rekord':
self.DodajRekord(values)
elif event == 'Sprawdź tabele':
self.windows.append(self.prostyWidokTabeli(
values['-tabela-']))
print(len(self.windows))
elif event =='Raporty1':
self.windows.append(self.prostyWidokTabeli(
"preatyarmy" ))
elif event =='Raporty2':
self.windows.append(self.prostyWidokTabeli(
"heroesability"))
elif event =='Raporty3':
self.windows.append(self.prostyWidokTabeli(
"zamekprodukuje"))
elif event =='Raporty4':
self.windows.append(self.prostyWidokTabeli(
"kingdom_strenght_show"))
elif event =='Zamknij pod okna':
for win in self.windows:
win.close()
self.windows.clear()
self.windowMain.close()
self.conn.close()
self.conn2.close()
return 0
def DodajRekord(self, values):
if(self.poziomdostepu == "S" or self.poziomdostepu == "W"):
Tabela = values['-tabela-']
coll = self.TableCol[Tabela]
stringCol = ""
stringColVall = ""
for x in coll:
if(values[f"-IN_{Tabela}_{x}-"]!=''):
if stringCol != "" :
stringCol += ', '
stringCol += x
if stringColVall :
stringColVall += ', '
val = values[f"-IN_{Tabela}_{x}-"]
stringColVall += "'"+val+"'"
cur = self.conn.cursor()
try:
cur.execute(f"INSERT INTO \"{Tabela}\" ({stringCol}) values ({stringColVall});" )
except Exception as e:
sg.popup_error(f"{e.pgerror}")
self.conn.commit()
print(f" dodanie rekordu do Tabeli{Tabela}: ({stringCol}) {stringColVall}")
else:
sg.popup("nie masz uprawnień do dodawania rekordów")
# get all coll identification
def getLayoutForTable(self,name):
cur = self.conn2.cursor()
cur.execute("SELECT *\
FROM information_schema.columns\
WHERE table_schema = 'public'\
AND table_name = '"+name+"'\
;")
rows = cur.fetchall()
tab = []
coll = []
for row in rows:
co = " " or row[5]
if (row[5] != "nextval('\""+name+"_id_seq\"'::regclass)"):
tab.append([sg.Text(row[3], size=(12, 1)),sg.Text(f"({row[5]})", size=(15, 1)),sg.Text(f"({row[7]})", size=(10, 1)),
sg.Input(key="-IN_"+name+'_'+row[3]+'-')])
coll.append(row[3] or '')
else:
tab.append([sg.Text(row[3], size=(37, 1)),
sg.Text("auto incrementacja")])
self.TableCol[name] = coll
return tab
def getBaseTab(self):
cur = self.conn.cursor()
cur.execute(
"SELECT * FROM information_schema.tables WHERE table_schema = 'public' AND table_type = 'BASE TABLE';")
rows = cur.fetchall()
tab = []
for row in rows:
name = row[2]
tab.append(sg.Tab(name, self.getLayoutForTable(name)))
return tab
def __init__(self,poziomdostepu="S", database="bbsssvid", username="bbsssvid", password= "9QxksMlVDsQ1iF3N46ax6jYZ5ueB-gle", host="mel.db.elephantsql.com", port="5432") :
self.poziomdostepu = poziomdostepu
self.conn = psycopg2.connect(database=database, user=username, password=password, host=host, port=port)
self.conn2 = psycopg2.connect(database=database, user=username, password= password, host=host, port=port)
print("Opened database successfully")
sg.theme('Dark')
self.TableCol = dict([])
self.TableColAll = dict([])
self.col = [
[sg.Button('Dodaj rekord')],
[sg.Button('Sprawdź tabele')],
[sg.Button('Raporty1')],
[sg.Button('Raporty2')],
[sg.Button('Raporty3')],
[sg.Button('Raporty4')],
[sg.Button('Zamknij pod okna')]
]
self.layout =[
[
sg.Column(self.col),
sg.TabGroup([self.getBaseTab()], key='-tabela-', tab_location='top', selected_title_color='purple')
],
[sg.Text("*wartości defoltowe w () jeśli None dana jest wymagana")] ]
self.windowMain = sg.Window('GUI DB Menager', self.layout, finalize=True)
self.windows = []
self.MainLoop()
def prostyWidokTabeli(self, name):
cur = self.conn.cursor()
# all information of any table takem from data base
layout = None
if(self.poziomdostepu == "S" or self.poziomdostepu == "R"):
cur.execute("SELECT * FROM \""+name+"\"")
rows = cur.fetchall()
dane = []
column_names = [desc[0] for desc in cur.description]
for row in rows:
wiersz =[row[i] for i in range(len(row))]
dane.append(wiersz)
layout=[[sg.Table(values=dane, headings=column_names, justification= "center" , num_rows= len(rows) if len(rows)<=25 else 25 )]]
else:
layout=[[sg.Text("nie masz uprawnień do odczytu")]]
return sg.Window(name, layout,finalize=True)