Some checks failed
pre-commit / pre-commit (pull_request) Failing after 1m45s
62 lines
1.8 KiB
Python
62 lines
1.8 KiB
Python
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
|
|
|
|
from __future__ import annotations
|
|
|
|
import base64
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
from odoo import http
|
|
from odoo.http import request, content_disposition
|
|
from odoo.addons.survey.controllers.main import Survey
|
|
|
|
if TYPE_CHECKING:
|
|
from werkzeug.wrappers import Response
|
|
|
|
|
|
class SurveyExtraFieldsController(Survey):
|
|
|
|
@http.route(
|
|
"/survey/file/<string:survey_token>/<string:answer_token>/<int:line_id>",
|
|
type="http",
|
|
auth="public",
|
|
)
|
|
def survey_file_download(
|
|
self,
|
|
survey_token: str,
|
|
answer_token: str,
|
|
line_id: int,
|
|
**kwargs: Any
|
|
) -> Response:
|
|
survey = request.env["survey.survey"].sudo().search(
|
|
[("access_token", "=", survey_token)], limit=1
|
|
)
|
|
if not survey:
|
|
return request.not_found()
|
|
|
|
answer = request.env["survey.user_input"].sudo().search(
|
|
[
|
|
("survey_id", "=", survey.id),
|
|
("access_token", "=", answer_token),
|
|
],
|
|
limit=1,
|
|
)
|
|
if not answer:
|
|
return request.not_found()
|
|
|
|
line = request.env["survey.user_input.line"].sudo().browse(line_id)
|
|
if not line.exists() or line.user_input_id != answer:
|
|
return request.not_found()
|
|
|
|
if not line.value_file:
|
|
return request.not_found()
|
|
|
|
file_content = base64.b64decode(line.value_file)
|
|
filename = line.value_file_fname or "file"
|
|
return request.make_response(
|
|
file_content,
|
|
headers=[
|
|
("Content-Type", "application/octet-stream"),
|
|
("Content-Disposition", content_disposition(filename)),
|
|
],
|
|
)
|