From 4eb1f6572bfe6497b3811c0b3cc04dee61339475 Mon Sep 17 00:00:00 2001 From: jordan <jordan@le-filament.com> Date: Wed, 19 Aug 2020 17:03:25 +0200 Subject: [PATCH] [fix] ajout contrainte de validation + TODO "prefilling with previous answers" --- controllers/main.py | 46 ++++++++++++++++++++++++++++++ models/survey_question.py | 19 ++++++++---- views/survey_question_template.xml | 5 ++-- 3 files changed, 61 insertions(+), 9 deletions(-) diff --git a/controllers/main.py b/controllers/main.py index 9af3e4c..059b44b 100644 --- a/controllers/main.py +++ b/controllers/main.py @@ -41,3 +41,49 @@ class WebsiteSurveyExtend(Survey): 'page_nr': 0, 'quizz_correction': True if survey.quizz_mode and token else False, 'user_input_line_upload_file': user_input_line_upload_file}) + + # TODO - Trouver un moyen d'afficher le fichier téléchargé cas de retour sur la page + # # AJAX prefilling of a survey + # @http.route(['/survey/prefill/<model("survey.survey"):survey>/<string:token>', + # '/survey/prefill/<model("survey.survey"):survey>/<string:token>/<model("survey.page"):page>'], + # type='http', auth='public', website=True) + # def prefill(self, survey, token, page=None, **post): + # UserInputLine = request.env['survey.user_input_line'] + # ret = {} + # + # # Fetch previous answers + # if page: + # previous_answers = UserInputLine.sudo().search([('user_input_id.token', '=', token), ('page_id', '=', page.id)]) + # else: + # previous_answers = UserInputLine.sudo().search([('user_input_id.token', '=', token)]) + # + # # Return non empty answers in a JSON compatible format + # for answer in previous_answers: + # if not answer.skipped: + # answer_tag = '%s_%s_%s' % (answer.survey_id.id, answer.page_id.id, answer.question_id.id) + # answer_value = None + # if answer.answer_type == 'free_text': + # answer_value = answer.value_free_text + # elif answer.answer_type == 'text' and answer.question_id.type == 'textbox': + # answer_value = answer.value_text + # elif answer.answer_type == 'text' and answer.question_id.type != 'textbox': + # # here come comment answers for matrices, simple choice and multiple choice + # answer_tag = "%s_%s" % (answer_tag, 'comment') + # answer_value = answer.value_text + # elif answer.answer_type == 'number': + # answer_value = str(answer.value_number) + # elif answer.answer_type == 'date': + # answer_value = fields.Date.to_string(answer.value_date) + # elif answer.answer_type == 'suggestion' and not answer.value_suggested_row: + # answer_value = answer.value_suggested.id + # elif answer.answer_type == 'suggestion' and answer.value_suggested_row: + # answer_tag = "%s_%s" % (answer_tag, answer.value_suggested_row.id) + # answer_value = answer.value_suggested.id + # # elif answer.answer_type == 'upload_file': + # # answer_value = answer.file + # if answer_value: + # ret.setdefault(answer_tag, []).append(answer_value) + # else: + # _logger.warning("[survey] No answer has been found for question %s marked as non skipped" % answer_tag) + # print('------- RET', ret) + # return json.dumps(ret, default=str) diff --git a/models/survey_question.py b/models/survey_question.py index d54a888..95a41a7 100644 --- a/models/survey_question.py +++ b/models/survey_question.py @@ -13,11 +13,21 @@ class SurveyQuestion(models.Model): ('textbox', 'Single Line Text Box'), ('numerical_box', 'Numerical Value'), ('date', 'Date'), - ('upload_file', 'Upload file'), + ('upload_file', 'Télécharger un fichier'), ('simple_choice', 'Multiple choice: only one answer'), ('multiple_choice', 'Multiple choice: multiple answers allowed'), ('matrix', 'Matrix')], string='Type of Question', default='free_text', required=True) + @api.multi + def validate_upload_file(self, post, answer_tag): + self.ensure_one() + errors = {} + answer = post[answer_tag] + # Empty answer to mandatory question + if self.constr_mandatory and not answer: + errors.update({answer_tag: self.constr_error_msg}) + return errors + class SurveyUserInputLine(models.Model): _inherit = 'survey.user_input_line' @@ -44,12 +54,9 @@ class SurveyUserInputLine(models.Model): 'skipped': False } - if question.constr_mandatory: - file = base64.encodebytes(post[answer_tag].read()) - else: - file = base64.encodebytes(post[answer_tag].read()) if post[answer_tag] else None + file = base64.encodebytes(post[answer_tag].read()) if post[answer_tag] else None - if answer_tag in post: + if file: vals.update({'answer_type': 'upload_file', 'file': file, 'file_type': post[answer_tag].mimetype}) else: vals.update({'answer_type': None, 'skipped': True}) diff --git a/views/survey_question_template.xml b/views/survey_question_template.xml index d52774d..f5295cb 100644 --- a/views/survey_question_template.xml +++ b/views/survey_question_template.xml @@ -16,8 +16,7 @@ <!-- UPLOAD FILE TEMPLATE --> <template id="upload_file" name="Upload file"> - <div class="input-file-container"> - + <div> <t t-if="user_input_line_upload_file"> <t t-foreach="user_input_line_upload_file" t-as="upload_file" t-if="upload_file.question_id.id == question.id"> <a width="100px" height="100px" t-att-href="'data:%s;base64,%s' % (upload_file.file_type, to_text(upload_file.file))" target="_blank">Telecharger</a> @@ -25,7 +24,7 @@ </t> <t t-else=""> <div class="file-field" > - <input class="input-file" id="my-file" type="file" t-att-name="prefix"/> + <input class="form-control" type="file" t-att-name="prefix"/> </div> <br/> </t> -- GitLab