Example #1
0
    def test_get_disasters_given_from_date_and_to_date(self):
        date_time = datetime.datetime(2014, 9, 17, 16, 0, 49, 807000)
        attributes = dict(name=self.disaster_type,
                          locations=[self.district],
                          description="Big Flood",
                          date=date_time,
                          status="Assessment")
        disaster1 = Disaster(**attributes).save()

        attr2 = attributes.copy()
        attr2["date"] = datetime.datetime(2014, 8, 17, 16, 0, 49, 807000)
        disaster2 = Disaster(**attr2).save()

        location_disasters = Disaster.from_(
            self.district, **{
                'from': '2014-08-17',
                'to': '2014-09-17'
            })
        self.assertEqual(1, location_disasters.count())
        self.assertIn(disaster2, location_disasters)
        self.assertNotIn(disaster1, location_disasters)

        location_disasters = Disaster.from_(self.district, **{
            'from': None,
            'to': None
        })
        self.assertEqual(2, location_disasters.count())
Example #2
0
    def test_create_disaster(self):
        attributes = dict(name=self.disaster_type,
                          locations=[self.district],
                          description="Big Flood",
                          date="2014-12-01",
                          status="Assessment")
        Disaster(**attributes).save()
        disasters = Disaster.objects(name=self.disaster_type,
                                     date="2014-12-01",
                                     status="Assessment")

        self.assertEqual(1, disasters.count())
Example #3
0
    def test_get_disaster_count_given_from_date_and_to_date(self):
        date_time = datetime.datetime(2014, 9, 17, 16, 0, 49, 807000)
        attributes = dict(name=self.disaster_type, locations=[self.district], description="Big Flood",
                          date=date_time, status="Assessment" )
        Disaster(**attributes).save()

        location_disasters = Disaster.count_(**{'from': '2014-08-17', 'to': '2014-10-17'})
        self.assertEqual(1, location_disasters)

        location_disasters = Disaster.count_(**{'from': '2014-11-17', 'to': '2014-12-17'})
        self.assertEqual(0, location_disasters)

        location_disasters = Disaster.count_(**{'from': None, 'to': None})
        self.assertEqual(1, location_disasters)
Example #4
0
    def test_create_disaster(self):
        attributes = dict(name=self.disaster_type, locations=[self.district], description="Big Flood",
                          date="2014-12-01", status="Assessment")
        Disaster(**attributes).save()
        disasters = Disaster.objects(name=self.disaster_type, date="2014-12-01", status="Assessment")

        self.assertEqual(1, disasters.count())
Example #5
0
class DisasterListCreateView(ListCreateAPIView):
    model = Disaster
    serializer_class = DisasterSerializer
    queryset = Disaster.objects()
    permission_classes = [
        Or(build_permission_class('dms.can_manage_disasters'), IsGetRequest)
    ]

    def get_queryset(self):
        query_params = Disaster.map_kwargs_to_db_params(
            self.request.GET.dict())

        location_queried = self.request.GET.get('location', None)
        if not location_queried:
            if self.request.user.has_perm('dms.can_view_disasters'):
                user_profile = UserProfile.objects(
                    user=self.request.user).first()
                user_group = self.request.user.group.name
                if user_profile and user_group in getattr(
                        settings, 'DISTRICT_GROUPS', []):
                    user_locations = get_user_district_locations(
                        self.request.user)
                    query_params.update({'locations__in': user_locations})
                else:
                    if not self.request.user.has_perm(
                            'dms.can_manage_disasters'):
                        user_location = user_profile.location.id
                        query_params.update({'locations__in': [user_location]})

        return Disaster.objects(**query_params)
 def test_should_serialize_location_object(self):
     self.disaster['date'] = '2014-12-01'
     self.serialized_disaster['date'] = '2014-12-01'
     disaster = Disaster(**self.disaster).save()
     serialized_object = DisasterSerializer(disaster)
     self.assertDictContainsSubset(self.serialized_disaster,
                                   serialized_object.data)
     self.assertIsNotNone(serialized_object.data['id'])
Example #7
0
    def test_get_disasters_given_from_date_and_to_date(self):
        date_time = datetime.datetime(2014, 9, 17, 16, 0, 49, 807000)
        attributes = dict(name=self.disaster_type, locations=[self.district], description="Big Flood",
                          date=date_time, status="Assessment")
        disaster1 = Disaster(**attributes).save()

        attr2 = attributes.copy()
        attr2["date"] = datetime.datetime(2014, 8, 17, 16, 0, 49, 807000)
        disaster2 = Disaster(**attr2).save()

        location_disasters = Disaster.from_(self.district, **{'from': '2014-08-17', 'to': '2014-09-17'})
        self.assertEqual(1, location_disasters.count())
        self.assertIn(disaster2, location_disasters)
        self.assertNotIn(disaster1, location_disasters)

        location_disasters = Disaster.from_(self.district, **{'from': None, 'to': None})
        self.assertEqual(2, location_disasters.count())
Example #8
0
 def _filter_disaster_type(cls, _queryset, kwargs):
     if kwargs.get('disaster_type', None):
         from dms.models.disaster import Disaster
         disasters = Disaster.objects(name=kwargs['disaster_type'])
         if disasters:
             return _queryset.filter(disaster__in=disasters)
         return _queryset.none()
     return _queryset
Example #9
0
    def get_queryset(self):
        query_params = Disaster.map_kwargs_to_db_params(self.request.GET.dict())

        location_queried = self.request.GET.get("location", None)
        if not location_queried:
            if self.request.user.has_perm("dms.can_view_disasters"):
                user_profile = UserProfile.objects(user=self.request.user).first()
                user_group = self.request.user.group.name
                if user_profile and user_group in getattr(settings, "DISTRICT_GROUPS", []):
                    user_locations = get_user_district_locations(self.request.user)
                    query_params.update({"locations__in": user_locations})
                else:
                    if not self.request.user.has_perm("dms.can_manage_disasters"):
                        user_location = user_profile.location.id
                        query_params.update({"locations__in": [user_location]})

        return Disaster.objects(**query_params)
Example #10
0
class DisasterView(MongoRetrieveUpdateView):
    model = Disaster
    serializer_class = DisasterSerializer
    queryset = Disaster.objects()
    permission_classes = [
        Or(build_permission_class('dms.can_manage_disasters'))
    ]

    def post(self, request, *args, **kwargs):
        return self.patch(request, *args, **kwargs)
Example #11
0
    def test_get_disaster_from_a_location(self):
        attributes = dict(name=self.disaster_type,
                          locations=[self.district],
                          description="Big Flood",
                          date="2014-12-01",
                          status="Assessment")
        disaster1 = Disaster(**attributes).save()

        attr2 = attributes.copy()
        attr2["locations"] = [
            Location(**dict(name='Some other location',
                            type='district',
                            parent=None)).save()
        ]
        disaster2 = Disaster(**attr2).save()

        location_disasters = Disaster.from_(self.district)
        self.assertEqual(1, location_disasters.count())
        self.assertIn(disaster1, location_disasters)
        self.assertNotIn(disaster2, location_disasters)
Example #12
0
    def test_get_messages_from_children_are_also_added(self):
        attributes = dict(name=self.disaster_type,
                          locations=[self.district],
                          description="Big Flood",
                          date="2014-12-01",
                          status="Assessment")
        disaster1 = Disaster(**attributes).save()

        attr2 = attributes.copy()
        attr2["locations"] = [
            Location(**dict(name='Kampala subcounty',
                            type='subcounty',
                            parent=self.district)).save()
        ]
        disaster2 = Disaster(**attr2).save()

        location_disasters = Disaster.from_(self.district)

        self.assertEqual(2, location_disasters.count())
        self.assertIn(disaster1, location_disasters)
        self.assertIn(disaster2, location_disasters)
Example #13
0
    def test_get_disaster_from_a_location(self):
        attributes = dict(name=self.disaster_type, locations=[self.district], description="Big Flood",
                          date="2014-12-01", status="Assessment")
        disaster1 = Disaster(**attributes).save()

        attr2 = attributes.copy()
        attr2["locations"] = [Location(**dict(name='Some other location', type='district', parent=None)).save()]
        disaster2 = Disaster(**attr2).save()

        location_disasters = Disaster.from_(self.district)
        self.assertEqual(1, location_disasters.count())
        self.assertIn(disaster1, location_disasters)
        self.assertNotIn(disaster2, location_disasters)
Example #14
0
    def get_queryset(self):
        query_params = Disaster.map_kwargs_to_db_params(
            self.request.GET.dict())

        location_queried = self.request.GET.get('location', None)
        if not location_queried:
            if self.request.user.has_perm('dms.can_view_disasters'):
                user_profile = UserProfile.objects(
                    user=self.request.user).first()
                user_group = self.request.user.group.name
                if user_profile and user_group in getattr(
                        settings, 'DISTRICT_GROUPS', []):
                    user_locations = get_user_district_locations(
                        self.request.user)
                    query_params.update({'locations__in': user_locations})
                else:
                    if not self.request.user.has_perm(
                            'dms.can_manage_disasters'):
                        user_location = user_profile.location.id
                        query_params.update({'locations__in': [user_location]})

        return Disaster.objects(**query_params)
Example #15
0
    def test_get_disaster_count_given_from_date_and_to_date(self):
        date_time = datetime.datetime(2014, 9, 17, 16, 0, 49, 807000)
        attributes = dict(name=self.disaster_type,
                          locations=[self.district],
                          description="Big Flood",
                          date=date_time,
                          status="Assessment")
        Disaster(**attributes).save()

        location_disasters = Disaster.count_(**{
            'from': '2014-08-17',
            'to': '2014-10-17'
        })
        self.assertEqual(1, location_disasters)

        location_disasters = Disaster.count_(**{
            'from': '2014-11-17',
            'to': '2014-12-17'
        })
        self.assertEqual(0, location_disasters)

        location_disasters = Disaster.count_(**{'from': None, 'to': None})
        self.assertEqual(1, location_disasters)
Example #16
0
    def test_get_messages_from_children_are_also_added(self):
        attributes = dict(name=self.disaster_type, locations=[self.district], description="Big Flood",
                          date="2014-12-01",
                          status="Assessment")
        disaster1 = Disaster(**attributes).save()

        attr2 = attributes.copy()
        attr2["locations"] = [Location(**dict(name='Kampala subcounty', type='subcounty', parent=self.district)).save()]
        disaster2 = Disaster(**attr2).save()

        location_disasters = Disaster.from_(self.district)

        self.assertEqual(2, location_disasters.count())
        self.assertIn(disaster1, location_disasters)
        self.assertIn(disaster2, location_disasters)
Example #17
0
class CSVDisasterView(ListCreateAPIView):
    model = Disaster
    serializer_class = CSVDisasterSerializer
    queryset = Disaster.objects()
    permission_classes = [
        Or(build_permission_class('dms.can_manage_disasters'), IsGetRequest)
    ]
    renderer_classes = [
        CSVRenderer,
    ] + api_settings.DEFAULT_RENDERER_CLASSES

    def get_queryset(self):
        params = self._filter_params(self.request)
        queryset = Disaster.objects.filter(**params)
        return queryset.order_by('-created_at')

    def _filter_params(self, req):
        start_date = req.GET.get('from')
        end_date = req.GET.get('to')
        status = req.GET.get('status')
        params = {}

        if start_date:
            if not self._undefined(start_date):
                params.update({'date__gte': start_date})

        if end_date:
            if not self._undefined(end_date):
                params.update({'date__lte': end_date})

        if status:
            if not self._undefined(status):
                params.update({'status': status})

        return params

    def _undefined(self, strValue):
        return strValue == u'undefined'