diff --git a/apollo/formsframework/forms.py b/apollo/formsframework/forms.py index 76ec8519b..62bdcf4a7 100644 --- a/apollo/formsframework/forms.py +++ b/apollo/formsframework/forms.py @@ -2,24 +2,18 @@ from datetime import datetime from functools import partial from itertools import ifilter -from mongoengine import signals from wtforms import ( - Form, - IntegerField, SelectField, SelectMultipleField, StringField, - validators, widgets -) + Form, IntegerField, SelectField, StringField, validators, widgets) from flask import g from flask.ext.mongoengine.wtf import model_form from flask.ext.wtf import Form as SecureForm from .. import services, models from ..frontend.helpers import DictDiffer -from ..participants.utils import update_participant_completion_rating from .custom_fields import IntegerSplitterField import json import re - -ugly_phone = re.compile('[^\d]*') +ugly_phone = re.compile(r'[^\d]*') def update_submission_version(sender, document, **kwargs): @@ -193,15 +187,7 @@ def save(self): verified=False, last_seen=datetime.utcnow()) participant.update(add_to_set__phones=phone_contact) - with signals.post_save.connected_to( - update_submission_version, - sender=services.submissions.__model__ - ): - submission.save() - - # update completion rating for participant - if submission.form.form_type == 'CHECKLIST': - update_participant_completion_rating(participant) + submission.save(clean=False) except models.Submission.DoesNotExist: pass @@ -261,9 +247,9 @@ def build_questionnaire(form, data=None): form_class = type('QuestionnaireForm', (BaseQuestionnaireForm,), fields) - return form_class(data) + FormForm = model_form( models.Form, SecureForm, only=[ diff --git a/apollo/messaging/helpers.py b/apollo/messaging/helpers.py index 270596c25..a7288fcab 100644 --- a/apollo/messaging/helpers.py +++ b/apollo/messaging/helpers.py @@ -27,7 +27,7 @@ def parse_message(form): if questionnaire.validate(): submission = questionnaire.save() - + # if submission returns empty, then the participant # was not meant to send this text. # TODO: add response for no recorded submission diff --git a/apollo/messaging/views_messaging.py b/apollo/messaging/views_messaging.py index 048a6d1dd..f933fa73a 100644 --- a/apollo/messaging/views_messaging.py +++ b/apollo/messaging/views_messaging.py @@ -5,6 +5,7 @@ from apollo.messaging.forms import KannelForm, TelerivetForm from apollo.messaging.helpers import parse_message from apollo.messaging.utils import parse_text +from apollo.submissions.tasks import update_submission from flask import Blueprint, make_response, request, g, current_app import json import re @@ -41,6 +42,7 @@ def lookup_participant(msg, event=None): def update_datastore(inbound, outbound, submission, had_errors): if submission: + update_submission.delay(str(submission.pk)) participant = submission.contributor else: participant = lookup_participant(inbound) diff --git a/apollo/submissions/tasks.py b/apollo/submissions/tasks.py index 194823c3c..cb6313be1 100644 --- a/apollo/submissions/tasks.py +++ b/apollo/submissions/tasks.py @@ -1,6 +1,10 @@ # -*- coding: utf-8 -*- +from mongoengine import signals + from apollo import models from apollo.factory import create_celery_app +from apollo.formsframework.forms import update_submission_version +from apollo.participants.utils import update_participant_completion_rating celery = create_celery_app() @@ -20,3 +24,23 @@ def init_submissions(deployment_pk, event_pk, form_pk, role_pk, models.Submission.init_submissions(deployment, event, form, role, location_type) + + +@celery.task +def update_submission(submission_pk): + try: + submission = models.Submission.objects.get(pk=submission_pk) + except models.Submission.DoesNotExist: + return + + with signals.post_save.connected_to( + update_submission_version, + models.Submission): + # save with precomputation enabled + submission.save() + + # update completion rating for participant + if submission.form.form_type == 'CHECKLIST': + participant = submission.contributor + if participant: + update_participant_completion_rating(participant)