Browse Source

Add OccasionsList view and update URL patterns for calendar

- Introduced `OccasionsList` view to retrieve calendar occasions with filtering options for month and last updated timestamp.
- Updated URL patterns to include a new endpoint for syncing occasions and modified the existing occasions endpoint.
- Enhanced the `get_queryset` method to support filtering by month and last updated date, improving data retrieval flexibility.
master
Mohsen Taba 3 months ago
parent
commit
abcf344523
  1. 6
      apps/dobodbi_calendar/urls.py
  2. 78
      apps/dobodbi_calendar/views.py

6
apps/dobodbi_calendar/urls.py

@ -1,9 +1,9 @@
from django.urls import path
from apps.dobodbi_calendar.views import CalendarList, AdjustmentConfigView
from apps.dobodbi_calendar.views import CalendarList, AdjustmentConfigView, OccasionsList
urlpatterns = [
path('occasions/', CalendarList.as_view()),
path('sync-occasions/', CalendarList.as_view()),
path('adjustemnts/', AdjustmentConfigView.as_view()),
path('occasions/', OccasionsList.as_view()),
]

78
apps/dobodbi_calendar/views.py

@ -96,3 +96,81 @@ class AdjustmentConfigView(APIView):
def get(self, request):
adjustment_config = get_config('calendar__Adjustment')
return Response(json.loads(adjustment_config))
class OccasionsList(ListAPIView):
serializer_class = CalendarSerializer
pagination_class = StandardResultsSetPagination
permission_classes = (AllowAny,)
@swagger_auto_schema(
operation_description="Get list of calendar occasions. Can filter by month.",
tags=["Dobodbi - Calendar"],
manual_parameters=[
openapi.Parameter(
'month', openapi.IN_QUERY,
description="Filter by month number (e.g. 1, 02, 12)",
type=openapi.TYPE_STRING,
required=False
),
openapi.Parameter(
'last_updated', openapi.IN_QUERY,
description="Filter occasions updated after this timestamp",
type=openapi.TYPE_STRING,
required=False
)
],
responses={
200: openapi.Response(
description="List of calendar occasions"
)
}
)
def get(self, request, *args, **kwargs):
return super().get(request, *args, **kwargs)
def get_queryset(self):
queryset = CalendarOccasions.objects.all()
req = self.request
# 1. Month Filter Logic
month = req.query_params.get('month')
if month:
# Ensure month is two digits (e.g., '2' -> '02') to match your JSON data
month_str = str(month).zfill(2)
# This looks inside the JSON array.
# It finds rows where the JSON list contains an object with this exact key/value.
queryset = queryset.filter(dates__contains=[{'month': month_str}])
# 2. Last Updated Filter Logic
if v := req.query_params.get('last_updated'):
queryset = queryset.filter(updated_at__gte=v)
return queryset
def list(self, request, *args, **kwargs):
q = self.get_queryset()
# Calculate last_updated based on the specific filtered result
last_item_date = q.order_by('-updated_at').first()
if last_item_date:
last_updated = last_item_date.updated_at + datetime.timedelta(microseconds=1)
last_updated = str(last_updated)
else:
last_updated = None
# Standard pagination handling (since you are inheriting ListAPIView)
page = self.paginate_queryset(q)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
# Fallback if pagination is disabled
d = self.get_serializer(q, many=True).data
data = OrderedDict({
'last_updated': last_updated,
'total': len(d),
'data': d,
})
return Response(data)
Loading…
Cancel
Save