dokomoforms.handlers.api.v0 package

Submodules

dokomoforms.handlers.api.v0.base module

The base class of the TornadoResource classes in the api module.

class dokomoforms.handlers.api.v0.base.BaseResource(*args, **kwargs)[source]

Bases: restless.tnd.TornadoResource

Set up the basics for the model resource.

BaseResource does some basic configuration for the restless resources. - sets the base request handler class which is used by the resources - providing reference to the ORM session via request handler - inserting a serializer for dokomo Models - setting up authentication

build_response(data, status=200)[source]

Finish the Tornado response.

This takes into account non-JSON content-types.

content_type

The format specified in the request.

current_user

The handler’s current_user.

current_user_model

The handler’s current_user_model.

default_sort_column_name

The default ORDER BY column name for list responses.

delete(model_id)[source]

Set the deleted attribute to True. Does not destroy the instance.

detail(model_id)[source]

Return a single instance of a model.

handle_error(err)[source]

Generate a serialized error message.

If the error came from Tornado, pass it along as such. Otherwise, turn certain expected errors into 400 BAD REQUEST instead of 500 INTERNAL SERVER ERROR.

is_authenticated(admin_only=True)[source]

Return whether the request has been authenticated.

list(where=None)[source]

Return a list of instances of this model.

Given a model class, build up the ORM query based on query params and return the query result.

objects_key

The key for list responses.

query_modifiers_applied

Whether there were any modifiers applied to the query.

resource_type

The model class for the resource.

serializer = <dokomoforms.handlers.api.v0.serializer.ModelJSONSerializer object>
session

The handler’s session.

update(model_id)[source]

Update a model.

wrap_list_response(data)[source]

Wrap a list response in a dict.

Takes a list of data & wraps it in a dictionary (within the objects key). For security in JSON responses, it’s better to wrap the list results in an object (due to the way the Array constructor can be attacked in Javascript). See http://haacked.com/archive/2009/06/25/json-hijacking.aspx/ & similar for details. Overridable to allow for modifying the key names, adding data (or just insecurely return a plain old list if that’s your thing). :param data: A list of data about to be serialized :type data: list :returns: A wrapping dict :rtype: dict

dokomoforms.handlers.api.v0.nodes module

TornadoResource class for dokomoforms.models.node.Node subclasses.

class dokomoforms.handlers.api.v0.nodes.NodeResource(*args, **kwargs)[source]

Bases: dokomoforms.handlers.api.v0.base.BaseResource

Restless resource for Nodes.

BaseResource sets the serializer, which uses the dokomo models’ ModelJSONEncoder for json conversion.

create()[source]

Create a new node.

default_sort_column_name = 'last_update_time'
objects_key = 'nodes'
resource_type

alias of Node

dokomoforms.handlers.api.v0.photos module

TornadoResource class for dokomoforms.models.answer.Photo.

class dokomoforms.handlers.api.v0.photos.PhotoResource(*args, **kwargs)[source]

Bases: dokomoforms.handlers.api.v0.base.BaseResource

Restless resource for Photos.

create()[source]

Create a Photo. Must match an existing PhotoAnswer.

default_sort_column_name = 'created_on'
is_authenticated()[source]

Allow unauthenticated POSTs.

objects_key = 'photos'
resource_type

alias of Photo

dokomoforms.handlers.api.v0.serializer module

The restless Serializer for the models.

class dokomoforms.handlers.api.v0.serializer.ModelJSONSerializer[source]

Bases: restless.serializers.JSONSerializer

Drop-in replacement for the restless-supplied JSONSerializer.

Uses dokomo’s ModelJSONEncoder in order to correctly serialize models to JSON.

serialize(data)[source]

The low-level serialization.

Underpins serialize, serialize_list & serialize_detail. Has no built-in smarts, simply dumps the JSON. :param data: The body for the response :type data: string :returns: A serialized version of the data :rtype: string

dokomoforms.handlers.api.v0.submissions module

TornadoResource class for dokomoforms.models.submission.Submission.

class dokomoforms.handlers.api.v0.submissions.SubmissionResource(*args, **kwargs)[source]

Bases: dokomoforms.handlers.api.v0.base.BaseResource

Restless resource for Submissions.

BaseResource sets the serializer, which uses the dokomo models’ ModelJSONEncoder for json conversion.

create()[source]

Create a new submission.

Uses the current_user_model (i.e. logged-in user) as creator.

default_sort_column_name = 'save_time'
detail(submission_id)[source]

Allow CSV export of a single submission.

is_authenticated()[source]

Allow unauthenticated POSTs under the right circumstances.

objects_key = 'submissions'
resource_type

alias of Submission

wrap_list_response(data)[source]

Allow CSV export of submission data.

This method adds CSV export functionality on top of the JSON list wrapping of BaseResource.wrap_list_response.

dokomoforms.handlers.api.v0.submissions.get_submission_for_handler(tornado_handler, submission_id)[source]

Maybe a handler needs a submission from the API.

dokomoforms.handlers.api.v0.surveys module

TornadoResource class for dokomoforms.models.survey.Survey.

class dokomoforms.handlers.api.v0.surveys.SurveyResource(*args, **kwargs)[source]

Bases: dokomoforms.handlers.api.v0.base.BaseResource

Restless resource for Surveys.

BaseResource sets the serializer, which uses the dokomo models’ ModelJSONEncoder for json conversion.

__init__(*args, **kwargs)[source]

Make submit return 201.

activity(survey_id)[source]

Get activity for a single survey.

activity_all()[source]

Get activity for all surveys.

create()[source]

Create a new survey.

Uses the current_user_model (i.e. logged-in user) as creator.

default_sort_column_name = 'created_on'
detail(survey_id)[source]

Return the given survey.

Public surveys don’t require authentication. Enumerator-only surveys do required authentication, and the user must be one of the survey’s enumerators or an administrator.

http_methods = {'list_submissions': {'GET': 'list_submissions'}, 'list': {'POST': 'create', 'DELETE': 'delete_list', 'PUT': 'update_list', 'GET': 'list'}, 'activity': {'GET': 'activity'}, 'submit': {'POST': 'submit'}, 'stats': {'GET': 'stats'}, 'activity_all': {'GET': 'activity_all'}, 'detail': {'POST': 'create_detail', 'DELETE': 'delete', 'PUT': 'update', 'GET': 'detail'}}
is_authenticated()[source]

GET detail is allowed unauthenticated.

list_submissions(survey_id)[source]

List all submissions for a survey.

objects_key = 'surveys'
resource_type

alias of Survey

stats(survey_id)[source]

Get stats for a survey.

submit(survey_id)[source]

Submit to a survey.

dokomoforms.handlers.api.v0.surveys.get_survey_for_handler(tornado_handler, survey_id)[source]

Maybe a handler needs a survey from the API.

Module contents

Handlers for the API endpoints.

class dokomoforms.handlers.api.v0.ModelJSONSerializer[source]

Bases: restless.serializers.JSONSerializer

Drop-in replacement for the restless-supplied JSONSerializer.

Uses dokomo’s ModelJSONEncoder in order to correctly serialize models to JSON.

serialize(data)[source]

The low-level serialization.

Underpins serialize, serialize_list & serialize_detail. Has no built-in smarts, simply dumps the JSON. :param data: The body for the response :type data: string :returns: A serialized version of the data :rtype: string

class dokomoforms.handlers.api.v0.BaseResource(*args, **kwargs)[source]

Bases: restless.tnd.TornadoResource

Set up the basics for the model resource.

BaseResource does some basic configuration for the restless resources. - sets the base request handler class which is used by the resources - providing reference to the ORM session via request handler - inserting a serializer for dokomo Models - setting up authentication

build_response(data, status=200)[source]

Finish the Tornado response.

This takes into account non-JSON content-types.

content_type

The format specified in the request.

current_user

The handler’s current_user.

current_user_model

The handler’s current_user_model.

default_sort_column_name

The default ORDER BY column name for list responses.

delete(model_id)[source]

Set the deleted attribute to True. Does not destroy the instance.

detail(model_id)[source]

Return a single instance of a model.

handle_error(err)[source]

Generate a serialized error message.

If the error came from Tornado, pass it along as such. Otherwise, turn certain expected errors into 400 BAD REQUEST instead of 500 INTERNAL SERVER ERROR.

is_authenticated(admin_only=True)[source]

Return whether the request has been authenticated.

list(where=None)[source]

Return a list of instances of this model.

Given a model class, build up the ORM query based on query params and return the query result.

objects_key

The key for list responses.

query_modifiers_applied

Whether there were any modifiers applied to the query.

resource_type

The model class for the resource.

serializer = <dokomoforms.handlers.api.v0.serializer.ModelJSONSerializer object>
session

The handler’s session.

update(model_id)[source]

Update a model.

wrap_list_response(data)[source]

Wrap a list response in a dict.

Takes a list of data & wraps it in a dictionary (within the objects key). For security in JSON responses, it’s better to wrap the list results in an object (due to the way the Array constructor can be attacked in Javascript). See http://haacked.com/archive/2009/06/25/json-hijacking.aspx/ & similar for details. Overridable to allow for modifying the key names, adding data (or just insecurely return a plain old list if that’s your thing). :param data: A list of data about to be serialized :type data: list :returns: A wrapping dict :rtype: dict

class dokomoforms.handlers.api.v0.SurveyResource(*args, **kwargs)[source]

Bases: dokomoforms.handlers.api.v0.base.BaseResource

Restless resource for Surveys.

BaseResource sets the serializer, which uses the dokomo models’ ModelJSONEncoder for json conversion.

__init__(*args, **kwargs)[source]

Make submit return 201.

activity(survey_id)[source]

Get activity for a single survey.

activity_all()[source]

Get activity for all surveys.

create()[source]

Create a new survey.

Uses the current_user_model (i.e. logged-in user) as creator.

default_sort_column_name = 'created_on'
detail(survey_id)[source]

Return the given survey.

Public surveys don’t require authentication. Enumerator-only surveys do required authentication, and the user must be one of the survey’s enumerators or an administrator.

http_methods = {'list_submissions': {'GET': 'list_submissions'}, 'list': {'POST': 'create', 'DELETE': 'delete_list', 'PUT': 'update_list', 'GET': 'list'}, 'activity': {'GET': 'activity'}, 'submit': {'POST': 'submit'}, 'stats': {'GET': 'stats'}, 'activity_all': {'GET': 'activity_all'}, 'detail': {'POST': 'create_detail', 'DELETE': 'delete', 'PUT': 'update', 'GET': 'detail'}}
is_authenticated()[source]

GET detail is allowed unauthenticated.

list_submissions(survey_id)[source]

List all submissions for a survey.

objects_key = 'surveys'
resource_type

alias of Survey

stats(survey_id)[source]

Get stats for a survey.

submit(survey_id)[source]

Submit to a survey.

dokomoforms.handlers.api.v0.get_survey_for_handler(tornado_handler, survey_id)[source]

Maybe a handler needs a survey from the API.

class dokomoforms.handlers.api.v0.SubmissionResource(*args, **kwargs)[source]

Bases: dokomoforms.handlers.api.v0.base.BaseResource

Restless resource for Submissions.

BaseResource sets the serializer, which uses the dokomo models’ ModelJSONEncoder for json conversion.

create()[source]

Create a new submission.

Uses the current_user_model (i.e. logged-in user) as creator.

default_sort_column_name = 'save_time'
detail(submission_id)[source]

Allow CSV export of a single submission.

is_authenticated()[source]

Allow unauthenticated POSTs under the right circumstances.

objects_key = 'submissions'
resource_type

alias of Submission

wrap_list_response(data)[source]

Allow CSV export of submission data.

This method adds CSV export functionality on top of the JSON list wrapping of BaseResource.wrap_list_response.

dokomoforms.handlers.api.v0.get_submission_for_handler(tornado_handler, submission_id)[source]

Maybe a handler needs a submission from the API.

class dokomoforms.handlers.api.v0.UserResource(*args, **kwargs)[source]

Bases: dokomoforms.handlers.api.v0.base.BaseResource

Restless resource for Users.

create()[source]

Create a new user.

default_sort_column_name = 'name'
objects_key = 'users'
resource_type

alias of User

update(user_id)[source]

Update a user.

class dokomoforms.handlers.api.v0.NodeResource(*args, **kwargs)[source]

Bases: dokomoforms.handlers.api.v0.base.BaseResource

Restless resource for Nodes.

BaseResource sets the serializer, which uses the dokomo models’ ModelJSONEncoder for json conversion.

create()[source]

Create a new node.

default_sort_column_name = 'last_update_time'
objects_key = 'nodes'
resource_type

alias of Node

class dokomoforms.handlers.api.v0.PhotoResource(*args, **kwargs)[source]

Bases: dokomoforms.handlers.api.v0.base.BaseResource

Restless resource for Photos.

create()[source]

Create a Photo. Must match an existing PhotoAnswer.

default_sort_column_name = 'created_on'
is_authenticated()[source]

Allow unauthenticated POSTs.

objects_key = 'photos'
resource_type

alias of Photo