Exemplo n.º 1
0
class BaseHandler(tornado.web.RequestHandler):
    def initialize(self):
        self.session = DB_Session()

    def on_finish(self):
        """
        Close sqlalchemy session connect
        """
        self.session.close()
Exemplo n.º 2
0
def refresh_all_status_by_date(date):
    from lib.models import DB_Session

    session = DB_Session()

    attendances = (
        session.query(Attendance).filter(Attendance.date == date.strftime("%Y-%m-%d")).order_by(Attendance.id).all()
    )
    for attendance in attendances:
        set_attendance_status(attendance)
        print attendance.attendance_status
    session.commit()
    print len(attendances)
Exemplo n.º 3
0
def update_history_late_overtime_monthly():
    """ Runs every month 1st. Calculate last month's late & overtime hour

    :return:
    """
    check_month = (datetime.date.today() - datetime.timedelta(days=2)).strftime("%Y-%m")
    session = DB_Session()
    users = session.query(User).filter(
        User.is_admin == 0,
        User.is_test == 0,
    ).order_by(User.id).all()
    for user in users:
        if user.is_present == 0 and user.dismiss_time.strftime("%Y-%m") != check_month:
            continue
        attendance_list = session.query(Attendance).filter(
            Attendance.date.like("{0}%".format(check_month)),
            Attendance.userid == user.id
        ).all()
        late, overtime = get_late_overtime_total(attendance_list)
        history = HistoryLateOvertimeHour(
            userid=user.id,
            month=check_month,
            late=late,
            overtime=overtime
        )
        session.add(history)
    session.commit()
Exemplo n.º 4
0
class BaseHandler(tornado.web.RequestHandler):

    def is_admin(self):
        """ Check out if current_user is admin

        :return bool: true if current_user is admin
        """
        if not self.current_user:
            return False
        else:
            return self.current_user in ["1"]

    def initialize(self):
        self.session = DB_Session()

    def on_finish(self):
        self.session.close()

    def get_current_user(self):
        return self.get_secure_cookie("uid")
Exemplo n.º 5
0
def update_attendances_daily(check_date=None):
    """ Runs on 23:59 per day

    :param check_date: datetime object
    :return:
    """
    if not check_date:
        check_date = datetime.datetime.now()

    default_time = check_date.replace(hour=0, minute=0, second=0, microsecond=0)
    date = default_time.strftime("%Y-%m-%d")
    session = DB_Session()
    users = session.query(User).filter(
        User.is_present == 1,
        User.is_admin == 0,
        User.is_test == 0,
    ).order_by(User.id).all()
    # no_check_out_users = filter(lambda x: x.check_out is None, users)
    for user in users:
        today_attendance = filter(lambda x: x.date == date, user.attendance)
        if today_attendance:
            today_attendance = today_attendance[0]
            if today_attendance.check_out:
                continue
            else:
                today_attendance.check_out = default_time
        else:
            legal_hour, legal_minute = map(int, user.banci.split(":"))
            legal_check_in = default_time.replace(hour=legal_hour, minute=legal_minute)
            legal_check_out = legal_check_in + datetime.timedelta(hours=9)
            today_attendance = Attendance(
                userid=user.id,
                check_out=default_time,
                check_in=default_time,
                date=date,
                updatetime=datetime.datetime.now(),
                legal_check_in=legal_check_in,
                legal_check_out=legal_check_out
            )
            session.add(today_attendance)
        set_attendance_status(today_attendance)

    session.commit()
Exemplo n.º 6
0
# -*- coding: utf-8 -*-
import datetime

from lib.models import DB_Session
from lib.models import Attendance, SpecialDate

session = DB_Session()


def set_attendance_status(attendance):
    """ Set attendance.attendance_status and attendance.is_maintainable
    This function may be called under 3 conditions:
    * user check out
    * daily check by system schedule
    * admin approval a user's info commit

    :param attendance: lib.models.Attendance Object
    :return: True if succeed, else False
    """
    approval_attendance = filter(lambda x: x.modify_status == 2, attendance.modify_attendance)
    HOLIDAY = map(lambda x: x[0], session.query(SpecialDate.date).filter(SpecialDate.should_rest == True).all())
    WORK = map(lambda x: x[0], session.query(SpecialDate.date).filter(SpecialDate.should_work == True).all())
    if approval_attendance:
        # 用户有审批通过的维护,取出最新一次审核通过的考勤,用于之后的考勤结算
        modify_attendance = sorted(approval_attendance, key=lambda x: x.id, reverse=True)[0]

        if modify_attendance.modify_attendance_status == 5:
            # 用户请了全天假
            # 直接设置状态,不改写考勤时间
            attendance.attendance_status = 5
            return True
Exemplo n.º 7
0
 def initialize(self):
     self.session = DB_Session()
Exemplo n.º 8
0
# -*- coding: utf-8 -*-
import datetime

from lib.models import DB_Session, Attendance, HistoryLateOvertimeHour, User, SpecialDate

session = DB_Session()


def get_late_overtime_hour(attendance):
    """ Calculate attendance's late_hour and overtime_hour

    * late_hour should be lesser than 8!
    * 12:00 ~ 13:00 is lunch time, late hour should NOT contains this one hour!

    :param attendance: lib.models.Attendance object
    :return: tuple, (late_hour, overtime_hour)
    """
    if not isinstance(attendance, Attendance):
        return 0, 0

    HOLIDAY = map(lambda x: x[0], session.query(SpecialDate.date).filter(SpecialDate.should_rest == True).all())
    WORK = map(lambda x: x[0], session.query(SpecialDate.date).filter(SpecialDate.should_work == True).all())
    late_hour, overtime_hour = 0, 0
    check_in, check_out = attendance.check_in, attendance.check_out
    legal_check_in, legal_check_out = attendance.legal_check_in, attendance.legal_check_out
    lunch_start = legal_check_in.replace(hour=12, minute=0, second=0, microsecond=0)
    lunch_end = lunch_start + datetime.timedelta(hours=1)
    status = attendance.attendance_status

    if status in [1, 11, 13]:
        # 待定、休息、法定假日,不计算时间