Projet

Général

Profil

0001-api-remove-user-bookings-on-subscription-deletion-61.patch

Lauréline Guérin, 03 février 2022 16:55

Télécharger (7,81 ko)

Voir les différences:

Subject: [PATCH 1/3] api: remove user bookings on subscription deletion
 (#61065)

 chrono/api/views.py            |  22 ++++++
 tests/api/test_subscription.py | 123 ++++++++++++++++++++++++++++++++-
 2 files changed, 144 insertions(+), 1 deletion(-)
chrono/api/views.py
1969 1969
        return self.get(request, *args, **kwargs)
1970 1970

  
1971 1971
    def delete(self, request, *args, **kwargs):
1972
        other_user_subscription_qs = Subscription.objects.filter(
1973
            agenda=self.subscription.agenda,
1974
            user_external_id=self.subscription.user_external_id,
1975
        ).exclude(pk=self.subscription.pk)
1976
        booking_qs = Booking.objects.filter(
1977
            # remove user bookings for this agenda
1978
            event__agenda=self.subscription.agenda,
1979
            user_external_id=self.subscription.user_external_id,
1980
            # in the period of the deleted subscription
1981
            event__start_datetime__gt=self.subscription.date_start,
1982
            event__start_datetime__lt=self.subscription.date_end + datetime.timedelta(days=1),
1983
        ).filter(
1984
            # but only in the future
1985
            event__start_datetime__gt=now(),
1986
        )
1987
        for subscription in other_user_subscription_qs:
1988
            # and only if they are not in another user subscription period
1989
            booking_qs = booking_qs.exclude(
1990
                event__start_datetime__gt=subscription.date_start,
1991
                event__start_datetime__lt=subscription.date_end + datetime.timedelta(days=1),
1992
            )
1993
        booking_qs.delete()
1972 1994
        self.subscription.delete()
1973 1995
        response = {'err': 0}
1974 1996
        return Response(response)
tests/api/test_subscription.py
1 1
import datetime
2 2

  
3 3
import pytest
4
from django.utils.timezone import make_aware, now
4 5

  
5
from chrono.agendas.models import Agenda, Subscription
6
from chrono.agendas.models import Agenda, Booking, Event, Subscription
6 7

  
7 8
pytestmark = pytest.mark.django_db
8 9

  
......
316 317
    assert Subscription.objects.filter(pk=subscription.pk).exists() is False
317 318

  
318 319

  
320
@pytest.mark.parametrize(
321
    'date_now, event_date, user_id, in_waiting_list, cancelled, deleted',
322
    [
323
        # event in the future, but no booking for the user
324
        ('2021-09-09 09:59', (2021, 9, 10, 12, 0), 'yyy', False, False, False),
325
        # event in the future
326
        ('2021-09-09 09:59', (2021, 9, 10, 12, 0), 'xxx', False, False, True),
327
        ('2021-09-09 09:59', (2021, 9, 10, 12, 0), 'xxx', True, False, True),
328
        ('2021-09-09 09:59', (2021, 9, 10, 12, 0), 'xxx', False, True, True),
329
        # event in the past
330
        ('2021-09-10 10:00', (2021, 9, 10, 12, 0), 'xxx', False, False, False),
331
        # event in the future and not covered by another subscription
332
        ('2021-08-01 10:00', (2021, 9, 6, 12, 0), 'xxx', False, False, True),
333
        ('2021-08-01 10:00', (2021, 9, 24, 12, 0), 'xxx', False, False, True),
334
        # event in the future but covered by another subscription
335
        ('2021-08-01 10:00', (2021, 9, 1, 12, 0), 'xxx', False, False, False),
336
        ('2021-08-01 10:00', (2021, 9, 5, 12, 0), 'xxx', False, False, False),
337
        ('2021-08-01 10:00', (2021, 9, 25, 12, 0), 'xxx', False, False, False),
338
        ('2021-08-01 10:00', (2021, 9, 30, 12, 0), 'xxx', False, False, False),
339
        # event in the future, before the period
340
        ('2021-08-01 10:00', (2021, 8, 14, 12, 0), 'xxx', False, False, False),
341
        ('2021-08-01 10:00', (2021, 8, 31, 12, 0), 'xxx', False, False, False),
342
        # event in the future, after the period
343
        ('2021-08-01 10:00', (2021, 10, 1, 12, 0), 'xxx', False, False, False),
344
        ('2021-08-01 10:00', (2021, 10, 16, 12, 0), 'xxx', False, False, False),
345
    ],
346
)
347
def test_api_delete_subscription_and_bookings(
348
    app, user, freezer, date_now, event_date, user_id, in_waiting_list, cancelled, deleted
349
):
350
    agenda = Agenda.objects.create(label='Foo bar', kind='events')
351
    subscription = Subscription.objects.create(
352
        agenda=agenda,
353
        user_external_id='xxx',
354
        date_start=datetime.date(year=2021, month=9, day=1),
355
        date_end=datetime.date(year=2021, month=9, day=30),
356
    )
357
    Subscription.objects.create(
358
        agenda=agenda,
359
        user_external_id='xxx',
360
        date_start=datetime.date(year=2021, month=8, day=15),
361
        date_end=datetime.date(year=2021, month=9, day=5),  # overlaps
362
    )
363
    Subscription.objects.create(
364
        agenda=agenda,
365
        user_external_id='xxx',
366
        date_start=datetime.date(year=2021, month=9, day=25),  # overlaps
367
        date_end=datetime.date(year=2021, month=10, day=15),
368
    )
369
    Subscription.objects.create(
370
        agenda=agenda,
371
        user_external_id='zzz',  # another user
372
        date_start=datetime.date(year=2021, month=9, day=1),
373
        date_end=datetime.date(year=2021, month=9, day=30),
374
    )
375

  
376
    other_agenda = Agenda.objects.create(label='Foo bar', kind='events')
377
    Subscription.objects.create(
378
        agenda=other_agenda,
379
        user_external_id='xxx',
380
        date_start=datetime.date(year=2021, month=9, day=1),
381
        date_end=datetime.date(year=2021, month=10, day=1),
382
    )
383

  
384
    app.authorization = ('Basic', ('john.doe', 'password'))
385

  
386
    freezer.move_to(date_now)
387
    event = Event.objects.create(
388
        agenda=agenda, start_datetime=make_aware(datetime.datetime(*event_date)), places=10
389
    )
390
    booking = Booking.objects.create(
391
        event=event,
392
        user_external_id=user_id,
393
        in_waiting_list=in_waiting_list,
394
        cancellation_datetime=(now() if cancelled else None),
395
    )
396
    resp = app.delete('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk))
397
    assert resp.json['err'] == 0
398
    assert Subscription.objects.filter(pk=subscription.pk).exists() is False
399
    assert Booking.objects.filter(pk=booking.pk).exists() is not deleted
400

  
401

  
402
@pytest.mark.parametrize(
403
    'event_date, deleted',
404
    [
405
        # just before first day
406
        ((2021, 8, 31, 12, 00), False),
407
        # first day
408
        ((2021, 9, 1, 12, 00), True),
409
        # last day
410
        ((2021, 9, 30, 12, 00), True),
411
        # just after last day
412
        ((2021, 10, 1, 12, 00), False),
413
    ],
414
)
415
def test_api_delete_subscription_and_bookings_on_limit(app, user, freezer, event_date, deleted):
416
    agenda = Agenda.objects.create(label='Foo bar', kind='events')
417
    subscription = Subscription.objects.create(
418
        agenda=agenda,
419
        user_external_id='xxx',
420
        date_start=datetime.date(year=2021, month=9, day=1),
421
        date_end=datetime.date(year=2021, month=9, day=30),
422
    )
423

  
424
    app.authorization = ('Basic', ('john.doe', 'password'))
425

  
426
    freezer.move_to('2021-08-01 10:00')
427
    event = Event.objects.create(
428
        agenda=agenda, start_datetime=make_aware(datetime.datetime(*event_date)), places=10
429
    )
430
    booking = Booking.objects.create(
431
        event=event,
432
        user_external_id='xxx',
433
    )
434
    resp = app.delete('/api/agenda/%s/subscription/%s/' % (agenda.slug, subscription.pk))
435
    assert resp.json['err'] == 0
436
    assert Subscription.objects.filter(pk=subscription.pk).exists() is False
437
    assert Booking.objects.filter(pk=booking.pk).exists() is not deleted
438

  
439

  
319 440
def test_api_patch_subscription(app, user):
320 441
    agenda = Agenda.objects.create(label='Foo bar', kind='events')
321 442
    subscription = Subscription.objects.create(
322
-