Skip to content

Commit f17c1ed

Browse files
committed
Dodano brisanje entitet
1 parent bc7dd58 commit f17c1ed

File tree

7 files changed

+216
-55
lines changed

7 files changed

+216
-55
lines changed

predavanja/primeri/banka/banka.py

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,50 @@
1515
bottle.debug(True) # za izpise pri razvoju
1616

1717

18+
def nastavi_piskotek(piskotek, vsebina):
19+
"""
20+
Nastavi podani piškotek.
21+
"""
22+
bottle.response.set_cookie(piskotek, vsebina, secret=SKRIVNOST, path='/')
23+
24+
25+
def preberi_piskotek(piskotek, izbrisi=False):
26+
"""
27+
Preberi podani piškotek.
28+
"""
29+
if izbrisi:
30+
pobrisi_piskotek(piskotek)
31+
return bottle.request.get_cookie(piskotek, secret=SKRIVNOST)
32+
33+
34+
def pobrisi_piskotek(piskotek):
35+
"""
36+
Pobriši podani piškotek.
37+
"""
38+
bottle.response.delete_cookie(piskotek, path='/')
39+
40+
41+
def nastavi_sporocilo(sporocilo):
42+
"""
43+
Nastavi podano sporočilo.
44+
"""
45+
nastavi_piskotek('sporocilo', sporocilo)
46+
47+
48+
def preberi_sporocilo():
49+
"""
50+
Vrni sporočilo in pobriši piškotek.
51+
"""
52+
return preberi_piskotek('sporocilo', izbrisi=True)
53+
54+
1855
bottle.BaseTemplate.defaults.update(
1956
Kraj=Kraj,
2057
Oseba=Oseba,
2158
Racun=Racun,
2259
Transakcija=Transakcija,
23-
url=bottle.url
60+
url=bottle.url,
61+
preberi_sporocilo=preberi_sporocilo
2462
)
2563

2664

@@ -41,24 +79,64 @@ def kraji():
4179
pass
4280

4381

82+
@bottle.post('/kraji/izbrisi/<posta:int>/')
83+
def izbrisi_kraj(posta):
84+
try:
85+
Kraj.izbrisi_id(posta)
86+
nastavi_sporocilo(f'Kraj s poštno številko {posta} uspešno pobrisan.')
87+
except ValueError:
88+
nastavi_sporocilo(f'Brisanje kraja s poštno številko {posta} neuspešno!')
89+
bottle.redirect(bottle.url('kraji'))
90+
91+
4492
@bottle.get('/komitenti/')
4593
@bottle.view('komitenti.html')
4694
def komitenti():
4795
pass
4896

4997

98+
@bottle.post('/komitenti/izbrisi/<emso>/')
99+
def izbrisi_komitenta(emso):
100+
try:
101+
Oseba.izbrisi_id(emso)
102+
nastavi_sporocilo(f'Komitent z EMŠOm {emso} uspešno pobrisan.')
103+
except ValueError:
104+
nastavi_sporocilo(f'Brisanje komitenta z EMŠOm {emso} neuspešno!')
105+
bottle.redirect(bottle.url('komitenti'))
106+
107+
50108
@bottle.get('/racuni/')
51109
@bottle.view('racuni.html')
52110
def racuni():
53111
pass
54112

55113

114+
@bottle.post('/racuni/izbrisi/<stevilka:int>/')
115+
def izbrisi_racun(stevilka):
116+
try:
117+
Racun.izbrisi_id(stevilka)
118+
nastavi_sporocilo(f'Račun s številko {stevilka} uspešno pobrisan.')
119+
except:
120+
nastavi_sporocilo(f'Brisanje računa s številko {stevilka} neuspešno!')
121+
bottle.redirect(bottle.url('racuni'))
122+
123+
56124
@bottle.get('/transakcije/')
57125
@bottle.view('transakcije.html')
58126
def transakcije():
59127
pass
60128

61129

130+
@bottle.post('/transakcije/izbrisi/<id:int>/')
131+
def izbrisi_transakcijo(id):
132+
try:
133+
Transakcija.izbrisi_id(id)
134+
nastavi_sporocilo(f'Transakcija z ID-jem {id} uspešno pobrisana.')
135+
except:
136+
nastavi_sporocilo(f'Brisanje transakcije z ID-jem {id} neuspešno!')
137+
bottle.redirect(bottle.url('transakcije'))
138+
139+
62140
with vzpostavi_povezavo(port=DB_PORT):
63141
# reloader=True nam olajša razvoj (osveževanje sproti - razvoj)
64142
bottle.run(host='localhost', port=SERVER_PORT, reloader=RELOADER)

predavanja/primeri/banka/orm.py

Lines changed: 73 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -251,71 +251,91 @@ def vstavi(self):
251251
podatki.append(sql.Literal(vrednost))
252252
elif stolpec.metadata["stevec"]:
253253
generirani.append(stolpec.name)
254-
with conn.transaction():
255-
with conn.cursor() as cur:
256-
cur.execute(sql.SQL("""
257-
INSERT INTO {tabela} ({stolpci}) VALUES ({podatki})
258-
{generirano};
259-
""").format(
260-
tabela=sql.Identifier(self.tabela()),
261-
stolpci=VEJICA.join(sql.Identifier(stolpec)
262-
for stolpec in stolpci),
263-
podatki=VEJICA.join(podatki),
264-
generirano=sql.SQL("RETURNING {generirani}").format(
265-
generirani=VEJICA.join(sql.Identifier(stolpec)
266-
for stolpec in generirani)
267-
) if generirani else PRAZNO
268-
))
269-
if generirani:
270-
for kljuc, vrednost in zip(generirani, cur.fetchone()):
271-
setattr(self, kljuc, vrednost)
272-
self.__nastavi_id()
254+
try:
255+
with conn.transaction():
256+
with conn.cursor() as cur:
257+
cur.execute(sql.SQL("""
258+
INSERT INTO {tabela} ({stolpci}) VALUES ({podatki})
259+
{generirano};
260+
""").format(
261+
tabela=sql.Identifier(self.tabela()),
262+
stolpci=VEJICA.join(sql.Identifier(stolpec)
263+
for stolpec in stolpci),
264+
podatki=VEJICA.join(podatki),
265+
generirano=sql.SQL("RETURNING {generirani}").format(
266+
generirani=VEJICA.join(sql.Identifier(stolpec)
267+
for stolpec in generirani)
268+
) if generirani else PRAZNO
269+
))
270+
if generirani:
271+
for kljuc, vrednost in zip(generirani, cur.fetchone()):
272+
setattr(self, kljuc, vrednost)
273+
self.__nastavi_id()
274+
except errors.IntegrityError:
275+
raise ValueError(f'Napaka pri vstavljanju {self.__class__.__name__} z ID-jem {id}!')
276+
except errors.DataError:
277+
raise TypeError(f'Napaka pri vstavljanju {self.__class__.__name__} z ID-jem {id}!')
273278

274279
def posodobi(self):
275280
"""
276281
Posodobi entiteto v bazi.
277282
"""
278283
assert self.__dbid is not None, "Entiteta še ni v bazi!"
279-
with conn.transaction():
280-
with conn.cursor() as cur:
281-
cur.execute(sql.SQL("""
282-
UPDATE {tabela} SET {vrednosti}
283-
WHERE {glavni_kljuc} = {id};
284-
""").format(
285-
tabela=sql.Identifier(self.tabela()),
286-
vrednosti=VEJICA.join(
287-
sql.SQL("""
288-
{stolpec} = {vrednost}
289-
""").format(
290-
stolpec=sql.Identifier(stolpec.name),
291-
vrednost=vrednost.niz
292-
if isinstance(vrednost, Funkcija)
293-
else sql.Literal(vrednost)
294-
)
295-
for stolpec in fields(self)
296-
for vrednost in [self[stolpec.name]]
297-
),
298-
glavni_kljuc=sql.Identifier(self.glavni_kljuc().name),
299-
id=sql.Literal(self.__dbid)
300-
))
301-
self.__nastavi_id()
284+
try:
285+
with conn.transaction():
286+
with conn.cursor() as cur:
287+
cur.execute(sql.SQL("""
288+
UPDATE {tabela} SET {vrednosti}
289+
WHERE {glavni_kljuc} = {id};
290+
""").format(
291+
tabela=sql.Identifier(self.tabela()),
292+
vrednosti=VEJICA.join(
293+
sql.SQL("""
294+
{stolpec} = {vrednost}
295+
""").format(
296+
stolpec=sql.Identifier(stolpec.name),
297+
vrednost=vrednost.niz
298+
if isinstance(vrednost, Funkcija)
299+
else sql.Literal(vrednost)
300+
)
301+
for stolpec in fields(self)
302+
for vrednost in [self[stolpec.name]]
303+
),
304+
glavni_kljuc=sql.Identifier(self.glavni_kljuc().name),
305+
id=sql.Literal(self.__dbid)
306+
))
307+
self.__nastavi_id()
308+
except errors.IntegrityError:
309+
raise ValueError(f'Napaka pri posodabljanju {self.__class__.__name__} z ID-jem {id}!')
310+
except errors.DataError:
311+
raise TypeError(f'Napaka pri posodabljanju {self.__class__.__name__} z ID-jem {id}!')
302312

303313
def izbrisi(self):
304314
"""
305315
Izbriši entiteto iz baze.
306316
"""
307317
assert self.__dbid is not None, "Entiteta še ni v bazi!"
308-
with conn.transaction():
309-
with conn.cursor() as cur:
310-
cur.execute(sql.SQL("""
311-
DELETE FROM {tabela}
312-
WHERE {glavni_kljuc} = {id};
313-
""").format(
314-
tabela=sql.Identifier(self.tabela()),
315-
glavni_kljuc=sql.Identifier(self.glavni_kljuc().name),
316-
id=sql.Literal(self.__dbid)
317-
))
318-
self.__dbid = None
318+
try:
319+
with conn.transaction():
320+
with conn.cursor() as cur:
321+
cur.execute(sql.SQL("""
322+
DELETE FROM {tabela}
323+
WHERE {glavni_kljuc} = {id};
324+
""").format(
325+
tabela=sql.Identifier(self.tabela()),
326+
glavni_kljuc=sql.Identifier(self.glavni_kljuc().name),
327+
id=sql.Literal(self.__dbid)
328+
))
329+
self.__dbid = None
330+
except errors.IntegrityError:
331+
raise ValueError(f'Napaka pri brisanju {self.__class__.__name__} z ID-jem {id}!')
332+
333+
@classmethod
334+
def izbrisi_id(cls, id):
335+
"""
336+
Izbriši entiteto s podanim ID-jem iz baze.
337+
"""
338+
cls.iz_baze(**{cls.glavni_kljuc().name: id}).izbrisi()
319339

320340
@classmethod
321341
def seznam(cls):

predavanja/primeri/banka/views/komitenti.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,23 @@ <h1 class="title">
99
<th>EMŠO</th>
1010
<th>Ime in priimek</th>
1111
<th>Naslov</th>
12+
<th></th>
1213
</tr>
1314
% for oseba in Oseba.seznam():
1415
<tr>
1516
<td>{{oseba.emso}}</td>
1617
<td>{{oseba.ime}} {{oseba.priimek}}</td>
1718
<td>{{oseba.naslov}}, {{oseba.kraj.posta}} {{oseba.kraj.kraj}}</td>
19+
<td>
20+
<form action="{{url('izbrisi_komitenta', emso=oseba.emso)}}" method="POST"
21+
onsubmit="return confirm('Ali res želi izbrisati komitenta z EMŠOm {{oseba.emso}}?')">
22+
<button class="button is-danger">
23+
<span class="icon is-small">
24+
<i class="fas fa-trash"></i>
25+
</span>
26+
</button>
27+
</form>
28+
</td>
1829
</tr>
1930
% end
2031
</table>

predavanja/primeri/banka/views/kraji.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,22 @@ <h1 class="title">
88
<tr>
99
<th>Poštna številka</th>
1010
<th>Kraj</th>
11+
<th></th>
1112
</tr>
1213
% for kraj in Kraj.seznam():
1314
<tr>
1415
<td>{{kraj.posta}}</td>
1516
<td>{{kraj.kraj}}</td>
17+
<td>
18+
<form action="{{url('izbrisi_kraj', posta=kraj.posta)}}" method="POST"
19+
onsubmit="return confirm('Ali res želi izbrisati kraj s poštno številko {{kraj.posta}}?')">
20+
<button class="button is-danger">
21+
<span class="icon is-small">
22+
<i class="fas fa-trash"></i>
23+
</span>
24+
</button>
25+
</form>
26+
</td>
1627
</tr>
1728
% end
1829
</table>

predavanja/primeri/banka/views/osnova.html

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
% setdefault('naslov', 'Banka')
2+
% sporocilo = preberi_sporocilo()
23

34
<!DOCTYPE html>
45
<html>
@@ -8,10 +9,22 @@
89
<meta name="viewport" content="width=device-width, initial-scale=1">
910
<title>{{naslov}}</title>
1011
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@1.0.2/css/bulma.min.css">
12+
<link rel="stylesheet" href="https://bulma.io/vendor/fontawesome-free-6.5.2-web/css/all.min.css">
13+
<script>
14+
document.addEventListener('DOMContentLoaded', () => {
15+
(document.querySelectorAll('.notification .delete') || []).forEach(($delete) => {
16+
const $notification = $delete.parentNode;
17+
18+
$delete.addEventListener('click', () => {
19+
$notification.parentNode.removeChild($notification);
20+
});
21+
});
22+
});
23+
</script>
1124
</head>
1225

1326
<body>
14-
<nav class="navbar" role="navigation" aria-label="main navigation">
27+
<nav class="navbar is-dark" role="navigation" aria-label="main navigation">
1528
<div class="navbar-brand">
1629
<a class="navbar-item {{'is-selected' if stran == 'index' else ''}}" href="{{url('index')}}">
1730
Banka
@@ -73,6 +86,12 @@
7386
</div>
7487
</div>
7588
</nav>
89+
% if sporocilo:
90+
<div class="notification is-warning">
91+
<button class="delete"></button>
92+
{{sporocilo}}
93+
</div>
94+
% end
7695
<section class="section">
7796
{{!base}}
7897
</section>

predavanja/primeri/banka/views/racuni.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,22 @@ <h1 class="title">
88
<tr>
99
<th>Številka</th>
1010
<th>Lastnik</th>
11+
<th></th>
1112
</tr>
1213
% for racun in Racun.seznam():
1314
<tr>
1415
<td>{{racun.stevilka}}</td>
1516
<td>{{racun.lastnik.ime}} {{racun.lastnik.priimek}} ({{racun.lastnik.emso}})</td>
17+
<td>
18+
<form action="{{url('izbrisi_racun', stevilka=racun.stevilka)}}" method="POST"
19+
onsubmit="return confirm('Ali res želi izbrisati račun s številko {{racun.stevilka}}?')">
20+
<button class="button is-danger">
21+
<span class="icon is-small">
22+
<i class="fas fa-trash"></i>
23+
</span>
24+
</button>
25+
</form>
26+
</td>
1627
</tr>
1728
% end
1829
</table>

predavanja/primeri/banka/views/transakcije.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ <h1 class="title">
1111
<th>Znesek</th>
1212
<th>Čas</th>
1313
<th>Opis</th>
14+
<th></th>
1415
</tr>
1516
% for transakcija in Transakcija.seznam():
1617
<tr>
@@ -19,6 +20,16 @@ <h1 class="title">
1920
<td>{{transakcija.znesek}}</td>
2021
<td>{{transakcija.cas}}</td>
2122
<td>{{transakcija.opis}}</td>
23+
<td>
24+
<form action="{{url('izbrisi_transakcijo', id=transakcija.id)}}" method="POST"
25+
onsubmit="return confirm('Ali res želi izbrisati transakcijo z ID-jem {{transakcija.id}}?')">
26+
<button class="button is-danger">
27+
<span class="icon is-small">
28+
<i class="fas fa-trash"></i>
29+
</span>
30+
</button>
31+
</form>
32+
</td>
2233
</tr>
2334
% end
2435
</table>

0 commit comments

Comments
 (0)