# -*- coding: utf-8 -*- import json import math import logging import datetime from odoo import http, tools, fields from .tool import MakeResponse _logger = logging.getLogger(__name__) class ActivityExploration(http.Controller): @http.route(['/activity_exploration'], type='http', auth="public", website=True) def activity_exploration(self, **kw): return http.request.render('tx_cms_upgrade.activityExploration') @http.route(['/activity_exploration_data', '/activity_exploration_data/page/'], methods=['get'], auth='public', website=True, csrf=False, cors="*") def activity_exploration_data(self, page=1, **kw): search = kw.get("search", False) state_id = int(kw.get("state_id", 0)) page = int(page) website = http.request.website.sudo() exploration_obj = http.request.env["tx.activity.exploration"].sudo() _post_per_page = 10 state = [ {"id": 0, "name": "全部", "active": True if state_id == 0 else False}, {"id": 1, "name": "报名中", "active": True if state_id == 1 else False}, {"id": 2, "name": "进行中", "active": True if state_id == 2 else False}, {"id": 3, "name": "已结束", "active": True if state_id == 3 else False} ] domain = [] if state_id == 1: domain.append(("start_date", ">", fields.Date.today())) if state_id == 2: domain.append(("start_date", "<", fields.Date.today())) domain.append(("end_date", ">", fields.Date.today())) if state_id == 3: domain.append(("end_date", "<", fields.Date.today())) explorationAll = exploration_obj.search(domain) pager = tools.lazy( lambda: http.request.website.pager(url='/activity_exploration_data', total=explorationAll.__len__(), page=page, step=_post_per_page, scope=_post_per_page, url_args=kw)) explorationData = explorationAll[ (page - 1) * _post_per_page:page * _post_per_page] records = [] for exploration in explorationData: records.append({ "id": exploration.id, "imgUrl": website.image_url(exploration, 'img'), "name": exploration.name, "createDate": f"""{exploration.create_date.year}年{exploration.create_date.month}月{exploration.create_date.day}日""", "briefIntroduction": str(exploration.brief_introduction), "state": exploration.get_state_value(exploration.state), }) return MakeResponse.success({ "stateList": state, "explorationData": records, "pager": pager._value, }) @http.route('/activity_exploration_details/', type='http', auth="public", website=True) def object(self, obj, **kw): return http.request.render('tx_cms_upgrade.activityExplorationDetails', { 'object': obj }) @http.route('/activity_exploration/details', methods=['get'], auth='public') def record_details(self, recordId, **kw): record_obj = http.request.env["tx.activity.exploration"].sudo() obj = record_obj.sudo().browse([int(recordId)]) return MakeResponse.success({ "name": obj.name, "start_date": obj.start_date.__str__(), "end_date": obj.end_date.__str__(), "content": obj.content, }) # 有数了 -- 活动探索 列表 @http.route("/tx_base/api/explore/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) explore_name = params.get("exploreName", "") status = params.get("status", []) model_obj = http.request.env["tx.activity.exploration"].sudo() records = [] domain = [] if explore_name: domain.append(("name", "like", f"%{explore_name}%")) if status: if type(status) == int: status = [status] data_ids = [] dict_ids = http.request.env["tx.data.dict"].sudo().search([("id", "in", status)]) for dict_id in dict_ids: status_domain = [] if dict_id.name == "报名中": status_domain.append(("start_date", ">", datetime.date.today())) elif dict_id.name == "进行中": status_domain.append(("start_date", "<=", datetime.date.today())) status_domain.append(("end_date", ">", datetime.date.today())) elif dict_id.name == "已结束": status_domain.append(("end_date", "<=", datetime.date.today())) data_ids += model_obj.search(domain+status_domain).ids if data_ids.__len__(): domain.append(("id", "in", data_ids)) else: domain.append(("id", "=", 0)) offset = (page_num - 1) * page_size if page_num > 1 else 0 total = model_obj.search_count(domain) dataAll = model_obj.search(domain, offset, page_size) for data in dataAll: records.append({ "id": data.id, "exploreAvatar": f"""{data.get_base_url()}{http.request.website.image_url(data, 'img')}""", "exploreName": data.name, "startTime": data.start_date.strftime("%Y-%m-%d") if data.start_date else datetime.date.today().strftime("%Y-%m-%d"), "exploreDescription": str(data.brief_introduction), "status": data.get_state_value(data.state), }) pages = math.ceil(total / page_size) return MakeResponse.opontekr_success(records, total, pages) # 有数了 -- 活动探索 详情 @http.route("/tx_base/api/explore/info", methods=['GET'], auth='public', csrf=False, website=True, cors="*") def record_info(self, **kwargs): try: exploreId = kwargs.get("exploreId", False) model_obj = http.request.env["tx.activity.exploration"].sudo() record = model_obj.search([("id", "=", exploreId)]) data = { "startTime": record.start_date.strftime("%Y-%m-%d") if record.start_date else datetime.date.today().strftime("%Y-%m-%d"), "endTime": record.end_date.strftime("%Y-%m-%d") if record.end_date else datetime.date.today().strftime("%Y-%m-%d"), "exploreName": record.name, "exploreContent": record.content } return MakeResponse.success(data) except Exception as e: _logger.error(e) return MakeResponse.error("参数错误")