OdooDigitizationService/addons/tx_cms/controllers/controllers.py

124 lines
5.6 KiB
Python

# -*- coding: utf-8 -*-
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.benchmarkingScenarios', {})
@http.route(['/benchmarking_scenarios_data', '/benchmarking_scenarios_data/page/<int: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/<model("tx.benchmarking.scenarios"):obj>', type='http', auth="public",
website=True)
def object(self, obj, **kw):
return http.request.render('tx_cms.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,
})