Planning API

class recipes.api.planning.CalendarViewSet(**kwargs)

Bases: rest_framework.mixins.CreateModelMixin, rest_framework.mixins.DestroyModelMixin, rest_framework.viewsets.ReadOnlyModelViewSet

This class provides the API get and retrieve views for the calendar month objects, plus three workers: - mine, which is simply a GET call that filters by the current user, to list those available for editing - monthly_data, which is used to get the bulk data for a whole month - recipe_id, which is used to put the id onto a

create(request, *args, **kwargs)

This function is a custom handler for the POST call into this endpoint This function adds in a creator field to the request data before passing the data to the serializer

Parameters:
  • request – An HTTP request
  • args – Ordered arguments, none are required here
  • kwargs – Keyword arguments, none are required here
Returns:

A JSON response

get_queryset()

This function allows Django to call this model but only retrieve a subset of the database. In this case, it is down-selecting to only the currently logged-in user

Returns:A queryset of Calendar objects belonging to the current user
mine(request)

This function provides an alternate GET endpoint to get a list of the planning months that are owned by the current user. This is used to list the months available for editing in the planner page. For the months page, we want to expose them all, read-only of course, so that endpoint should just use the regular list method.

Returns:A JSON response with all the calendar objects available for this user
monthly_data(request, pk)

Creates a custom response on the API for getting monthly data, including date numbers, recipes, etc.

Parameters:
  • request – A full django request object
  • pk – The primary key for this particular calendar instance
Returns:

A JSONResponse with two keys: data and num_weeks.

  • num_weeks returns the number of weeks in this month for convenience, either 4, 5 or 6
  • data is an array of 4, 5, or 6 items, with each item being weekly data. Each weekly data item is an array of 7 items, with each item being daily data. Each daily data item is a dictionary containing keys date_number, recipe0, recipe0title, recipe1, and recipe1title. The date_number key can be “-” to represent this day does not belong in the current month. The recipe0 and recipe1 keys are ids to recipe objects in the database. The recipe0title and recipe1title keys are simply the recipe titles for convenience.

recipe_id(request, pk)

Sets the recipe for this particular calendar date and recipe id Expects three parameters on the request body: date_num (1-31), daily_recipe_id (0 or 1), and recipe_pk If recipe_pk is 0, that indicates this recipe item should be cleared

Parameters:
  • request – A full django request object
  • pk – The primary key of the calendar to modify
Returns:

A JSONResponse object with keys success and message. The status code will also be set accordingly

serializer_class

alias of recipes.serializers.planning.CalendarSerializer