Skip to content

Commit

Permalink
Fix syntax error
Browse files Browse the repository at this point in the history
  • Loading branch information
m1k3lm committed Jul 16, 2024
1 parent 88002d2 commit 622ffae
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 80 deletions.
2 changes: 1 addition & 1 deletion survey_question_type_binary/models/survey_question.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def validate_binary(self, answers):
if self.constr_mandatory and not answers:
errors.update({self.id: self.constr_error_msg})
if answers:
if not isinstance(answers | (list, tuple)):
if not isinstance(answers, (list | tuple)):
answers = [answers]
for answer in answers:
try:
Expand Down
4 changes: 2 additions & 2 deletions survey_question_type_binary/models/survey_user_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def _save_lines(self, question, answer, comment=None, overwrite_existing=True):
)

if question.question_type in ("binary", "multi_binary"):
if not isinstance(answer | (list, tuple)):
if not isinstance(answer, (list | tuple)):
answer = [answer]
if not answer:
answer = [False]
Expand All @@ -36,7 +36,7 @@ def _get_line_answer_values(self, question, answer, answer_type):
del vals["value_binary"]
else:
del vals["value_multi_binary"]
if not isinstance(answer | (list, tuple)):
if not isinstance(answer, (list | tuple)):
answer = [answer]
answer_binary_datas = []
for answer_binary in answer:
Expand Down
145 changes: 73 additions & 72 deletions survey_question_type_binary/static/src/js/survey_form.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,80 @@
/** @odoo-module **/
odoo.define("survey_question_type_binary", [], function () {
"use strict";

import SurveyFormWidget from "@survey/js/survey_form";
import {sprintf} from "@web/core/utils/strings";
const survey_form = odoo.loader.modules.get("@survey/js/survey_form")[
Symbol.for("default")
];

SurveyFormWidget.include({
_getSubmitBinariesValues: function (params) {
const self = this;
let promises = [];
survey_form.include({
_getSubmitBinariesValues: function (params) {
const self = this;
let promises = [];

this.$("[data-question-type]").each(function () {
switch ($(this).data("questionType")) {
case "binary":
case "multi_binary":
promises = promises.concat(
self._getSubmitAnswersBinary(params, $(this))
);
break;
}
});
return promises;
},
_getSubmitAnswersBinary: function (params, $input) {
const question_id = $input.attr("name");
return Array.prototype.map.call($input[0].files, (file) => {
return this._readFileAsDataURL(file).then(function (sDataURL) {
if (!params[question_id]) {
params[question_id] = [];
this.$("[data-question-type]").each(function () {
switch ($(this).data("questionType")) {
case "binary":
case "multi_binary":
promises = promises.concat(
self._getSubmitAnswersBinary(params, $(this))
);
break;
}
params[question_id].push({
data: sDataURL.split(",")[1],
filename: file.name,
size: file.size,
type: file.type,
});
return promises;
},
_getSubmitAnswersBinary: function (params, $input) {
const question_id = $input.attr("name");
return Array.prototype.map.call($input[0].files, (file) => {
return this._readFileAsDataURL(file).then(function (sDataURL) {
if (!params[question_id]) {
params[question_id] = [];
}
params[question_id].push({
data: sDataURL.split(",")[1],
filename: file.name,
size: file.size,
type: file.type,
});
});
});
});
},
_readFileAsDataURL: function (file) {
return $.Deferred(function (deferred) {
$.extend(new FileReader(), {
onload: function (e) {
var sDataURL = e.target.result;
deferred.resolve(sDataURL);
},
onerror: function () {
deferred.reject(this);
},
}).readAsDataURL(file);
}).promise();
},
_submitForm: function (options) {
const self = this;
const params = {};
const binaryPrimises = this._getSubmitBinariesValues(params);
if (binaryPrimises.length > 0 && !this.options.isStartScreen) {
const $form = this.$("form");
const formData = new FormData($form[0]);
if (options.previousPageId) {
params.previous_page_id = options.previousPageId;
},
_readFileAsDataURL: function (file) {
return $.Deferred(function (deferred) {
$.extend(new FileReader(), {
onload: function (e) {
var sDataURL = e.target.result;
deferred.resolve(sDataURL);
},
onerror: function () {
deferred.reject(this);
},
}).readAsDataURL(file);
}).promise();
},
_submitForm: function (options) {
const self = this;
const params = {};
const binaryPrimises = self._getSubmitBinariesValues(params);

if (binaryPrimises.length > 0 && !self.options.isStartScreen) {
const $form = self.$("form");
const formData = new FormData($form[0]);

if (options.previousPageId) {
params.previous_page_id = options.previousPageId;
}
self._prepareSubmitValues(formData, params);
var route = "/survey/submit";
Promise.all(binaryPrimises).then(function () {
const submitPromise = self.rpc(
`${route}/${self.options.surveyToken}/${self.options.answerToken}`,
params
);
self._nextScreen(submitPromise, options);
});
} else {
return self._super(options);
}
this._prepareSubmitValues(formData, params);
Promise.all(binaryPrimises).then(function () {
const submitPromise = self.rpc(
sprintf(
"%s/%s/%s",
"/survey/submit",
self.options.surveyToken,
self.options.answerToken
),
params
);
self._nextScreen(submitPromise, options);
});
} else {
return this._super(options);
}
},
});
},
});
});
7 changes: 2 additions & 5 deletions survey_question_type_binary/tests/test_survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,10 @@ def test_01_question_binary_with_error_values(self):
{self.question_binary.id: f"The file cannot exceed {1 / 1024}MB in size."},
)
self.question_binary.max_filesize = 2097152 # Increse to 2.0MB
message = "Only files with {} mime types are allowed."
self.assertEqual(
self.question_binary.validate_question({"data": self.image_base64}),
{
self.question_binary.id: "Only files with {} mime types are allowed.".format(
"application/pdf"
)
},
{self.question_binary.id: message.format("application/pdf")},
)

def test_02_question_binary_with_valid_values(self):
Expand Down

0 comments on commit 622ffae

Please sign in to comment.