api: return events param in fillslot_url (#55316)
This commit is contained in:
parent
daf613525d
commit
9ac90f720a
|
@ -22,7 +22,7 @@ import uuid
|
|||
import django
|
||||
from django.conf import settings
|
||||
from django.db import transaction
|
||||
from django.db.models import BooleanField, Count, ExpressionWrapper, F, Max, Prefetch, Q, Value
|
||||
from django.db.models import BooleanField, Count, ExpressionWrapper, F, Prefetch, Q, Value
|
||||
from django.db.models.functions import TruncDay
|
||||
from django.http import Http404, HttpResponse
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
@ -446,7 +446,9 @@ def get_event_text(event, agenda, day=None):
|
|||
return event_text
|
||||
|
||||
|
||||
def get_event_detail(request, event, agenda=None, min_places=1, booked_user_external_id=None):
|
||||
def get_event_detail(
|
||||
request, event, agenda=None, min_places=1, booked_user_external_id=None, show_events=None
|
||||
):
|
||||
agenda = agenda or event.agenda
|
||||
details = {
|
||||
'id': event.slug,
|
||||
|
@ -486,6 +488,8 @@ def get_event_detail(request, event, agenda=None, min_places=1, booked_user_exte
|
|||
},
|
||||
'places': get_event_places(event),
|
||||
}
|
||||
if show_events is not None:
|
||||
details['api']['fillslot_url'] += '?events=%s' % show_events
|
||||
if booked_user_external_id:
|
||||
if getattr(event, 'user_places_count', 0) > 0:
|
||||
details['booked_for_external_user'] = 'main-list'
|
||||
|
@ -495,7 +499,7 @@ def get_event_detail(request, event, agenda=None, min_places=1, booked_user_exte
|
|||
return details
|
||||
|
||||
|
||||
def get_events_meta_detail(request, events, agenda=None, min_places=1):
|
||||
def get_events_meta_detail(request, events, agenda=None, min_places=1, show_events=None):
|
||||
bookable_datetimes_number_total = 0
|
||||
bookable_datetimes_number_available = 0
|
||||
first_bookable_slot = None
|
||||
|
@ -504,7 +508,9 @@ def get_events_meta_detail(request, events, agenda=None, min_places=1):
|
|||
if not is_event_disabled(event, min_places=min_places):
|
||||
bookable_datetimes_number_available += 1
|
||||
if not first_bookable_slot:
|
||||
first_bookable_slot = get_event_detail(request, event, agenda=agenda, min_places=min_places)
|
||||
first_bookable_slot = get_event_detail(
|
||||
request, event, agenda=agenda, min_places=min_places, show_events=show_events
|
||||
)
|
||||
return {
|
||||
'no_bookable_datetimes': bool(bookable_datetimes_number_available == 0),
|
||||
'bookable_datetimes_number_total': bookable_datetimes_number_total,
|
||||
|
@ -753,7 +759,8 @@ class Datetimes(APIView):
|
|||
http_status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
|
||||
show_events = request.GET.get('events') or 'future'
|
||||
show_events_raw = request.GET.get('events')
|
||||
show_events = show_events_raw or 'future'
|
||||
show_past = show_events in ['all', 'past']
|
||||
show_future = show_events in ['all', 'future']
|
||||
|
||||
|
@ -784,10 +791,13 @@ class Datetimes(APIView):
|
|||
agenda=agenda,
|
||||
min_places=min_places,
|
||||
booked_user_external_id=booked_user_external_id,
|
||||
show_events=show_events_raw,
|
||||
)
|
||||
for x in entries
|
||||
],
|
||||
'meta': get_events_meta_detail(request, entries, agenda=agenda, min_places=min_places),
|
||||
'meta': get_events_meta_detail(
|
||||
request, entries, agenda=agenda, min_places=min_places, show_events=show_events_raw
|
||||
),
|
||||
}
|
||||
return Response(response)
|
||||
|
||||
|
|
|
@ -765,12 +765,20 @@ def test_past_datetimes(app, user):
|
|||
assert len(data) == 1
|
||||
assert data[0]['id'] == 'today-after-now'
|
||||
assert data[0]['disabled'] is False
|
||||
assert (
|
||||
data[0]['api']['fillslot_url']
|
||||
== 'http://testserver/api/agenda/foo-bar/fillslot/today-after-now/?events=future'
|
||||
)
|
||||
|
||||
resp = app.get('/api/agenda/%s/datetimes/' % agenda.slug, params={'events': 'past'})
|
||||
data = resp.json['data']
|
||||
assert len(data) == 1
|
||||
assert data[0]['id'] == 'today-before-now'
|
||||
assert data[0]['disabled'] is False
|
||||
assert (
|
||||
data[0]['api']['fillslot_url']
|
||||
== 'http://testserver/api/agenda/foo-bar/fillslot/today-before-now/?events=past'
|
||||
)
|
||||
|
||||
resp = app.get('/api/agenda/%s/datetimes/' % agenda.slug, params={'events': 'all'})
|
||||
data = resp.json['data']
|
||||
|
@ -779,6 +787,14 @@ def test_past_datetimes(app, user):
|
|||
assert data[1]['id'] == 'today-after-now'
|
||||
assert data[0]['disabled'] is False
|
||||
assert data[1]['disabled'] is False
|
||||
assert (
|
||||
data[0]['api']['fillslot_url']
|
||||
== 'http://testserver/api/agenda/foo-bar/fillslot/today-before-now/?events=all'
|
||||
)
|
||||
assert (
|
||||
data[1]['api']['fillslot_url']
|
||||
== 'http://testserver/api/agenda/foo-bar/fillslot/today-after-now/?events=all'
|
||||
)
|
||||
|
||||
# check canceled
|
||||
event1.cancel()
|
||||
|
|
Loading…
Reference in New Issue