def test_list_view_returns_readings_within_query_parameters(self):
        now = to_unix(datetime.datetime.now())

        for lat in range(5):
            for lon in range(5):
                for days_delta in range(-2, 3):
                    daterecorded = now + days_delta
                    self.factory(
                        latitude=lat,
                        longitude=lon,
                        daterecorded=daterecorded,
                    ).save()

        response = self.client.get(reverse(self.url_name), {
            'min_latitude': 2.0,
            'max_latitude': 4.0,
            'min_longitude': 2.0,
            'max_longitude': 4.0,
            'start_time': now - 1,
            'end_time': now + 1,
            'limit': 1000,
        })

        data = json.loads(response.content)

        self.assertEquals(len(data), 27)
    def test_list_view_without_limit_defaults_to_global_limit(self):
        now = to_unix(datetime.datetime.now())

        for i in range(11):
            self.factory(latitude=1.0, longitude=1.0, daterecorded=now).save()

        response = self.client.get(reverse(self.url_name), {
            'min_latitude': 1.0,
            'max_latitude': 1.0,
            'min_longitude': 1.0,
            'max_longitude': 1.0,
            'start_time': now,
            'end_time': now,
            'limit': 3,
        })

        data = json.loads(response.content)

        self.assertEquals(len(data), 3)

        response = self.client.get(reverse(self.url_name), {
            'min_latitude': 1.0,
            'max_latitude': 1.0,
            'min_longitude': 1.0,
            'max_longitude': 1.0,
            'start_time': now,
            'end_time': now,
        })

        data = json.loads(response.content)

        self.assertEquals(len(data), 10)
    def test_list_view_returns_at_most_limit_readings(self):
        now = to_unix(datetime.datetime.now())

        for i in range(3):
            self.factory(latitude=1.0, longitude=1.0, daterecorded=now).save()

        response = self.client.get(reverse(self.url_name), {
            'min_latitude': 1.0,
            'max_latitude': 1.0,
            'min_longitude': 1.0,
            'max_longitude': 1.0,
            'start_time': now,
            'end_time': now,
            'limit': 1000,
        })

        data = json.loads(response.content)

        self.assertEquals(len(data), 3)

        response = self.client.get(reverse(self.url_name), {
            'min_latitude': 1.0,
            'max_latitude': 1.0,
            'min_longitude': 1.0,
            'max_longitude': 1.0,
            'start_time': now,
            'end_time': now,
            'limit': 1,
        })

        data = json.loads(response.content)

        self.assertEquals(len(data), 1)
    def changelist_view(self, request, extra_context=None):
        now = datetime.datetime.now()
        current_date = datetime.datetime(now.year, now.month, now.day, now.hour)

        readings_per_hour = []
        active_users_per_hour = []
        for num_hours in range(1, 150):
            start_date = to_unix(current_date - datetime.timedelta(hours=num_hours))
            end_date = to_unix(current_date - datetime.timedelta(hours=(num_hours - 1)))

            cache_key = 'admin:active_users:%s:%s' % (start_date, end_date)
            readings = cache.get(cache_key)

            if not readings:
                readings = Reading.objects.all().filter(daterecorded__gte=start_date, daterecorded__lte=end_date).count()
                cache.set(cache_key, readings, 9999999999)

            readings_per_hour.append([end_date, readings])

            cache_key = 'admin:readings_per_hour:%s:%s' % (start_date, end_date)
            active_users = cache.get(cache_key)

            if not active_users:
                active_users = Reading.objects.all().filter(daterecorded__gte=start_date, daterecorded__lte=end_date).values_list('user_id').distinct().count()
                cache.set(cache_key, active_users, 9999999999)

            active_users_per_hour.append([end_date, active_users])

        context = {
            'readings_data': json.dumps(readings_per_hour),
            'active_user_data': json.dumps(active_users_per_hour),
        }

        if extra_context:
            context.update(extra_context)

        return super(ReadingAdmin, self).changelist_view(request, context)
Beispiel #5
0
    def get_queryset(self):
        parameters = self.unpack_parameters()

        customer = Customer.objects.get(
            api_key=parameters['api_key'])  # TODO: Handle DoesNotExist case
        queryset = super(LoggedLocationListView, self).get_queryset()

        if not parameters['global_data']:
            queryset = queryset.filter(
                latitude__gte=parameters['min_latitude'],
                latitude__lte=parameters['max_latitude'],
                longitude__gte=parameters['min_longitude'],
                longitude__lte=parameters['max_longitude'],
            )

        call_logs = CustomerCallLog.objects.filter(customer=customer)
        if parameters['since_last_call'] and call_logs.exists():
            call_log = call_logs.order_by('-timestamp')[:1].get()
            queryset = queryset.filter(daterecorded__gte=to_unix(
                call_log.timestamp), )
        else:
            queryset = queryset.filter(
                daterecorded__gte=parameters['start_time'],
                daterecorded__lte=parameters['end_time'],
            )

        if customer.customer_type == customer_choices.CUSTOMER_PUBLIC:
            queryset = queryset.filter(sharing=readings_choices.SHARING_PUBLIC)

        elif customer.customer_type == customer_choices.CUSTOMER_RESEARCHER:
            queryset = queryset.filter(sharing__in=[
                readings_choices.SHARING_PUBLIC,
                readings_choices.SHARING_RESEARCHERS_FORECASTERS,
                readings_choices.SHARING_RESEARCHERS,
            ])

        elif customer.customer_type == customer_choices.CUSTOMER_FORECASTER:
            queryset = queryset.filter(sharing__in=[
                readings_choices.SHARING_PUBLIC,
                readings_choices.SHARING_RESEARCHERS_FORECASTERS,
            ])

        return queryset[:parameters['results_limit']]
Beispiel #6
0
    def get_queryset(self):
        parameters = self.unpack_parameters()

        customer = Customer.objects.get(api_key=parameters['api_key'])  # TODO: Handle DoesNotExist case
        queryset = super(LoggedLocationListView, self).get_queryset()

        if not parameters['global_data']:
            queryset = queryset.filter(
                latitude__gte=parameters['min_latitude'],
                latitude__lte=parameters['max_latitude'],
                longitude__gte=parameters['min_longitude'],
                longitude__lte=parameters['max_longitude'],
            )

        call_logs = CustomerCallLog.objects.filter(customer=customer)
        if parameters['since_last_call'] and call_logs.exists():
            call_log = call_logs.order_by('-timestamp')[:1].get()
            queryset = queryset.filter(
                daterecorded__gte=to_unix(call_log.timestamp),
            )
        else:
            queryset = queryset.filter(
                daterecorded__gte=parameters['start_time'],
                daterecorded__lte=parameters['end_time'],
            )

        if customer.customer_type == customer_choices.CUSTOMER_PUBLIC:
            queryset = queryset.filter(sharing=readings_choices.SHARING_PUBLIC)

        elif customer.customer_type == customer_choices.CUSTOMER_RESEARCHER:
            queryset = queryset.filter(sharing__in=[
                readings_choices.SHARING_PUBLIC,
                readings_choices.SHARING_RESEARCHERS_FORECASTERS,
                readings_choices.SHARING_RESEARCHERS,
            ])

        elif customer.customer_type == customer_choices.CUSTOMER_FORECASTER:
            queryset = queryset.filter(sharing__in=[
                readings_choices.SHARING_PUBLIC,
                readings_choices.SHARING_RESEARCHERS_FORECASTERS,
            ])

        return queryset[:parameters['results_limit']]