2020-01-17 00:05:58 +00:00
from flask import Blueprint , request , jsonify
2020-01-19 08:07:45 +00:00
from geruecht . decorator import login_required
2020-01-18 23:37:40 +00:00
import geruecht . controller . userController as uc
2020-01-17 00:05:58 +00:00
from geruecht . model import USER
from datetime import datetime
2020-02-23 21:31:22 +00:00
from geruecht . exceptions import DayLocked
2020-03-10 08:19:11 +00:00
from geruecht . logger import getDebugLogger , getCreditLogger , getJobsLogger
2020-01-17 00:05:58 +00:00
user = Blueprint ( " user " , __name__ )
2020-01-18 23:37:40 +00:00
userController = uc . UserController ( )
2020-03-10 08:19:11 +00:00
debug = getDebugLogger ( )
creditL = getCreditLogger ( )
jobL = getJobsLogger ( )
2020-01-17 00:05:58 +00:00
2020-01-19 08:07:45 +00:00
@user.route ( " /user/main " )
@login_required ( groups = [ USER ] )
def _main ( * * kwargs ) :
2020-03-10 08:19:11 +00:00
debug . info ( " /user/main " )
try :
if ' accToken ' in kwargs :
accToken = kwargs [ ' accToken ' ]
accToken . user = userController . getUser ( accToken . user . uid )
retVal = accToken . user . toJSON ( )
retVal [ ' creditList ' ] = { credit . year : credit . toJSON ( ) for credit in accToken . user . geruechte }
2020-03-10 10:08:24 +00:00
debug . debug ( " return {{ {} }} " . format ( retVal ) )
2020-03-10 08:19:11 +00:00
return jsonify ( retVal )
2020-03-10 10:08:24 +00:00
except Exception :
2020-03-10 08:19:11 +00:00
debug . debug ( " exception " , exc_info = True )
return jsonify ( " error " , " something went wrong " ) , 500
2020-01-17 00:05:58 +00:00
@user.route ( " /user/addAmount " , methods = [ ' POST ' ] )
2020-01-19 08:07:45 +00:00
@login_required ( groups = [ USER ] )
def _addAmount ( * * kwargs ) :
2020-03-10 08:19:11 +00:00
debug . info ( " /user/addAmount " )
try :
if ' accToken ' in kwargs :
accToken = kwargs [ ' accToken ' ]
data = request . get_json ( )
amount = int ( data [ ' amount ' ] )
date = datetime . now ( )
userController . addAmount ( accToken . user . uid , amount , year = date . year , month = date . month )
accToken . user = userController . getUser ( accToken . user . uid )
retVal = accToken . user . toJSON ( )
retVal [ ' creditList ' ] = { credit . year : credit . toJSON ( ) for credit in accToken . user . geruechte }
2020-03-10 10:08:24 +00:00
debug . debug ( " return {{ {} }} " . format ( retVal ) )
2020-03-10 08:19:11 +00:00
creditL . info ( " {} {} {} fügt sich selbst {} € Schulden hinzu " . format ( date , accToken . user . firstname , accToken . user . lastname , amount / 100 ) )
return jsonify ( retVal )
2020-03-10 10:08:24 +00:00
except Exception :
2020-03-10 08:19:11 +00:00
debug . debug ( " exception " , exc_info = True )
return jsonify ( { " error " : " something went wrong " } ) , 500
2020-01-26 22:31:22 +00:00
@user.route ( " /user/saveConfig " , methods = [ ' POST ' ] )
@login_required ( groups = [ USER ] )
def _saveConfig ( * * kwargs ) :
2020-03-10 08:19:11 +00:00
debug . info ( " /user/saveConfig " )
2020-01-26 22:31:22 +00:00
try :
if ' accToken ' in kwargs :
accToken = kwargs [ ' accToken ' ]
data = request . get_json ( )
accToken . user = userController . modifyUser ( accToken . user , accToken . ldap_conn , data )
2020-01-26 22:54:18 +00:00
retVal = accToken . user . toJSON ( )
retVal [ ' creditList ' ] = { credit . year : credit . toJSON ( ) for credit in accToken . user . geruechte }
2020-03-10 10:08:24 +00:00
debug . debug ( " return {{ {} }} " . format ( retVal ) )
2020-01-26 22:54:18 +00:00
return jsonify ( retVal )
2020-01-26 22:31:22 +00:00
except Exception as err :
2020-03-10 08:19:11 +00:00
debug . debug ( " exception " , exc_info = True )
2020-02-25 21:50:32 +00:00
return jsonify ( { " error " : str ( err ) } ) , 409
2020-01-27 19:16:04 +00:00
@user.route ( " /user/job " , methods = [ ' POST ' ] )
@login_required ( groups = [ USER ] )
2020-02-22 10:15:20 +00:00
def _getUser ( * * kwargs ) :
2020-03-10 08:19:11 +00:00
debug . info ( " /user/job " )
try :
data = request . get_json ( )
day = data [ ' day ' ]
month = data [ ' month ' ]
year = data [ ' year ' ]
date = datetime ( year , month , day , 12 )
lockedDay = userController . getLockedDay ( date )
if not lockedDay :
lockedDay = {
' date ' : {
' year ' : year ,
' month ' : month ,
' day ' : day
} ,
' locked ' : False
}
else :
lockedDay = {
' date ' : {
' year ' : year ,
' month ' : month ,
' day ' : day
} ,
' locked ' : lockedDay [ ' locked ' ]
}
retVal = {
' worker ' : userController . getWorker ( date ) ,
' day ' : lockedDay
2020-02-23 21:31:22 +00:00
}
2020-03-10 10:08:24 +00:00
debug . debug ( " retrun {{ {} }} " . format ( retVal ) )
2020-03-10 08:19:11 +00:00
return jsonify ( retVal )
except Exception as err :
debug . debug ( " exception " , exc_info = True )
return jsonify ( { " error " : str ( err ) } ) , 500
2020-02-23 21:31:22 +00:00
@user.route ( " /user/addJob " , methods = [ ' POST ' ] )
@login_required ( groups = [ USER ] )
def _addUser ( * * kwargs ) :
2020-03-10 08:19:11 +00:00
debug . info ( " /user/addJob " )
2020-02-23 21:31:22 +00:00
try :
if ' accToken ' in kwargs :
accToken = kwargs [ ' accToken ' ]
user = accToken . user
data = request . get_json ( )
day = data [ ' day ' ]
month = data [ ' month ' ]
year = data [ ' year ' ]
date = datetime ( year , month , day , 12 )
retVal = userController . addWorker ( user . uid , date , userExc = True )
2020-03-10 10:08:24 +00:00
debug . debug ( " return {{ {} }} " . format ( retVal ) )
2020-03-10 08:19:11 +00:00
jobL . info ( " Mitglied {} {} schreib sich am {} zum Dienst ein. " . format ( user . firstname , user . lastname , date . date ( ) ) )
2020-02-23 21:31:22 +00:00
return jsonify ( retVal )
except DayLocked as err :
2020-03-10 08:19:11 +00:00
debug . debug ( " exception " , exc_info = True )
2020-02-25 21:50:32 +00:00
return jsonify ( { ' error ' : str ( err ) } ) , 403
2020-02-23 21:31:22 +00:00
except Exception as err :
2020-03-10 08:19:11 +00:00
debug . debug ( " exception " , exc_info = True )
2020-02-25 21:50:32 +00:00
return jsonify ( { ' error ' : str ( err ) } ) , 409
2020-02-24 17:55:57 +00:00
@user.route ( " /user/deleteJob " , methods = [ ' POST ' ] )
@login_required ( groups = [ USER ] )
def _deletJob ( * * kwargs ) :
2020-03-10 08:19:11 +00:00
debug . info ( " /user/deleteJob " )
2020-02-24 17:55:57 +00:00
try :
if ' accToken ' in kwargs :
accToken = kwargs [ ' accToken ' ]
user = accToken . user
data = request . get_json ( )
day = data [ ' day ' ]
month = data [ ' month ' ]
year = data [ ' year ' ]
date = datetime ( year , month , day , 12 )
userController . deleteWorker ( user . uid , date , True )
2020-03-10 08:19:11 +00:00
debug . debug ( " return ok " )
jobL . info ( " Mitglied {} {} entfernt sich am {} aus dem Dienst " . format ( user . firstname , user . lastname , date . date ( ) ) )
2020-02-24 17:55:57 +00:00
return jsonify ( { " ok " : " ok " } )
except DayLocked as err :
2020-03-10 08:19:11 +00:00
debug . debug ( " exception " , exc_info = True )
2020-02-25 21:50:32 +00:00
return jsonify ( { " error " : str ( err ) } ) , 403
2020-02-24 17:55:57 +00:00
except Exception as err :
2020-03-10 08:19:11 +00:00
debug . debug ( " exception " , exc_info = True )
2020-02-25 21:50:32 +00:00
return jsonify ( { " error " : str ( err ) } ) , 409
@user.route ( " /user/transactJob " , methods = [ ' POST ' ] )
@login_required ( groups = [ USER ] )
def _transactJob ( * * kwargs ) :
2020-03-10 08:19:11 +00:00
debug . info ( " /user/transactJob " )
2020-02-25 21:50:32 +00:00
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 )
2020-03-10 08:19:11 +00:00
from_userl = retVal [ ' from_user ' ]
to_userl = retVal [ ' to_user ' ]
2020-02-25 21:50:32 +00:00
retVal [ ' from_user ' ] = retVal [ ' from_user ' ] . toJSON ( )
retVal [ ' to_user ' ] = retVal [ ' to_user ' ] . toJSON ( )
retVal [ ' date ' ] = { ' year ' : year , ' month ' : month , ' day ' : day }
2020-03-10 10:08:24 +00:00
debug . debug ( " return {{ {} }} " . format ( retVal ) )
2020-03-10 08:19:11 +00:00
jobL . info ( " Mitglied {} {} sendet Dienstanfrage an Mitglied {} {} am {} " . format ( from_userl . firstname , from_userl . lastname , to_userl . firstname , to_userl . lastname , date . date ( ) ) )
2020-02-25 21:50:32 +00:00
return jsonify ( retVal )
except Exception as err :
2020-03-10 08:19:11 +00:00
debug . debug ( " exception " , exc_info = True )
2020-02-25 21:50:32 +00:00
return jsonify ( { " error " : str ( err ) } ) , 409
@user.route ( " /user/answerTransactJob " , methods = [ ' POST ' ] )
@login_required ( groups = [ USER ] )
def _answer ( * * kwargs ) :
2020-03-10 08:19:11 +00:00
debug . info ( " /user/answerTransactJob " )
2020-02-25 21:50:32 +00:00
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 )
2020-03-10 08:19:11 +00:00
from_userl = retVal [ ' from_user ' ]
to_userl = retVal [ ' to_user ' ]
2020-02-25 21:50:32 +00:00
retVal [ ' from_user ' ] = retVal [ ' from_user ' ] . toJSON ( )
retVal [ ' to_user ' ] = retVal [ ' to_user ' ] . toJSON ( )
retVal [ ' date ' ] = { ' year ' : year , ' month ' : month , ' day ' : day }
2020-03-10 10:08:24 +00:00
debug . debug ( " return {{ {} }} " . format ( retVal ) )
2020-03-10 08:19:11 +00:00
jobL . info ( " Mitglied {} {} beantwortet Dienstanfrage von {} {} am {} mit {} " . format ( to_userl . firstname , to_userl . lastname , from_userl . firstname , from_userl . lastname , date . date ( ) , ' JA ' if answer else ' NEIN ' ) )
2020-02-25 21:50:32 +00:00
return jsonify ( retVal )
except Exception as err :
2020-03-10 08:19:11 +00:00
debug . debug ( " exception " , exc_info = True )
2020-02-25 21:50:32 +00:00
return jsonify ( { " error " : str ( err ) } ) , 409
@user.route ( " /user/jobRequests " , methods = [ ' POST ' ] )
@login_required ( groups = [ USER ] )
def _requests ( * * kwargs ) :
2020-03-10 08:19:11 +00:00
debug . info ( " /user/jobRequests " )
2020-02-25 21:50:32 +00:00
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 }
2020-03-10 10:08:24 +00:00
debug . debug ( " return {{ {} }} " . format ( retVal ) )
2020-02-25 21:50:32 +00:00
return jsonify ( retVal )
except Exception as err :
2020-03-10 08:19:11 +00:00
debug . debug ( " exception " , exc_info = True )
2020-02-25 21:50:32 +00:00
return jsonify ( { " error " : str ( err ) } ) , 409
@user.route ( " /user/getTransactJobs " , methods = [ ' POST ' ] )
@login_required ( groups = [ USER ] )
def _getTransactJobs ( * * kwargs ) :
2020-03-10 08:19:11 +00:00
debug . info ( " /user/getTransactJobs " )
2020-02-25 21:50:32 +00:00
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 }
2020-03-10 10:08:24 +00:00
debug . debug ( " return {{ {} }} " . format ( retVal ) )
2020-02-25 21:50:32 +00:00
return jsonify ( retVal )
except Exception as err :
2020-03-10 08:19:11 +00:00
debug . debug ( " exception " , exc_info = True )
2020-02-25 21:50:32 +00:00
return jsonify ( { " error " : str ( err ) } ) , 409
@user.route ( " /user/deleteTransactJob " , methods = [ ' POST ' ] )
@login_required ( groups = [ USER ] )
def _deleteTransactJob ( * * kwargs ) :
2020-03-10 08:19:11 +00:00
debug . info ( " /user/deleteTransactJob " )
2020-02-25 21:50:32 +00:00
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 )
2020-03-10 08:19:11 +00:00
debug . debug ( " return ok " )
jobL . info ( " Mitglied {} {} entfernt Dienstanfrage an {} {} am {} " . format ( from_user . firstname , from_user . lastname , to_user . firstname , to_user . lastname , date . date ( ) ) )
2020-02-25 21:50:32 +00:00
return jsonify ( { " ok " : " ok " } )
except Exception as err :
2020-03-10 08:19:11 +00:00
debug . debug ( " exception " , exc_info = True )
2020-02-27 14:53:49 +00:00
return jsonify ( { " error " : str ( err ) } ) , 409
@user.route ( " /user/storno " , methods = [ ' POST ' ] )
@login_required ( groups = [ USER ] )
def _storno ( * * kwargs ) :
""" Function for Baruser to storno amount
This function added to the user with the posted userID the posted amount .
Returns :
JSON - File with userID and the amount
or ERROR 401 Permission Denied
"""
2020-03-10 08:19:11 +00:00
debug . info ( " /user/storno " )
2020-02-27 14:53:49 +00:00
try :
if ' accToken ' in kwargs :
accToken = kwargs [ ' accToken ' ]
user = accToken . user
data = request . get_json ( )
amount = int ( data [ ' amount ' ] )
date = datetime . now ( )
userController . addCredit ( user . uid , amount , year = date . year , month = date . month )
accToken . user = userController . getUser ( accToken . user . uid )
retVal = accToken . user . toJSON ( )
retVal [ ' creditList ' ] = { credit . year : credit . toJSON ( ) for credit in accToken . user . geruechte }
2020-03-10 10:08:24 +00:00
debug . debug ( " return {{ {} }} " . format ( retVal ) )
2020-03-10 08:19:11 +00:00
creditL . info ( " {} {} {} storniert {} € " . format ( date , user . firstname , user . lastname , amount / 100 ) )
2020-02-27 14:53:49 +00:00
return jsonify ( retVal )
except Exception as err :
2020-03-10 08:19:11 +00:00
debug . debug ( " exception " , exc_info = True )
2020-02-27 14:53:49 +00:00
return jsonify ( { " error " : str ( err ) } ) , 409