From 68df3d8eabbf2ba4e9cfb03541ca06e99a0824c1 Mon Sep 17 00:00:00 2001 From: juuso-j <68938778+juuso-j@users.noreply.github.com> Date: Wed, 5 Jun 2024 13:23:14 +0300 Subject: [PATCH 01/11] Add field is_test_event_area --- parkings/models/event_area.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/parkings/models/event_area.py b/parkings/models/event_area.py index f8156fc1..102e0d2b 100644 --- a/parkings/models/event_area.py +++ b/parkings/models/event_area.py @@ -68,7 +68,9 @@ class EventArea(AbstractParkingArea): null=True, blank=True, default=list) description = models.TextField(null=True, blank=True, verbose_name=_('description')) - + is_test_event_area = models.BooleanField(default=False, verbose_name=_('is test event area'), help_text=_( + 'if set to True the event area is ment only for testing purposes, can be deleted and' + ' is ignored in event_area statistics ')) objects = EventAreaQuerySet.as_manager() @property From 4578c942ca7798ff484414a7da7df571a8e98ff6 Mon Sep 17 00:00:00 2001 From: juuso-j <68938778+juuso-j@users.noreply.github.com> Date: Wed, 5 Jun 2024 14:27:10 +0300 Subject: [PATCH 02/11] Allow deletion of test event areas --- parkings/admin.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/parkings/admin.py b/parkings/admin.py index 87f7ed12..f1daa01a 100644 --- a/parkings/admin.py +++ b/parkings/admin.py @@ -106,7 +106,10 @@ def save_related(self, request, form, formsets, change): form.instance.parking_areas.add(parking_area) def has_delete_permission(self, request, obj=None): - return False + if obj is None: # + return super().has_delete_permission(request, obj) + # Allow deletion only if is_test_event_area is True + return obj.is_test_event_area @admin.register(EventParking) From fa68bc42c22bb132b9a2e47df21c83eef6e94ffd Mon Sep 17 00:00:00 2001 From: juuso-j <68938778+juuso-j@users.noreply.github.com> Date: Wed, 5 Jun 2024 14:44:40 +0300 Subject: [PATCH 03/11] Rename is_test_event_area to is_test --- parkings/admin.py | 6 +++--- parkings/models/event_area.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/parkings/admin.py b/parkings/admin.py index f1daa01a..e5bfbb88 100644 --- a/parkings/admin.py +++ b/parkings/admin.py @@ -86,8 +86,8 @@ def __init__(self, *args, **kwargs): class EventAreaAdmin(WithAreaField, OSMGeoAdmin): form = EventAreaForm area_scale = 1 - list_display = ['id', 'is_active', 'origin_id', 'domain', 'time_start', 'time_end', 'time_period_time_start', - 'time_period_time_end', 'days_of_week', 'price', 'price_unit_length', + list_display = ['id', 'is_active', 'is_test', 'origin_id', 'domain', 'time_start', 'time_end', + 'time_period_time_start', 'time_period_time_end', 'days_of_week', 'price', 'price_unit_length', 'capacity_estimate', 'estimated_capacity', 'area', 'overlapping_parking_areas'] list_filter = ['domain'] ordering = ('origin_id',) @@ -109,7 +109,7 @@ def has_delete_permission(self, request, obj=None): if obj is None: # return super().has_delete_permission(request, obj) # Allow deletion only if is_test_event_area is True - return obj.is_test_event_area + return obj.is_test @admin.register(EventParking) diff --git a/parkings/models/event_area.py b/parkings/models/event_area.py index 102e0d2b..2cea273c 100644 --- a/parkings/models/event_area.py +++ b/parkings/models/event_area.py @@ -68,7 +68,7 @@ class EventArea(AbstractParkingArea): null=True, blank=True, default=list) description = models.TextField(null=True, blank=True, verbose_name=_('description')) - is_test_event_area = models.BooleanField(default=False, verbose_name=_('is test event area'), help_text=_( + is_test = models.BooleanField(default=False, verbose_name=_('is test event area'), help_text=_( 'if set to True the event area is ment only for testing purposes, can be deleted and' ' is ignored in event_area statistics ')) objects = EventAreaQuerySet.as_manager() From 89b71d7981b9c5b835a8879079040365be380ed1 Mon Sep 17 00:00:00 2001 From: juuso-j <68938778+juuso-j@users.noreply.github.com> Date: Thu, 6 Jun 2024 08:56:53 +0300 Subject: [PATCH 04/11] Fix is_test help_text and verbose_name --- parkings/models/event_area.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/parkings/models/event_area.py b/parkings/models/event_area.py index 2cea273c..6164bdcf 100644 --- a/parkings/models/event_area.py +++ b/parkings/models/event_area.py @@ -68,9 +68,9 @@ class EventArea(AbstractParkingArea): null=True, blank=True, default=list) description = models.TextField(null=True, blank=True, verbose_name=_('description')) - is_test = models.BooleanField(default=False, verbose_name=_('is test event area'), help_text=_( + is_test = models.BooleanField(default=False, verbose_name=_('is test'), help_text=_( 'if set to True the event area is ment only for testing purposes, can be deleted and' - ' is ignored in event_area statistics ')) + ' is not visible in the public API.')) objects = EventAreaQuerySet.as_manager() @property From 25195801b2c4c17391f1f760ec0994fb385502e8 Mon Sep 17 00:00:00 2001 From: juuso-j <68938778+juuso-j@users.noreply.github.com> Date: Thu, 6 Jun 2024 10:30:23 +0300 Subject: [PATCH 05/11] Fix comment --- parkings/admin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parkings/admin.py b/parkings/admin.py index e5bfbb88..8380eeb2 100644 --- a/parkings/admin.py +++ b/parkings/admin.py @@ -108,7 +108,7 @@ def save_related(self, request, form, formsets, change): def has_delete_permission(self, request, obj=None): if obj is None: # return super().has_delete_permission(request, obj) - # Allow deletion only if is_test_event_area is True + # Allow deletion only if is_test is True return obj.is_test From 601b7f38f5bf59f5bf5452a4f5fd3dcac3a64b25 Mon Sep 17 00:00:00 2001 From: juuso-j <68938778+juuso-j@users.noreply.github.com> Date: Thu, 6 Jun 2024 10:40:43 +0300 Subject: [PATCH 06/11] Add is_test to model EventArea --- parkings/migrations/0065_eventarea_is_test.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 parkings/migrations/0065_eventarea_is_test.py diff --git a/parkings/migrations/0065_eventarea_is_test.py b/parkings/migrations/0065_eventarea_is_test.py new file mode 100644 index 00000000..a699900c --- /dev/null +++ b/parkings/migrations/0065_eventarea_is_test.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.8 on 2024-06-05 13:50 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('parkings', '0064_alter_archivedparking_domain_and_more'), + ] + + operations = [ + migrations.AddField( + model_name='eventarea', + name='is_test', + field=models.BooleanField( + default=False, help_text='if set to True the event area is ment only for testing purposes, can be deleted and is not serialized.', verbose_name='is test'), + ), + ] From b6da128be5ab1d5eae007794593681badc2deb02 Mon Sep 17 00:00:00 2001 From: juuso-j <68938778+juuso-j@users.noreply.github.com> Date: Thu, 6 Jun 2024 10:48:38 +0300 Subject: [PATCH 07/11] Test is_test field --- parkings/tests/api/public/test_event_area.py | 10 ++++++++++ .../tests/api/public/test_event_area_statistics.py | 10 ++++++++++ .../api/public/test_event_area_total_statistics.py | 10 ++++++++++ 3 files changed, 30 insertions(+) diff --git a/parkings/tests/api/public/test_event_area.py b/parkings/tests/api/public/test_event_area.py index 6c7b4041..ffaf667c 100644 --- a/parkings/tests/api/public/test_event_area.py +++ b/parkings/tests/api/public/test_event_area.py @@ -46,6 +46,16 @@ def test_get_list_check_data(api_client, event_area): assert properties_data['capacity_estimate'] == event_area.capacity_estimate +def test_get_list_test_event_area(api_client, event_area_factory): + event_area = event_area_factory.create() + stats_data = get(api_client, list_url) + assert stats_data["count"] == 1 + event_area.is_test = True + event_area.save() + stats_data = get(api_client, list_url) + assert stats_data["count"] == 0 + + def test_event_area_with_price_and_price_unit_lenght(api_client, event_area): event_area.price = Decimal(str('1.23')) event_area.price_unit_length = 8 diff --git a/parkings/tests/api/public/test_event_area_statistics.py b/parkings/tests/api/public/test_event_area_statistics.py index 82ac284a..9db211d4 100644 --- a/parkings/tests/api/public/test_event_area_statistics.py +++ b/parkings/tests/api/public/test_event_area_statistics.py @@ -150,6 +150,16 @@ def test_get_list_check_data(api_client, event_parking_factory, event_area_facto assert stats_data_3['current_parking_count'] == 0 # not valid parkings currently +def test_get_list_test_event_area(api_client, event_area_factory): + event_area = event_area_factory.create() + stats_data = get(api_client, list_url) + assert stats_data["count"] == 1 + event_area.is_test = True + event_area.save() + stats_data = get(api_client, list_url) + assert stats_data["count"] == 0 + + def test_get_detail_check_data(api_client, event_parking_factory, event_area): event_parking_factory.create_batch(3, event_area=event_area) stats_data = get(api_client, get_detail_url(event_area)) diff --git a/parkings/tests/api/public/test_event_area_total_statistics.py b/parkings/tests/api/public/test_event_area_total_statistics.py index 4c34220f..995c339b 100644 --- a/parkings/tests/api/public/test_event_area_total_statistics.py +++ b/parkings/tests/api/public/test_event_area_total_statistics.py @@ -38,6 +38,16 @@ def test_get_list_check_data(api_client, event_parking_factory, event_area_facto assert results[4]['total_parking_count'] == 0 +def test_get_list_test_event_area(api_client, event_area_factory): + event_area = event_area_factory.create() + stats_data = get(api_client, list_url) + assert stats_data["count"] == 1 + event_area.is_test = True + event_area.save() + stats_data = get(api_client, list_url) + assert stats_data["count"] == 0 + + @pytest.mark.django_db def test_get_detail_check_data(api_client, event_parking_factory, event_area_factory): event_area = event_area_factory.create() From 42ce5f60e648b33d227fba75dd1b46f220a05bd5 Mon Sep 17 00:00:00 2001 From: juuso-j <68938778+juuso-j@users.noreply.github.com> Date: Thu, 6 Jun 2024 11:17:26 +0300 Subject: [PATCH 08/11] Filter is_test=False Serialize only if event area is not for testing purposes --- parkings/api/public/event_area_total_statistics.py | 2 +- parkings/models/event_area.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/parkings/api/public/event_area_total_statistics.py b/parkings/api/public/event_area_total_statistics.py index faef26ff..ca05dfbb 100644 --- a/parkings/api/public/event_area_total_statistics.py +++ b/parkings/api/public/event_area_total_statistics.py @@ -25,4 +25,4 @@ class PublicAPIEventAreaTotalStatisticsViewSet(viewsets.ReadOnlyModelViewSet): permission_classes = [permissions.AllowAny] serializer_class = EventAreaTotalStatisticsSerializer pagination_class = Pagination - queryset = EventAreaStatistics.objects.all().order_by('-created_at') + queryset = EventAreaStatistics.objects.filter(event_area__is_test=False).order_by('-created_at') diff --git a/parkings/models/event_area.py b/parkings/models/event_area.py index 6164bdcf..36c45331 100644 --- a/parkings/models/event_area.py +++ b/parkings/models/event_area.py @@ -26,7 +26,7 @@ def get_active_queryset(self): Q(time_period_time_end__gte=now) & Q(time_period_time_start__lte=now) & Q( time_period_days_of_week__contains=[iso_weekday]) ) - return qs.order_by('origin_id') + return qs.filter(is_test=False).order_by('origin_id') class EventArea(AbstractParkingArea): From a0e289b45cc4e0915f31ac51b1a5fb95d4c18e0c Mon Sep 17 00:00:00 2001 From: juuso-j <68938778+juuso-j@users.noreply.github.com> Date: Thu, 6 Jun 2024 11:18:39 +0300 Subject: [PATCH 09/11] Delete EventAreaStatistics for test event areas --- parkings/signals.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/parkings/signals.py b/parkings/signals.py index f99e275c..8ebd7036 100644 --- a/parkings/signals.py +++ b/parkings/signals.py @@ -2,11 +2,11 @@ from math import ceil from django.db import transaction -from django.db.models.signals import post_delete, post_save +from django.db.models.signals import post_delete, post_save, pre_delete from django.dispatch import receiver from django.utils import timezone -from parkings.models import EventParking +from parkings.models import EventArea, EventAreaStatistics, EventParking @transaction.atomic @@ -42,3 +42,11 @@ def event_parking_on_save(sender, **kwargs): def event_parking_on_delete(sender, **kwargs): obj = kwargs["instance"] update_statistics(obj.event_area) + + +@receiver(pre_delete, sender=EventArea) +def event_area_on_delete(sender, **kwargs): + obj = kwargs["instance"] + # Only test event areas can be deleted. + if obj.is_test: + EventAreaStatistics.objects.filter(event_area=obj).delete() From febacd1a8958b4db7ac23c5755ae16540859008e Mon Sep 17 00:00:00 2001 From: juuso-j <68938778+juuso-j@users.noreply.github.com> Date: Thu, 6 Jun 2024 11:19:07 +0300 Subject: [PATCH 10/11] Test EventAreaStatistics is deleted --- parkings/tests/test_event_area_statistics_model.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/parkings/tests/test_event_area_statistics_model.py b/parkings/tests/test_event_area_statistics_model.py index 780eb7b1..23fd3108 100644 --- a/parkings/tests/test_event_area_statistics_model.py +++ b/parkings/tests/test_event_area_statistics_model.py @@ -31,6 +31,15 @@ def test_event_area_statistics_is_created(event_area_data): assert statistics.total_parking_count == 0 +@pytest.mark.django_db +def test_event_area_statistics_is_deleted(event_area_data): + event_area_data["is_test"] = True + event_area = EventArea.objects.create(**event_area_data) + assert EventAreaStatistics.objects.count() == 1 + event_area.delete() + assert EventAreaStatistics.objects.count() == 0 + + @pytest.mark.django_db def test_event_area_statistics_is_not_deleted(event_area_data): event_area = EventArea.objects.create(**event_area_data) From c03ccced80d0334f480b39b5c8d8a779f5b62461 Mon Sep 17 00:00:00 2001 From: juuso-j <68938778+juuso-j@users.noreply.github.com> Date: Thu, 6 Jun 2024 11:53:02 +0300 Subject: [PATCH 11/11] Add is_test to eventarea --- .sanitizerconfig | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.sanitizerconfig b/.sanitizerconfig index 5d086244..8cef9de6 100644 --- a/.sanitizerconfig +++ b/.sanitizerconfig @@ -145,7 +145,7 @@ strategy: normalized_reg_num: parkkihubi.registration_number operator_id: null event_area_id: null - registration_number: parkkihubi.registration_number + registration_number: parkkihubi.registration_number time_end: null time_start: null location_gk25fin: null @@ -168,6 +168,7 @@ strategy: time_period_time_end: null time_period_days_of_week: null description: null + is_test: null parkings_eventareastatistics: id: null created_at: null @@ -175,7 +176,7 @@ strategy: event_area_id: null total_parking_charges: null total_parking_income: null - total_parking_count: null + total_parking_count: null parkings_parkingcheck: allowed: null created_at: null @@ -187,7 +188,7 @@ strategy: registration_number: parkkihubi.registration_number result: null time: null - time_overridden: null + time_overridden: null parkings_parkingterminal: created_at: null domain_id: null