def filter_queryset(self, request, queryset, view): if not hasattr(view, 'date_range_filter_fields'): return queryset try: fields = dict(view.date_range_filter_fields) except ValueError: msg = "View {} datetime_filter_fields set is error".format( view.name) logging.error(msg) return queryset kwargs = {} for attr, date_range_keyword in fields.items(): if len(date_range_keyword) != 2: continue for i, v in enumerate(date_range_keyword): value = request.query_params.get(v) if not value: continue try: field = DateTimeField() value = field.to_internal_value(value) if i == 0: lookup = "__gte" else: lookup = "__lte" kwargs[attr + lookup] = value except ValidationError as e: print(e) continue if kwargs: queryset = queryset.filter(**kwargs) return queryset
def filter_queryset(self, request, queryset, view): fields = self._get_date_range_filter_fields(view) kwargs = {} for attr, date_range_keyword in fields.items(): if len(date_range_keyword) != 2: continue for i, v in enumerate(date_range_keyword): value = request.query_params.get(v) if not value: continue try: field = DateTimeField() value = field.to_internal_value(value) if i == 0: lookup = "__gte" else: lookup = "__lte" kwargs[attr + lookup] = value except ValidationError as e: print(e) continue if kwargs: queryset = queryset.filter(**kwargs) return queryset
def log_model(self): start_ts = self.request.GET.get('start_ts') or '' try: f = DateTimeField() distr = f.to_internal_value(start_ts).strftime('%Y%m%d') except Exception: distr = None return create_index_model(self.index, distr=distr)
def log_model(self): start_ts = self.request.GET.get("start_ts") or "" try: f = DateTimeField() distr = f.to_internal_value(start_ts).strftime("%Y%m%d") except Exception: distr = None return create_index_model(self.index, distr=distr)
def django_time_format(dtstr): """Convert a datetime to string according to the API settings""" from rest_framework.fields import DateTimeField field = DateTimeField() return field.to_internal_value(dtstr)