Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Commit

Permalink
Adds the ability to scope a submission collection to a specific form.
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krienbühl committed Jun 9, 2015
1 parent 41fba94 commit 2873d20
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
3 changes: 3 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Changelog
Unreleased
~~~~~~~~~~

- Adds the ability to scope a submission collection to a specific form.
[href]

0.5.1 (2015-06-08)
~~~~~~~~~~~~~~~~~~~

Expand Down
8 changes: 7 additions & 1 deletion onegov/form/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
log = logging.getLogger('onegov.form') # noqa
log.addHandler(logging.NullHandler()) # noqa

from onegov.form.collection import FormCollection
from onegov.form.collection import (
FormCollection,
FormSubmissionCollection,
FormDefinitionCollection
)
from onegov.form.core import Form, with_options
from onegov.form.display import render_field
from onegov.form.models import (
Expand All @@ -15,6 +19,8 @@
__all__ = [
'Form',
'FormCollection',
'FormDefinitionCollection',
'FormSubmissionCollection',
'FormDefinition',
'FormSubmission',
'PendingFormSubmission',
Expand Down
20 changes: 15 additions & 5 deletions onegov/form/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def definitions(self):
def submissions(self):
return FormSubmissionCollection(self.session)

def scoped_submissions(self, name, ensure_existance=True):
if not ensure_existance or self.definitions.by_name(name):
return FormSubmissionCollection(self.session, name)


class FormDefinitionCollection(object):
""" Manages a collection of forms. """
Expand Down Expand Up @@ -79,13 +83,19 @@ def by_name(self, name):
class FormSubmissionCollection(object):
""" Manages a collection of submissions. """

def __init__(self, session):
def __init__(self, session, name=None):
self.session = session
self.name = name

def query(self):
return self.session.query(FormSubmission)
query = self.session.query(FormSubmission)

if self.name is not None:
query = query.filter(FormSubmission.name == self.name)

def add(self, form_name, form, state):
return query

def add(self, name, form, state):
""" Takes a form 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
Expand All @@ -106,7 +116,7 @@ def add(self, form_name, form, state):

submission = (_mapper and _mapper.class_ or FormSubmission)()
submission.id = uuid4()
submission.name = form_name
submission.name = name
submission.state = state

self.update(submission, form)
Expand Down Expand Up @@ -199,7 +209,7 @@ def remove_old_pending_submissions(self, older_than):
query = query.filter(FormSubmission.last_change < older_than)
query.delete('fetch')

def by_form_name(self, name):
def by_name(self, name):
""" Return all submissions for the given form-name. """
return self.query().filter(FormSubmission.name == name).all()

Expand Down

0 comments on commit 2873d20

Please sign in to comment.