pcari.views module¶
This module defines the application’s views, which are needed to render pages.
- These views can be broadly categorized into two groups:
- API endpoints that can dynamically send and receive data (typically as JSON). The client can use these endpoints to operate asynchronously.
- HTML pages that show up to users. These pages are typically largely static and lend themselves to caching by service workers.
References
-
pcari.views.
generate_ratings_matrix
()¶ Fetch quantitative question ratings in the form of a
numpy
matrix.Each row in the matrix represents the ratings of one respondent, and each column represents the ratings for one question.
Returns: - Tuple of three items:
respondent_id_map
(dict): A length-m map from respondent identifiers to matrix row indicies.question_id_map
(dict): A length-n map from quantitative question identifiers to matrix column indicies.ratings_matrix
(numpy.ndarray): An m by n NumPy array of ratings.
Only active respondents, active questions, and active ratings are used (see
pcari.models.History.active
).Return type: tuple
-
pcari.views.
normalize_ratings_matrix
(ratings_matrix)¶ Normalize a ratings matrix so the ellipsoid of ratings is centered at the origin, and missing values are imputed with zero. (That is, the mean of the column before centering.)
Parameters: ratings_matrix (numpy.ndarray) – An m by n matrix of ratings (as provided by generate_ratings_matrix()
).Returns: An m by n matrix of normalized ratings with no np.nan
values.Return type: numpy.ndarray
-
pcari.views.
calculate_principal_components
(normalized_ratings, num_components=2)¶ Calculate the principal components of a normalized ratings matrix.
Parameters: - normalized_ratings (numpy.ndarray) – An m by n normalized ratings
matrix (as provided by
normalize_ratings_matrix()
). - num_components (int) – The number of principal components to select (p).
Returns: A p by n matrix whose rows are principal components.
Return type: numpy.ndarray
- normalized_ratings (numpy.ndarray) – An m by n normalized ratings
matrix (as provided by
-
pcari.views.
fetch_comments
(request, *args, **kwargs)¶ Fetch a list of comments as JSON.
Parameters: request – May contain a limit GET parameter that specifies how many comments to get (by default: 300). Very high limits may decrease performance noticeably. Returns: A JsonResponse
containing an JSON object of the form:{ "<comment.id>": { "msg": "<comment.message>", "sem": <comment.score_sem>, "pos": <author question ratings projection>, "tag": "<comment.tag>", "qid": <comment.question_id> }, ... }
The
pos
property is calculated by projecting the quantitative question ratings vector of the comment’s author onto the first two principal components of the question ratings dataset (fromcalculate_principal_components()
). This property is a list containing two numbers: the first and second projections, respectively.
-
pcari.views.
fetch_quantitative_questions
(request, *args, **kwargs)¶ Fetch quantitative question data as JSON.
Parameters: request – This parameter is ignored. Returns: A JsonResponse
containing a JSON object of the form:[ { "id": <question.id>, "prompts": { "<language-code>": "<translated question.prompt>", ... }, "left-anchors": { "<language-code>": "<translated question.left_anchor>", ... }, "right-anchors": { "<language-code>": "<translated question.right_anchor>", ... }, "min-score": <question.min_score>, "max-score": <question.max_score>, "input-type": <question.input_type>, "order": <question.order>, "show-statistics": <question.show-statistics> }, ... ]
Each language code is obtained from
settings.LANGUAGES
.
-
pcari.views.
fetch_option_questions
(request, *args, **kwargs)¶ Fetch option question data as JSON.
Parameters: request – This parameter is ignored. Returns: A JsonResponse
containing a JSON object with the following structure:{ "id": <question.id>, "prompts": { "<language-code>": "<translated question.prompt>", ... }, "options": { "<language-code>": ["<translated member of question.options>", ...], ... }, "input-type": "<question.input_type>", "order": <question.order> }
-
pcari.views.
fetch_qualitative_questions
(request, *args, **kwargs)¶ Fetch qualitative question data as JSON.
Parameters: request – This parameter is ignored. Returns: A JsonResponse
containing a JSON object of the form:{ "<question.id>": { "<language-code>": "<translated question.prompt>", ... }, ... }
Each language code is obtained from
settings.LANGUAGES
.
-
pcari.views.
fetch_question_ratings
(request, *args, **kwargs)¶ Fetch quantitative question ratings as JSON.
Parameters: request – This parameter is ignored. Returns: A JsonResponse
containing a JSON object of the form:{ "<rating.id>": { "qid": <question.id>, "score": <rating.score> }, ... }
-
pcari.views.
save_response
(request, *args, **kwargs)¶ Write a single user’s responses as a JSON object to the database.
The request body should contain the string representation of a JSON object (that is, a Python dict) of the following form:
{ "question-ratings": { "<question.id>": <score>, ... }, "question-choices": { "<question.id>": "<choice>", ... }, "comments": [ "<question.id>": "<message>", ... ], "comment-ratings": [ "<comment.id>": <score>, ... ], "respondent-data": { "uuid": "<uuid>", "age": <age>, "gender": "<gender>", "location": "<location>", "language": "<language-code>", "submitted-personal-data": <bool>, "completed-survey": <bool> } }
No validation is performed. Validation through
full_clean
should be performed during analysis.Returns: A HttpResponse
with a status code of 200 if the data were saved successfully, or aHttpResponseBadRequest
with a status code of 400 otherwise. The general-purpose bad request response is returned when the data were successfully received, but contained syntactical or logical errors (for instance, providing theid
of a nonexistent question, or malformed JSON). In that case, no new instances are written to the database, and the client should not send another request without modifications to the payload.
-
pcari.views.
export_data
(queryset, data_format=u'csv')¶ Create and write data to a response as a file for download.
Parameters: - data_format (str) – The file format the data should be exported as.
Current options are:
csv
(default),xlsx
. - queryset – The instances to export.
Returns: An
HttpResponse
with the requested data as an attached file, or anHttpResponseBadRequest
with a status code of 400 with an invaliddata_format
.- data_format (str) – The file format the data should be exported as.
Current options are:
-
pcari.views.
landing
(request, *args, **kwargs)¶ Render a landing page.
-
pcari.views.
qualitative_questions
(request, *args, **kwargs)¶ Render a page asking respondents for comments (i.e. suggestions).
-
pcari.views.
peer_responses
(request, *args, **kwargs)¶ Render a page showing respondents how others rated the quantitative questions.
-
pcari.views.
handle_page_not_found
(request, *args, **kwargs)¶ Render a page for HTTP 404 errors (page not found).
-
pcari.views.
handle_internal_server_error
(request, *args, **kwargs)¶ Render a page for HTTP 500 errors (internal server error).