finished ##169 and ##170 (without config.yml)

This commit is contained in:
Tim Gröger 2020-02-25 22:50:32 +01:00
parent 4e91587731
commit 90d503f6aa
5 changed files with 264 additions and 12 deletions

View File

@ -3,7 +3,7 @@ import geruecht.controller as gc
import geruecht.controller.ldapController as lc import geruecht.controller.ldapController as lc
import geruecht.controller.userController as uc import geruecht.controller.userController as uc
from datetime import datetime from datetime import datetime
from geruecht.model import BAR, MONEY from geruecht.model import BAR, MONEY, USER
from geruecht.decorator import login_required from geruecht.decorator import login_required
baruser = Blueprint("baruser", __name__) baruser = Blueprint("baruser", __name__)
@ -146,7 +146,7 @@ def _getUser(**kwargs):
@baruser.route("/search", methods=['POST']) @baruser.route("/search", methods=['POST'])
@login_required(groups=[BAR, MONEY]) @login_required(groups=[BAR, MONEY, USER])
def _search(**kwargs): def _search(**kwargs):
data = request.get_json() data = request.get_json()
searchString = data['searchString'] searchString = data['searchString']

View File

@ -259,12 +259,116 @@ class DatabaseController(metaclass=Singleton):
except Exception as err: except Exception as err:
traceback.print_exc() traceback.print_exc()
self.db.connection.rollback() self.db.connection.rollback()
raise DatabaseController("Something went wrong with Database: {}".format(err)) raise DatabaseExecption("Something went wrong with Database: {}".format(err))
except Exception as err: except Exception as err:
traceback.print_exc() traceback.print_exc()
self.db.connection.rollback() self.db.connection.rollback()
raise DatabaseExecption("Something went worng with Datatabase: {}".format(err)) raise DatabaseExecption("Something went worng with Datatabase: {}".format(err))
def setTransactJob(self, from_user, to_user, date):
try:
exists = self.getTransactJob(from_user, to_user, date)
if exists:
raise IntegrityError("job_transact already exists")
cursor = self.db.connection.cursor()
cursor.execute("insert into job_transact (jobdate, from_user_id, to_user_id) VALUES ('{}', {}, {})".format(date, from_user.id, to_user.id))
self.db.connection.commit()
return self.getTransactJob(from_user, to_user, date)
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Somethin went wrong with Database: {}".format(err))
def getTransactJob(self, from_user, to_user, date):
try:
cursor = self.db.connection.cursor()
cursor.execute("select * from job_transact where from_user_id={} and to_user_id={} and jobdate='{}'".format(from_user.id, to_user.id, date))
data = cursor.fetchone()
if data:
return {"from_user": from_user, "to_user": to_user, "date": data['jobdate'], "answerd": data['answerd'], "accepted": data['accepted']}
return None
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Something went worng with Database: {}".format(err))
def getAllTransactJobFromUser(self, from_user, date):
try:
cursor = self.db.connection.cursor()
cursor.execute("select * from job_transact where from_user_id={}".format(from_user.id))
data = cursor.fetchall()
retVal = []
for transact in data:
if date <= transact['jobdate']:
retVal.append({"from_user": from_user, "to_user": self.getUserById(transact['to_user_id']), "date": transact['jobdate'], "accepted": transact['accepted'], "answerd": transact['answerd']})
return retVal
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Somethin went wrong with Database: {}".format(err))
def getAllTransactJobToUser(self, to_user, date):
try:
cursor = self.db.connection.cursor()
cursor.execute("select * from job_transact where to_user_id={}".format(to_user.id))
data = cursor.fetchall()
retVal = []
for transact in data:
if date <= transact['jobdate']:
retVal.append({"to_user": to_user, "from_user": self.getUserById(transact['from_user_id']), "date": transact['jobdate'], "accepted": transact['accepted'], "answerd": transact['answerd']})
return retVal
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Somethin went wrong with Database: {}".format(err))
def getTransactJobToUser(self, to_user, date):
try:
cursor = self.db.connection.cursor()
cursor.execute("select * from job_transact where to_user_id={} and jobdate='{}'".format(to_user.id, date))
data = cursor.fetchone()
if data:
return {"from_user": self.getUserById(data['from_user_id']), "to_user": to_user, "date": data['jobdate'], "accepted": data['accepted'], "answerd": data['answerd']}
else:
return None
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Somethin went wrong with Database: {}".format(err))
def updateTransactJob(self, from_user, to_user, date, accepted):
try:
cursor = self.db.connection.cursor()
cursor.execute("update job_transact set accepted={}, answerd=true where to_user_id={} and jobdate='{}'".format(accepted, to_user.id, date))
self.db.connection.commit()
return self.getTransactJob(from_user, to_user, date)
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Somethin went wrong with Database: {}".format(err))
def getTransactJobFromUser(self, user, date):
try:
cursor = self.db.connection.cursor()
cursor.execute("select * from job_transact where from_user_id={} and jobdate='{}'".format(user.id, date))
data = cursor.fetchall()
return [{"from_user": user, "to_user": self.getUserById(transact['to_user_id']), "date": transact['jobdate'], "accepted": transact['accepted'], "answerd": transact['answerd']} for transact in data]
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Somethin went wrong with Database: {}".format(err))
def deleteTransactJob(self, from_user, to_user, date):
try:
cursor = self.db.connection.cursor()
cursor.execute("delete from job_transact where from_user_id={} and to_user_id={} and jobdate='{}'".format(from_user.id, to_user.id, date))
self.db.connection.commit()
except Exception as err:
traceback.print_exc()
self.db.connection.rollback()
raise DatabaseExecption("Something went wrong with Database: {}".format(err))
if __name__ == '__main__': if __name__ == '__main__':
db = DatabaseController() db = DatabaseController()
user = db.getUser('jhille') user = db.getUser('jhille')

View File

@ -6,7 +6,7 @@ import calendar
from geruecht.model.user import User from geruecht.model.user import User
from geruecht.exceptions import PermissionDenied from geruecht.exceptions import PermissionDenied
from datetime import datetime, timedelta from datetime import datetime, timedelta
from geruecht.exceptions import UsernameExistLDAP, UsernameExistDB, DatabaseExecption, LDAPExcetpion, DayLocked from geruecht.exceptions import UsernameExistLDAP, UsernameExistDB, DatabaseExecption, LDAPExcetpion, DayLocked, TansactJobIsAnswerdException
db = dc.DatabaseController() db = dc.DatabaseController()
ldap = lc.LDAPController(ldapConfig['dn']) ldap = lc.LDAPController(ldapConfig['dn'])
@ -17,6 +17,33 @@ class UserController(metaclass=Singleton):
def __init__(self): def __init__(self):
pass pass
def setTransactJob(self, from_user, to_user, date):
return db.setTransactJob(from_user, to_user, date.date())
def getTransactJobFromUser(self, user, date):
return db.getTransactJobFromUser(user, date.date())
def getAllTransactJobFromUser(self, user, date):
return db.getAllTransactJobFromUser(user, date.date())
def getAllTransactJobToUser(self, user, date):
return db.getAllTransactJobToUser(user, date.date())
def getTransactJob(self, from_user, to_user, date):
return db.getTransactJob(from_user, to_user, date.date())
def deleteTransactJob(self, from_user, to_user, date):
transactJob = self.getTransactJob(from_user, to_user, date)
if transactJob['answerd']:
raise TansactJobIsAnswerdException("TransactJob is already answerd")
db.deleteTransactJob(from_user, to_user, date.date())
def answerdTransactJob(self, from_user, to_user, date, answer):
transactJob = db.updateTransactJob(from_user, to_user, date.date(), answer)
if answer:
self.addWorker(to_user.uid, date)
return transactJob
def setLockedDay(self, date, locked, hard=False): def setLockedDay(self, date, locked, hard=False):
return db.setLockedDay(date.date(), locked, hard) return db.setLockedDay(date.date(), locked, hard)
@ -56,12 +83,19 @@ class UserController(metaclass=Singleton):
return self.getWorker(date, username=username) return self.getWorker(date, username=username)
def deleteWorker(self, username, date, userExc=False): def deleteWorker(self, username, date, userExc=False):
user = self.getUser(username)
if userExc: if userExc:
lockedDay = self.getLockedDay(date) lockedDay = self.getLockedDay(date)
if lockedDay: if lockedDay:
if lockedDay['locked']: if lockedDay['locked']:
transactJobs = self.getTransactJobFromUser(user, date)
found = False
for job in transactJobs:
if job['accepted'] and job['answerd']:
found = True
break
if not found:
raise DayLocked("Day is locked. You can't delete the Job") raise DayLocked("Day is locked. You can't delete the Job")
user = self.getUser(username)
db.deleteWorker(user, date) db.deleteWorker(user, date)
def lockUser(self, username, locked): def lockUser(self, username, locked):

View File

@ -10,3 +10,5 @@ class LDAPExcetpion(Exception):
pass pass
class DayLocked(Exception): class DayLocked(Exception):
pass pass
class TansactJobIsAnswerdException(Exception):
pass

View File

@ -48,7 +48,7 @@ def _saveConfig(**kwargs):
retVal['creditList'] = {credit.year: credit.toJSON() for credit in accToken.user.geruechte} retVal['creditList'] = {credit.year: credit.toJSON() for credit in accToken.user.geruechte}
return jsonify(retVal) return jsonify(retVal)
except Exception as err: except Exception as err:
return jsonify({"error": err}), 409 return jsonify({"error": str(err)}), 409
@user.route("/user/job", methods=['POST']) @user.route("/user/job", methods=['POST'])
@login_required(groups=[USER]) @login_required(groups=[USER])
@ -100,9 +100,9 @@ def _addUser(**kwargs):
print(retVal) print(retVal)
return jsonify(retVal) return jsonify(retVal)
except DayLocked as err: except DayLocked as err:
return jsonify({'error': err}), 403 return jsonify({'error': str(err)}), 403
except Exception as err: except Exception as err:
return jsonify({'error': err}), 409 return jsonify({'error': str(err)}), 409
@user.route("/user/deleteJob", methods=['POST']) @user.route("/user/deleteJob", methods=['POST'])
@login_required(groups=[USER]) @login_required(groups=[USER])
@ -119,6 +119,118 @@ def _deletJob(**kwargs):
userController.deleteWorker(user.uid, date, True) userController.deleteWorker(user.uid, date, True)
return jsonify({"ok": "ok"}) return jsonify({"ok": "ok"})
except DayLocked as err: except DayLocked as err:
return jsonify({"error": err}), 403 return jsonify({"error": str(err)}), 403
except Exception as err: except Exception as err:
return jsonify({"error": err}), 409 return jsonify({"error": str(err)}), 409
@user.route("/user/transactJob", methods=['POST'])
@login_required(groups=[USER])
def _transactJob(**kwargs):
try:
if 'accToken' in kwargs:
accToken = kwargs['accToken']
user = accToken.user
data = request.get_json()
year = data['year']
month = data['month']
day = data['day']
username = data['user']
date = datetime(year, month, day, 12)
to_user = userController.getUser(username)
retVal = userController.setTransactJob(user, to_user, date)
retVal['from_user'] = retVal['from_user'].toJSON()
retVal['to_user'] = retVal['to_user'].toJSON()
retVal['date'] = {'year': year, 'month': month, 'day': day}
print(retVal)
return jsonify(retVal)
except Exception as err:
return jsonify({"error": str(err)}), 409
@user.route("/user/answerTransactJob", methods=['POST'])
@login_required(groups=[USER])
def _answer(**kwargs):
try:
if 'accToken' in kwargs:
accToken = kwargs['accToken']
user = accToken.user
data = request.get_json()
year = data['year']
month = data['month']
day = data['day']
answer = data['answer']
username = data['username']
date = datetime(year, month, day, 12)
from_user = userController.getUser(username)
retVal = userController.answerdTransactJob(from_user, user, date, answer)
retVal['from_user'] = retVal['from_user'].toJSON()
retVal['to_user'] = retVal['to_user'].toJSON()
retVal['date'] = {'year': year, 'month': month, 'day': day}
print(retVal)
return jsonify(retVal)
except Exception as err:
return jsonify({"error": str(err)}), 409
@user.route("/user/jobRequests", methods=['POST'])
@login_required(groups=[USER])
def _requests(**kwargs):
try:
if 'accToken' in kwargs:
accToken = kwargs['accToken']
user = accToken.user
data = request.get_json()
year = data['year']
month = data['month']
day = data['day']
date = datetime(year, month, day, 12)
retVal = userController.getAllTransactJobToUser(user, date)
for data in retVal:
data['from_user'] = data['from_user'].toJSON()
data['to_user'] = data['to_user'].toJSON()
data_date = data['date']
data['date'] = {'year': data_date.year, 'month': data_date.month, 'day': data_date.day}
print(retVal)
return jsonify(retVal)
except Exception as err:
return jsonify({"error": str(err)}), 409
@user.route("/user/getTransactJobs", methods=['POST'])
@login_required(groups=[USER])
def _getTransactJobs(**kwargs):
try:
if 'accToken' in kwargs:
accToken = kwargs['accToken']
user = accToken.user
data = request.get_json()
year = data['year']
month = data['month']
day = data['day']
date = datetime(year, month, day, 12)
retVal = userController.getAllTransactJobFromUser(user, date)
for data in retVal:
data['from_user'] = data['from_user'].toJSON()
data['to_user'] = data['to_user'].toJSON()
data_date = data['date']
data['date'] = {'year': data_date.year, 'month': data_date.month, 'day': data_date.day}
print(retVal)
return jsonify(retVal)
except Exception as err:
return jsonify({"error": str(err)}), 409
@user.route("/user/deleteTransactJob", methods=['POST'])
@login_required(groups=[USER])
def _deleteTransactJob(**kwargs):
try:
if 'accToken' in kwargs:
accToken = kwargs['accToken']
from_user = accToken.user
data = request.get_json()
year = data['year']
month = data['month']
day = data['day']
username = data['username']
date = datetime(year, month, day, 12)
to_user = userController.getUser(username)
userController.deleteTransactJob(from_user, to_user, date)
return jsonify({"ok": "ok"})
except Exception as err:
return jsonify({"error": str(err)}), 409