Exemplo n.º 1
0
def load_spray_data():
    """Loads up test spray data submissions into the database."""
    path = os.path.join(FIXTURES_DIR, "spray_data.json")
    with codecs.open(path, encoding="utf-8") as spray_data_file:
        data = json.load(spray_data_file)
        for row in data:
            add_spray_data(row)
Exemplo n.º 2
0
 def test_add_spray_data_with_exception(self):  # pylint: disable=C0103
     """
     test that raises Validation error in case no data is passed
     """
     data = {}
     with self.assertRaises(ValidationError):
         add_spray_data(data)
Exemplo n.º 3
0
 def test_add_spray_data(self, mock):
     """
     test that add spray daya works as expected
     """
     count = SprayDay.objects.count()
     add_spray_data(SUBMISSION_DATA[0])
     self.assertTrue(SprayDay.objects.count() > count)
     self.assertTrue(mock.called)
     _args, kwargs = mock.call_args_list[0]
     self.assertEqual(kwargs["sprayday"], SprayDay.objects.first())
     self.assertEqual(kwargs["data"], SUBMISSION_DATA[0])
Exemplo n.º 4
0
    def _add_sprayday(self, day, geom, submission_id):
        choice = random.choice(self.yes_no_list)

        if choice == 'yes':
            data = self._get_submission_data_yes()
        else:
            data = self._get_submission_data_no()
            data['unsprayed/reason'] = random.choice(['1', '2', '3', '4', '5'])

        geolocation = "%s %s" % (geom.x, geom.y)
        data['structure_gps'] = geolocation
        data['date'] = "%s" % day
        data['_id'] = submission_id
        data['sprayed/was_sprayed'] = choice
        add_spray_data(data)
Exemplo n.º 5
0
    def create(self, request, *args, **kwargs):
        has_id = request.data.get(DATA_ID_FIELD)
        spray_date = request.data.get(DATE_FIELD)

        if not has_id or not spray_date:
            data = {
                "error":
                _("Not a valid submission: _id - %s, date - %s" %
                  (has_id, spray_date))
            }
            status_code = status.HTTP_400_BAD_REQUEST
        else:
            request_data = request.data
            if isinstance(request.data, QueryDict):
                request_data = request.data.dict()

            try:
                sprayday = add_spray_data(request_data)
            except ValidationError as error:
                data = {"error": "%s" % error}
                status_code = status.HTTP_400_BAD_REQUEST
            except IntegrityError as error:
                data = {"error": "%s" % error}
                status_code = status.HTTP_400_BAD_REQUEST
            else:
                data = {
                    "success":
                    _("Successfully imported submission with"
                      " submission id %(submission_id)s." %
                      {"submission_id": has_id})
                }
                status_code = status.HTTP_201_CREATED
                delete_cached_target_area_keys(sprayday)

        return Response(data, status=status_code)
Exemplo n.º 6
0
    def handle(self, *args, **options):
        formid = int(options["formid"]) if "formid" in options else FORMID

        if formid != 0:
            latest = (SprayDay.objects.filter().order_by(
                "-submission_id").values_list("submission_id",
                                              flat=True).first())
            data = fetch_form_data(formid, latest)
            if data is not None and isinstance(data, list):
                for rec in data:
                    if SprayDay.objects.filter(
                            submission_id=rec.get("_id")).count():
                        continue
                    try:
                        add_spray_data(rec)
                    except IntegrityError:
                        continue
Exemplo n.º 7
0
 def test_link_spraypoint_with_osm(self, mock):
     """
     Test that we can successfully link spraypoint wiht osm
     """
     self._load_fixtures()
     mock.return_value = OSMXML.strip()
     spray_day = add_spray_data(SUBMISSION_DATA)
     link_spraypoint_with_osm(spray_day.pk)
     spray_day = SprayDay.objects.get(pk=spray_day.pk)
     self.assertTrue(spray_day.location is not None)
Exemplo n.º 8
0
 def test_add_spray_data_by_osmid(self):
     """Test spray data is linked by osmid"""
     data_setup()
     path = os.path.join(FIXTURES_DIR, "spray_data.json")
     with codecs.open(path, encoding="utf-8") as spray_data_file:
         data = json.load(spray_data_file)[0]
         spray = add_spray_data(data)
         self.assertTrue(spray.location is not None)
         self.assertTrue(spray.household is not None)
         self.assertTrue(spray.household.visited)
    def handle(self, *args, **options):
        formid = int(options['formid']) if 'formid' in options else FORMID

        if formid != 0:
            data = fetch_form_data(formid, edited_only=True)
            if data is not None and isinstance(data, list):
                for rec in data:
                    try:
                        sprayday = SprayDay.objects.get(
                            submission_id=rec.get('_id')
                        )
                    except SprayDay.DoesNotExist:
                        pass
                    else:
                        sprayday.delete()
                        try:
                            add_spray_data(rec)
                        except IntegrityError:
                            continue
Exemplo n.º 10
0
def update_edited_records():
    """
    Update edited records.
    """
    count = 0
    if FORM_ID:
        data = fetch_form_data(FORM_ID, dataids_only=True, edited_only=True)
        if not data:
            return count

        pks = [i["_id"] for i in data]
        edited_submissions = SprayDay.objects.filter(submission_id__in=pks)
        for rec in edited_submissions:
            data = fetch_form_data(FORM_ID, dataid=rec.submission_id)
            if data:
                from mspray.apps.main.utils import add_spray_data

                add_spray_data(data)
                count += 1

    return count
Exemplo n.º 11
0
 def test_add_spray_data_with_osm(self, mock, _mock2):
     """
     Test that add_spray_data createsa SprayDay object and that if it has
     OSM data it runs run_tasks_after_spray_data
     """
     self._load_fixtures()
     # get submission data that has OSM info
     spray = SprayDay.objects.filter(
         data__has_key=settings.MSPRAY_OSM_PRESENCE_FIELD).first()
     data = spray.data
     # change the _id field so that we can reuse this data
     data[DATA_ID_FIELD] = 111
     count = SprayDay.objects.count()
     spray = add_spray_data(data)
     self.assertTrue(SprayDay.objects.count() > count)
     self.assertTrue(mock.called)
     args, _kwargs = mock.call_args_list[0]
     self.assertEqual(args[0], spray)
     self.assertIsNotNone(spray.location)
     self.assertIsNotNone(spray.rhc)
     self.assertIsNotNone(spray.district)
Exemplo n.º 12
0
    def test_add_mda_data(self):
        """Test processing MDA status submissions."""
        data_setup()
        data = get_mda_data()
        self.assertTrue(len(data) > 4)

        # noteligible
        self.assertEqual(SprayDay.objects.count(), 0)
        sprayday = add_spray_data(data[0])
        self.assertEqual(SprayDay.objects.count(), 1)
        self.assertIsInstance(sprayday, SprayDay)
        self.assertIsNotNone(sprayday.household)
        self.assertIsNotNone(sprayday.location)
        self.assertEqual(sprayday.location.name, "Akros_2")
        self.assertFalse(sprayday.sprayable)
        self.assertFalse(sprayday.was_sprayed)
        self.assertEqual(sprayday.data["values_from_omk/mda_status"],
                         "noteligible")
        self.assertEqual(sprayday.data.get("_population_eligible"), 0)

        sprayday = add_spray_data(data[1])
        self.assertEqual(SprayDay.objects.count(), 2)
        self.assertIsInstance(sprayday, SprayDay)
        self.assertIsNotNone(sprayday.household)
        self.assertTrue(sprayday.household.sprayable)
        self.assertIsNotNone(sprayday.location)
        self.assertEqual(sprayday.location.name, "Akros_2")
        self.assertTrue(sprayday.sprayable)
        self.assertTrue(sprayday.was_sprayed)
        self.assertEqual(sprayday.data["values_from_omk/mda_status"],
                         "all_received")
        self.assertEqual(sprayday.data.get("_population_eligible"), 1)
        self.assertEqual(sprayday.data.get("_population_treatment"), 1)

        sprayday = add_spray_data(data[2])
        self.assertEqual(SprayDay.objects.count(), 3)
        self.assertIsInstance(sprayday, SprayDay)
        self.assertIsNotNone(sprayday.household)
        self.assertIsNotNone(sprayday.location)
        self.assertEqual(sprayday.location.name, "Akros_2")
        self.assertTrue(sprayday.sprayable)
        self.assertTrue(sprayday.was_sprayed)
        self.assertEqual(sprayday.data["values_from_omk/mda_status"],
                         "some_received")
        self.assertEqual(sprayday.data.get("_population_eligible"), 0)
        self.assertEqual(sprayday.data.get("_population_treatment"), 0)

        sprayday = add_spray_data(data[3])
        self.assertEqual(SprayDay.objects.count(), 4)
        self.assertIsInstance(sprayday, SprayDay)
        self.assertIsNotNone(sprayday.household)
        self.assertIsNotNone(sprayday.location)
        self.assertEqual(sprayday.location.name, "Akros_2")
        self.assertTrue(sprayday.sprayable)
        self.assertFalse(sprayday.was_sprayed)
        self.assertEqual(sprayday.data["values_from_omk/mda_status"],
                         "none_received")
        self.assertEqual(sprayday.data.get("_population_eligible"), 0)
        self.assertEqual(sprayday.data.get("_population_treatment"), 0)

        sprayday = add_spray_data(data[5])
        self.assertEqual(SprayDay.objects.count(), 5)
        self.assertIsInstance(sprayday, SprayDay)
        self.assertIsNone(sprayday.household)
        self.assertIsNotNone(sprayday.location)
        self.assertEqual(sprayday.location.name, "Akros_2")
        self.assertTrue(sprayday.sprayable)
        self.assertTrue(sprayday.was_sprayed)
        self.assertEqual(sprayday.data["values_from_omk/mda_status"],
                         "some_received")
        self.assertEqual(sprayday.data.get("_population_eligible"), 0)
        self.assertEqual(sprayday.data.get("_population_treatment"), 0)
Exemplo n.º 13
0
def load_mda_data():
    """Loads up test spray data submissions into the database."""
    data = get_mda_data()
    for row in data:
        add_spray_data(row)