Skip to content

Commit

Permalink
[FIX] survey: Generate unique access_token
Browse files Browse the repository at this point in the history
Populate a unique token for each existing survey, as default method populates the same
for all.

Co-authored by: Graeme Gellatly <gdgellatly@gmail.com>
  • Loading branch information
pedrobaeza committed Nov 20, 2020
1 parent f3a9f52 commit f54c0c0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ survey / survey.survey / certification_mail_template_id (many2o
# DONE: pre-migration (renamed)

survey / survey.survey / access_mode (selection) : NEW required, selection_keys: ['public', 'token'], req_default: function, hasdefault
survey / survey.survey / access_token (char) : NEW hasdefault
survey / survey.survey / attempts_limit (integer) : NEW hasdefault
survey / survey.survey / category (selection) : NEW required, selection_keys: ['default'], req_default: function, hasdefault
survey / survey.survey / certificate (boolean) : NEW
Expand All @@ -96,6 +95,9 @@ survey / survey.survey / scoring_type (selection) : NEW re
survey / survey.survey / time_limit (float) : NEW
# NOTHING TO DO: new feature with proper defaults

survey / survey.survey / access_token (char) : NEW hasdefault
# DONE: pre-migration: populate a unique UUID before the constraint is added, as the default brings the same for all

survey / survey.user_input / input_type (selection) : NEW required, selection_keys: ['link', 'manually'], req_default: function, hasdefault
survey / survey.user_input / type (selection) : DEL required, selection_keys: ['link', 'manually'], req_default: function
# DONE: pre-migration (renamed)
Expand Down
16 changes: 16 additions & 0 deletions addons/survey/migrations/13.0.3.0/pre-migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright 2020 ForgeFlow <https://www.forgeflow.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openupgradelib import openupgrade
import uuid

_model_renames = [
('survey.mail.compose.message', 'survey.invite'),
Expand Down Expand Up @@ -82,6 +83,20 @@ def fill_survey_user_input_line_question_sequence(env):
)


def unique_survey_tokens(env):
"""Populate a unique token for each existing survey, as default method
populates the same for all. Done this on pre-migration for avoiding the
unique constraint.
"""
openupgrade.add_fields(env, [("access_token", "survey.survey", "survey_survey", "char", False, "survey")])
env.cr.execute("SELECT id FROM survey_survey")
for row in env.cr.fetchall():
env.cr.execute(
"UPDATE survey_survey SET access_token = %s WHERE id = %s",
(str(uuid.uuid4()), row[0])
)


@openupgrade.migrate()
def migrate(env, version):
openupgrade.rename_models(env.cr, _model_renames)
Expand All @@ -91,6 +106,7 @@ def migrate(env, version):
openupgrade.rename_xmlids(env.cr, _xmlid_renames)
add_helper_survey_question_page_rel(env)
fill_survey_user_input_line_question_sequence(env)
unique_survey_tokens(env)
openupgrade.set_xml_ids_noupdate_value(
env,
"survey",
Expand Down

0 comments on commit f54c0c0

Please sign in to comment.