yun_product/yuthon_will_task/controllers/main.py
2026-07-25 14:23:19 +08:00

199 lines
8.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import base64
import json
from odoo import http, fields
from odoo.http import request
from odoo.tools import float_is_zero,float_round
from odoo.exceptions import UserError
from datetime import datetime
from urllib.parse import quote
class YuthonWillTask(http.Controller):
@http.route('/yuthon/yuthon/will/task', methods=['POST'], type='http', auth='none', csrf=False)
def index2(self, **kwargs):
id = kwargs.get('id')
data = request.env['yuthon.will.task'].sudo().search(domain = [("id", "=", int(id))])
employee_ids = []
for em in data.employee_ids:
employee_ids.append({
'id':em.id,
'name':em.name,
'image_1920':em.image_128.decode('utf-8') if em.image_128 else '',
})
result = {
'id':data.id,
'name':data.name,
'code':data.code,
'start_date':fields.Date.to_string(data.start_date),
'request_hour_from':data.request_hour_from,
'request_hour_to':data.request_hour_to,
'priority':data.priority,
'user_id':data.user_id.name,
'user_avatar':base64.b64decode(data.user_id.avatar_128).decode('utf-8') if data.user_id.avatar_128 else '',
'remarks':data.remarks,
'address':data.address,
'employee_ids':employee_ids,
}
# 转换 request_hour_from 和 request_hour_to 取模型 yuthon.will.task 的 request_hour_from 的selection
request_hour_from_selection = request.env['yuthon.will.task'].sudo().fields_get(allfields=['request_hour_from'])['request_hour_from']['selection']
request_hour_to_selection = request.env['yuthon.will.task'].sudo().fields_get(allfields=['request_hour_to'])['request_hour_to']['selection']
for f in request_hour_from_selection:
if f[0] == data.request_hour_from:
result['request_hour_from'] = f[1]
for f in request_hour_to_selection:
if f[0] == data.request_hour_to:
result['request_hour_to'] = f[1]
# 计算 start_date 星期几
result['start_date_week'] = fields.Date.from_string(result['start_date']).strftime('%A')
return json.dumps({
'data': result,
'code': 200,
'message': 'success'
})
@http.route('/task/<int:task_id>', type='http', auth='user', website=False)
def open_task(self, task_id, **kwargs):
"""通过ID打开特定任务的详情页"""
# 查询任务数据(带权限校验)
Task = request.env['yuthon.will.task']
task = Task.sudo().browse(task_id)
if not task.exists():
return request.not_found("任务不存在或已被删除")
# 格式化时间显示将0.5转为0:30格式
def format_time(hour_str):
if not hour_str:
return ""
if hour_str.endswith('.5'):
return f"{int(float(hour_str))}:30"
return f"{int(float(hour_str))}:00"
# 准备模板所需数据
task_data = {
'task': task,
'format_time': format_time, # 传递格式化函数到模板
'employee_names': ', '.join(task.employee_ids.mapped('name')),
'driver_names': ', '.join(task.employee_ids2.mapped('name')) or '',
}
@http.route('/yuthon/task/detail/<int:task_id>',type='http',auth='public',website=False)
def task_detail(self, task_id, **kw):
try:
task = request.env['yuthon.will.task'].sudo().browse(task_id)
if not task.exists():
raise MissingError(f"任务ID {task_id} 不存在")
participant_names = []
if task.employee_ids:
participant_names.extend(task.employee_ids.mapped('name'))
time_display = ""
if task.start_date:
time_display = str(task.start_date)
task_data = {
'name': task.name or '',
'start_date': time_display,
'address': task.address or '',
'user_id': task.user_id.name if task.user_id else '',
'employee_ids': ', '.join(participant_names) if participant_names else '',
'code': task.code or '',
'request_hour_from': task.request_hour_from,
'request_hour_to': task.request_hour_to,
}
return request.render('yuthon_will_task.task_detail_template', task_data)
except MissingError as e:
return f"<h3>错误:{str(e)}</h3>"
except Exception as e:
return f"<h3>加载失败:{str(e)}</h3>"
@http.route('/will/task/list', methods=['POST'], type='http', auth='public', csrf=False)
def will_task_list(self, **kwargs):
request_hour_selection = request.env['yuthon.will.task'].sudo().fields_get(allfields=['request_hour_from'])['request_hour_from']['selection']
employee_id = kwargs.get('employee_id')
data = request.env['yuthon.will.task'].sudo().search(
['|', ('employee_ids', 'in', [employee_id]), ('employee_id.id', '=', employee_id)])
result_list = []
for task in data:
weekday_str = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"][task.start_date.weekday()]
file_list = []
for attach in task.document_ids:
attach_id = request.env['ir.attachment'].sudo().search([('id', '=', attach.id)])
file_url = 'http://view.officeapps.live.com/op/view.aspx?src=' + quote(
'https://oa.thtzjt.com' + '/web/content/office_preview/' + str(attach_id.id))
file_list.append({
'file_url': file_url,
'file_name': attach_id.display_name,
})
result = {
'id': task.id,
'name': task.name,
'employee_id': task.employee_id.name,
'employee_ids': ', '.join(task.employee_ids.mapped('name')),
'start_date': fields.Date.to_string(task.start_date),
'weekday': weekday_str,
'request_hour_from': '',
'request_hour_to': '',
'file_list': file_list,
}
for f in request_hour_selection:
if f[0] == task.request_hour_from:
result['request_hour_from'] = f[1]
if f[0] == task.request_hour_to:
result['request_hour_to'] = f[1]
result_list.append(result)
return json.dumps({
'data': result_list,
'code': 200,
'message': 'success'
})
@http.route('/will/task/record', methods=['POST'], type='http', auth='public', csrf=False)
def will_task_record(self, **kwargs):
task_id = kwargs.get('id')
task = request.env['yuthon.will.task'].sudo().search([('id', '=', task_id)], limit=1)
request_hour_selection = request.env['yuthon.will.task'].sudo().fields_get(allfields=['request_hour_from'])['request_hour_from']['selection']
file_list = []
for attach in task.document_ids:
attach_id = request.env['ir.attachment'].sudo().search([('id', '=', attach.id)], limit=1)
if attach_id:
file_url = 'http://view.officeapps.live.com/op/view.aspx?src=' + quote(
'https://oa.thtzjt.com' + '/web/content/office_preview/' + str(attach_id.id))
file_list.append({
'file_url': file_url,
'file_name': attach_id.display_name,
})
weekday_str = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"][task.start_date.weekday()]
result = {
'id': task.id,
'name': task.name,
'employee_id': task.employee_id.name,
'employee_ids': ', '.join(task.employee_ids.mapped('name')),
'start_date': fields.Date.to_string(task.start_date),
'weekday': weekday_str,
'request_hour_from': '',
'request_hour_to': '',
'file_list': file_list,
}
hour_dict = dict(request_hour_selection)
result['request_hour_from'] = hour_dict.get(task.request_hour_from)
result['request_hour_to'] = hour_dict.get(task.request_hour_to)
return json.dumps({
'data': result,
'code': 200,
'message': 'success'
})