153 lines
6.2 KiB
Python
153 lines
6.2 KiB
Python
from .. import Singleton, mailConfig
|
|
import geruecht.controller.databaseController as dc
|
|
import geruecht.controller.ldapController as lc
|
|
import geruecht.controller.emailController as ec
|
|
from geruecht.model.user import User
|
|
from datetime import datetime, timedelta
|
|
from geruecht.logger import getDebugLogger
|
|
from ..mainController import mainJobKindController, mainCreditListController, mainPricelistController, mainUserController, mainWorkerController, mainWorkgroupController, mainJobInviteController, mainJobRequestController, mainRegistrationController, mainPasswordReset
|
|
|
|
db = dc.DatabaseController()
|
|
ldap = lc.LDAPController()
|
|
emailController = ec.EmailController()
|
|
|
|
debug = getDebugLogger()
|
|
|
|
|
|
class MainController(mainJobKindController.Base,
|
|
mainCreditListController.Base,
|
|
mainPricelistController.Base,
|
|
mainUserController.Base,
|
|
mainWorkerController.Base,
|
|
mainWorkgroupController.Base,
|
|
mainJobInviteController.Base,
|
|
mainJobRequestController.Base,
|
|
mainRegistrationController.Base,
|
|
mainPasswordReset.Base,
|
|
metaclass=Singleton):
|
|
|
|
def __init__(self):
|
|
debug.debug("init UserController")
|
|
pass
|
|
|
|
def setLockedDay(self, date, locked, hard=False):
|
|
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 getLockedDaysFromList(self, date_list):
|
|
debug.info("get locked days from list {{ {} }}".format(date_list))
|
|
retVal = []
|
|
for on_date in date_list:
|
|
day = datetime(on_date['on_date']['year'], on_date['on_date']['month'], on_date['on_date']['day'], 12)
|
|
retVal.append(self.getLockedDay(day))
|
|
return retVal
|
|
|
|
def getLockedDay(self, date):
|
|
debug.info("get locked day on {{ {} }}".format(date))
|
|
now = datetime.now()
|
|
debug.debug("now is {{ {} }}".format(now))
|
|
oldMonth = False
|
|
debug.debug("check if date old month or current month")
|
|
for i in range(1, 8):
|
|
if datetime(now.year, now.month, i).weekday() == 2:
|
|
if now.day < i:
|
|
oldMonth = True
|
|
break
|
|
debug.debug("oldMonth is {{ {} }}".format(oldMonth))
|
|
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")
|
|
lockedYear = lockedYear if lockedMonth != 12 else (lockedYear + 1)
|
|
lockedMonth = (lockedMonth + 1) if lockedMonth != 12 else 1
|
|
for i in range(1, 8):
|
|
nextMonth = datetime(lockedYear, lockedMonth, i)
|
|
if nextMonth.weekday() == 2:
|
|
endDay = i
|
|
break
|
|
|
|
monthLockedEndDate = datetime(
|
|
lockedYear, lockedMonth, endDay) - timedelta(1)
|
|
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 __updateDataFromLDAP(self, user):
|
|
debug.info("update data from ldap for user {{ {} }}".format(user))
|
|
groups = ldap.getGroup(user.uid)
|
|
debug.debug("ldap gorups are {{ {} }}".format(groups))
|
|
user_data = ldap.getUserData(user.uid)
|
|
debug.debug("ldap data is {{ {} }}".format(user_data))
|
|
user_data['gruppe'] = groups
|
|
user_data['group'] = groups
|
|
user.updateData(user_data)
|
|
db.updateUser(user)
|
|
|
|
def checkBarUser(self, user):
|
|
debug.info("check if user {{ {} }} is baruser")
|
|
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)
|
|
if date > zero and end > date:
|
|
startdatetime = startdatetime - timedelta(days=1)
|
|
enddatetime = startdatetime + timedelta(days=1)
|
|
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 sendMail(self, username):
|
|
debug.info("send mail to user {{ {} }}".format(username))
|
|
if type(username) == User:
|
|
user = username
|
|
if type(username) == str:
|
|
user = db.getUser(username)
|
|
retVal = emailController.sendMail(user)
|
|
debug.debug("send mail is {{ {} }}".format(retVal))
|
|
return retVal
|
|
|
|
def sendAllMail(self):
|
|
debug.info("send mail to all user")
|
|
retVal = []
|
|
users = db.getAllUser()
|
|
debug.debug("users are {{ {} }}".format(users))
|
|
for user in users:
|
|
retVal.append(self.sendMail(user))
|
|
debug.debug("send mails are {{ {} }}".format(retVal))
|
|
return retVal
|
|
|
|
|