215 lines
8.9 KiB
Python
215 lines
8.9 KiB
Python
from geruecht.exceptions import DatabaseExecption, UsernameExistDB
|
|
from geruecht.model.user import User
|
|
import traceback
|
|
|
|
class Base:
|
|
def getAllUser(self, extern=False, workgroups=True):
|
|
try:
|
|
cursor = self.db.connection.cursor()
|
|
cursor.execute("select * from user")
|
|
data = cursor.fetchall()
|
|
|
|
if data:
|
|
retVal = []
|
|
for value in data:
|
|
if extern and value['uid'] == 'extern':
|
|
continue
|
|
user = User(value)
|
|
creditLists = self.getCreditListFromUser(user)
|
|
user.initGeruechte(creditLists)
|
|
if workgroups:
|
|
user.workgroups = self.getWorkgroupsOfUser(user.id)
|
|
retVal.append(user)
|
|
return retVal
|
|
except Exception as err:
|
|
traceback.print_exc()
|
|
self.db.connection.rollback()
|
|
raise DatabaseExecption("Something went worng with Datatabase: {}".format(err))
|
|
|
|
def getUser(self, username, workgroups=True):
|
|
try:
|
|
retVal = None
|
|
cursor = self.db.connection.cursor()
|
|
cursor.execute("select * from user where uid='{}'".format(username))
|
|
data = cursor.fetchone()
|
|
if data:
|
|
retVal = User(data)
|
|
creditLists = self.getCreditListFromUser(retVal)
|
|
retVal.initGeruechte(creditLists)
|
|
if workgroups:
|
|
retVal.workgroups = self.getWorkgroupsOfUser(retVal.id)
|
|
if retVal:
|
|
if retVal.uid == username:
|
|
return retVal
|
|
else:
|
|
return None
|
|
except Exception as err:
|
|
traceback.print_exc()
|
|
self.db.connection.rollback()
|
|
raise DatabaseExecption("Something went worng with Datatabase: {}".format(err))
|
|
|
|
def getUserById(self, id, workgroups=True, geruecht=True):
|
|
try:
|
|
retVal = None
|
|
cursor = self.db.connection.cursor()
|
|
cursor.execute("select * from user where id={}".format(id))
|
|
data = cursor.fetchone()
|
|
if data:
|
|
retVal = User(data)
|
|
if geruecht:
|
|
creditLists = self.getCreditListFromUser(retVal)
|
|
retVal.initGeruechte(creditLists)
|
|
if workgroups:
|
|
retVal.workgroups = self.getWorkgroupsOfUser(retVal.id)
|
|
return retVal
|
|
except Exception as err:
|
|
traceback.print_exc()
|
|
self.db.connection.rollback()
|
|
raise DatabaseExecption("Something went worng with Datatabase: {}".format(err))
|
|
|
|
def _convertGroupToString(self, groups):
|
|
retVal = ''
|
|
print('groups: {}'.format(groups))
|
|
if groups:
|
|
for group in groups:
|
|
if len(retVal) != 0:
|
|
retVal += ','
|
|
retVal += group
|
|
return retVal
|
|
|
|
|
|
def insertUser(self, user):
|
|
try:
|
|
cursor = self.db.connection.cursor()
|
|
groups = self._convertGroupToString(user.group)
|
|
cursor.execute("insert into user (uid, dn, firstname, lastname, gruppe, lockLimit, locked, autoLock, mail) VALUES ('{}','{}','{}','{}','{}',{},{},{},'{}')".format(
|
|
user.uid, user.dn, user.firstname, user.lastname, groups, user.limit, user.locked, user.autoLock, user.mail))
|
|
self.db.connection.commit()
|
|
except Exception as err:
|
|
traceback.print_exc()
|
|
self.db.connection.rollback()
|
|
raise DatabaseExecption("Something went worng with Datatabase: {}".format(err))
|
|
|
|
|
|
def updateUser(self, user):
|
|
try:
|
|
cursor = self.db.connection.cursor()
|
|
groups = self._convertGroupToString(user.group)
|
|
sql = "update user set dn='{}', firstname='{}', lastname='{}', gruppe='{}', lockLimit={}, locked={}, autoLock={}, mail='{}' where uid='{}'".format(
|
|
user.dn, user.firstname, user.lastname, groups, user.limit, user.locked, user.autoLock, user.mail, user.uid)
|
|
print(sql)
|
|
cursor.execute(sql)
|
|
self.db.connection.commit()
|
|
except Exception as err:
|
|
traceback.print_exc()
|
|
self.db.connection.rollback()
|
|
raise DatabaseExecption("Something went worng with Datatabase: {}".format(err))
|
|
|
|
def updateLastSeen(self, user, time):
|
|
try:
|
|
cursor = self.db.connection.cursor()
|
|
sql = "update user set last_seen='{}' where uid='{}'".format(
|
|
time, user.uid)
|
|
print(sql)
|
|
cursor.execute(sql)
|
|
self.db.connection.commit()
|
|
|
|
except Exception as err:
|
|
traceback.print_exc()
|
|
self.db.connection.rollback()
|
|
raise DatabaseExecption("Something went worng with Datatabase: {}".format(err))
|
|
|
|
def changeUsername(self, user, newUsername):
|
|
try:
|
|
cursor= self.db.connection.cursor()
|
|
cursor.execute("select * from user where uid='{}'".format(newUsername))
|
|
data = cursor.fetchall()
|
|
if data:
|
|
raise UsernameExistDB("Username already exists")
|
|
else:
|
|
cursor.execute("update user set uid='{}' where id={}".format(newUsername, user.id))
|
|
self.db.connection.commit()
|
|
except Exception as err:
|
|
traceback.print_exc()
|
|
self.db.connection.rollback()
|
|
raise DatabaseExecption("Something went worng with Datatabase: {}".format(err))
|
|
|
|
def getAllStatus(self):
|
|
try:
|
|
cursor = self.db.connection.cursor()
|
|
cursor.execute('select * from statusgroup')
|
|
return cursor.fetchall()
|
|
except Exception as err:
|
|
traceback.print_exc()
|
|
self.db.connection.rollback()
|
|
raise DatabaseExecption("Something went worng with Databes: {}".format(err))
|
|
|
|
def getStatus(self, name):
|
|
try:
|
|
cursor = self.db.connection.cursor()
|
|
if type(name) == str:
|
|
sql = "select * from statusgroup where name='{}'".format(name)
|
|
elif type(name) == int:
|
|
sql = 'select * from statusgroup where id={}'.format(name)
|
|
else:
|
|
raise DatabaseExecption("name as no type int or str. name={}, type={}".format(name, type(name)))
|
|
cursor.execute(sql)
|
|
return cursor.fetchone()
|
|
except Exception as err:
|
|
traceback.print_exc()
|
|
self.db.connection.rollback()
|
|
raise DatabaseExecption("Something went worng with Databes: {}".format(err))
|
|
|
|
def setStatus(self, name):
|
|
try:
|
|
cursor = self.db.connection.cursor()
|
|
cursor.execute("insert into statusgroup (name) values ('{}')".format(name))
|
|
self.db.connection.commit()
|
|
return self.getStatus(name)
|
|
except Exception as err:
|
|
traceback.print_exc()
|
|
self.db.connection.rollback()
|
|
raise DatabaseExecption("Something went worng with Databes: {}".format(err))
|
|
|
|
def updateStatus(self, status):
|
|
try:
|
|
cursor = self.db.connection.cursor()
|
|
cursor.execute("update statusgroup set name='{}' where id={}".format(status['name'], status['id']))
|
|
self.db.connection.commit()
|
|
return self.getStatus(status['id'])
|
|
except Exception as err:
|
|
traceback.print_exc()
|
|
self.db.connection.rollback()
|
|
raise DatabaseExecption("Something went worng with Databes: {}".format(err))
|
|
|
|
def deleteStatus(self, status):
|
|
try:
|
|
cursor = self.db.connection.cursor()
|
|
cursor.execute("delete from statusgroup where id={}".format(status['id']))
|
|
self.db.connection.commit()
|
|
except Exception as err:
|
|
traceback.print_exc()
|
|
self.db.connection.rollback()
|
|
raise DatabaseExecption("Something went worng with Databes: {}".format(err))
|
|
|
|
def updateStatusOfUser(self, username, status):
|
|
try:
|
|
cursor = self.db.connection.cursor()
|
|
cursor.execute("update user set statusgroup={} where uid='{}'".format(status['id'], username))
|
|
self.db.connection.commit()
|
|
return self.getUser(username)
|
|
except Exception as err:
|
|
traceback.print_exc()
|
|
self.db.connection.rollback()
|
|
raise DatabaseExecption("Something went worng with Databes: {}".format(err))
|
|
|
|
def updateVotingOfUser(self, username, voting):
|
|
try:
|
|
cursor = self.db.connection.cursor()
|
|
cursor.execute("update user set voting={} where uid='{}'".format(voting, username))
|
|
self.db.connection.commit()
|
|
return self.getUser(username)
|
|
except Exception as err:
|
|
traceback.print_exc()
|
|
self.db.connection.rollback()
|
|
raise DatabaseExecption("Something went worng with Databes: {}".format(err)) |