# -*- coding: utf-8 -*- import json import math import datetime import logging from odoo import http, tools from odoo.addons.portal.controllers.web import Home from .tool import MakeResponse _logger = logging.getLogger(__name__) class Website(Home): def _login_redirect(self, uid, redirect=None): """ Redirect regular users (employees) to the backend) and others to the frontend """ if not redirect and http.request.params.get('login_success'): if http.request.env['res.users'].browse(uid)._is_internal(): redirect = '/web?' + http.request.httprequest.query_string.decode() else: redirect = '/' return super()._login_redirect(uid, redirect=redirect) class benchmarkingScenarios(http.Controller): @http.route('/benchmarking_scenarios', type='http', auth="public", website=True) def list(self, **kw): return http.request.render('tx_cms_upgrade.benchmarkingScenarios', {}) @http.route(['/benchmarking_scenarios_data', '/benchmarking_scenarios_data/page/'], methods=['get'], auth='public', website=True, csrf=False, cors="*") def listData(self, technical_id=0, industry_id=0, page=1, **kw): scenarios_obj = http.request.env["tx.benchmarking.scenarios"].sudo() _post_per_page = 10 technicalList = [] industryList = [] page = int(page) industry_id = int(industry_id) technical_id = int(technical_id) technical_ids = scenarios_obj.technical_ids.sudo().search([("code", "=", "TF")])._get_dict_values() for index, item in enumerate(technical_ids): item["active"] = True if item["id"] == technical_id else False technicalList.append(item) industry_ids = scenarios_obj.technical_ids.sudo().search([("code", "=", "IF")])._get_dict_values() for index, item in enumerate(industry_ids): item["active"] = True if item["id"] == industry_id else False industryList.append(item) scenariosRecommendDomain = [("recommend", "=", True)] if industry_id: scenariosRecommendDomain.append(("industry_ids", "in", [industry_id])) if technical_id: scenariosRecommendDomain.append(("technical_ids", "in", [technical_id])) scenariosRecommendData = scenarios_obj.sudo().search(scenariosRecommendDomain, limit=4) scenariosDomain = [("id", "not in", scenariosRecommendData.ids)] if industry_id: scenariosDomain.append(("industry_ids", "in", [industry_id])) if technical_id: scenariosDomain.append(("technical_ids", "in", [technical_id])) scenariosDataAll = scenarios_obj.sudo().search(scenariosDomain) pager = tools.lazy( lambda: http.request.website.pager(url='/benchmarking_scenarios_data', total=scenariosDataAll.__len__(), page=page, step=_post_per_page, scope=_post_per_page, url_args=kw)) if page == 1: scenariosData = scenariosDataAll[ (page - 1) * _post_per_page:page * _post_per_page - scenariosRecommendData.__len__()] else: scenariosData = scenariosDataAll[ (page - 1) * _post_per_page - scenariosRecommendData.__len__():page * _post_per_page] scenariosRecommendData = scenarios_obj records = [] for scenarios in scenariosRecommendData: records.append({ "id": scenarios.id, "name": scenarios.name, "briefIntroduction": scenarios.brief_introduction, "createDate": f"{scenarios.create_date.year}年{scenarios.create_date.month}月{scenarios.create_date.day}日", "technical_ids": [technical_id.name for technical_id in scenarios.technical_ids], "industry_ids": [industry_id.name for industry_id in scenarios.industry_ids], "tag_ids": [tag_id.name for tag_id in scenarios.tag_ids], }) for scenarios in scenariosData: records.append({ "id": scenarios.id, "name": scenarios.name, "briefIntroduction": scenarios.brief_introduction, "createDate": f"{scenarios.create_date.year}年{scenarios.create_date.month}月{scenarios.create_date.day}日", "technical_ids": [technical_id.name for technical_id in scenarios.technical_ids], "industry_ids": [industry_id.name for industry_id in scenarios.industry_ids], "tag_ids": [tag_id.name for tag_id in scenarios.tag_ids], }) return MakeResponse.success({ "technicalList": technicalList, "industryList": industryList, "records": records, "pager": pager._value, 'technical_id': technical_id, 'industry_id': industry_id, }) @http.route('/benchmarking_scenarios_details/', type='http', auth="public", website=True) def object(self, obj, **kw): return http.request.render('tx_cms_upgrade.benchmarkingScenarioDetails', { 'object': obj }) @http.route('/benchmarking_scenarios/details', methods=['get'], auth='public') def record_details(self, recordId, **kw): record_obj = http.request.env["tx.benchmarking.scenarios"].sudo() obj = record_obj.sudo().browse([int(recordId)]) return MakeResponse.success({ "name": obj.name, "create_date": obj.create_date.date().__str__(), "content": obj.content, }) # 有数了 -- 标杆场景 列表 @http.route("/tx_base/api/scenes/list", methods=['POST'], auth='public', csrf=False, website=True, cors="*") def scenes_list(self, **kw): params = json.loads(http.request.httprequest.data.decode("utf-8")) page_num = params.get("pageNum", 1) page_size = params.get("pageSize", 10) scenes_name = params.get("scenesName", "") technical_type = params.get("technicalType", []) industry_type = params.get("industryType", []) model_obj = http.request.env["tx.benchmarking.scenarios"].sudo() records = [] domain = [] if technical_type.__len__(): domain.append(("technical_ids", "in", technical_type)) if industry_type.__len__(): domain.append(("industry_ids", "in", industry_type)) if scenes_name: domain.append(("name", "like", f"%{scenes_name}%")) total = model_obj.search_count(domain) offset = (page_num - 1) * page_size if page_num > 1 else 0 dataAll = model_obj.search(domain, offset, page_size) for data in dataAll: records.append({ "id": data.id, "scenesName": f"{data.name}", "scenesDescription": data.brief_introduction, "company": data.company_name, "isRecommend": "Y" if data.recommend else "N", "createTime": data.create_date.strftime("%Y-%m-%d") if data.create_date else datetime.date.today().strftime("%Y-%m-%d"), "labelType": ",".join([industry_id.name for industry_id in data.industry_ids]), }) pages = math.ceil(total / page_size) return MakeResponse.opontekr_success(records, total, pages) # 有数了 -- 标杆场景 详情 @http.route("/tx_base/api/scenes/info", methods=['GET'], auth='public', csrf=False, website=True, cors="*") def record_info(self, **kwargs): try: scenesId = kwargs.get("scenesId", False) model_obj = http.request.env["tx.benchmarking.scenarios"].sudo() record = model_obj.search([("id", "=", scenesId)]) data = { "createTime": record.create_date.strftime("%Y-%m-%d") if record.create_date else datetime.date.today().strftime("%Y-%m-%d"), "scenesName": record.name, "scenesContent": record.content } return MakeResponse.success(data) except Exception as e: _logger.error(e) return MakeResponse.error("参数错误")