def test_end_date_but_no_start_date_fails(self, mock_timezone, start_date, end_date): mock_timezone.return_value = datetime(2019, 1, 4, tzinfo=timezone.utc) try: parse_datetimes({'end_datetime': end_date}) assert False except ValidationError: assert True
def test_time_used_for_specifc_date_fails(self, mock_timezone, start_date, end_date): mock_timezone.return_value = datetime(2019, 1, 4, tzinfo=timezone.utc) try: parse_datetimes({'on_date': '{}T01:01:01'.format(start_date)}) assert False except ValidationError: assert True
def test_time_in_one_but_not_the_other_fails(self, mock_timezone, start_date, end_date): mock_timezone.return_value = datetime(2019, 1, 4, tzinfo=timezone.utc) try: parse_datetimes({ 'end_datetime': end_date, 'start_datetime': '{}T01:01:01'.format(start_date), }) assert False except ValidationError: assert True
def get(self, *args, **kwargs): query_params = getattr(self.request, 'query_params', self.request.GET) interval = query_params.get('interval', 'day') start_datetime, end_datetime = parse_datetimes(query_params) search = self.metric.search(after=start_datetime) search = search.filter('range', timestamp={ 'gte': start_datetime, 'lt': end_datetime }) search.aggs.bucket('dates', 'date_histogram', field='timestamp', interval=interval) \ .bucket('preprints', 'terms', field='preprint_id') \ .metric('total', 'sum', field='count') search = self.add_search(search, query_params, **kwargs) response = self.execute_search(search) resp_dict = self.format_response(response, query_params) return JsonResponse(resp_dict)