打卡记录导出
This commit is contained in:
parent
d4e9406666
commit
f0ee937af7
|
@ -23,11 +23,16 @@ from apps.hrm.services import HrmService
|
||||||
|
|
||||||
from apps.third.dahua import dhClient
|
from apps.third.dahua import dhClient
|
||||||
from apps.third.tapis import dhapis
|
from apps.third.tapis import dhapis
|
||||||
|
from apps.utils.export import export_excel
|
||||||
from apps.utils.viewsets import CustomGenericViewSet, CustomModelViewSet
|
from apps.utils.viewsets import CustomGenericViewSet, CustomModelViewSet
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
|
|
||||||
|
epTypeOptions = {'employee': '正式员工', 'remployee': '相关方', 'visitor': '访客', 'driver': '货车司机'}
|
||||||
|
crOptions = {10: '上班打卡', 20: '下班打卡'}
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
|
|
||||||
|
|
||||||
class EmployeeViewSet(CustomModelViewSet):
|
class EmployeeViewSet(CustomModelViewSet):
|
||||||
"""
|
"""
|
||||||
人员管理
|
人员管理
|
||||||
|
@ -221,6 +226,29 @@ class ClockRecordViewSet(ListModelMixin, CustomGenericViewSet):
|
||||||
filterset_class = ClockRecordFilterSet
|
filterset_class = ClockRecordFilterSet
|
||||||
ordering = ['-pk']
|
ordering = ['-pk']
|
||||||
|
|
||||||
|
@action(methods=['get'], detail=False, perms_map={'get': '*'},
|
||||||
|
serializer_class=serializers.Serializer)
|
||||||
|
def export_excel(self, request, pk=None):
|
||||||
|
"""导出excel
|
||||||
|
导出excel
|
||||||
|
"""
|
||||||
|
field_data = ['人员类型', '人员', '所属部门', '打卡类型', '触发形式', '体温', '打卡时间']
|
||||||
|
queryset = self.filter_queryset(self.get_queryset())
|
||||||
|
odata = ClockRecordListSerializer(queryset, many=True).data
|
||||||
|
# 处理数据
|
||||||
|
data = []
|
||||||
|
for i in odata:
|
||||||
|
data.append(
|
||||||
|
[epTypeOptions[i['employee_']['type']],
|
||||||
|
i['employee_']['name'],
|
||||||
|
i['employee_']['belong_dept_name'],
|
||||||
|
crOptions[i['type']],
|
||||||
|
i['detail'].get('deviceName', None),
|
||||||
|
i['detail'].get('curTemp', None),
|
||||||
|
i['create_time']]
|
||||||
|
)
|
||||||
|
return Response({'path': export_excel(field_data, data, '打卡记录')})
|
||||||
|
|
||||||
|
|
||||||
class NotWorkRemarkViewSet(ListModelMixin, CustomGenericViewSet):
|
class NotWorkRemarkViewSet(ListModelMixin, CustomGenericViewSet):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -0,0 +1,111 @@
|
||||||
|
import xlwt
|
||||||
|
import time
|
||||||
|
import os
|
||||||
|
from django.conf import settings
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
|
def len_byte(value):
|
||||||
|
# 获取字符串长度,一个中文的长度为2
|
||||||
|
length = len(value)
|
||||||
|
utf8_length = len(value.encode('utf-8'))
|
||||||
|
length = (utf8_length - length) / 2 + length
|
||||||
|
return int(length)
|
||||||
|
|
||||||
|
|
||||||
|
def export_excel(field_data: list, data: list, FileName: str):
|
||||||
|
"""
|
||||||
|
Excel导出
|
||||||
|
:param request: 请求request
|
||||||
|
:param data: 数据源
|
||||||
|
:param field_data: 首行数据源(表头)
|
||||||
|
:param file_path: 文件保存路径(默认保存在media路径)
|
||||||
|
:param FileName: 文件保存名字
|
||||||
|
:return:返回文件的下载url完整路径
|
||||||
|
"""
|
||||||
|
wbk = xlwt.Workbook(encoding='utf-8')
|
||||||
|
sheet = wbk.add_sheet('Sheet1', cell_overwrite_ok=True) # 第二参数用于确认同一个cell单元是否可以重设值。
|
||||||
|
style = xlwt.XFStyle() # 赋值style为XFStyle(),初始化样式
|
||||||
|
# 设置居中
|
||||||
|
wbk.set_colour_RGB(0x23, 0, 60, 139)
|
||||||
|
xlwt.add_palette_colour("custom_colour_35", 0x23)
|
||||||
|
tab_al = xlwt.Alignment()
|
||||||
|
tab_al.horz = 0x02 # 设置水平居中
|
||||||
|
tab_al.vert = 0x01 # 设置垂直居中
|
||||||
|
# 设置表头单元格背景颜色
|
||||||
|
tab_pattern = xlwt.Pattern() # 创建一个模式
|
||||||
|
tab_pattern.pattern = xlwt.Pattern.SOLID_PATTERN # 设置其模式为实型
|
||||||
|
tab_pattern.pattern_fore_colour = 55
|
||||||
|
# 设置单元格内字体样式
|
||||||
|
tab_fnt = xlwt.Font() # 创建一个文本格式,包括字体、字号和颜色样式特性
|
||||||
|
tab_fnt.height = 200
|
||||||
|
default_width = 14
|
||||||
|
tab_fnt.name = u'楷体' # 设置其字体为微软雅黑
|
||||||
|
tab_fnt.colour_index = 1 # 设置其字体颜色
|
||||||
|
# 设置单元格下框线样式
|
||||||
|
tab_borders = xlwt.Borders()
|
||||||
|
tab_borders.left = xlwt.Borders.THIN
|
||||||
|
tab_borders.right = xlwt.Borders.THIN
|
||||||
|
tab_borders.top = xlwt.Borders.THIN
|
||||||
|
tab_borders.bottom = xlwt.Borders.THIN
|
||||||
|
tab_borders.left_colour = 23
|
||||||
|
tab_borders.right_colour = 23
|
||||||
|
tab_borders.bottom_colour = 23
|
||||||
|
tab_borders.top_colour = 23
|
||||||
|
# 把数据写入excel中
|
||||||
|
# 所有表格单元格样式
|
||||||
|
# 先生成表头
|
||||||
|
style.alignment = tab_al # 设置居中
|
||||||
|
style.pattern = tab_pattern # 设置表头单元格背景颜色
|
||||||
|
style.font = tab_fnt # 设置单元格内字体样式
|
||||||
|
style.borders = tab_borders
|
||||||
|
for index, ele in enumerate(field_data):
|
||||||
|
sheet.write_merge(0, 0, index, index, ele, style) # (列开始, 列结束, 行开始, 行结束, '数据内容')
|
||||||
|
|
||||||
|
# 确定栏位宽度
|
||||||
|
col_width = []
|
||||||
|
for index, ele in enumerate(data):
|
||||||
|
for inx, values in enumerate(ele):
|
||||||
|
if index == 0:
|
||||||
|
col_width.append(len_byte(str(values)))
|
||||||
|
else:
|
||||||
|
if col_width[inx] < len_byte(str(values)):
|
||||||
|
col_width[inx] = len_byte(str(values))
|
||||||
|
# 设置栏位宽度,栏位宽度小于10时候采用默认宽度
|
||||||
|
for i in range(len(col_width)):
|
||||||
|
if col_width[i] > 10:
|
||||||
|
width = col_width[i] if col_width[i] < 36 else 36
|
||||||
|
sheet.col(i).width = 256 * (width + 6)
|
||||||
|
else:
|
||||||
|
sheet.col(i).width = 256 * (default_width)
|
||||||
|
|
||||||
|
row = 1
|
||||||
|
# 内容背景颜色
|
||||||
|
left_pattern = xlwt.Pattern() # 创建一个模式
|
||||||
|
left_pattern.pattern = xlwt.Pattern.SOLID_PATTERN # 设置其模式为实型
|
||||||
|
left_pattern.pattern_fore_colour = 1
|
||||||
|
|
||||||
|
# 设置单元格内字体样式
|
||||||
|
left_fnt = xlwt.Font() # 创建一个文本格式,包括字体、字号和颜色样式特性
|
||||||
|
left_fnt.height = 200
|
||||||
|
left_fnt.name = u'楷体' # 设置其字体为微软雅黑
|
||||||
|
left_fnt.colour_index = 0 # 设置其字体颜色
|
||||||
|
|
||||||
|
left_style = style
|
||||||
|
left_style.pattern = left_pattern
|
||||||
|
left_style.font = left_fnt
|
||||||
|
|
||||||
|
for results in data:
|
||||||
|
for index, values in enumerate(results):
|
||||||
|
sheet.write(row, index, label=values, style=left_style)
|
||||||
|
row += 1
|
||||||
|
|
||||||
|
FileNameF = FileName + datetime.now().strftime('%Y%m%d%H%M%S') + '.xls'
|
||||||
|
path = '/media/temp'
|
||||||
|
pathRoot = settings.BASE_DIR + path
|
||||||
|
if not os.path.exists(pathRoot):
|
||||||
|
os.makedirs(pathRoot)
|
||||||
|
|
||||||
|
path_name = os.path.join(pathRoot, FileNameF)
|
||||||
|
wbk.save(path_name)
|
||||||
|
return path + FileNameF
|
Loading…
Reference in New Issue