def add_ocena(ucenik, predmet, vrednost_ocene): check_type(ucenik, Ucenik) check_type(predmet, Predmet) if not isinstance(vrednost_ocene, int) or vrednost_ocene < 1 or vrednost_ocene > 5: raise ValueError('vrednost_ocene is out of predefined domain') daos = {'korisnik': None, 'predmet': None} try: with session_scope() as session: daos['korisnik'] = DAOManager.get_korisnik_dao(session) daos['predmet'] = DAOManager.get_predmet_dao(session) slusa = daos['korisnik'].get_uceniks_predmets_slusa( ucenik, predmet) if slusa is None: raise UpdateInfoError('Ucenik ne slusa uneti predmet') ocena = Ocena(vrednost_ocene, slusa) daos['predmet'].add_ocena(ocena) return True except UpdateInfoError: raise except: return False finally: for dao in daos.values(): DAOManager.release(dao)
def __get_korisnik_by_pk(korisnik_dao_call, primary_key): dao = None try: with session_scope() as session: dao = DAOManager.get_korisnik_dao(session) return korisnik_dao_call(dao, primary_key) except: return None finally: DAOManager.release(dao)
def get_uceniks_slusa(ucenik): dao = None try: with session_scope() as session: dao = DAOManager.get_korisnik_dao(session) return dao.get_uceniks_slusa(ucenik) except: return None finally: DAOManager.release(dao)
def add_predmet(naziv): dao = None try: with session_scope() as session: dao = DAOManager.get_predmet_dao(session) dao.add_predmet(Predmet(naziv)) return True except: return False finally: DAOManager.release(dao)
def get_profesors_predmets_slusa(predmet, profesor): dao = None try: with session_scope() as session: dao = DAOManager.get_korisnik_dao(session) slusas = dao.get_profesors_predmets_slusa(predmet, profesor) return slusas except: return None finally: DAOManager.release(dao)
def get_slusa_by_pk(ucenik_id, predmet_id): dao = None try: with session_scope() as session: dao = DAOManager.get_korisnik_dao(session) slusa = dao.get_slusa_by_pk(ucenik_id, predmet_id) return slusa except: return None finally: DAOManager.release(dao)
def get_predmet_by_naziv(naziv): dao = None try: with session_scope() as session: dao = DAOManager.get_predmet_dao(session) predmet = dao.get_predmet_by_name(naziv) return predmet except: return None finally: DAOManager.release(dao)
def get_razred_by_godina(godina): dao = None try: with session_scope() as session: dao = DAOManager.get_razred_dao(session) return dao.get_razred_by_godina(godina) except: return None finally: if dao is not None: DAOManager.release(dao)
def delete_latest_ocena(slusa): dao = None try: with session_scope() as session: dao = DAOManager.get_predmet_dao(session) dao.delete_latest_ocena(slusa) return True except: return False finally: DAOManager.release(dao)
def get_predmet_by_pk(primary_key): dao = None try: with session_scope() as session: dao = DAOManager.get_predmet_dao(session) predmet = dao.get_predmet_by_pk(primary_key) predmet.razredi.sort(key=lambda razred: razred.godina) return predmet except: return None finally: DAOManager.release(dao)
def get_profesors_predmets(profesor): dao = None try: with session_scope() as session: dao = DAOManager.get_predmet_dao(session) predmeti = dao.get_all_predmet_connected_with_the_profesor( profesor) return {predmet.id: predmet for predmet in predmeti} except: return None finally: DAOManager.release(dao)
def add_razred(godina): dao = None try: with session_scope() as session: dao = DAOManager.get_razred_dao(session) dao.add_razred(Razred(godina)) return True except: return False finally: if dao is not None: DAOManager.release(dao)
def change_password(korisnik, new_password): new_password = new_password.strip() dao = None try: KorisnikLogic.validate_password(new_password) with session_scope() as session: dao = DAOManager.get_korisnik_dao(session) dao.update_korisnik_attribute(korisnik, 'password', new_password) return True except: return False finally: DAOManager.release(dao)
def __get_all_korisnik(korisnik_dao_call): dao = None try: with session_scope() as session: dao = DAOManager.get_korisnik_dao(session) korisnici = korisnik_dao_call(dao) if korisnici is None: return None else: return { korisnik.id: korisnik for korisnik in korisnici } except: return None finally: DAOManager.release(dao)
def change_prezime(korisnik, prezime): prezime = prezime.strip() dao = None try: KorisnikLogic.validate_name('prezime', prezime) with session_scope() as session: dao = DAOManager.get_korisnik_dao(session) dao.update_korisnik_attribute(korisnik, 'prezime', prezime) return True except InvalidKorisnikInfoError: raise except: return False finally: DAOManager.release(dao)
def get_korisnik_by_username(username): dao = None try: with session_scope() as session: dao = DAOManager.get_korisnik_dao(session) korisnik = dao.get_korisnik_by_username(username) if korisnik is not None: session.refresh(korisnik) return korisnik else: return None except: return None finally: DAOManager.release(dao)
def add_profesor_predmet_relation(profesor, predmet): dao = None try: with session_scope() as session: dao = DAOManager.get_korisnik_dao(session) if predmet in profesor.predmeti: raise UpdateInfoError('Profesor vec predaje uneti predmet') dao.add_predmet_to_profesor(profesor, predmet) return True except UpdateInfoError: raise except: return False finally: DAOManager.release(dao)
def get_all_razred(): dao = None try: with session_scope() as session: dao = DAOManager.get_razred_dao(session) razredi = dao.get_all_razred() if razredi is None: return None else: return { razred.id: razred for razred in razredi} except: return None finally: if dao is not None: DAOManager.release(dao)
def get_all_predmet(): dao = None try: with session_scope() as session: dao = DAOManager.get_predmet_dao(session) predmeti = dao.get_all_predmet() if predmeti is None: return None else: for predmet in predmeti: predmet.razredi.sort(key=lambda razred: razred.godina) return {predmet.id: predmet for predmet in predmeti} except: return None finally: DAOManager.release(dao)
def change_predmet_naziv(predmet, naziv): dao = None try: with session_scope() as session: dao = DAOManager.get_predmet_dao(session) if dao.get_predmet_by_name(naziv) is not None: raise UpdateInfoError( 'Vec postoji predmet sa unetim imenom') dao.update_predmet_attribute(predmet, 'naziv', naziv) return True except UpdateInfoError: raise except: return False finally: DAOManager.release(dao)
def register_user(**params): korisnik = None try: uloga = params['uloga'].strip().lower() username = params['username'].strip() KorisnikLogic.validate_username(username) password = params['password'].strip() KorisnikLogic.validate_password(password) password = bcrypt.hashpw(password.encode(), bcrypt.gensalt()) if uloga in ['admin', 'administrator']: korisnik = Admin(username, password) else: assert uloga in ['ucenik', 'profesor'] ime = params['ime'].strip() prezime = params['prezime'].strip() KorisnikLogic.validate_name('ime', ime) KorisnikLogic.validate_name('prezime', prezime) if uloga == 'profesor': korisnik = Profesor(username, password, ime, prezime) else: godina = params['razred'] if not isinstance(godina, int): raise InvalidKorisnikInfoError('Razred mora biti ceo broj') razred = RazredLogic.get_razred_by_godina(godina) if razred is None: raise InvalidKorisnikInfoError('Uneti razred ne postoji') korisnik = Ucenik(username, password, ime, prezime, razred) except InvalidKorisnikInfoError: raise except: debug_print('Insufficient params passed to register_user\n', traceback.format_exc()) return False dao = None try: with session_scope() as session: dao = DAOManager.get_korisnik_dao(session) if dao.get_korisnik_by_username(korisnik.username) is not None: raise InvalidKorisnikInfoError('Vec postoji korisnik sa datim username-om') dao.add_korisnik(korisnik) return True except InvalidKorisnikInfoError: raise except: return False finally: DAOManager.release(dao)
def change_username(korisnik, new_username): new_username = new_username.strip() dao = None try: KorisnikLogic.validate_username(new_username) with session_scope() as session: dao = DAOManager.get_korisnik_dao(session) if dao.get_korisnik_by_username(new_username) is not None: raise InvalidKorisnikInfoError('Vec postoji korisnik sa datim username-om') dao.update_korisnik_attribute(korisnik, 'username', new_username) return True except InvalidKorisnikInfoError: raise except: return False finally: DAOManager.release(dao)
def add_slusa(ucenik, predmet, profesor): dao = None try: with session_scope() as session: dao = DAOManager.get_korisnik_dao(session) if profesor not in predmet.profesori: raise UpdateInfoError('Profesor ne predaje uneti predmet') if dao.does_slusa_exists(ucenik, predmet): raise UpdateInfoError('Ucenik vec slusa dati predmet') dao.add_predmet_to_ucenik(ucenik, predmet, profesor) return True except UpdateInfoError: raise except: return False finally: DAOManager.release(dao)
def get_korisnik_by_pk(primary_key): # Ispostavilo se da fali session.refresh da bi radilo sa ostalim podklasama od korisnika (sto sam shvatio tek pri kraju pisanja programa) # ali posto se njihove pojedinacne metode vec koriste u velikom delu programa ostavicu tako da ne bih napravio slucajno neki bug dao = None try: with session_scope() as session: dao = DAOManager.get_korisnik_dao(session) korisnik = dao.get_korisnik_by_pk(primary_key) if korisnik is not None: session.refresh(korisnik) return korisnik else: return None except: return None finally: DAOManager.release(dao)
def get_predmets_avaliable_to_ucenik(ucenik): dao = None try: with session_scope() as session: dao = DAOManager.get_predmet_dao(session) predmeti = dao.get_predmets_avaliable_to_ucenik(ucenik) if predmeti is None: return None predmeti = list( filter(lambda predmet: len(predmet.profesori) > 0, predmeti)) return predmeti except UpdateInfoError: raise except: return None finally: DAOManager.release(dao)
def add_ocena_with_slusa(slusa, vrednost_ocene): check_type(slusa, Slusa) if not isinstance(vrednost_ocene, int) or vrednost_ocene < 1 or vrednost_ocene > 5: raise ValueError('vrednost_ocene is out of predefined domain') dao = None try: with session_scope() as session: dao = DAOManager.get_predmet_dao(session) ocena = Ocena(vrednost_ocene, slusa) dao.add_ocena(ocena) return True except UpdateInfoError: raise except: return False finally: DAOManager.release(dao)
def authenticate_user(username, password): dao = None try: username = username.strip() password = password.strip() with session_scope() as session: dao = DAOManager.get_korisnik_dao(session) user = dao.get_korisnik_by_username(username) if user is None: return None if KorisnikLogic.do_passwords_match(user, password): session.refresh(user) return user else: return None except: return None finally: DAOManager.release(dao)
def add_predmet_razred_relation(predmet, godina): daos = {'predmet': None, 'razred': None} try: with session_scope() as session: daos['predmet'] = DAOManager.get_predmet_dao(session) daos['razred'] = DAOManager.get_razred_dao(session) razred = daos['razred'].get_razred_by_godina(godina) if razred in predmet.razredi: raise UpdateInfoError( 'Predmet se vec slusa u unetom razredu') daos['predmet'].add_razred_to_predmet(predmet, razred) return True except UpdateInfoError: raise except: return False finally: for dao in daos.values(): DAOManager.release(dao)
def get_all_ucenik_from_godina(godina): razred = None if isinstance(godina, Razred): razred = godina elif not isinstance(godina, int): return None daos = {'razred': None, 'korisnik': None} try: with session_scope() as session: daos['razred'] = DAOManager.get_razred_dao(session) daos['korisnik'] = DAOManager.get_korisnik_dao(session) if razred is None: razred = daos['razred'].get_razred_by_godina(godina) return daos['korisnik'].get_all_ucenik_from_razred(razred) except: return None finally: for dao in daos.values(): if dao is not None: DAOManager.release(dao)