mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2026-01-05 06:08:46 -05:00
Introduce ical action on MealPlanViewSet to expose ical format for listing meal plans.
This commit is contained in:
@@ -41,7 +41,7 @@ from recipe_scrapers._exceptions import NoSchemaFoundInWildMode
|
||||
from requests.exceptions import MissingSchema
|
||||
from rest_framework import decorators, status, viewsets
|
||||
from rest_framework.authtoken.views import ObtainAuthToken
|
||||
from rest_framework.decorators import api_view, permission_classes
|
||||
from rest_framework.decorators import action, api_view, permission_classes
|
||||
from rest_framework.exceptions import APIException, PermissionDenied
|
||||
from rest_framework.pagination import PageNumberPagination
|
||||
from rest_framework.parsers import MultiPartParser
|
||||
@@ -804,6 +804,13 @@ class MealPlanViewSet(viewsets.ModelViewSet):
|
||||
queryset = queryset.filter(meal_type__in=meal_type)
|
||||
|
||||
return queryset
|
||||
|
||||
@action(detail=False)
|
||||
def ical(self, request):
|
||||
from_date = self.request.query_params.get('from_date', None)
|
||||
to_date = self.request.query_params.get('to_date', None)
|
||||
return meal_plans_to_ical(self.get_queryset(), f'meal_plan_{from_date}-{to_date}.ics')
|
||||
|
||||
|
||||
|
||||
class AutoPlanViewSet(viewsets.ViewSet):
|
||||
@@ -1786,6 +1793,9 @@ def get_plan_ical(request, from_date=datetime.date.today(), to_date=None):
|
||||
if to_date is not None:
|
||||
queryset = queryset.filter(to_date__lte=to_date)
|
||||
|
||||
return meal_plans_to_ical(queryset, f'meal_plan_{from_date}-{to_date}.ics')
|
||||
|
||||
def meal_plans_to_ical(queryset, filename):
|
||||
cal = Calendar()
|
||||
|
||||
for p in queryset:
|
||||
@@ -1801,7 +1811,7 @@ def get_plan_ical(request, from_date=datetime.date.today(), to_date=None):
|
||||
cal.add_component(event)
|
||||
|
||||
response = FileResponse(io.BytesIO(cal.to_ical()))
|
||||
response["Content-Disposition"] = f'attachment; filename=meal_plan_{from_date}-{to_date}.ics' # noqa: E501
|
||||
response["Content-Disposition"] = f'attachment; filename={filename}' # noqa: E501
|
||||
|
||||
return response
|
||||
|
||||
|
||||
Reference in New Issue
Block a user