| 
									
										
										
										
											2019-05-01 20:43:28 +00:00
										 |  |  | from flask import Blueprint, request, jsonify | 
					
						
							| 
									
										
										
										
											2019-05-02 00:21:50 +00:00
										 |  |  | from datetime import datetime | 
					
						
							| 
									
										
										
										
											2020-05-22 19:55:14 +00:00
										 |  |  | import geruecht.controller.mainController as mc | 
					
						
							| 
									
										
										
										
											2019-12-28 20:52:49 +00:00
										 |  |  | from geruecht.model import MONEY | 
					
						
							| 
									
										
										
										
											2020-01-19 08:07:45 +00:00
										 |  |  | from geruecht.decorator import login_required | 
					
						
							| 
									
										
										
										
											2020-03-10 08:19:11 +00:00
										 |  |  | from geruecht.logger import getDebugLogger, getCreditLogger | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | debug = getDebugLogger() | 
					
						
							|  |  |  | creditL = getCreditLogger() | 
					
						
							| 
									
										
										
										
											2019-05-01 20:43:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | finanzer = Blueprint("finanzer", __name__) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-22 19:55:14 +00:00
										 |  |  | mainController = mc.MainController() | 
					
						
							| 
									
										
										
										
											2019-05-01 20:43:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-19 08:07:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-01 20:43:28 +00:00
										 |  |  | @finanzer.route("/getFinanzerMain") | 
					
						
							| 
									
										
										
										
											2020-01-19 08:07:45 +00:00
										 |  |  | @login_required(groups=[MONEY]) | 
					
						
							|  |  |  | def _getFinanzer(**kwargs): | 
					
						
							| 
									
										
										
										
											2019-05-01 20:43:28 +00:00
										 |  |  |     """ Function for /getFinanzerMain
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Retrieves all User for the groupe 'moneymaster' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Returns: | 
					
						
							| 
									
										
										
										
											2019-05-02 16:50:59 +00:00
										 |  |  |             A JSON-File with Users | 
					
						
							|  |  |  |             or ERROR 401 Permission Denied. | 
					
						
							| 
									
										
										
										
											2019-05-01 20:43:28 +00:00
										 |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2020-03-10 08:19:11 +00:00
										 |  |  |     debug.info("/getFinanzerMain") | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2020-05-22 19:55:14 +00:00
										 |  |  |         users = mainController.getAllUsersfromDB() | 
					
						
							| 
									
										
										
										
											2020-03-10 08:19:11 +00:00
										 |  |  |         dic = {} | 
					
						
							|  |  |  |         for user in users: | 
					
						
							|  |  |  |             dic[user.uid] = user.toJSON() | 
					
						
							| 
									
										
										
										
											2020-03-10 10:08:24 +00:00
										 |  |  |             dic[user.uid]['creditList'] = { | 
					
						
							|  |  |  |                 credit.year: credit.toJSON() for credit in user.geruechte} | 
					
						
							|  |  |  |         debug.debug("return {{ {} }}".format(dic)) | 
					
						
							| 
									
										
										
										
											2020-03-10 08:19:11 +00:00
										 |  |  |         return jsonify(dic) | 
					
						
							|  |  |  |     except Exception as err: | 
					
						
							|  |  |  |         debug.debug("exception", exc_info=True) | 
					
						
							|  |  |  |         return jsonify({"error": str(err)}), 500 | 
					
						
							| 
									
										
										
										
											2019-05-01 20:43:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-10 10:08:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 00:21:50 +00:00
										 |  |  | @finanzer.route("/finanzerAddAmount", methods=['POST']) | 
					
						
							| 
									
										
										
										
											2020-01-19 08:07:45 +00:00
										 |  |  | @login_required(groups=[MONEY]) | 
					
						
							|  |  |  | def _addAmount(**kwargs): | 
					
						
							| 
									
										
										
										
											2019-05-02 16:50:59 +00:00
										 |  |  |     """ Add Amount to User
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         This Function add an amount to the user with posted userID. | 
					
						
							|  |  |  |         If year is not posted the default is the actual Year. | 
					
						
							|  |  |  |         If month is not posted the default is the actual Month. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Returns: | 
					
						
							|  |  |  |             JSON-File with geruecht of year | 
					
						
							|  |  |  |             or ERROR 401 Permission Denied | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2020-03-10 08:19:11 +00:00
										 |  |  |     debug.info("/finanzerAddAmount") | 
					
						
							| 
									
										
										
										
											2020-01-19 08:07:45 +00:00
										 |  |  |     try: | 
					
						
							| 
									
										
										
										
											2020-03-10 08:19:11 +00:00
										 |  |  |         data = request.get_json() | 
					
						
							|  |  |  |         userID = data['userId'] | 
					
						
							|  |  |  |         amount = int(data['amount']) | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             year = int(data['year']) | 
					
						
							| 
									
										
										
										
											2020-03-10 10:08:24 +00:00
										 |  |  |         except KeyError: | 
					
						
							| 
									
										
										
										
											2020-03-10 08:19:11 +00:00
										 |  |  |             year = datetime.now().year | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             month = int(data['month']) | 
					
						
							| 
									
										
										
										
											2020-03-10 10:08:24 +00:00
										 |  |  |         except KeyError: | 
					
						
							| 
									
										
										
										
											2020-03-10 08:19:11 +00:00
										 |  |  |             month = datetime.now().month | 
					
						
							| 
									
										
										
										
											2020-05-22 19:55:14 +00:00
										 |  |  |         mainController.addAmount( | 
					
						
							| 
									
										
										
										
											2020-03-10 10:08:24 +00:00
										 |  |  |             userID, amount, year=year, month=month, finanzer=True) | 
					
						
							| 
									
										
										
										
											2020-05-22 19:55:14 +00:00
										 |  |  |         user = mainController.getUser(userID) | 
					
						
							| 
									
										
										
										
											2020-03-10 10:08:24 +00:00
										 |  |  |         retVal = {str(geruecht.year): geruecht.toJSON() | 
					
						
							|  |  |  |                   for geruecht in user.geruechte} | 
					
						
							| 
									
										
										
										
											2020-03-10 08:19:11 +00:00
										 |  |  |         retVal['locked'] = user.locked | 
					
						
							| 
									
										
										
										
											2020-03-10 10:08:24 +00:00
										 |  |  |         debug.debug("return {{ {} }}".format(retVal)) | 
					
						
							|  |  |  |         creditL.info("{} Finanzer {} {} fügt {} {} {} € Schulden hinzu.".format(datetime(year, month, 1).date( | 
					
						
							|  |  |  |         ), kwargs['accToken'].user.firstname, kwargs['accToken'].user.lastname, user.firstname, user.lastname, amount/100)) | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2019-05-02 00:21:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-10 10:08:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 00:21:50 +00:00
										 |  |  | @finanzer.route("/finanzerAddCredit", methods=['POST']) | 
					
						
							| 
									
										
										
										
											2020-01-19 08:07:45 +00:00
										 |  |  | @login_required(groups=[MONEY]) | 
					
						
							|  |  |  | def _addCredit(**kwargs): | 
					
						
							| 
									
										
										
										
											2019-05-02 16:50:59 +00:00
										 |  |  |     """ Add Credit to User
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         This Function add an credit to the user with posted userID. | 
					
						
							|  |  |  |         If year is not posted the default is the actual Year. | 
					
						
							|  |  |  |         If month is not posted the default is the actual Month. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Returns: | 
					
						
							|  |  |  |             JSON-File with geruecht of year | 
					
						
							|  |  |  |             or ERROR 401 Permission Denied | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2020-03-10 08:19:11 +00:00
										 |  |  |     debug.info("/finanzerAddCredit") | 
					
						
							| 
									
										
										
										
											2020-01-19 08:07:45 +00:00
										 |  |  |     try: | 
					
						
							| 
									
										
										
										
											2020-03-10 08:19:11 +00:00
										 |  |  |         data = request.get_json() | 
					
						
							|  |  |  |         userID = data['userId'] | 
					
						
							|  |  |  |         credit = int(data['credit']) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             year = int(data['year']) | 
					
						
							| 
									
										
										
										
											2020-03-10 10:08:24 +00:00
										 |  |  |         except KeyError: | 
					
						
							| 
									
										
										
										
											2020-03-10 08:19:11 +00:00
										 |  |  |             year = datetime.now().year | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             month = int(data['month']) | 
					
						
							| 
									
										
										
										
											2020-03-10 10:08:24 +00:00
										 |  |  |         except KeyError: | 
					
						
							| 
									
										
										
										
											2020-03-10 08:19:11 +00:00
										 |  |  |             month = datetime.now().month | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-22 19:55:14 +00:00
										 |  |  |         mainController.addCredit( | 
					
						
							| 
									
										
										
										
											2020-03-10 10:08:24 +00:00
										 |  |  |             userID, credit, year=year, month=month).toJSON() | 
					
						
							| 
									
										
										
										
											2020-05-22 19:55:14 +00:00
										 |  |  |         user = mainController.getUser(userID) | 
					
						
							| 
									
										
										
										
											2020-03-10 10:08:24 +00:00
										 |  |  |         retVal = {str(geruecht.year): geruecht.toJSON() | 
					
						
							|  |  |  |                   for geruecht in user.geruechte} | 
					
						
							| 
									
										
										
										
											2020-03-10 08:19:11 +00:00
										 |  |  |         retVal['locked'] = user.locked | 
					
						
							| 
									
										
										
										
											2020-03-10 10:08:24 +00:00
										 |  |  |         debug.debug("return {{ {} }}".format(retVal)) | 
					
						
							|  |  |  |         creditL.info("{} Finanzer {} {} fügt {} {} {} € Guthaben hinzu.".format(datetime(year, month, 1).date( | 
					
						
							|  |  |  |         ), kwargs['accToken'].user.firstname, kwargs['accToken'].user.lastname, user.firstname, user.lastname,  credit / 100)) | 
					
						
							| 
									
										
										
										
											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-01-19 08:07:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-29 16:55:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | @finanzer.route("/finanzerLock", methods=['POST']) | 
					
						
							| 
									
										
										
										
											2020-01-19 08:07:45 +00:00
										 |  |  | @login_required(groups=[MONEY]) | 
					
						
							|  |  |  | def _finanzerLock(**kwargs): | 
					
						
							| 
									
										
										
										
											2020-03-10 08:19:11 +00:00
										 |  |  |     debug.info("/finanzerLock") | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         data = request.get_json() | 
					
						
							|  |  |  |         username = data['userId'] | 
					
						
							|  |  |  |         locked = bool(data['locked']) | 
					
						
							| 
									
										
										
										
											2020-05-22 19:55:14 +00:00
										 |  |  |         retVal = mainController.lockUser(username, locked).toJSON() | 
					
						
							| 
									
										
										
										
											2020-03-10 10:08:24 +00:00
										 |  |  |         debug.debug("return {{ {} }}".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-01-19 08:07:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-29 16:55:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | @finanzer.route("/finanzerSetConfig", methods=['POST']) | 
					
						
							| 
									
										
										
										
											2020-01-19 08:07:45 +00:00
										 |  |  | @login_required(groups=[MONEY]) | 
					
						
							|  |  |  | def _finanzerSetConfig(**kwargs): | 
					
						
							| 
									
										
										
										
											2020-03-10 08:19:11 +00:00
										 |  |  |     debug.info("/finanzerSetConfig") | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         data = request.get_json() | 
					
						
							|  |  |  |         username = data['userId'] | 
					
						
							|  |  |  |         autoLock = bool(data['autoLock']) | 
					
						
							|  |  |  |         limit = int(data['limit']) | 
					
						
							| 
									
										
										
										
											2020-05-22 19:55:14 +00:00
										 |  |  |         retVal = mainController.updateConfig( | 
					
						
							| 
									
										
										
										
											2020-03-10 10:08:24 +00:00
										 |  |  |             username, {'lockLimit': limit, 'autoLock': autoLock}).toJSON() | 
					
						
							|  |  |  |         debug.debug("return {{ {} }}".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 | 
					
						
							| 
									
										
										
										
											2019-12-29 20:36:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-10 10:08:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-29 20:36:42 +00:00
										 |  |  | @finanzer.route("/finanzerAddUser", methods=['POST']) | 
					
						
							| 
									
										
										
										
											2020-01-19 08:07:45 +00:00
										 |  |  | @login_required(groups=[MONEY]) | 
					
						
							|  |  |  | def _finanzerAddUser(**kwargs): | 
					
						
							| 
									
										
										
										
											2020-03-10 08:19:11 +00:00
										 |  |  |     debug.info("/finanzerAddUser") | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         data = request.get_json() | 
					
						
							|  |  |  |         username = data['userId'] | 
					
						
							| 
									
										
										
										
											2020-05-22 19:55:14 +00:00
										 |  |  |         mainController.getUser(username) | 
					
						
							|  |  |  |         users = mainController.getAllUsersfromDB() | 
					
						
							| 
									
										
										
										
											2020-03-10 08:19:11 +00:00
										 |  |  |         dic = {} | 
					
						
							|  |  |  |         for user in users: | 
					
						
							|  |  |  |             dic[user.uid] = user.toJSON() | 
					
						
							| 
									
										
										
										
											2020-03-10 10:08:24 +00:00
										 |  |  |             dic[user.uid]['creditList'] = { | 
					
						
							|  |  |  |                 credit.year: credit.toJSON() for credit in user.geruechte} | 
					
						
							|  |  |  |         debug.debug("return {{ {} }}".format(dic)) | 
					
						
							| 
									
										
										
										
											2020-03-10 08:19:11 +00:00
										 |  |  |         return jsonify(dic), 200 | 
					
						
							|  |  |  |     except Exception as err: | 
					
						
							|  |  |  |         debug.debug("exception", exc_info=True) | 
					
						
							|  |  |  |         return jsonify({"error": str(err)}), 500 | 
					
						
							| 
									
										
										
										
											2020-01-05 13:15:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-10 10:08:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-05 13:15:02 +00:00
										 |  |  | @finanzer.route("/finanzerSendOneMail", methods=['POST']) | 
					
						
							| 
									
										
										
										
											2020-01-19 08:07:45 +00:00
										 |  |  | @login_required(groups=[MONEY]) | 
					
						
							|  |  |  | def _finanzerSendOneMail(**kwargs): | 
					
						
							| 
									
										
										
										
											2020-03-10 08:19:11 +00:00
										 |  |  |     debug.info("/finanzerSendOneMail") | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         data = request.get_json() | 
					
						
							|  |  |  |         username = data['userId'] | 
					
						
							| 
									
										
										
										
											2020-05-22 19:55:14 +00:00
										 |  |  |         retVal = mainController.sendMail(username) | 
					
						
							| 
									
										
										
										
											2020-03-10 10:08:24 +00:00
										 |  |  |         debug.debug("return {{ {} }}".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-01-05 13:15:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-10 10:08:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-05 13:15:02 +00:00
										 |  |  | @finanzer.route("/finanzerSendAllMail", methods=['GET']) | 
					
						
							| 
									
										
										
										
											2020-01-19 08:07:45 +00:00
										 |  |  | @login_required(groups=[MONEY]) | 
					
						
							|  |  |  | def _finanzerSendAllMail(**kwargs): | 
					
						
							| 
									
										
										
										
											2020-03-10 08:19:11 +00:00
										 |  |  |     debug.info("/finanzerSendAllMail") | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2020-05-22 19:55:14 +00:00
										 |  |  |         retVal = mainController.sendAllMail() | 
					
						
							| 
									
										
										
										
											2020-03-10 10:08:24 +00:00
										 |  |  |         debug.debug("return {{ {} }}".format(retVal)) | 
					
						
							| 
									
										
										
										
											2020-03-10 08:19:11 +00:00
										 |  |  |         return jsonify(retVal) | 
					
						
							|  |  |  |     except Exception as err: | 
					
						
							|  |  |  |         debug.debug("exception", exc_info=True) | 
					
						
							| 
									
										
										
										
											2020-03-10 10:08:24 +00:00
										 |  |  |         return jsonify({"error": str(err)}), 500 |