import base64 import json from odoo import http, fields from odoo.http import request 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))]) request.env['yuthon.will.task'].create({ 'fields':kwargs.get('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('/will/task/list', methods=['POST'], type='http', auth='public', csrf=False) def will_task_list(self, **kwargs): employee_id = kwargs.get('employee_id') start = kwargs.get('start') request_hour_selection = request.env['yuthon.will.task'].sudo().fields_get(allfields=['request_hour_from'])['request_hour_from']['selection'] result_list = [] # for task in request.env['yuthon.will.task'].sudo().search(['|', ('employee_ids', 'in', [employee_id]), ('employee_id.id', '=', employee_id)]): task_ids = request.env['yuthon.will.task'].sudo().search([], offset=start, limit=7) for task in task_ids: 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')), 'employee_num': len(task.employee_ids), 'start_date': fields.Date.to_string(task.start_date), 'weekday': weekday_str, 'request_hour_from': '', 'request_hour_to': '', } 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) 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)]) 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)]) 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')), 'employee_num': len(task.employee_ids), 'start_date': fields.Date.to_string(task.start_date), 'weekday': weekday_str, 'request_hour_from': '', 'request_hour_to': '', 'remarks': task.remarks, 'address': task.address, # 'file_list': file_list, 'file_list': [{ 'file_url':'https://view.officeapps.live.com/op/view.aspx?src=https%3A//oa.thtzjt.com/web/content/office_preview/13672', 'file_name':'excel', }, { 'file_url':'https://oa.thtzjt.com/web/content/preview/13673', 'file_name':'pdf', } ], } 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' }) @http.route('/will/task/create', methods=['POST'], type='http', auth='public', csrf=False) def will_task_create(self, **kwargs): """ 手机端创建日程接口 """ request.env['yuthon.will.task'].sudo().create({ 'name': kwargs.get('name'), 'employee_id': kwargs.get('id'), 'employee_ids': kwargs.get('id'), 'start_date': kwargs.get('id'), 'request_hour_from': '', 'request_hour_to': '', 'remarks': kwargs.get('remarks'), 'address': kwargs.get('address'), }) return json.dumps({ 'code': 200, 'message': '数据创建成功' })