API

This is the app which contains all API routes and classes which map the cms models to API JSON responses. This is not the API documentation itself, but the Django developer documentation. The usage documentation can be found here: https://lunes.tuerantuer.org/api/docs/

Apps

class lunes_cms.api.apps.ApiConfig(app_name, app_module)

Bases: AppConfig

Application settings for the api app, which is the app providing the REST API. Inherits from AppConfig.

name = 'lunes_cms.api'
verbose_name = 'API'

Permissions

class lunes_cms.api.permissions.VerifyGroupKey

Bases: AllowAny

Simple permissions class that blocks requests if no valid API-Token is delivered. Inherits from permissions.AllowAny.

has_permission(request, view)

Checks whether a valid API-Key is send by the user in the authorization header

Parameters:
  • request (HttpRequest) – http request

  • view (viewsets.ModelViewSet) – restframework view

Returns:

False if user doesn’t send a API-key

Return type:

bool

Urls

URL patterns for the Lunes API

lunes_cms.api.urls.app_name = 'api'

The namespace for this URL config (see django.urls.ResolverMatch.app_name)

lunes_cms.api.urls.urlpatterns = [<URLResolver <module 'lunes_cms.api.v1.urls' from '/home/docs/checkouts/readthedocs.org/user_builds/lunes-cms/envs/latest/lib/python3.9/site-packages/lunes_cms/api/v1/urls.py'> (v1:default) ''>, <URLResolver <module 'lunes_cms.api.v1.urls' from '/home/docs/checkouts/readthedocs.org/user_builds/lunes-cms/envs/latest/lib/python3.9/site-packages/lunes_cms/api/v1/urls.py'> (v1:v1) 'v1/'>]

The url patterns of this module (see URL dispatcher)

[
    <URLResolver <module 'lunes_cms.api.v1.urls' from '/home/docs/checkouts/readthedocs.org/user_builds/lunes-cms/envs/latest/lib/python3.9/site-packages/lunes_cms/api/v1/urls.py'> (v1:default) ''>,
    <URLResolver <module 'lunes_cms.api.v1.urls' from '/home/docs/checkouts/readthedocs.org/user_builds/lunes-cms/envs/latest/lib/python3.9/site-packages/lunes_cms/api/v1/urls.py'> (v1:v1) 'v1/'>,
]

Utils

A collection of helper methods and classes

class lunes_cms.api.utils.OptionalSlashRouter

Bases: DefaultRouter

Custom router to allow routes with and without trailing slash to work without redirects

__init__()
lunes_cms.api.utils.check_group_object_permissions(request, group_id)

Function to check if the API-Key of the passed request object matches one of the hashed keys stored in the database of the corresponding group id.

Parameters:
  • request (HttpRequest) – current request

  • group_id (str) – group id

Raises:
  • PermissionDenied – Exception if no API-Key is delivered

  • PermissionDenied – Exception if API-Key doesn’t belong to passed group id

lunes_cms.api.utils.get_discipline_by_group_queryset(discipline_view_set)

Returns overview of disciplines for a given group id, which must be in the keyword arguments of the passed discipline view set. All elements are root nodes and recursively either have at least one sub-discipline or one training set. Additionally, they need to be released by the creator group.

Parameters:

discipline_view_set (class) – A handle to the DisciplineViewSet

Returns:

(filtered) queryset

Return type:

QuerySet

lunes_cms.api.utils.get_filtered_discipline_queryset(discipline_view_set)

Returns child disciplines belonging to the discipline id of the passed discipline view set. Only released and non-empty objects are returned. The number of training sets contained by a child is annotated as well.

Parameters:

discipline_view_set (class) – A handle to the DisciplineViewSet

Returns:

(filtered) queryset

Return type:

QuerySet

lunes_cms.api.utils.get_key(request, keyword='Api-Key')

Retrieve API Key from Authorization header of http request. Optionally, a custom keyword can be specified. The function espects the key to be delivered as follows: {“Authorization”: “<keyword> <api-key>}

Parameters:
  • request (HttpRequest) – get request

  • keyword (str, optional) – keyword for api key in authorization header, defaults to “Api-Key”

Returns:

api key in authorization header

Return type:

str

lunes_cms.api.utils.get_non_empty_disciplines(queryset)

Filters a discipline queryset so that every element recursively either have at least one sub-discipline or one training set.

Parameters:

queryset (QuerySet) – Queryset of ~lunes_cms.cms.models.Discipline objects

Returns:

(filtered) queryset

Return type:

QuerySet

lunes_cms.api.utils.get_overview_discipline_queryset()

Returns the general disciplines created by super users if the are root nodes and recursively either has at least one sub-discipline or one training set. Additionally, they need to be released by the creator group.

Returns:

(filtered) queryset

Return type:

QuerySet