OdooDigitizationService/addons/tx_cms_upgrade/controllers/digital_ally.py

133 lines
5.2 KiB
Python

# -*- coding: utf-8 -*-
import json
import math
import logging
from odoo import http, tools
from .tool import MakeResponse
_logger = logging.getLogger(__name__)
class digitalAlly(http.Controller):
@http.route(['/digital_ally'], type='http', auth="public", website=True)
def list(self, **kw):
return http.request.render('tx_cms_upgrade.digitalAlly', {})
@http.route(['/digital_ally_data', '/digital_ally_data/page/<int:page>'], type='http', auth="public",
website=True)
def listData(self, industry_id=0, page=1, **kw):
ally_obj = http.request.env["tx.digital.ally"].sudo()
_post_per_page = 9
page = int(page)
industryList = []
industry_id = int(industry_id)
industry_ids = ally_obj.is_industry_ids.sudo().search([("code", "=", "II")])._get_dict_values()
for index, item in enumerate(industry_ids):
item["active"] = True if item["id"] == industry_id else False
industryList.append(item)
domain = []
if industry_id:
domain.append(("is_industry_ids", "in", [industry_id]))
allyAll = ally_obj.search(domain)
pager = tools.lazy(
lambda: http.request.website.pager(url='/digital_ally_data', total=allyAll.__len__(),
page=page, step=_post_per_page, scope=_post_per_page,
url_args=kw))
allyData = allyAll[(page - 1) * _post_per_page:page * _post_per_page]
records = []
for ally in allyData:
records.append({
"id": ally.id,
"imgUrl": http.request.website.image_url(ally, 'img'),
"name": ally.name,
"companyName": ally.company_name,
"technicalIds": [technical_id.name for technical_id in ally.technical_ids],
"industryIds": [industry_id.name for industry_id in ally.is_industry_ids],
})
return MakeResponse.success({
"industryList": industryList,
"records": records,
"industry_id": industry_id,
"pager": pager._value,
})
@http.route('/digital_ally_details/<model("tx.digital.ally"):obj>', type='http', auth="public",
website=True)
def object(self, obj, **kw):
return http.request.render('tx_cms_upgrade.digitalAllyDetails', {
'object': obj
})
@http.route('/digital_ally/details', methods=['get'], auth='public', website=True)
def record_details(self, recordId, **kw):
record_obj = http.request.env["tx.digital.ally"].sudo()
obj = record_obj.sudo().browse([int(recordId)])
return MakeResponse.success({
"imgUrl": http.request.website.image_url(obj, 'img'),
"name": obj.name,
"briefIntroduction": obj.brief_introduction,
"technicalList": [technical_id.name for technical_id in obj.technical_ids],
"content": obj.content,
})
# 有数了 -- 数字化盟友 列表
@http.route("/tx_base/api/allies/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)
digital_name = params.get("digitalName", "")
industry_type = params.get("industryType", [])
model_obj = http.request.env["tx.digital.ally"].sudo()
records = []
domain = []
if industry_type.__len__():
domain.append(("is_industry_ids", "in", industry_type))
if digital_name:
domain.append(("name", "like", f"%{digital_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,
"alliesAvatar": f"""{data.get_base_url()}{http.request.website.image_url(data, 'img')}""",
"alliesName": f"{data.name}",
"industryType": ",".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/allies/info", methods=['GET'], auth='public', csrf=False, website=True, cors="*")
def record_info(self, **kwargs):
try:
alliesId = kwargs.get("alliesId", False)
model_obj = http.request.env["tx.digital.ally"].sudo()
record = model_obj.search([("id", "=", alliesId)])
data = {
"alliesAvatar": f"""{record.get_base_url()}{http.request.website.image_url(record, 'img')}""",
"alliesName": record.name,
"alliesDescription": record.brief_introduction,
"alliesContent": record.content,
}
return MakeResponse.success(data)
except Exception as e:
_logger.error(e)
return MakeResponse.error("参数错误")