예제 #1
0
    def test_should_log_submission(self):
        transport_info = TransportInfo(transport="sms",
                                       source="1234",
                                       destination="5678")
        request = Request(transportInfo=transport_info,
                          message="reg +N buddy +S DOG3 +T dog")

        response = self.sms_player.accept(request)
        submission_log = self.dbm._load_document(response.submission_id,
                                                 SubmissionLogDocument)
        self.assertIsInstance(submission_log, SubmissionLogDocument)
        self.assertEquals(transport_info.transport, submission_log.channel)
        self.assertEquals(transport_info.source, submission_log.source)
        self.assertEquals(transport_info.destination,
                          submission_log.destination)
        self.assertEquals(True, submission_log.status)
        self.assertEquals("reg", submission_log.form_code)
        self.assertEquals({
            'n': 'buddy',
            's': 'DOG3',
            't': 'dog',
            'l': None
        }, submission_log.values)
        self.assertEquals(transport_info.destination,
                          submission_log.destination)
        self.assertEquals(response.datarecord_id,
                          submission_log.data_record_id)
예제 #2
0
 def send_sms(self, text):
     transport_info = TransportInfo(transport="sms",
                                    source="1234",
                                    destination="5678")
     response = self.sms_player.accept(
         Request(transportInfo=transport_info, message=text))
     return response
예제 #3
0
def _validate_post_data(dbm, request):
    form = ReporterRegistrationForm(request.POST)
    message = None
    success = False
    form_errors = []
    form_errors.extend(form.non_field_errors())
    if form.is_valid():
        form_errors = []
        form_data = {
            k: v
            for (k, v) in form.cleaned_data.items() if not is_empty(v)
        }
        try:
            entered_telephone_number = form_data.get("telephone_number")
            tel_number = _get_telephone_number(entered_telephone_number)
            if not helper.unique(dbm, tel_number):
                raise MultipleReportersForANumberException(
                    entered_telephone_number)

            web_player = WebPlayer(dbm, SubmissionHandler(dbm))
            response = web_player.accept(
                Request(message=_get_data(form_data),
                        transportInfo=TransportInfo(transport='web',
                                                    source='web',
                                                    destination='mangrove')))
            message = get_success_msg_for_registration_using(response, "web")
            success = True
        except MangroveException as exception:
            form_errors.append(exception.message)
            success = False
    return form, form_errors, message, success
예제 #4
0
def submit(request):
    dbm = get_database_manager(request)
    post = _get_submission(request.POST)
    success = True
    try:
        web_player = WebPlayer(dbm, SubmissionHandler(dbm))
        message = {
            k: v
            for (k, v) in post.get('message').items() if not is_empty(v)
        }
        if message.get(LOCATION_TYPE_FIELD_CODE) is not None:
            message[LOCATION_TYPE_FIELD_CODE] += COUNTRY
        request = Request(message=message,
                          transportInfo=TransportInfo(
                              transport=post.get('transport'),
                              source=post.get('source'),
                              destination=post.get('destination')))
        response = web_player.accept(request)
        if response.success:
            message = get_success_msg_for_registration_using(response, "web")
        else:
            message = get_submission_error_message_for(response.errors)
        entity_id = response.datarecord_id
    except MangroveException as exception:
        message = get_exception_message_for(exception=exception, channel="web")
        success = False
        entity_id = None
    return HttpResponse(
        json.dumps({
            'success': success,
            'message': message,
            'entity_id': entity_id
        }))
예제 #5
0
 def setUp(self):
     self.dbm = get_db_manager(database='mangrove-test')
     self._create_entities()
     self.player = SMSPlayer(self.dbm, SubmissionHandler(self.dbm))
     self.transport = TransportInfo(transport="sms",
                                    source="1234",
                                    destination="5678")
     initializer.run(self.dbm)
예제 #6
0
    def test_should_not_parse_if_two_question_codes(self):
        transport = TransportInfo(transport="sms",
                                  source="1234",
                                  destination="5678")
        self.request = Request(transportInfo=transport,
                               message="cli001 +na tester1 +na tester2")
        with self.assertRaises(SubmissionParseException):
            self.sms_player.accept(self.request)

        self.assertEqual(0, self.submission_handler_mock.accept.call_count)
예제 #7
0
 def setUp(self):
     loc_tree = Mock()
     loc_tree.get_hierarchy_path.return_value = None
     self.dbm = Mock(spec=DatabaseManager)
     self.submission_handler_mock = Mock(spec=SubmissionHandler)
     self.reporter_patcher = patch(
         'mangrove.transport.player.player.reporter')
     self.reporter_module = self.reporter_patcher.start()
     self._mock_reporter()
     self.transport = TransportInfo(transport="sms",
                                    source="1234",
                                    destination="5678")
     self.request = Request(transportInfo=self.transport,
                            message="FORM_CODE +ID 1 +M hello world")
     self.sms_player = SMSPlayer(self.dbm, self.submission_handler_mock,
                                 loc_tree)
     self.generate_code_patcher = patch(
         "mangrove.transport.player.player.Player._handle_registration_form"
     )
     self.generate_code_patcher.start()
예제 #8
0
def sms(request):
    _message = request.POST["message"]
    _from, _to = _get_from_and_to_numbers(request)
    try:
        dbm = get_db_manager_for(_to)
        sms_player = SMSPlayer(dbm, SubmissionHandler(dbm))
        transportInfo = TransportInfo(transport=SMS,
                                      source=_from,
                                      destination=_to)
        response = sms_player.accept(
            Request(transportInfo=transportInfo, message=_message))
        message = SMSResponse(response).text()

    except (
            SubmissionParseException,
            FormModelDoesNotExistsException,
    ) as exception:
        message = get_exception_message_for(exception=exception, channel=SMS)
        log = DatawinnerLog(message=_message,
                            from_number=_from,
                            to_number=_to,
                            form_code=exception.data[0],
                            error=message)
        log.save()
    except NumberNotRegisteredException as exception:
        message = get_exception_message_for(exception=exception, channel=SMS)
        log = DatawinnerLog(message=_message,
                            from_number=_from,
                            to_number=_to,
                            form_code=None,
                            error=message)
        log.save()
    except MangroveException as exception:
        message = get_exception_message_for(exception=exception, channel=SMS)
    except Exception as exception:
        logger.exception('SMS Processing failure: message')
        message = get_exception_message_for(exception=exception, channel=SMS)

    return HttpResponse(message)
예제 #9
0
 def test_should_throw_error_if_transport_is_none(self):
     with self.assertRaises(AssertionError):
         TransportInfo(transport=None, source="", destination="")
예제 #10
0
 def test_should_throw_error_if_message_is_none(self):
     with self.assertRaises(AssertionError):
         transport = TransportInfo(transport="", source="", destination="")
         Request(transportInfo=transport, message=None)
def load_sms_data_for_cli001(manager):
    FEB = datetime(2011, 02, 28, hour=12, minute=00, second=00, tzinfo=UTC)
    MARCH = datetime(2011, 03, 01, tzinfo=UTC)
    DEC_2010 = datetime(2010,
                        12,
                        28,
                        hour=00,
                        minute=00,
                        second=59,
                        tzinfo=UTC)
    NOV_2010 = datetime(2010,
                        11,
                        26,
                        hour=23,
                        minute=59,
                        second=59,
                        tzinfo=UTC)
    today = datetime.today()
    THIS_MONTH = datetime(today.year, today.month, 5, 12, 45, 58)
    PREV_MONTH = THIS_MONTH - timedelta(days=8)
    tree = get_location_tree()
    sms_player = SMSPlayer(manager,
                           SubmissionHandler(manager),
                           location_tree=tree)
    FROM_NUMBER = '1234567890'
    TO_NUMBER = '261333782943'
    transport = TransportInfo(SMS, FROM_NUMBER, TO_NUMBER)

    message1 = "reg +t  clinic +n  Clinic in Analalava  +l  Analalava  +g  -14.6333  47.7667  +d This is a Clinic in Analalava +m 987654321"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "reg +t  clinic +n  Clinic in Andapa  +l  Andapa  +g  -14.65  49.6167  +d This is a Clinic in Andapa  +m 87654322"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "reg +t  clinic +n  Clinic in Antalaha  +l  Antalaha  +g  -14.8833  50.25  +d This is a Clinic in Antalaha  +m 87654323"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "reg +t  clinic +n  Clinic in ANALAMANGA  +l  ANALAMANGA  +g  -18.8  47.4833  +d This is a Clinic in Antananarivo  +m 87654324"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "reg +t  clinic +n  Clinic in TSIMANARIRAZANA +l  TSIMANARIRAZANA +g  -12.35  49.3  +d This is a Clinic in Diégo–Suarez +m 87654325"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "reg +t  clinic +n  Clinic in Antsirabe  +l  Antsirabe  +g  -19.8167  47.0667  +d This is a Clinic in Antsirabe  +m 87654326"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "reg +t  clinic +n  Clinic in Besalampy  +l  Besalampy  +g  -16.75  44.5  +d This is a Clinic in Besalampy  +m 87654327"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "reg +t  clinic +n  clinique à Farafangana  +l  Farafangana  +g  -22.8  47.8333  +d This is a Clinic in Farafangana  +m 87654328"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "reg +t  clinic +n  Clinic in Fianarantsoa I +l  Fianarantsoa I +g  -21.45  47.1 +d  C'est une clinique à Fianarantsoa +m 87654329"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "reg +t  clinic +n  Clinic in Sainte Marie  +l  Sainte Marie  +g  -17.0833  49.8167  +d This is a Clinic in Île Sainte–Marie  +m 87654330"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "reg +t  clinic +n  Clinic in Mahajanga +l  Mahajanga +g  -15.6667  46.35  +d This is a Clinic in Mahajanga +m 87654331"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))

    datetime_mocker = DateTimeMocker()
    datetime_mocker.set_date_time_now(FEB)
    # Total number of identical records = 3
    message1 = "cli001 +EID cid001 +NA Mr. Tessy +FA 58 +RD 28.02.2011 +BG c +SY ade +GPS 79.2 20.34567"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cid002 +NA Mr. Adam +FA 62 +RD 15.02.2011 +BG a +SY ab +GPS 74.2678 23.3567"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cid003 +NA Ms. Beth +FA 75 +RD 09.02.2011 +BG b +SY bc +GPS 18.245 29.3123"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))

    datetime_mocker.set_date_time_now(MARCH)
    # Total number of identical records = 4
    message1 = "cli001 +EID cid004 +NA Jannita +FA 90 +RD 07.03.2011 +BG b +SY bbe +GPS 45.233 28.3324"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cid005 +NA Aanda +RD 12.03.2011 +BG c +SY bd +GPS 40.2 69.3123"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = 'cli001 +EID cid001 +NA Ianda (",) +FA 34 +RD 27.03.2011 +BG d +SY be +GPS 38.3452 15.3345'
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cid001 +NA ànita +FA 45 +RD 07.03.2011 +BG b +SY bbe +GPS 45.233 28.3324"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cid004 +NA Amanda +RD 12.03.2011 +BG c +SY bd +GPS 40.2 69.3123"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = 'cli001 +EID cid005 +NA Vanda (",) +FA 34 +RD 27.03.2011 +BG d +SY be +GPS 38.3452 15.3345'
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cid003 +NA ànnita +FA 80 +RD 07.03.2011 +BG b +SY bbe +GPS 45.233 28.3324"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cid002 +NA Amanda +RD 12.03.2011 +BG c +SY bd +GPS 40.2 69.3123"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = 'cli001 +EID cid004 +NA Panda (",) +FA 34 +RD 27.03.2011 +BG d +SY be +GPS 38.3452 15.3345'
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cid005 +NA ànnita +FA 50 +RD 07.03.2011 +BG b +SY bbe +GPS 45.233 28.3324"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cid003 +NA Jimanda +RD 12.03.2011 +BG c +SY bd +GPS 40.2 69.3123"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = 'cli001 +EID cli10 +NA Kanda (",) +FA 64 +RD 27.03.2011 +BG d +SY be +GPS 38.3452 15.3345'
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cid004 +NA ànnita +FA 30 +RD 07.03.2011 +BG b +SY bbe +GPS 45.233 28.3324"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cid005 +NA Qamanda +RD 12.03.2011 +BG c +SY bd +GPS 40.2 69.3123"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = 'cli001 +EID cid001 +NA Huanda (*_*) +FA 74 +RD 27.03.2011 +BG d +SY be +GPS 38.3452 15.3345'
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))

    datetime_mocker.set_date_time_now(DEC_2010)
    # Total number of identical records = 4
    message1 = "cli001 +EID cli12 +FA 47 +RD 15.12.2010 +BG d +SY ace +GPS -58.3452 19.3345"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cli11 +NA De'melo +FA 38 +RD 27.12.2010 +BG c +SY ba +GPS 81.672 92.33456"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cli13 +NA Dono`mova +FA 24 +RD 06.12.2010 +BG b +SY cd +GPS 65.23452 -28.3456"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cli15 +NA Aàntra +FA 89 +RD 11.12.2010 +BG a +SY bd +GPS 45.234 89.32345"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))

    datetime_mocker.set_date_time_now(NOV_2010)
    # Total number of identical records = 3
    message1 = "cli001 +EID cli12 +NA ànnita +FA 90 +RD 07.11.2010 +BG b +SY bbe +GPS 45.233 28.3324"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cli14 +NA Amanda +RD 12.11.2010 +BG c +SY bd +GPS 40.2 69.3123"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = 'cli001 +EID cli8 +NA Kanda (",) +FA 34 +RD 27.11.2010 +BG d +SY be +GPS 38.3452 15.3345'
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cli9 +NA ànnita +FA 90 +RD 17.11.2010 +BG b +SY bbe +GPS 45.233 28.3324"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cid007 +NA Amanda +RD 12.11.2010 +BG c +SY bd +GPS 40.2 69.3123"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = 'cli001 +EID cli8 +NA Kanda (",) +FA 34 +RD 27.11.2010 +BG d +SY be +GPS 38.3452 15.3345'
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))

    datetime_mocker.set_date_time_now(PREV_MONTH)
    # Total number of identical records = 4
    message1 = "cli001 +EID cli9 +NA Demelo +FA 38 +RD 17.05.2011 +BG c +SY ba +GPS 19.672 92.33456"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cli10 +NA Zorro +FA 48 +RD 05.05.2011 +BG b +SY cd +GPS 23.23452 -28.3456"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cli11 +NA Aàntra +FA 98 +RD 12.05.2011 +BG a +GPS -45.234 89.32345"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cli12 +NA ànnita +FA 37 +RD 05.05.2011 +BG d +SY cbe +GPS -78.233 -28.3324"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cli9 +NA Demelo +FA 38 +RD 17.05.2011 +BG c +SY ba +GPS 19.672 92.33456"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cli10 +NA Zorro +FA 48 +RD 02.05.2011 +BG b +SY cd +GPS 23.23452 -28.3456"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cli11 +NA Aàntra +FA 95 +RD 12.05.2011 +BG a +GPS -45.234 89.32345"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cli12 +NA ànnita +FA 35 +RD 09.05.2010 +BG d +SY cbe +GPS -78.233 -28.3324"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cli9 +NA Demelo +FA 32 +RD 27.05.2011 +BG c +SY ba +GPS 19.672 92.33456"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cli10 +NA Zorro +FA 43 +RD 05.05.2011 +BG b +SY cd +GPS 23.23452 -28.3456"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cli11 +NA Aàntra +FA 91 +RD 12.05.2011 +BG a +GPS -45.234 89.32345"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cli12 +NA ànnita +FA 45 +RD 15.05.2010 +BG d +SY cbe +GPS -78.233 -28.3324"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))

    datetime_mocker.set_date_time_now(THIS_MONTH)
    # Total number of identical records = 4
    message1 = "cli001 +EID cli13 +NA Dmanda +FA 69 +RD 05.06.2011 +BG c +SY ce +GPS 40.2 69.3123"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cli14 +NA Vamand +FA 36 +RD 03.06.2011 +BG a +SY ace +GPS 58.3452 115.3345"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cli15 +NA M!lo +FA 88 +RD 02.06.2011 +SY ba +GPS 19.672 92.33456"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cli16 +NA K!llo +FA 88 +GPS 19.672 92.33456"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cli13 +NA Dmanda +FA 89 +RD 04.06.2011 +BG c +SY ce +GPS 40.2 69.3123"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cli14 +NA Vamand +FA 56 +RD 01.06.2011 +BG a +SY ace +GPS 58.3452 115.3345"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cli15 +NA M!lo +FA 45 +RD 07.06.2011 +SY ba +GPS 19.672 92.33456"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cli16 +NA K!llo +FA 28 +GPS 19.672 92.33456"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))

    datetime_mocker.end_mock()

    # Total number of identical records = 3
    message1 = "cli001 +EID cli17 +NA Catty +FA 78 +RD 15.06.2011 +BG b +SY dce +GPS 33.23452 -68.3456"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cli18 +NA àntra +FA 28 +RD 12.06.2011 +BG a +SY adb +GPS -45.234 169.32345"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cli9 +NA Tinnita +FA 37 +BG d +SY ace +GPS -78.233 -28.3324"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))

    message1 = "cli001 +EID cli17 +NA Catty +FA 98 +RD 25.06.2011 +BG b +SY dce +GPS 33.23452 -68.3456"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cli18 +NA àntra +FA 58 +RD 22.06.2011 +BG a +SY adb +GPS -45.234 169.32345"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))
    message1 = "cli001 +EID cli9 +NA Tinnita +FA 27 +BG d +SY ace +GPS -78.233 -28.3324"
    response = sms_player.accept(
        Request(transportInfo=transport, message=message1))