def get(self, uid, month, *args, **kwargs): if not self.current_user.is_admin: return self.send_error(status_code=404) if not uid: # 用户管理页面 template_param = { "current_user": self.current_user, "active_tag": "user_manage", "users": [], "teams": self.teams, "companies": self.companies, "present_num": 0, "current_team": "", "current_company": "" } row2dict = lambda rows: {row.name: row.id for row in rows} team_dict = row2dict(self.teams) company_dict = row2dict(self.companies) search_team = self.get_argument("search-team", default=self.current_user.team_name.name) search_company = self.get_argument("search-company", default="") self.log.info(u"filter_user: search_team={0}&search_company={1}".format(search_team, search_company)) try: search_team_id = team_dict[search_team] if search_team else 1 except KeyError: self.log.error(u"team {0} not found!".format(search_team)) search_team_id = 1 try: search_company_id = company_dict[search_company] if search_company else 1 except KeyError: self.log.error(u"company {0} not found!".format(search_company)) search_company_id = 1 self.log.debug(u"filter_user: team_id={0}&company_id={1}".format(search_team_id, search_company_id)) users = self.filter_user(company_id=search_company_id, team_id=search_team_id) present_users = self.filter_user(company_id=search_company_id, team_id=search_team_id, is_present=1, is_admin=0, is_test=0) template_param["users"] = users template_param["present_num"] = len(present_users) template_param["current_company"] = search_company template_param["current_team"] = search_team self.render("admin/user_manage.html", **template_param) else: # 单个用户考勤管理页面 template_param = { "current_user": self.current_user, "active_tag": "user_manage", "late_hour": 0, "overtime_hour": 0, "late_hour_total": 0, "overtime_hour_total": 0, "the_month": month if month else datetime.datetime.now().strftime("%Y-%m"), "user": None, "attendances": [] } user = self.get_user_by_uid(uid) template_param["user"] = user template_param["attendances"] = self.get_attendance_by_month(template_param["the_month"], user=user) late_hour, overtime_hour = get_late_overtime_total(template_param["attendances"]) history_late, history_overtime = get_history_late_overtime(user) latest_attendance = self.get_attendance_by_month(datetime.datetime.now().strftime("%Y-%m"), user=user) latest_late, latest_overtime = get_late_overtime_total(latest_attendance) template_param["late_hour"] = late_hour template_param["overtime_hour"] = overtime_hour template_param["late_hour_total"] = history_late + latest_late template_param["overtime_hour_total"] = history_overtime + latest_overtime self.render("admin/user_attendance.html", **template_param)
def get(self, option, *args, **kwargs): if not self.current_user.is_admin: return self.send_error(status_code=404) current_time = datetime.datetime.now() if current_time.day <= 20: current_month = (current_time.replace(day=1) - datetime.timedelta(days=2)).strftime("%Y-%m") else: current_month = current_time.strftime("%Y-%m") users = self.filter_user(is_admin=0, is_test=0) lines = list() if not option or option == "summary": file_name = u"{0}总体数据.csv".format(current_month) file_path = "./export_data/" + file_name self.set_header('Content-Disposition', "attachment; filename=" + file_name) header = [u"姓名", u"公司", u"工作组", u"当月缺勤/h", u"当月加班/h", u"累计缺勤/h", u"累计加班/h", u"入职日期(仅限当月入职)", u"离职日期", u"服务工作日"] lines.append(",".join(header)) for user in users: if user.is_present == 0 and user.dismiss_time.strftime("%Y-%m") < current_month: continue attendance_list = self.get_attendance_by_month(current_month, user) month_late_hour, month_overtime_hour = map(str, get_late_overtime_total(attendance_list)) history_late, history_overtime = map(str, get_history_late_overtime(user)) row = list() row.append(user.name) row.append(user.company_name.name) row.append(user.team_name.name) row.append(month_late_hour) row.append(month_overtime_hour) row.append(history_late) row.append(history_overtime) row.append(user.createdate.strftime("%Y-%m-%d") if user.createdate.strftime("%Y-%m") == current_month else "") row.append(user.dismiss_time.strftime("%Y-%m-%d") if not user.is_present else "") row.append(str(get_workday_count_by_month(user, current_month))) lines.append(",".join(row)) elif option == "overtime_detail": file_name = u"{0}加班明细.csv".format(current_month) file_path = "./export_data/" + file_name self.set_header('Content-Disposition', "attachment; filename=" + file_name) header = [u"姓名", u"所在公司", u"补偿方式", u"开始时间/年", u"开始时间/月", u"开始时间/日", u"开始时间/时", u"截止时间/年", u"截止时间/月", u"截止时间/日", u"截止时间/时", u"加班时数(小时)"] lines.append(",".join(header)) for user in users: attendances = self.get_attendance_by_month(month=current_month, user=user) for attendance in attendances: late, overtime = get_late_overtime_hour(attendance) if overtime == 0: continue row = list() row.append(user.name) row.append(user.company_name.name) row.append(u"调休") row.extend(attendance.legal_check_out.strftime("%Y-%m-%d-%H:%M").split("-")) row.extend(attendance.check_out.strftime("%Y-%m-%d-%H:%M").split("-")) row.append(str(overtime)) lines.append(",".join(row)) elif option == "late_detail": file_name = u"{0}缺勤明细.csv".format(current_month) file_path = "./export_data/" + file_name self.set_header('Content-Disposition', "attachment; filename=" + file_name) header = [u"姓名", u"所在公司", u"缺勤状态", u"年", u"月", u"日", u"理论上班/时", u"实际上班/时", u"理论下班/时", u"实际下班/时", u"缺勤时数(小时)"] lines.append(",".join(header)) for user in users: attendances = self.get_attendance_by_month(month=current_month, user=user) for attendance in attendances: late, overtime = get_late_overtime_hour(attendance) if late == 0: continue row = list() row.append(user.name) row.append(user.company_name.name) row.append(attendance.status.status_name) row.extend(attendance.legal_check_in.strftime("%Y-%m-%d-%H:%M").split("-")) row.append(attendance.check_in.strftime("%H:%M")) row.append(attendance.legal_check_out.strftime("%H:%M")) row.append(attendance.check_out.strftime("%H:%M")) row.append(str(late)) lines.append(",".join(row)) else: return self.send_error(status_code=404) self.finish("\n".join(map(lambda line: line.encode("gbk"), lines))) with codecs.open(file_path, "w", encoding="utf-8") as csv_file: for line in lines: csv_file.write(line + "\n")