Ejemplo n.º 1
0
def get_custom_response_message(sender, xform, **kwargs):
    """
    This signal sends a custom response to xform submissions. 
    If the domain has one.
    """
    if xform.metadata and xform.metadata.userID:
        userID = xform.metadata.userID
        xmlns = xform.form.get('@xmlns')
        domain = xform.domain

        try:
            app = get_app(domain, xform.app_id)
        except Exception:
            app = Application.get_by_xmlns(domain, xmlns)

        if app and hasattr(app, 'langs'):
            try:
                lang = xform.openrosa_headers[OPENROSA_ACCEPT_LANGUAGE]
            except (AttributeError, KeyError, TypeError):
                lang = "default"
            if lang == "default":
                lang = app.build_langs[0] if app.build_langs else None
            message = app.success_message.get(lang)
            if message:
                success_message = SuccessMessage(
                    message, userID, domain=domain,
                    tz=timedelta(hours=0)).render()
                return ReceiverResult(
                    xml.get_simple_response_xml(
                        success_message, nature=ResponseNature.SUBMIT_SUCCESS),
                    Certainty.STRONG)
Ejemplo n.º 2
0
def get_custom_response_message(sender, xform, **kwargs):
    """
    This signal sends a custom response to xform submissions. 
    If the domain has one.
    """
    if xform.metadata and xform.metadata.userID:
        userID = xform.metadata.userID
        xmlns = xform.form.get('@xmlns')
        domain = xform.domain

        try:
            app = get_app(domain, xform.app_id)
        except Exception:
            app = Application.get_by_xmlns(domain, xmlns)

        if app and hasattr(app, 'langs'):
            try:
                lang = xform.openrosa_headers[OPENROSA_ACCEPT_LANGUAGE]
            except (AttributeError, KeyError, TypeError):
                lang = "default"
            if lang == "default":
                lang = app.build_langs[0] if app.build_langs else None
            message = app.success_message.get(lang)
            if message:
                success_message = SuccessMessage(message, userID, domain=domain, tz=timedelta(hours=0)).render()
                return ReceiverResult(xml.get_simple_response_xml(
                    success_message, nature=ResponseNature.SUBMIT_SUCCESS),
                    Certainty.STRONG)
Ejemplo n.º 3
0
 def test_hours(self):
     self.num_forms_today = 0
     self.num_forms_this_week = 0
     now = datetime.utcnow()
     tznow = now + self.tz
     week_start = tznow - timedelta(days=tznow.weekday())
     week_start = datetime(week_start.year, week_start.month,
                           week_start.day) - self.tz
     day_start = datetime(tznow.year, tznow.month, tznow.day) - self.tz
     spacing = 6
     for h in xrange((24 / spacing) * 8):
         time = now - timedelta(hours=spacing * h)
         response = self.fake_form_submission(time=time)
         if time > week_start:
             self.num_forms_this_week += 1
         if time > day_start:
             self.num_forms_today += 1
         self.assertEqual(
             response.content,
             get_simple_response_xml((
                 "Thanks {self.first_name} ({self.first_name} {self.last_name})! "
                 "You have submitted {self.num_forms_today} forms today "
                 "and {self.num_forms_this_week} forms since Monday."
             ).format(self=self),
                                     nature=ResponseNature.SUBMIT_SUCCESS))
     self.assertEqual(
         self.sm.render(),
         ("Thanks {self.first_name} ({self.first_name} {self.last_name})! "
          "You have submitted {self.num_forms_today} forms today "
          "and {self.num_forms_this_week} forms since Monday.").format(
              self=self))
 def test_hours(self):
     self.num_forms_today = 0
     self.num_forms_this_week = 0
     now = datetime.utcnow()
     tznow = now + self.tz
     week_start = tznow - timedelta(days=tznow.weekday())
     week_start = datetime(week_start.year, week_start.month, week_start.day) - self.tz
     day_start = datetime(tznow.year, tznow.month, tznow.day) - self.tz
     spacing = 6
     for h in xrange((24/spacing)*8):
         time = now-timedelta(hours=spacing*h)
         response = self.fake_form_submission(time=time)
         if time > week_start:
             self.num_forms_this_week += 1
         if time > day_start:
             self.num_forms_today += 1
         self.assertEqual(
             response.content,
             get_simple_response_xml((
                 "Thanks {self.first_name} ({self.first_name} {self.last_name})! "
                 "You have submitted {self.num_forms_today} forms today "
                 "and {self.num_forms_this_week} forms since Monday."
             ).format(self=self), nature=ResponseNature.SUBMIT_SUCCESS)
         )
     self.assertEqual(
         self.sm.render(),
         ("Thanks {self.first_name} ({self.first_name} {self.last_name})! "
         "You have submitted {self.num_forms_today} forms today "
         "and {self.num_forms_this_week} forms since Monday.").format(self=self)
     )
Ejemplo n.º 5
0
 def get_response(self):
     try:
         return HttpResponse(self.get_payload(), mimetype="text/xml")
     except RestoreException, e:
         logging.exception(
             "%s error during restore submitted by %s: %s" % (type(e).__name__, self.user.username, str(e))
         )
         response = get_simple_response_xml(e.message, ResponseNature.OTA_RESTORE_ERROR)
         return HttpResponse(response, mimetype="text/xml", status=412)  # precondition failed
Ejemplo n.º 6
0
 def get_response(self):
     try:
         return HttpResponse(self.get_payload(), mimetype="text/xml")
     except RestoreException, e:
         logging.exception("%s error during restore submitted by %s: %s" %
                           (type(e).__name__, self.user.username, str(e)))
         response = get_simple_response_xml(
             e.message, ResponseNature.OTA_RESTORE_ERROR)
         return HttpResponse(response, mimetype="text/xml",
                             status=412)  # precondition failed
    def setUp(self):
        create_domain(self.domain)
        couch_user = CommCareUser.create(self.domain, self.username, self.password)
        userID = couch_user.user_id
        couch_user.first_name = self.first_name
        couch_user.last_name = self.last_name
        couch_user.save()
        self.sm = SuccessMessage(self.message, userID, tz=self.tz)

        c = Client()

        app = Application.new_app(self.domain, "Test App", application_version=APP_V1)
        app.add_module(Module.new_module("Test Module", "en"))
        form = app.new_form(0, "Test Form", "en")
        form.xmlns = self.xmlns
        app.success_message = {"en": self.message}
        app.save()
        # hack: prime the view once so the success message takes even though we use stale queries in submissions
        Application.get_db().view('exports_forms/by_xmlns', limit=1).one()
        def fake_form_submission(userID=userID, username=self.username, xmlns=self.xmlns, time=None):
            submission = submission_template % {
                "userID": userID,
                "username": username,
                "xmlns": xmlns
            }
            f = StringIO(submission.encode('utf-8'))
            f.name = "tempfile.xml"
            kwargs = dict(HTTP_X_SUBMIT_TIME=json_format_datetime(time)) if time else {}
            response = c.post("/a/{self.domain}/receiver/".format(self=self), {
                'xml_submission_file': f,
            }, **kwargs)
            return response

        self.num_forms_today = 0
        self.num_forms_this_week = 0
        now = datetime.utcnow()
        tznow = now + self.tz
        week_start = tznow - timedelta(days=tznow.weekday())
        week_start = datetime(week_start.year, week_start.month, week_start.day) - self.tz
        day_start = datetime(tznow.year, tznow.month, tznow.day) - self.tz
        spacing = 6
        for h in xrange((24/spacing)*8):
            time = now-timedelta(hours=spacing*h)
            response = fake_form_submission(time=time)
            if time > week_start:
                self.num_forms_this_week += 1
            if time > day_start:
                self.num_forms_today += 1
            self.assertEqual(
                response.content,
                get_simple_response_xml(("Thanks {self.first_name} ({self.first_name} {self.last_name})! "
                "You have submitted {self.num_forms_today} forms today "
                "and {self.num_forms_this_week} forms since Monday.").format(self=self),
                    nature=ResponseNature.SUBMIT_SUCCESS)
            )