web_direct_print/controllers/main.py
2025-11-27 10:11:39 +07:00

90 lines
3.4 KiB
Python

from odoo import http
from odoo.http import request
import json
import base64
class DirectPrintController(http.Controller):
@http.route('/web/direct_print', type='json', auth='user')
def direct_print(self, report_name, docids, data=None):
"""
Controller method to handle direct print requests
:param report_name: Name of the report to print
:param docids: IDs of documents to print
:param data: Additional data for the report
:return: JSON response with print data
"""
try:
import logging
_logger = logging.getLogger(__name__)
_logger.info(f"Controller received: report_name={report_name} (type: {type(report_name)}), docids={docids} (type: {type(docids)}), data={data}")
# Handle parameters that might come as different types
if isinstance(report_name, list):
report_name = report_name[0] if report_name else ''
if isinstance(docids, str):
try:
# Try to convert string to list of integers
if ',' in docids:
docids = [int(x.strip()) for x in docids.split(',') if x.strip()]
else:
docids = [int(docids)]
except ValueError:
docids = []
elif not isinstance(docids, list):
docids = [docids] if docids else []
_logger.info(f"Processed parameters: report_name={report_name}, docids={docids}")
# Call the direct print model method with proper context
result = request.env['web.direct.print'].sudo().direct_print_action(
report_name, docids, data, context=request.context
)
return result
except Exception as e:
import logging
_logger = logging.getLogger(__name__)
_logger.error(f"Controller error: {str(e)}")
_logger.exception("Full traceback:")
return {
'success': False,
'error': str(e)
}
@http.route('/web/direct_print/get_reports', type='json', auth='user')
def get_available_reports(self):
"""
Controller method to get available reports for direct printing
:return: JSON response with available reports
"""
try:
result = request.env['web.direct.print'].sudo().get_available_reports()
return result
except Exception as e:
return {
'success': False,
'error': str(e)
}
@http.route('/web/direct_print/test', type='http', auth='user', website=True)
def test_direct_print(self, **kwargs):
"""
Test endpoint for direct printing functionality
"""
# This could be used for testing purposes
html_content = """
<!DOCTYPE html>
<html>
<head>
<title>Direct Print Test</title>
</head>
<body>
<h1>Direct Print Test Page</h1>
<p>This page demonstrates the direct print functionality.</p>
<button onclick="window.print()">Print this page</button>
</body>
</html>
"""
return html_content