Add event status filter to search #348

This commit is contained in:
Daniel Grams 2021-12-30 20:20:24 +01:00
parent 3644873b84
commit 340ba75792
4 changed files with 52 additions and 1 deletions

View File

@ -237,6 +237,10 @@ class EventSearchRequestSchema(PaginationRequestSchema):
sort = fields.Str(
metadata={"description": "Sort result items."},
)
status = fields.List(
EnumField(EventStatus),
metadata={"description": "Looks for events with this stati."},
)
class EventSearchResponseSchema(PaginationResponseSchema):

View File

@ -68,6 +68,13 @@ def fill_event_filter(event_filter, params):
event_filter, Event.categories.any(EventCategory.id.in_(category_ids))
)
if params.status:
if type(params.status) is list:
stati = params.status
else: # pragma: no cover
stati = [params.status]
event_filter = and_(event_filter, Event.status.in_(stati))
if params.event_list_id:
if type(params.event_list_id) is list:
event_list_ids = params.event_list_id

View File

@ -27,6 +27,7 @@ class EventSearchParams(object):
self.event_list_id = None
self.weekday = None
self.sort = None
self.status = None
@property
def date_from(self):
@ -95,8 +96,25 @@ class EventSearchParams(object):
if len(item_ids) > 0:
return item_ids
return None
def load_status_list_param(self):
stati = self.load_list_param("status")
if stati is None: # pragma: no cover
return None
from project.models import EventStatus
result = list()
for status in stati:
if status in EventStatus.__members__:
result.append(EventStatus.__members__[status])
return result
def load_from_request(self):
if "date_from" in request.args:
self.date_from_str = request.args["date_from"]
@ -130,3 +148,6 @@ class EventSearchParams(object):
if "organization_id" in request.args:
self.admin_unit_id = request.args["organization_id"]
if "status" in request.args:
self.status = self.load_status_list_param()

View File

@ -40,7 +40,7 @@ def test_list(client, seeder, utils):
assert response.json["items"][0]["id"] == 1
def test_search(client, seeder, utils):
def test_search(client, seeder, utils, app, db):
from project.dateutils import create_berlin_date
user_id, admin_unit_id = seeder.setup_base()
@ -91,6 +91,25 @@ def test_search(client, seeder, utils):
assert len(response.json["items"]) == 1
assert response.json["items"][0]["event"]["id"] == listed_event_id
url = utils.get_url("api_v1_event_date_search", status="scheduled")
response = utils.get_ok(url)
assert len(response.json["items"]) == 2
with app.app_context():
from project.models import Event, EventStatus
event = Event.query.get(event_id)
event.status = EventStatus.cancelled
db.session.commit()
url = utils.get_url("api_v1_event_date_search", status="scheduled")
response = utils.get_ok(url)
assert len(response.json["items"]) == 1
url = utils.get_url("api_v1_event_date_search", status=["scheduled", "cancelled"])
response = utils.get_ok(url)
assert len(response.json["items"]) == 2
def test_search_oneDay(client, seeder, utils):
from project.dateutils import create_berlin_date