From e8d0dfd753c4bb9b65ef73f28c8d45ee9b031909 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Krienb=C3=BChl?= Date: Wed, 20 Dec 2017 16:21:46 +0100 Subject: [PATCH] Adds a meta column to the submissions table --- HISTORY.rst | 4 ++++ onegov/form/collection.py | 9 ++++++--- onegov/form/models/submission.py | 3 +++ onegov/form/upgrade.py | 11 +++++++++++ 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 8f6875b..6fe2ac4 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -1,5 +1,9 @@ Changelog --------- + +- Adds a meta column to the submissions table. + [href] + 0.28.0 (2017-12-19) ~~~~~~~~~~~~~~~~~~~ diff --git a/onegov/form/collection.py b/onegov/form/collection.py index 5396329..206b3d6 100644 --- a/onegov/form/collection.py +++ b/onegov/form/collection.py @@ -133,7 +133,7 @@ def query(self): return query - def add(self, name, form, state, id=None, payment_method=None): + def add(self, name, form, state, id=None, payment_method=None, meta=None): """ Takes a filled-out form instance and stores the submission in the database. The form instance is expected to have a ``_source`` parameter, which contains the source used to build the form (as only @@ -169,6 +169,7 @@ def add(self, name, form, state, id=None, payment_method=None): submission.id = id or uuid4() submission.name = name submission.state = state + submission.meta = meta or {} submission.payment_method = ( payment_method or definition and definition.payment_method or @@ -189,7 +190,8 @@ def add(self, name, form, state, id=None, payment_method=None): return submission - def add_external(self, form, state, id=None, payment_method=None): + def add_external(self, form, state, id=None, payment_method=None, + meta=None): """ Takes a filled-out form instance and stores the submission in the database. The form instance is expected to have a ``_source`` parameter, which contains the source used to build the form (as only @@ -205,7 +207,8 @@ def add_external(self, form, state, id=None, payment_method=None): form=form, state=state, id=id, - payment_method=payment_method + payment_method=payment_method, + meta=meta, ) def complete_submission(self, submission): diff --git a/onegov/form/models/submission.py b/onegov/form/models/submission.py index c6a04c4..b0b2c9a 100644 --- a/onegov/form/models/submission.py +++ b/onegov/form/models/submission.py @@ -49,6 +49,9 @@ class FormSubmission(Base, TimestampMixin, Payable, AssociatedFiles): #: checksums are guaranteed to have the exact same definition checksum = Column(Text, nullable=False) + #: metadata about this submission + meta = Column(JSON, nullable=False) + #: the submission data data = Column(JSON, nullable=False) diff --git a/onegov/form/upgrade.py b/onegov/form/upgrade.py index 4f6fb56..99c48b8 100644 --- a/onegov/form/upgrade.py +++ b/onegov/form/upgrade.py @@ -10,6 +10,7 @@ from onegov.form import FormDefinitionCollection from onegov.form import FormFile from onegov.form import FormSubmission +from onegov.core.orm.types import JSON from sqlalchemy import Column, Text @@ -97,3 +98,13 @@ def add_payment_method_to_definitions_and_submissions(context): for form in context.records_per_table('forms'): form.content.pop('payment_method', None) + + +@upgrade_task('Add meta dictionary to submissions') +def add_meta_directory_to_submissions(context): + + context.add_column_with_defaults( + table='submissions', + column=Column('meta', JSON, nullable=False), + default=lambda submission: dict() + )