def test_should_retrieve_message_stats_in_subcounties_when_district_name_supplied( self): RapidProMessage(**self.message).save() bugolobi_name = 'Bugolobi' bugolobi = Location(**dict( name=bugolobi_name, parent=self.kampala, type='subcounty')).save() text = "NECOC.%s. flood" % bugolobi_name message_bugolobi = dict(phone_no='123444', text=text, received_at=self.date_time, relayer_id=234, run_id=23243) RapidProMessage(**message_bugolobi).save() Disaster(**self.disaster_attr).save() disaster_attr_bugolobi = self.disaster_attr.copy() disaster_attr_bugolobi["locations"] = [bugolobi] Disaster(**disaster_attr_bugolobi).save() multi_location_stats_service = MultiLocationStatsService( self.kampala.name) stats = multi_location_stats_service.stats() self.assertEqual(1, len(stats)) self.assertEqual(1, stats['Bugolobi'].messages.count) self.assertEqual(50, stats['Bugolobi'].messages.percentage) self.assertEqual(1, stats['Bugolobi'].disasters.count) self.assertEqual(50, stats['Bugolobi'].disasters.percentage)
def _non_location_queried_messages(self): fields = RapidProMessage.get_fields() converted_params = self._get_param_conversion(fields) query_params = { converted_params[key]: value or None for key, value in self.request.GET.items() if key in converted_params } return RapidProMessage.objects(**query_params)
def test_should_retrieve_messages_percentage_in_a_location(self): RapidProMessage(**self.message).save() message1 = self.message.copy() message1["text"] = "some message that is not coming from Kampala" RapidProMessage(**message1).save() location_stats_service = LocationStatsService(location=self.district) stats = location_stats_service.aggregate_stats() message_stats = stats.messages self.assertEqual(1, message_stats.count) self.assertEqual(50, message_stats.percentage)
def test_should_retrieve_message_count_in_a_location(self): RapidProMessage(**self.message).save() message1 = self.message.copy() message1["phone_number"] = "12345" RapidProMessage(**message1).save() location_stats_service = LocationStatsService(location=self.district) stats = location_stats_service.aggregate_stats() message_stats = stats.messages self.assertEqual(2, message_stats.count) self.assertEqual(100, message_stats.percentage)
def test_should_retrieve_message_stats_in_all_locations(self): RapidProMessage(**self.message).save() RapidProMessage(**self.message_bukoto).save() Disaster(**self.disaster_attr).save() Disaster(**self.disaster_attr_bukoto).save() multi_location_stats_service = MultiLocationStatsService(location=None) stats = multi_location_stats_service.stats() self.assertEqual(2, len(stats)) self.assertEqual(1, stats['Kampala'].messages.count) self.assertEqual(50, stats['Kampala'].messages.percentage) self.assertEqual(1, stats['Kampala'].disasters.count) self.assertEqual(50, stats['Kampala'].disasters.percentage) self.assertEqual(1, stats['Bukoto'].messages.count) self.assertEqual(50, stats['Bukoto'].messages.percentage) self.assertEqual(1, stats['Bukoto'].disasters.count) self.assertEqual(50, stats['Bukoto'].disasters.percentage)
def get_queryset(self): queryset = self._non_location_queried_messages() location_queried = self.request.GET.get('location', None) if not location_queried: if self.request.user.has_perm('dms.can_view_messages') and \ not self.request.user.has_perm('dms.can_manage_messages'): user_profile = UserProfile.objects(user=self.request.user).first() if user_profile: location_queried = user_profile.location.id if location_queried: location = Location.objects(id=location_queried).first() queryset = RapidProMessage.from_(location, _queryset=queryset) disaster_type = self.request.GET.get('disaster_type', None) if disaster_type: queryset = self.query_by_disaster_type(disaster_type, queryset) return queryset.order_by('-received_at')
def get_queryset(self): queryset = self._non_location_queried_messages() location_queried = self.request.GET.get('location', None) if not location_queried: if self.request.user.has_perm('dms.can_view_messages') and \ not self.request.user.has_perm('dms.can_manage_messages'): user_profile = UserProfile.objects( user=self.request.user).first() if user_profile: location_queried = user_profile.location.id if location_queried: location = Location.objects(id=location_queried).first() queryset = RapidProMessage.from_(location, _queryset=queryset) disaster_type = self.request.GET.get('disaster_type', None) if disaster_type: queryset = self.query_by_disaster_type(disaster_type, queryset) return queryset.order_by('-received_at')
def _non_location_queried_messages(self): fields = RapidProMessage.get_fields() converted_params = self._get_param_conversion(fields) query_params = {converted_params[key]: value or None for key, value in self.request.GET.items() if key in converted_params} return RapidProMessage.objects(**query_params)