add route /sm/getUsers

updatet usercontroller for getUsers
updatet getLockedDay -> only the day what requestet will be locked (if nessecerie)
This commit is contained in:
Tim Gröger 2020-03-11 18:32:57 +01:00
parent 8900367a3e
commit cc692bb82c
2 changed files with 149 additions and 75 deletions

View File

@ -11,10 +11,12 @@ from geruecht.logger import getDebugLogger
db = dc.DatabaseController()
ldap = lc.LDAPController()
emailController = ec.EmailController(mailConfig['URL'], mailConfig['user'], mailConfig['passwd'], mailConfig['crypt'], mailConfig['port'], mailConfig['email'])
emailController = ec.EmailController(
mailConfig['URL'], mailConfig['user'], mailConfig['passwd'], mailConfig['crypt'], mailConfig['port'], mailConfig['email'])
debug = getDebugLogger()
class UserController(metaclass=Singleton):
def __init__(self):
@ -36,7 +38,8 @@ class UserController(metaclass=Singleton):
def setStatus(self, name):
debug.info("set status of user {{ {} }}".format(name))
retVal = db.setStatus(name)
debug.debug("settet status of user {{ {} }} is {{ {} }}".format(name, retVal))
debug.debug(
"settet status of user {{ {} }} is {{ {} }}".format(name, retVal))
return retVal
def deleteStatus(self, status):
@ -50,15 +53,19 @@ class UserController(metaclass=Singleton):
return retVal
def updateStatusOfUser(self, username, status):
debug.info("update status {{ {} }} of user {{ {} }}".format(status, username))
debug.info("update status {{ {} }} of user {{ {} }}".format(
status, username))
retVal = db.updateStatusOfUser(username, status)
debug.debug("updatet status of user {{ {} }} is {{ {} }}".format(username, retVal))
debug.debug(
"updatet status of user {{ {} }} is {{ {} }}".format(username, retVal))
return retVal
def updateVotingOfUser(self, username, voting):
debug.info("update voting {{ {} }} of user {{ {} }}".format(voting, username))
debug.info("update voting {{ {} }} of user {{ {} }}".format(
voting, username))
retVal = db.updateVotingOfUser(username, voting)
debug.debug("updatet voting of user {{ {} }} is {{ {} }}".format(username, retVal))
debug.debug(
"updatet voting of user {{ {} }} is {{ {} }}".format(username, retVal))
return retVal
def deleteDrinkType(self, type):
@ -106,49 +113,61 @@ class UserController(metaclass=Singleton):
return list
def setTransactJob(self, from_user, to_user, date):
debug.info("set transact job from {{ {} }} to {{ {} }} on {{ {} }}".format(from_user, to_user, date))
debug.info("set transact job from {{ {} }} to {{ {} }} on {{ {} }}".format(
from_user, to_user, date))
jobtransact = db.setTransactJob(from_user, to_user, date.date())
debug.debug("transact job is {{ {} }}".format(jobtransact))
debug.info("send mail with transact job to user")
emailController.sendMail(jobtransact['to_user'], 'jobtransact', jobtransact)
emailController.sendMail(
jobtransact['to_user'], 'jobtransact', jobtransact)
return jobtransact
def getTransactJobFromUser(self, user, date):
debug.info("get transact job from user {{ {} }} on {{ {} }}".format(user, date))
debug.info(
"get transact job from user {{ {} }} on {{ {} }}".format(user, date))
retVal = db.getTransactJobFromUser(user, date.date())
debug.debug("transact job from user {{ {} }} is {{ {} }}".format(user, retVal))
debug.debug(
"transact job from user {{ {} }} is {{ {} }}".format(user, retVal))
return retVal
def getAllTransactJobFromUser(self, user, date):
debug.info("get all transact job from user {{ {} }} start on {{ {} }}".format(user, date))
debug.info(
"get all transact job from user {{ {} }} start on {{ {} }}".format(user, date))
retVal = db.getAllTransactJobFromUser(user, date.date())
debug.debug("all transact job are {{ {} }}".format(retVal))
return retVal
def getAllTransactJobToUser(self, user, date):
debug.info("get all transact job from to_user {{ {} }} start on {{ {} }}".format(user, date))
debug.info(
"get all transact job from to_user {{ {} }} start on {{ {} }}".format(user, date))
retVal = db.getAllTransactJobToUser(user, date.date())
debug.debug("all transact job are {{ {} }}".format(retVal))
return retVal
def getTransactJob(self, from_user, to_user, date):
debug.info("get transact job from user {{ {} }} to user {{ {} }} on {{ {} }}".format(from_user, to_user, date))
debug.info("get transact job from user {{ {} }} to user {{ {} }} on {{ {} }}".format(
from_user, to_user, date))
retVal = db.getTransactJob(from_user, to_user, date.date())
debug.debug("transact job is {{ {} }}".format(retVal))
return retVal
def deleteTransactJob(self, from_user, to_user, date):
debug.info("delete transact job from user {{ {} }} to user {{ {} }} on {{ {} }}".format(from_user, to_user, date))
debug.info("delete transact job from user {{ {} }} to user {{ {} }} on {{ {} }}".format(
from_user, to_user, date))
transactJob = self.getTransactJob(from_user, to_user, date)
debug.debug("transact job is {{ {} }}".format(transactJob))
if transactJob['answerd']:
debug.warning("transactjob {{ {} }} can not delete because is answerd")
raise TansactJobIsAnswerdException("TransactJob is already answerd")
debug.warning(
"transactjob {{ {} }} can not delete because is answerd")
raise TansactJobIsAnswerdException(
"TransactJob is already answerd")
db.deleteTransactJob(from_user, to_user, date.date())
def answerdTransactJob(self, from_user, to_user, date, answer):
debug.info("answer transact job from user {{ {} }} to user {{ {} }} on {{ {} }} with answer {{ {} }}".format(from_user, to_user, date, answer))
transactJob = db.updateTransactJob(from_user, to_user, date.date(), answer)
debug.info("answer transact job from user {{ {} }} to user {{ {} }} on {{ {} }} with answer {{ {} }}".format(
from_user, to_user, date, answer))
transactJob = db.updateTransactJob(
from_user, to_user, date.date(), answer)
debug.debug("transactjob is {{ {} }}".format(transactJob))
if answer:
debug.info("add worker on date {{ {} }}".format(date))
@ -156,11 +175,26 @@ class UserController(metaclass=Singleton):
return transactJob
def setLockedDay(self, date, locked, hard=False):
debug.info("set day locked on {{ {} }} with state {{ {} }}".format(date, locked))
debug.info(
"set day locked on {{ {} }} with state {{ {} }}".format(date, locked))
retVal = db.setLockedDay(date.date(), locked, hard)
debug.debug("seted day locked is {{ {} }}".format(retVal))
return retVal
def getLockedDays(self, from_date, to_date):
debug.info("get locked days from {{ {} }} to {{ {} }}".format(
from_date.date(), to_date.date()))
oneDay = timedelta(1)
delta = to_date.date() - from_date.date()
retVal = []
startdate = from_date - oneDay
for _ in range(delta.days + 1):
startdate += oneDay
lockday = self.getLockedDay(startdate)
retVal.append(lockday)
debug.debug("lock days are {{ {} }}".format(retVal))
return retVal
def getLockedDay(self, date):
debug.info("get locked day on {{ {} }}".format(date))
now = datetime.now()
@ -173,33 +207,34 @@ class UserController(metaclass=Singleton):
oldMonth = True
break
debug.debug("oldMonth is {{ {} }}".format(oldMonth))
lockedYear = date.year
lockedMonth = date.month if date.month < now.month else now.month - 1 if oldMonth else now.month
daysInMonth = calendar.monthrange(lockedYear, lockedMonth)[1]
startDay = 1
debug.debug("calculate start day of month")
lockedYear = now.year
lockedMonth = now.month if now.month < now.month else now.month - \
1 if oldMonth else now.month
endDay = 1
debug.debug("calculate end day of month")
for i in range(1, 8):
if datetime(lockedYear, lockedMonth, i).weekday() == 2:
startDay = i
nextMonth = datetime(lockedYear, lockedMonth + 1, i)
if nextMonth.weekday() == 2:
endDay = i
break
debug.debug("start day of month is {{ {} }}".format(startDay))
debug.debug("check if date should be locked")
if lockedYear <= now.year and lockedMonth <= now.month:
for i in range(startDay, daysInMonth + 1):
debug.debug("lock day {{ {} }}".format(datetime(lockedYear, lockedMonth, i)))
self.setLockedDay(datetime(lockedYear, lockedMonth, i), True)
for i in range(1, 8):
nextMonth = datetime(lockedYear, lockedMonth + 1, i)
if nextMonth.weekday() == 2:
break
debug.debug("lock day {{ {} }}".format(datetime(lockedYear, lockedMonth, i)))
self.setLockedDay(nextMonth, True)
monthLockedEndDate = datetime(lockedYear, lockedMonth + 1, endDay)
debug.debug("get lock day from database")
retVal = db.getLockedDay(date.date())
if not retVal:
debug.debug(
"lock day not exists, retVal is {{ {} }}".format(retVal))
if date.date() <= monthLockedEndDate.date():
debug.debug("lock day {{ {} }}".format(date.date()))
self.setLockedDay(date, True)
retVal = db.getLockedDay(date.date())
else:
retVal = {"daydate": date.date(), "locked": False}
debug.debug("locked day is {{ {} }}".format(retVal))
return retVal
def getWorker(self, date, username=None):
debug.info("get worker on {{ {} }}".format(username, date))
debug.info("get worker {{ {} }} on {{ {} }}".format(username, date))
if (username):
user = self.getUser(username)
debug.debug("user is {{ {} }}".format(user))
@ -230,7 +265,8 @@ class UserController(metaclass=Singleton):
return retVal
def deleteWorker(self, username, date, userExc=False):
debug.info("delete worker {{ {} }} on date {{ {} }}".format(username, date))
debug.info(
"delete worker {{ {} }} on date {{ {} }}".format(username, date))
user = self.getUser(username)
debug.debug("user is {{ {} }}".format(user))
if userExc:
@ -238,9 +274,11 @@ class UserController(metaclass=Singleton):
lockedDay = self.getLockedDay(date)
if lockedDay:
if lockedDay['locked']:
debug.debug("day is locked, check if accepted transact job exists")
debug.debug(
"day is locked, check if accepted transact job exists")
transactJobs = self.getTransactJobFromUser(user, date)
debug.debug("transact job is {{ {} }}".format(transactJobs))
debug.debug(
"transact job is {{ {} }}".format(transactJobs))
found = False
for job in transactJobs:
if job['accepted'] and job['answerd']:
@ -249,11 +287,13 @@ class UserController(metaclass=Singleton):
break
if not found:
debug.debug("no accepted transact job found")
raise DayLocked("Day is locked. You can't delete the Job")
raise DayLocked(
"Day is locked. You can't delete the Job")
db.deleteWorker(user, date)
def lockUser(self, username, locked):
debug.info("lock user {{ {} }} for credit with status {{ {} }}".format(username, locked))
debug.info("lock user {{ {} }} for credit with status {{ {} }}".format(
username, locked))
user = self.getUser(username)
debug.debug("user is {{ {} }}".format(user))
user.updateData({'locked': locked})
@ -263,7 +303,8 @@ class UserController(metaclass=Singleton):
return retVal
def updateConfig(self, username, data):
debug.info("update config of user {{ {} }} with config {{ {} }}".format(username, data))
debug.info(
"update config of user {{ {} }} with config {{ {} }}".format(username, data))
user = self.getUser(username)
debug.debug("user is {{ {} }}".format(user))
user.updateData(data)
@ -290,26 +331,30 @@ class UserController(metaclass=Singleton):
credit = user.getGeruecht(year=datetime.now().year).getSchulden()
limit = -1*user.limit
if credit <= limit:
debug.debug("credit {{ {} }} is more than user limit {{ {} }}".format(credit, limit))
debug.debug(
"credit {{ {} }} is more than user limit {{ {} }}".format(credit, limit))
debug.debug("lock user")
user.updateData({'locked': True})
debug.debug("send mail to user")
emailController.sendMail(user)
else:
debug.debug("cretid {{ {} }} is less than user limit {{ {} }}".format(credit, limit))
debug.debug(
"cretid {{ {} }} is less than user limit {{ {} }}".format(credit, limit))
debug.debug("unlock user")
user.updateData({'locked': False})
db.updateUser(user)
def addAmount(self, username, amount, year, month, finanzer=False):
debug.info("add amount {{ {} }} to user {{ {} }} no month {{ {} }}, year {{ {} }}".format(amount, username, month, year))
debug.info("add amount {{ {} }} to user {{ {} }} no month {{ {} }}, year {{ {} }}".format(
amount, username, month, year))
user = self.getUser(username)
debug.debug("user is {{ {} }}".format(user))
if user.uid == 'extern':
debug.debug("user is extern user, so exit add amount")
return
if not user.locked or finanzer:
debug.debug("user is not locked {{ {} }} or is finanzer execution {{ {} }}".format(user.locked, finanzer))
debug.debug("user is not locked {{ {} }} or is finanzer execution {{ {} }}".format(
user.locked, finanzer))
user.addAmount(amount, year=year, month=month)
creditLists = user.updateGeruecht()
debug.debug("creditList is {{ {} }}".format(creditLists))
@ -323,7 +368,8 @@ class UserController(metaclass=Singleton):
return retVal
def addCredit(self, username, credit, year, month):
debug.info("add credit {{ {} }} to user {{ {} }} on month {{ {} }}, year {{ {} }}".format(credit, username, month, year))
debug.info("add credit {{ {} }} to user {{ {} }} on month {{ {} }}, year {{ {} }}".format(
credit, username, month, year))
user = self.getUser(username)
debug.debug("user is {{ {} }}".format(user))
if user.uid == 'extern':
@ -362,18 +408,19 @@ class UserController(metaclass=Singleton):
date = datetime.now()
zero = date.replace(hour=0, minute=0, second=0, microsecond=0)
end = zero + timedelta(hours=12)
startdatetime = date.replace(hour=12, minute=0, second=0, microsecond=0)
startdatetime = date.replace(
hour=12, minute=0, second=0, microsecond=0)
if date > zero and end > date:
startdatetime = startdatetime - timedelta(days=1)
startdatetime = startdatetime - timedelta(days=1)
enddatetime = startdatetime + timedelta(days=1)
debug.debug("startdatetime is {{ {} }} and enddatetime is {{ {} }}".format(startdatetime, end))
debug.debug("startdatetime is {{ {} }} and enddatetime is {{ {} }}".format(
startdatetime, end))
result = False
if date >= startdatetime and date < enddatetime:
result = db.getWorker(user, startdatetime)
debug.debug("worker is {{ {} }}".format(result))
return True if result else False
def getUser(self, username):
debug.info("get user {{ {} }}".format(username))
user = db.getUser(username)
@ -428,7 +475,8 @@ class UserController(metaclass=Singleton):
return retVal
def modifyUser(self, user, ldap_conn, attributes):
debug.info("modify user {{ {} }} with attributes {{ {} }} with ldap_conn {{ {} }}".format(user, attributes, ldap_conn))
debug.info("modify user {{ {} }} with attributes {{ {} }} with ldap_conn {{ {} }}".format(
user, attributes, ldap_conn))
try:
if 'username' in attributes:
debug.debug("change username, so change first in database")
@ -443,7 +491,8 @@ class UserController(metaclass=Singleton):
debug.debug("user is {{ {} }}".format(retVal))
return retVal
except UsernameExistLDAP as err:
debug.debug("username exists on ldap, rechange username on database", exc_info=True)
debug.debug(
"username exists on ldap, rechange username on database", exc_info=True)
db.changeUsername(user, user.uid)
raise Exception(err)
except LDAPExcetpion as err:

View File

@ -1,5 +1,5 @@
from flask import Blueprint, request, jsonify
from datetime import datetime
from datetime import datetime, time
import geruecht.controller.userController as uc
import geruecht.controller.ldapController as lc
from geruecht.decorator import login_required
@ -111,6 +111,41 @@ def _addUser(**kwargs):
return jsonify({"error": str(err)}), 500
@vorstand.route("/sm/getUsers", methods=['POST'])
@login_required(groups=[MONEY, GASTRO, VORSTAND])
def _getUsers(**kwrags):
debug.info("/sm/getUsers")
try:
data = request.get_json()
from_date = data['from_date']
to_date = data['to_date']
from_date = datetime(
from_date['year'], from_date['month'], from_date['day'])
to_date = datetime(to_date['year'], to_date['month'], to_date['day'])
lockedDays = userController.getLockedDays(from_date, to_date)
retVal = []
for lockedDay in lockedDays:
day = datetime.combine(lockedDay['daydate'], time(12))
retDay = {
"worker": userController.getWorker(day),
"day": {
"date": {
"year": day.year,
"month": day.month,
"day": day.day
},
"locked": lockedDay['locked']
}
}
retVal.append(retDay)
debug.debug("return {{ {} }}".format(retVal))
return jsonify(retVal)
except Exception as err:
debug.debug("exception", exc_info=True)
return jsonify({"error": str(err)}), 500
@vorstand.route("/sm/getUser", methods=['POST'])
@login_required(groups=[MONEY, GASTRO, VORSTAND])
def _getUser(**kwargs):
@ -122,24 +157,14 @@ def _getUser(**kwargs):
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']
}
lockedDay = {
'date': {
'year': year,
'month': month,
'day': day
},
'locked': lockedDay['locked']
}
retVal = {
'worker': userController.getWorker(date),
'day': lockedDay