Generic Helper Views

These classes are for generic help building a view to be displayed by Cantemo.

These are the base views for the Portal framework responsible for returning response to a Http request.

class portal.generic.baseviews.CView(**kwargs)

This is the base view all 2013 class based views.

Inherits from APIView which inherits from Django Generic View.

check_permissions(request)

Wrapper around check_permission from rest_framework.

If the request is not authenticated and is not ajax or API request (JSON or XML requested), redirect to Portal login page.

handle_exception(exc)

Wrapper around handle_exception from rest_framework catching PortalRedirectException

class portal.generic.baseviews.ClassView(request, *args, **kwargs)

This is the base view class, you have to pass in a template that you wish to show whether the loginpass is set to show pages that don’t require a login and it will accept other parameters.

These class based views are based upon clsviews by Zachery Voase.

Http404(request, extra_context=None, *args, **kwargs)

Return a 404 with context

apply_extra_context(extra_context, context)

Add items from extra_context dict to the given context, calling any callables in extra_context. Return the updated context. This is for sending back into the page.

binary_response(message, statuscode=200, *args, **kwargs)

Returns a binary response with a custom status code (defaults to 200 OK).

custom_responsetype(message, statuscode, *args, **kwargs)

This creates a custom404 or other response type with a message. Typical usage for sending a 400, 404 or 500 in response to an Ajax call.

Content Type can be set with ‘content_type = value’ for example content_type = ‘application/xml’

Builds up a list of cookies to delete when creating the response

econtext_processors = <ExtensionPoint IContextProcessor env=cantemo>
esimple_context_processors = <ExtensionPoint ISimpleContextProcessor env=cantemo>
etemplate_parser = <ExtensionPoint ITemplateChooser env=cantemo>
get_context(request)

Returns the Context as RequestContext to make sure that contextprocessors are used

get_template(template_name)

Returns the loaded Template, either from the theme for a user, or the generic template settings.

get_var(var_to_get)

Try and get a variable either from the request or using the raw request as parsed through when RESTful clients are using the system.

When POSTing (or PUTing) data from an application and not a web browser Django puts the response into request.body. We need to get it and grab it out.

json_response(message, statuscode, *args, **kwargs)

Returns a JSON response with a custom status code. Typical usage for sending data back to an Ajax call with a response code.

main(request, template_name=None, extra_context=None, status_code=200, cookie=None, content_type=None)

Called from processRequest to make a standard response.

processRequest(request, template_name, extra_context=None, *args, **kwargs)

Override this function when subclassing to provide extra functionality if you just wish to leave the __call__ function standard.

reDirect(request, redirect_location=None, extra_context=None)

Redirects to another template if needed

Builds up a list of cookies to delete when creating the response

xml_response(message, statuscode=200, *args, **kwargs)

Returns a XML response with a custom status code (defaults to 200 OK). The xml can be a string in the ‘message’ variable or it can be any class that takes the serializeDocument method that excepts the “pretty” argument (such as a eulxml.xmlmap.XmlObject)

class portal.generic.baseviews.DocStringSchema(manual_fields=None)

This class extends Django Rest Framework’s AutoSchema to take a schema definition as yaml from the docstring and add this to the schema. This is needed for backwards compatibility with the API documentation we already have in Portal.

Generate coreapi.Link for self.view, path and method.

This is the main _public_ access point.

Parameters:

  • path: Route path for view from URLConf.

  • method: The HTTP request method.

  • base_url: The project “mount point” as given to SchemaGenerator

exception portal.generic.baseviews.PortalRedirectException(location)

Exception signaling that the browser should be redirected to another location

portal.generic.baseviews.apply_extra_context(caller, extra_context, context)

Add items from extra_context dict to the given context, calling any callables in extra_context. Return the updated context. This is for sending back into the page.