# -*- coding: utf-8 -*- from odoo import http, fields from odoo.http import request import logging _logger = logging.getLogger(__name__) class ProjectDashboardController(http.Controller): def _verify_api_key(self, api_key): if not api_key: return None key_rec = request.env['dashboard.api.key'].sudo().search([('api_key', '=', api_key), ('active', '=', True)], limit=1) if key_rec: key_rec.write({'last_used': fields.Datetime.now()}) return key_rec.company_id return None @http.route('/project_dashboard_advanced/get_kpis', type='json', auth='user') def get_kpis(self, filters=None, company_id=None, api_key=None): if api_key: company = self._verify_api_key(api_key) if company: company_id = company.id else: return {'error': 'Invalid API Key'} return request.env['project.dashboard'].sudo().get_dashboard_kpis(filters, company_id) @http.route('/project_dashboard_advanced/tasks_by_project', type='json', auth='user') def tasks_by_project(self, filters=None, company_id=None, api_key=None): if api_key: company = self._verify_api_key(api_key) if company: company_id = company.id else: return {'error': 'Invalid API Key'} return request.env['project.dashboard'].sudo().get_tasks_by_project(filters, company_id) @http.route('/project_dashboard_advanced/tasks_by_stage', type='json', auth='user') def tasks_by_stage(self, filters=None, company_id=None, api_key=None): if api_key: company = self._verify_api_key(api_key) if company: company_id = company.id else: return {'error': 'Invalid API Key'} return request.env['project.dashboard'].sudo().get_tasks_by_stage(filters, company_id) @http.route('/project_dashboard_advanced/get_all_tasks', type='json', auth='user') def get_all_tasks(self, filters=None, limit=10, offset=0, company_id=None, api_key=None): if api_key: company = self._verify_api_key(api_key) if company: company_id = company.id else: return {'error': 'Invalid API Key'} return request.env['project.dashboard'].sudo().get_all_tasks(filters, limit, offset, company_id) @http.route('/project_dashboard_advanced/get_companies', type='json', auth='user') def get_companies(self): return request.env['project.dashboard'].sudo().get_companies() @http.route('/project_dashboard_advanced/get_managers', type='json', auth='user') def get_managers(self): user = request.env.user users = request.env['res.users'].search([('share', '=', False), ('active', '=', True)], order='name') if user.has_group( 'project.group_project_manager') else user return [{'id': u.id, 'name': u.name} for u in users] @http.route('/project_dashboard_advanced/get_customers', type='json', auth='user') def get_customers(self): customers = request.env['res.partner'].search([('customer_rank', '>', 0), ('active', '=', True)], order='name') return [{'id': c.id, 'name': c.name} for c in customers] @http.route('/project_dashboard_advanced/get_projects', type='json', auth='user') def get_projects(self, manager_id=None, customer_id=None, company_id=None): domain = [] if company_id: domain.append(('company_id', '=', company_id)) if manager_id: domain.append(('user_id', '=', int(manager_id))) if customer_id: domain.append(('partner_id', '=', int(customer_id))) if not request.env.user.has_group('project.group_project_manager'): domain.append(('user_id', '=', request.env.user.id)) projects = request.env['project.project'].search(domain, order='name') return [{'id': p.id, 'name': p.name} for p in projects] @http.route('/project_dashboard_advanced/get_activities', type='json', auth='user') def get_activities(self, filters=None, limit=10, offset=0): if filters is None: filters = {} domain = [('res_model', '=', 'project.task')] if filters.get('project_id'): tids = request.env['project.task'].search([('project_id', '=', filters['project_id'])]).ids if tids: domain.append(('res_id', 'in', tids)) if not request.env.user.has_group('project.group_project_manager'): domain.append(('user_id', '=', request.env.user.id)) activities = request.env['mail.activity'].search(domain, limit=limit, offset=offset, order='date_deadline desc') total = request.env['mail.activity'].search_count(domain) return { 'activities': [{ 'id': a.id, 'task': a.res_name or 'Unknown', 'activity': a.activity_type_id.name if a.activity_type_id else 'Unknown', 'summary': a.summary or '', 'date': a.date_deadline.strftime('%Y-%m-%d') if a.date_deadline else '' } for a in activities], 'total': total } # ✅ MISSING ROUTE RESTORED @http.route('/project_dashboard_advanced/timesheet_hours', type='json', auth='user') def timesheet_hours(self, filters=None, company_id=None, api_key=None): if api_key: company = self._verify_api_key(api_key) if company: company_id = company.id else: return {'error': 'Invalid API Key'} return request.env['project.dashboard'].sudo().get_timesheet_hours(filters, company_id) @http.route('/project_dashboard_advanced/task_deadline', type='json', auth='user') def task_deadline(self, filters=None, company_id=None, api_key=None): if api_key: company = self._verify_api_key(api_key) if company: company_id = company.id else: return {'error': 'Invalid API Key'} return request.env['project.dashboard'].sudo().get_task_deadline_chart(filters, company_id) @http.route('/project_dashboard_advanced/priority_wise', type='json', auth='user') def priority_wise(self, filters=None, company_id=None, api_key=None): if api_key: company = self._verify_api_key(api_key) if company: company_id = company.id else: return {'error': 'Invalid API Key'} return request.env['project.dashboard'].sudo().get_priority_wise_tasks(filters, company_id)