Skip to content

Commit

Permalink
Feature/submission background processing (#153)
Browse files Browse the repository at this point in the history
* feat: put submission updates on background queue

* fix: change task invocation arguments
  • Loading branch information
dodumosu authored and takinbo committed Feb 9, 2019
1 parent 42716ac commit a25717c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
22 changes: 4 additions & 18 deletions apollo/formsframework/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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=[
Expand Down
2 changes: 1 addition & 1 deletion apollo/messaging/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions apollo/messaging/views_messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions apollo/submissions/tasks.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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)

0 comments on commit a25717c

Please sign in to comment.