HTML View Response Logic

HTML-serving views and JSON APIs.

Jin Cheng, 02/12/16

class server_side.controls.views.CalorimeterStatusAPI(**kwargs)[source]

Bases: rest_framework.views.APIView

Gives or updates JSONified data about the status of a single calorimeter.

delete(request, format=None)[source]

A non-standard implementation of the DELETE HTTP request, instructing the device to stop heating immediately.

get(request, format=None)[source]

Get serialized JSON response containing calorimeter status.

get_object()[source]
permission_classes = (<class 'server_side.controls.views.DeviceAccessPermission'>,)
put(request, format=None)[source]

Periodical updates from the device about its current temperatures when a job isn’t running.

class server_side.controls.views.CsrfExemptSessionAuthentication[source]

Bases: rest_framework.authentication.SessionAuthentication

Django and Django REST framework by default requires all POST requests to include a X-CSRFToken header, which is not applicable for when the raspberry pi sends data because it won’t have any existing response from an active session.

The enforce_csrf method is overridden to circumvent the CSRF check.

enforce_csrf(request)[source]
server_side.controls.views.DataDownloadView(request, run_id)[source]

On-the-fly generation of CSV data files and HTTP attachment response to client’s download request.

class server_side.controls.views.DataPointListAPI(**kwargs)[source]

Bases: rest_framework.views.APIView

Gives a list of all data points for a specific run measured after a specified time.

authentication_classes = (<class 'server_side.controls.views.CsrfExemptSessionAuthentication'>, <class 'rest_framework.authentication.BasicAuthentication'>)
get(request, format=None)[source]

Get data points for a run (with its ID specified in GET parameter run), and after a certain time (with POSIX timestamp or ISO formatted string, optional). :return: JSON Response

permission_classes = (<class 'server_side.controls.views.DeviceAccessPermission'>,)
post(request, format=None)[source]

Main entry point for raspberry pi DSC device. Periodically receives data points measured on-device, containing a list of data point dicts. The dict should contain info on: time measured, temperatures (ref and sample), heat used (ref and sample).

Time measured: should be either POSIX time or ISO formatted string Temperatures, heat outputss: floats

Returns:JSON Response, containing the Calorimeter stop flag (Bool), errors (List), data points (List). The stop flag, if true, should instruct the device to immediately stop heating or cooling. The error list will be empty if no error is found.
put(request, format=None)[source]

Toggles a run’s is_ready param, which indicates whether it has an inserted sample and should heat beyond the start temp.

class server_side.controls.views.DeviceAccessPermission[source]

Bases: rest_framework.permissions.BasePermission

Permission check done with every HTTP request.

Should be implemented with all API points to prevent abuse.

has_object_permission(request, view, obj)[source]
has_permission(request, view)[source]
server_side.controls.views.IndexView(request, *args, **kwargs)[source]

Serves index HTML document as the HTTP response to any browser (non-API) request. :param request: Django HTTPRequest object :return: Django HTTPResponse object, containing the HTML body and metadata

class server_side.controls.views.RunDetailsAPI(**kwargs)[source]

Bases: rest_framework.generics.RetrieveUpdateDestroyAPIView

Gives details about a Run, or changes the Run parameters, or delete the Run completely.

authentication_classes = (<class 'server_side.controls.views.DeviceAccessPermission'>,)
queryset
serializer_class

alias of RunSerializer

class server_side.controls.views.RunListAPI(**kwargs)[source]

Bases: rest_framework.views.APIView

Gives a list of all runs conducted by a calorimeter, or create a new run to be started immediately.

get(request, format=None, **kwargs)[source]
permission_classes = (<class 'server_side.controls.views.DeviceAccessPermission'>,)
post(request, format=None)[source]
server_side.controls.views.datetime_parser(raw_input)[source]

Parses a raw string input using tools. Tests its format by regex and initiates appropriate datetime constructor.

Parameters:raw_input – any string representation of datetime, in either POSIX or ISO format
Returns:a Python datetime object