@@ -72,14 +72,60 @@ def z_id(cls, id):
7272 raise ValueError (f"Kraj s pošto { id } ne obstaja!" )
7373 return Kraj (* vrstica )
7474
75+ @classmethod
76+ def seznam (cls ):
77+ with conn .transaction ():
78+ with conn .cursor () as cur :
79+ cur .execute (
80+ """
81+ SELECT posta, kraj FROM kraj
82+ ORDER BY posta
83+ """
84+ )
85+ for vrstica in cur :
86+ yield Kraj (* vrstica )
87+
88+ def vstavi (self ):
89+ with conn .transaction ():
90+ with conn .cursor () as cur :
91+ cur .execute (
92+ """
93+ INSERT INTO kraj (posta, kraj)
94+ VALUES (%(posta)s, %(kraj)s)
95+ """ , self .kot_slovar ()
96+ )
97+
98+ def posodobi (self ):
99+ with conn .transaction ():
100+ with conn .cursor () as cur :
101+ cur .execute (
102+ """
103+ UPDATE kraj SET kraj = %(kraj)s
104+ WHERE posta = %(posta)s
105+ """ , self .kot_slovar ()
106+ )
107+
108+ def izbrisi (self ):
109+ with conn .transaction ():
110+ with conn .cursor () as cur :
111+ cur .execute (
112+ """
113+ DELETE FROM kraj
114+ WHERE posta = %s
115+ """ , (self .posta , )
116+ )
117+
118+ def kot_slovar (self ):
119+ return dict (posta = self .posta , kraj = self .kraj )
120+
75121@dataclass
76122class Oseba :
77123 emso : str
78124 ime : str
79125 priimek : str
80- naslov : str
81- kraj : Kraj
82- uporabnisko_ime : str
126+ naslov : str = None
127+ kraj : Kraj = None
128+ uporabnisko_ime : str = None
83129 geslo : bytes = None
84130 admin : bool = False
85131
@@ -155,6 +201,102 @@ def z_id(cls, id):
155201 * podatki , posta , kraj , uporabnisko_ime , admin = vrstica
156202 return Oseba (* podatki , Kraj (posta , kraj ), uporabnisko_ime , admin = admin )
157203
204+ @classmethod
205+ def prijavi (cls , uporabnisko_ime , geslo ):
206+ with conn .transaction ():
207+ with conn .cursor () as cur :
208+ cur .execute (
209+ """
210+ SELECT emso, ime, priimek, naslov, kraj.posta, kraj.kraj, uporabnisko_ime, geslo, admin FROM oseba
211+ JOIN kraj ON oseba.kraj = posta
212+ WHERE uporabnisko_ime = %s
213+ """ , (uporabnisko_ime , )
214+ )
215+ vrstica = cur .fetchone ()
216+ if vrstica is None :
217+ raise ValueError (f"Uporabnik z uporabniškim imenom { uporabnisko_ime } ne obstaja!" )
218+ * podatki , posta , kraj , uporabnisko_ime , zgostitev , admin = vrstica
219+ if not Oseba ._preveri_geslo (geslo , zgostitev ):
220+ raise ValueError (f"Geslo za uporabnika z uporabniškim imenom { uporabnisko_ime } ni pravilno!" )
221+ return Oseba (* podatki , Kraj (posta , kraj ), uporabnisko_ime , admin = admin )
222+
223+ @classmethod
224+ def seznam (cls ):
225+ with conn .transaction ():
226+ with conn .cursor () as cur :
227+ cur .execute (
228+ """
229+ SELECT emso, ime, priimek, naslov, kraj.posta, kraj.kraj, uporabnisko_ime, admin FROM oseba
230+ JOIN kraj ON oseba.kraj = posta
231+ ORDER BY priimek, ime
232+ """
233+ )
234+ for * podatki , posta , kraj , uporabnisko_ime , admin in cur :
235+ yield Oseba (* podatki , Kraj (posta , kraj ), uporabnisko_ime , admin = admin )
236+
237+ def racuni (self ):
238+ with conn .transaction ():
239+ with conn .cursor () as cur :
240+ cur .execute (
241+ """
242+ SELECT stevilka FROM racun
243+ WHERE lastnik = %s
244+ ORDER BY stevilka
245+ """ , (self .emso , )
246+ )
247+ for stevilka , in cur :
248+ yield Racun (stevilka , self )
249+
250+ def dodaj_racun (self ):
251+ racun = Racun (lastnik = self )
252+ racun .vstavi ()
253+ return racun
254+
255+ def vstavi (self ):
256+ with conn .transaction ():
257+ with conn .cursor () as cur :
258+ cur .execute (
259+ """
260+ INSERT INTO oseba (emso, ime, priimek, naslov, kraj, uporabnisko_ime, geslo, admin)
261+ VALUES (%(emso)s, %(ime)s, %(priimek)s, %(naslov)s, %(kraj)s,
262+ %(uporabnisko_ime)s, %(geslo)s, %(admin)s)
263+ """ , self .kot_slovar ()
264+ )
265+ self .geslo = None
266+
267+ def posodobi (self ):
268+ with conn .transaction ():
269+ with conn .cursor () as cur :
270+ cur .execute (
271+ """
272+ UPDATE oseba SET ime = %(ime)s, priimek = %(priimek)s, naslov = %(naslov)s,
273+ kraj = %(kraj)s, uporabnisko_ime = %(uporabnisko_ime)s, admin = %(admin)s
274+ WHERE emso = %(emso)s
275+ """ , self .kot_slovar ()
276+ )
277+
278+ def izbrisi (self ):
279+ with conn .transaction ():
280+ with conn .cursor () as cur :
281+ cur .execute (
282+ """
283+ DELETE FROM oseba
284+ WHERE emso = %s
285+ """ , (self .emso , )
286+ )
287+
288+ def kot_slovar (self ):
289+ if isinstance (self .kraj , Kraj ):
290+ kraj = self .kraj .posta
291+ else :
292+ kraj = self .kraj
293+ if self .geslo is None :
294+ geslo = None
295+ else :
296+ geslo = Oseba ._nastavi_geslo (self .geslo )
297+ return dict (emso = self .emso , ime = self .ime , priimek = self .priimek , naslov = self .naslov , kraj = kraj ,
298+ uporabnisko_ime = self .uporabnisko_ime , geslo = geslo , admin = self .admin )
299+
158300 @staticmethod
159301 def _nastavi_geslo (geslo ):
160302 """
@@ -175,8 +317,8 @@ def _preveri_geslo(geslo, zgostitev):
175317
176318@dataclass
177319class Racun :
178- stevilka : int
179- lastnik : Oseba
320+ stevilka : int = None
321+ lastnik : Oseba = None
180322
181323 @classmethod
182324 def ustvari_tabelo (cls , pobrisi = False , ce_ne_obstaja = False ):
@@ -250,11 +392,81 @@ def z_id(cls, id):
250392 stevilka , * podatki , posta , kraj , uporabnisko_ime , admin = vrstica
251393 return Racun (stevilka , Oseba (* podatki , Kraj (posta , kraj ), uporabnisko_ime , admin = admin ))
252394
395+ @classmethod
396+ def seznam (cls ):
397+ with conn .transaction ():
398+ with conn .cursor () as cur :
399+ cur .execute (
400+ """
401+ SELECT stevilka, emso, ime, priimek FROM racun
402+ JOIN oseba ON lastnik = emso
403+ ORDER BY stevilka
404+ """
405+ )
406+ for stevilka , * podatki in cur :
407+ yield Racun (stevilka , Oseba (* podatki ))
408+
409+ def transakcije (self ):
410+ with conn .transaction ():
411+ with conn .cursor () as cur :
412+ cur .execute (
413+ """
414+ SELECT id, znesek, cas, opis FROM transakcija
415+ WHERE racun = %s
416+ ORDER BY cas DESC
417+ """ , (self .stevilka , )
418+ )
419+ for id , * podatki in cur :
420+ yield Transakcija (id , self , * podatki )
421+
422+ def vstavi (self ):
423+ assert self .stevilka is None , f"Račun že ima dodeljeno številko { self .stevilka } !"
424+ with conn .transaction ():
425+ with conn .cursor () as cur :
426+ cur .execute (
427+ """
428+ INSERT INTO racun (lastnik)
429+ VALUES (%(lastnik)s)
430+ RETURNING stevilka
431+ """ , self .kot_slovar ()
432+ )
433+ self .stevilka , = cur .fetchone ()
434+
435+ def posodobi (self ):
436+ assert self .stevilka is not None , "Račun še nima dodeljene številke!"
437+ with conn .transaction ():
438+ with conn .cursor () as cur :
439+ cur .execute (
440+ """
441+ UPDATE racun SET lastnik = %(lastnik)s
442+ WHERE stevilka = %(stevilka)s
443+ """ , self .kot_slovar ()
444+ )
445+
446+ def izbrisi (self ):
447+ assert self .stevilka is not None , "Račun še nima dodeljene številke!"
448+ with conn .transaction ():
449+ with conn .cursor () as cur :
450+ cur .execute (
451+ """
452+ DELETE FROM racun
453+ WHERE stevilka = %s
454+ """ , (self .stevilka , )
455+ )
456+
457+ def kot_slovar (self ):
458+ if isinstance (self .lastnik , Oseba ):
459+ lastnik = self .lastnik .emso
460+ else :
461+ lastnik = self .lastnik
462+ return dict (stevilka = self .stevilka , lastnik = lastnik )
463+
464+
253465@dataclass
254466class Transakcija :
255- id : int
256- racun : Racun
257- znesek : int
467+ id : int = None
468+ racun : Racun = None
469+ znesek : int = None
258470 cas : datetime = None
259471 opis : str = None
260472
@@ -336,6 +548,62 @@ def z_id(cls, id):
336548 return Transakcija (id , Racun (racun , Oseba (* podatki , Kraj (posta , kraj ), uporabnisko_ime , admin = admin )),
337549 znesek , cas , opis )
338550
551+ @classmethod
552+ def seznam (cls ):
553+ with conn .transaction ():
554+ with conn .cursor () as cur :
555+ cur .execute (
556+ """
557+ SELECT id, racun, znesek, cas, opis FROM transakcija
558+ ORDER BY cas DESC
559+ """
560+ )
561+ for podatki in cur :
562+ yield Transakcija (* podatki )
563+
564+ def vstavi (self ):
565+ assert self .id is None , f"Transakcija že ima dodeljen ID { self .id } !"
566+ assert self .cas is None , f"Transakcija že ima dodeljen čas { self .cas } !"
567+ with conn .transaction ():
568+ with conn .cursor () as cur :
569+ cur .execute (
570+ """
571+ INSERT INTO transakcija (racun, znesek, opis)
572+ VALUES (%(racun)s, %(znesek)s, %(opis)s)
573+ RETURNING id, cas
574+ """ , self .kot_slovar ()
575+ )
576+ self .id , self .cas = cur .fetchone ()
577+
578+ def posodobi (self ):
579+ assert self .id is not None , "Transakcija še nima dodeljenega ID-ja!"
580+ with conn .transaction ():
581+ with conn .cursor () as cur :
582+ cur .execute (
583+ """
584+ UPDATE transakcija SET racun = %(racun)s, znesek = %(znesek)s, cas = %(cas)s, opis = %(opis)s
585+ WHERE id = %(id)s
586+ """ , self .kot_slovar ()
587+ )
588+
589+ def izbrisi (self ):
590+ assert self .id is not None , "Transakcija še nima dodeljenega ID-ja!"
591+ with conn .transaction ():
592+ with conn .cursor () as cur :
593+ cur .execute (
594+ """
595+ DELETE FROM transakcija
596+ WHERE id = %s
597+ """ , (self .id , )
598+ )
599+
600+ def kot_slovar (self ):
601+ if isinstance (self .racun , Racun ):
602+ racun = self .racun .stevilka
603+ else :
604+ racun = self .racun
605+ return dict (id = self .id , racun = racun , znesek = self .znesek , cas = self .cas , opis = self .opis )
606+
339607
340608RAZREDI = [Kraj , Oseba , Racun , Transakcija ]
341609
0 commit comments