增加检验记录导出功能

This commit is contained in:
caoqianming 2022-04-21 11:32:12 +08:00
parent e82e903448
commit f92af5df27
8 changed files with 57 additions and 1 deletions

View File

@ -3,6 +3,6 @@ ENV = 'production'
# base api # base api
#VUE_APP_BASE_API = 'http://192.168.1.250/api' #VUE_APP_BASE_API = 'http://192.168.1.250/api'
VUE_APP_BASE_API = 'http://49.232.14.174:2222/api' VUE_APP_BASE_API = 'http://127.0.0.1:3333/api'
#VUE_APP_BASE_API = 'http://127.0.0.1:8000/api' #VUE_APP_BASE_API = 'http://127.0.0.1:8000/api'

View File

@ -29,6 +29,7 @@
"file-saver": "^2.0.2", "file-saver": "^2.0.2",
"fuse.js": "^6.4.6", "fuse.js": "^6.4.6",
"html-docx-js": "^0.3.1", "html-docx-js": "^0.3.1",
"html-docx-js-typescript": "^0.1.5",
"html2canvas": "^1.4.1", "html2canvas": "^1.4.1",
"js-cookie": "^3.0.0", "js-cookie": "^3.0.0",
"jspdf": "^2.5.1", "jspdf": "^2.5.1",

View File

@ -0,0 +1,41 @@
from docxtpl import DocxTemplate, InlineImage
from apps.qm.models import TestRecord
from apps.qm.serializers import TestRecordDetailSerializer
from server.settings import BASE_DIR
from utils.tools import ranstr
from docx.shared import Mm
from apps.mtm.models import RecordFormField
def exprot_test_record(tr:TestRecord):
data = TestRecordDetailSerializer(instance=tr).data
doc = DocxTemplate(BASE_DIR + tr.form.export_template)
edata = {}
edata['formName'] = data['form_']['name']
edata['glassSpec'] = data['wproduct_']['material_']['specification'] if data['wproduct_'] else ''
edata['glassNum'] = data['wproduct_']['number'] if data['wproduct_'] else ''
edata['testDate'] = data['update_time'][0:11] if data['update_time'] else data['create_time'][0:11]
tester_s = data['update_by_']['signature'] if data['update_by_'] else data['create_by_']['signature']
if tester_s: # 签名
edata['tester'] = InlineImage(doc, BASE_DIR + tester_s, height=Mm(10))
for i in data['record_data']:
if i['field_type'] == str(RecordFormField.FIELD_DRAW):
edata[i['field_key']] = {
's': InlineImage(doc, BASE_DIR + i['field_value'], width=Mm(40)),
'm': InlineImage(doc, BASE_DIR + i['field_value'], width=Mm(60)),
'l': InlineImage(doc, BASE_DIR + i['field_value'], width=Mm(80)),
}
else:
edata[i['field_key']] = i['field_value']
if i.get('origin_value', ''):
edata[i['field_key']] = '{}({})'.format(i['origin_value'] + i['field_value'])
# 开始生成word
doc.render(edata)
filename = edata['formName'] + '_' + ranstr(6)
path = '/media/export/' + filename + '.docx'
filepath = BASE_DIR + path
doc.save(filepath)
return path

View File

@ -1,5 +1,7 @@
from rest_framework import exceptions, serializers from rest_framework import exceptions, serializers
from rest_framework.mixins import CreateModelMixin, DestroyModelMixin, ListModelMixin, RetrieveModelMixin, UpdateModelMixin from rest_framework.mixins import CreateModelMixin, DestroyModelMixin, ListModelMixin, RetrieveModelMixin, UpdateModelMixin
from apps.mtm.models import RecordFormField
from apps.qm.export import exprot_test_record
from apps.qm.serializers import StandardCreateUpdateSerializer, StandardSerializer, TestItemCreateUpdateSerializer, TestItemSerializer, TestRecordCreateSerializer, TestRecordDetailSerializer, TestRecordListSerializer, TestRecordUpdateSerializer from apps.qm.serializers import StandardCreateUpdateSerializer, StandardSerializer, TestItemCreateUpdateSerializer, TestItemSerializer, TestRecordCreateSerializer, TestRecordDetailSerializer, TestRecordListSerializer, TestRecordUpdateSerializer
from apps.qm.models import Standard, TestItem, TestRecord, TestRecordItem from apps.qm.models import Standard, TestItem, TestRecord, TestRecordItem
from django.shortcuts import render from django.shortcuts import render
@ -13,6 +15,7 @@ from rest_framework.decorators import action
from apps.wpm.models import WProduct from apps.wpm.models import WProduct
from apps.wpm.services import WpmService from apps.wpm.services import WpmService
from rest_framework.exceptions import ParseError
# Create your views here. # Create your views here.
class StandardViewSet(CreateUpdateModelAMixin, ModelViewSet): class StandardViewSet(CreateUpdateModelAMixin, ModelViewSet):
""" """
@ -98,6 +101,17 @@ class TestRecordViewSet(ListModelMixin, UpdateModelMixin, RetrieveModelMixin, De
WpmService.update_wproduct_by_test(obj, request.user) # 这里已经做了日志记录和进度计算 WpmService.update_wproduct_by_test(obj, request.user) # 这里已经做了日志记录和进度计算
return Response() return Response()
@action(methods=['get'], detail=True, perms_map={'get':'*'}, serializer_class=serializers.Serializer)
def export(self, request, pk=None):
"""
按模板导出
"""
instance = self.get_object()
if '/media/default/temp.docx':
path = exprot_test_record(tr=instance)
return Response({'path':path})
raise ParseError('未配置导出模板')
# def create(self, request, *args, **kwargs): # def create(self, request, *args, **kwargs):
# serializer = self.get_serializer(data=request.data) # serializer = self.get_serializer(data=request.data)
# serializer.is_valid(raise_exception=True) # serializer.is_valid(raise_exception=True)

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 499 KiB

Binary file not shown.