增加检验记录导出功能
This commit is contained in:
parent
e82e903448
commit
f92af5df27
|
@ -3,6 +3,6 @@ ENV = 'production'
|
|||
|
||||
# base 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'
|
||||
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
"file-saver": "^2.0.2",
|
||||
"fuse.js": "^6.4.6",
|
||||
"html-docx-js": "^0.3.1",
|
||||
"html-docx-js-typescript": "^0.1.5",
|
||||
"html2canvas": "^1.4.1",
|
||||
"js-cookie": "^3.0.0",
|
||||
"jspdf": "^2.5.1",
|
||||
|
|
|
@ -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
|
|
@ -1,5 +1,7 @@
|
|||
from rest_framework import exceptions, serializers
|
||||
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.models import Standard, TestItem, TestRecord, TestRecordItem
|
||||
from django.shortcuts import render
|
||||
|
@ -13,6 +15,7 @@ from rest_framework.decorators import action
|
|||
|
||||
from apps.wpm.models import WProduct
|
||||
from apps.wpm.services import WpmService
|
||||
from rest_framework.exceptions import ParseError
|
||||
# Create your views here.
|
||||
class StandardViewSet(CreateUpdateModelAMixin, ModelViewSet):
|
||||
"""
|
||||
|
@ -98,6 +101,17 @@ class TestRecordViewSet(ListModelMixin, UpdateModelMixin, RetrieveModelMixin, De
|
|||
WpmService.update_wproduct_by_test(obj, request.user) # 这里已经做了日志记录和进度计算
|
||||
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):
|
||||
# serializer = self.get_serializer(data=request.data)
|
||||
# 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.
Loading…
Reference in New Issue