提交弹窗的表单数据,
#在项目中创建一个upload目录,用于存放上传的文件
PROJECT_BASE_DIR = Path(__file__).resolve().parent.parent.parent
# 获取本机的IP
HOST_IP = socket.gethostbyname(socket.gethostname())
UPLOAD_ROOT = r'\\' + HOST_IP
MAIL_DOMAIN = '@unisoc.com'
MAIL_FROM = 'ibuildmaster' + MAIL_DOMAIN
def validatefilename(filename): # 验证文件名,有/ \ < > : " |这些字符时去掉,windows系统不能包含这些字符
rstr = r"[\/\\\:\*\?\"\<\>\|]" # '/\:*?"<>|'
new_filename = re.sub(rstr, "", filename)
return new_filename
def send_html_mail(subject, html_content, recipient_list, sender, cc, build_info=''):
EmailThread(subject, html_content, recipient_list, sender, cc, build_info).start()
class EmailThread(threading.Thread):
def __init__(self, subject, html_content, recipient_list, sender, cc, build_info):
self.subject = subject
self.recipient_list = recipient_list
self.html_content = html_content
self.sender = sender.encode('utf-8')
self.cc = cc
self.build_info = build_info
threading.Thread.__init__(self)
def get_mail_format_content_new_ticket(dict_info):
content = '<table cellspacing="0" cellPadding="1" border="1" style="BORDER-RIGHT: #000000 1px groove; BORDER-TOP: #000000 1px groove; BORDER-LEFT: #000000 1px groove; BORDER-COLLAPSE: collapse" >" >'
content += '<tr ><th style="background-color: rgba(177, 98, 230, 0.39);">ID:</th><td width="500px"><a href=' + 'http://10.29.68.61:8080' + '/support-vue/ticket/?ticketid=' + \
dict_info['ID'] + '>' + dict_info['ID'] + '</a></td></tr>'
content += '<tr ><th style="background-color: rgba(177, 98, 230, 0.39);">Type:</th><td>' + dict_info['Type'] + '</td></tr>'
content += '<tr ><th style="background-color: rgba(177, 98, 230, 0.39);">Title:</th><td>' + dict_info['Title'] + '</td></tr>'
content += '<tr ><th style="background-color: rgba(177, 98, 230, 0.39);">Status:</th><td>' + dict_info['Status'] + '</td></tr>'
content += '<tr ><th style="background-color: rgba(177, 98, 230, 0.39);">Submitter:</th><td>' + dict_info['Submitter'] + '</td></tr>'
content += '<tr ><th style="background-color: rgba(177, 98, 230, 0.39);">Submitter_Time:</th><td>' + dict_info['Submitter_Time'] + '</td></tr>'
content += '<tr ><th style="background-color: rgba(177, 98, 230, 0.39);">Assignee:</th><td>' + dict_info['Assignee'] + '</td></tr>'
issue_description = dict_info['Description'].decode().split('\n')
content += '<tr ><th style="background-color: rgba(177, 98, 230, 0.39);">Description:</th><td style="word-wrap:break-word; word-break:break-all;">'
for issue_index in issue_description:
content += issue_index.replace("<", "<").replace(">",
">") + '<br/>' # 在 HTML 中不能使用小于号(<)和大于号(>),这是因为浏览器会误认为它们是标签,应使用实体字符替换
content += '</td></tr>'
content += '<tr ><th style="background-color: rgba(177, 98, 230, 0.39);">Attachedfile_Url:</th><td style="word-wrap:break-word; word-break:break-all;">' + dict_info[
'Attachedfile_Url'] + '</td></tr></table>'
return content
class TestDialog(View):
def post(self, request):
current_user = request.user.username
status = 'Opened'
title = request.POST.get('title', '')
ticket_type = request.POST.get('type', '')
assignee = request.POST.get('assignee', '')
assignee_name = assignee.strip()
issue_description = request.POST.get('description', '')
files = request.FILES.getlist('fileOBJ')
assigned_to_id = User.objects.get(username=assignee_name)
issue_attachedfile_url = ''
create_ticket_success = False # 标记创建的ticket是否成功
try:
check_attached_status = 0
ticket_obj = Ticket.objects.create(title=title, status=status, ticket_type=ticket_type,
submitter=request.user, assigned_to=assigned_to_id,
issue_description=issue_description,
issue_attachedfile_url=issue_attachedfile_url)
create_ticket_success = True # 执行到这里说明ticket创建成功了
if files:
uploaded_folder_name = str(ticket_obj.id)
BASE_DIR_UPLOAD = os.path.join(PROJECT_BASE_DIR, 'upload\\' + uploaded_folder_name)
if not os.path.exists(BASE_DIR_UPLOAD):
os.mkdir(BASE_DIR_UPLOAD)
for uploadFile in files:
try:
uploadFilename = validatefilename(uploadFile.name) # 处理文件中含有/ \ < > : " |这些字符串的异常
destination = open(os.path.join(BASE_DIR_UPLOAD, uploadFilename), 'wb+')
for chunk in uploadFile.chunks():
destination.write(chunk)
destination.close()
check_attached_status = 1
except:
print(sys.exc_info())
s_error_log = str(sys.exc_info())
return HttpResponse(
json.dumps({'success': False, 'info': s_error_log,
'create_ticket_success': create_ticket_success}),
content_type="application/json")
if check_attached_status == 1:
ticket_obj.issue_attachedfile_url = os.path.join(settings.UPLOAD_ROOT, 'upload', str(ticket_obj.id))
ticket_obj.save()
history_note = new_history(request.user, ticket_obj, 'new ticket was created', comment="")
# send the email after created the ticket
# mail_from =ibuildmaster@unisoc.com
mail_from = settings.MAIL_FROM
mail_to_set = set()
mail_to_set.add(current_user + settings.MAIL_DOMAIN)
mail_to_set.add(assignee + settings.MAIL_DOMAIN)
# mail_to=['xuan.li@unisoc.com', ' young.yu@unisoc.com']
mail_to = list(mail_to_set)
# mail_cc=['yihui.hu@unisoc.com', 'young.yu@unisoc.com', 'nina.leng@unisoc.com', 'ran.ju@unisoc.com', 'ming.jiang1@unisoc.com', 'panda.luo@unisoc.com', 'olivine.peng@unisoc.com', 'blake.zhu@unisoc.com', 'peng.yang@unisoc.com', 'qingqing.he@unisoc.com', 'chun.li@unisoc.com', 'hanmeng.zhang@unisoc.com']
mail_cc = []
for cm_user in cm_user_list:
mail_cc.append(cm_user + settings.MAIL_DOMAIN)
mail_title = 'New Ticket was submitted ---- ID : %d' % (ticket_obj.id)
print("mail_from =%s \nmail_to=%s\nmail_cc=%s" % (mail_from, mail_to, mail_cc))
info_content = {}
info_content['ID'] = str(ticket_obj.id)
info_content['Type'] = ticket_type
info_content['Status'] = status
info_content['Submitter'] = current_user
info_content['Submitter_Time'] = str(ticket_obj.submitted_time.strftime('%Y-%m-%d %H:%M:%S'))
info_content['Assignee'] = assignee
info_content['Title'] = ticket_obj.title
info_content['Description'] = issue_description.encode('utf-8')
info_content['Attachedfile_Url'] = ticket_obj.issue_attachedfile_url
if 'xxxxxx.com' in mail_cc:
mail_cc.remove('xxxxx.com')
print('-----------',get_mail_format_content_new_ticket(info_content))
#send_html_mail(email标题,email正文,email发给谁,)
send_html_mail(mail_title, get_mail_format_content_new_ticket(info_content), mail_to, mail_from, mail_cc)
except:
print(sys.exc_info())
s_error_log = str(sys.exc_info())
return HttpResponse(
json.dumps({'success': False, 'info': s_error_log, 'create_ticket_success': create_ticket_success}),
content_type="application/json")
info = []
info.append(ticket_obj.toJSON())
return HttpResponse(json.dumps({'success': True, 'info': info, 'history_note': history_note}),
content_type="application/json")
<!-- 弹窗 -->
<el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="50%" @close="closeDialogForm()" :close-on-click-modal="false" v-alterELDialogMarginTop="{marginTop:'5vh'}">
<el-divider></el-divider>
<el-form :model='ticketform' :rules="rules" ref="newTicket" label-position="right" label-width='100px'>
<el-row>
<el-col :span="24">
<el-form-item label="Title" >
<!-- <el-input v-model="ticketform.title" @input="changeValue" @select="handleSelect(ticketform.title)" placeholder="简短描述你的问题..." title="请使用简短的文字总结你遇到的问题,我们会根据关键词搜索你的问题" style="width:90%" type="text" ></el-input> -->
<!-- <el-input v-model="ticketform.title" placeholder="简短描述你的问题..." title="请使用简短的文字总结你遇到的问题,我们会根据关键词搜索你的问题" style="width:90%" v-on:input ="inputFunc"></el-input> -->
<el-autocomplete
v-model="state"
:fetch-suggestions="querySearchAsync"
placeholder="请输入内容"
@select="handleSelect(state)"
style="width:90%"
@input="loadAll"
></el-autocomplete>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span='12'>
<el-form-item label="Type" required>
<el-select v-model="ticketform.type" placeholder="请选择" @change="changeSelect()" style="width:75%" >
<el-option
v-for="(item,index) in Newticket_Type"
:key="index"
:label="item"
:value="item"
>
</el-option>
</el-select>
</el-form-item>
</el-col>
<el-col :span='12'>
<el-form-item label="Assignee" required>
<el-select v-model="ticketform.assignee" placeholder="请选择" style="width:75%">
<el-option
v-for="(item1,index1) in ass_options"
:key="index1"
:label="item1"
:value='item1'
>
</el-option>
</el-select>
</el-form-item>
</el-col>
</el-row>
<el-form-item label="Description">
<el-input v-model="ticketform.data3" type="textarea" :rows="8" style="width:90%"
></el-input>
</el-form-item>
</el-form>
<el-upload
class="upload-demo"
ref="upload"
action=""
multiple
:on-exceed="handleExceed"
:on-remove="handleRemove"
:limit="1"
:file-list="fileList"
:auto-upload="false"
:on-change="fileChange"
>
<el-button slot="trigger" size="small" type="primary" v-model="fileList" style="margin-left:100px">选取文件</el-button>
<div slot="tip" class="el-upload__tip" style="display: inline-block;margin-left: 20px">请选择单个文件上传</div>
</el-upload>
<span slot="footer" class="dialog-footer">
<el-button type="primary" @click="httpRequest('newTicket')">确 定</el-button>
<el-button type="info" @click="closeDialogForm()">取 消</el-button>
</span>
</el-dialog>
//newTicket弹窗
httpRequest(param){
console.log(param,'请求')
let formdata = new FormData()
formdata.append('fileOBJ', this.ticketform.file)
formdata.append('title', this.state)
formdata.append('type', this.ticketform.type)
formdata.append('assignee', this.ticketform.assignee)
formdata.append('description', this.ticketform.data3)
let that = this
that.$axios.post(that.baseURL+'PROD/testdialog/documents/',formdata)
.then(res =>{
console.log(res.data)
if(res.data.code== 1){
that.ticketform.url = res.data.name
// 拼接imageuri
that.$message.success(`${res.data.msg}`);
}
else{
this.$message.error(`${res.data.msg}`);
}
})
.catch(err=>{
console.log(err);
that.$message.error('获取后端查询结果出现异常!')
})
},
handleExceed(files, fileList) {
this.$message.warning(`当前限制选择单个文件`);
},
fileChange(file, fileList) {
this.ticketform.file=file.raw;
console.log(this.fileList);
},
// 文件删除时
handleRemove (file, fileList) {
console.log(file, fileList,'-------------')
this.fileList = [] // 文件列表置空
},