Skip to content

Commit

Permalink
feat: add data source filter to date period list
Browse files Browse the repository at this point in the history
  • Loading branch information
danipran committed Apr 5, 2024
1 parent 44059c2 commit 91b4910
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions hours/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def filter(self, qs, value):


class DatePeriodFilter(filters.FilterSet):
data_source = filters.CharFilter(field_name="origins__data_source")
resource = filters.CharFilter(method="resource_filter")
start_date = MaybeRelativeNullableDateFilter()
end_date = MaybeRelativeNullableDateFilter()
Expand Down
30 changes: 29 additions & 1 deletion hours/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from hours.models import (
DataSource,
DatePeriod,
PeriodOrigin,
Resource,
ResourceOrigin,
Rule,
Expand Down Expand Up @@ -74,11 +75,38 @@ class Meta:

@register
class DatePeriodFactory(factory.django.DjangoModelFactory):
class Meta:
model = DatePeriod

name = factory.LazyAttribute(lambda x: "DP-" + faker.pystr())
start_date = factory.LazyAttribute(lambda x: faker.date())

@factory.post_generation
def origins(self, create, extracted, **__):
if not create or not extracted:
return

for origin in extracted:
self.origins.add(origin)

@factory.post_generation
def data_sources(self, create, extracted, **__):
if not create or not extracted:
return

for data_source in extracted:
# Create a new origin for each data source, since data sources
# are accessed through origins.
self.origins.add(PeriodOriginFactory(data_source=data_source, period=self))


@register
class PeriodOriginFactory(factory.django.DjangoModelFactory):
class Meta:
model = DatePeriod
model = PeriodOrigin

origin_id = factory.LazyAttribute(lambda x: "OID-" + faker.pystr())
data_source = factory.SubFactory(DataSourceFactory)


@register
Expand Down
26 changes: 26 additions & 0 deletions hours/tests/test_dateperiod_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from hours.enums import RuleContext, RuleSubject, State, Weekday
from hours.models import DatePeriod
from hours.tests.utils import assert_response_status_code


@pytest.mark.django_db
Expand Down Expand Up @@ -106,6 +107,31 @@ def test_list_date_periods_filter_by_resource(
assert response.data[0]["id"] == date_period.id


@pytest.mark.django_db
def test_list_date_periods_filter_by_data_source(
admin_client, resource, data_source_factory, date_period_factory
):
expected_data_source = data_source_factory()
expected_date_period = date_period_factory(
resource=resource,
data_sources=[expected_data_source],
)
date_period_factory(
resource=resource,
data_sources=[data_source_factory()],
)

url = reverse("date_period-list")

response = admin_client.get(
url, data={"resource": resource.id, "data_source": [expected_data_source.id]}
)

assert_response_status_code(response, 200)
assert len(response.data) == 1
assert response.data[0]["id"] == expected_date_period.id


@pytest.mark.django_db
def test_list_date_periods_filter_start_date_lte(
admin_client, resource, date_period_factory
Expand Down
6 changes: 6 additions & 0 deletions hours/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,12 @@ def copy_date_periods(self, request, pk=None):
OpenApiParameter.QUERY,
description="Filter by resource id or multiple resource ids (comma-separated)", # noqa
),
OpenApiParameter(
"data_source",
OpenApiTypes.STR,
OpenApiParameter.QUERY,
description="Filter by data source",
),
OpenApiParameter(
"end_date",
OpenApiTypes.DATE,
Expand Down

0 comments on commit 91b4910

Please sign in to comment.