Views¶
Status View¶
Status view of the confirm Django app.
- class confirm.django.views.status.StatusView(**kwargs)¶
Extensible status view.
The status view is a
text/plain
view which provides different status items. Its purpose is mainly for the following topics:Automated testing in the CI/CD pipeline
Automated monitoring of a live system
Quick summary of the system status for users
The implementation is quite straight forward. When a
GET
request is coming in, theget()
method will be called. Theget()
method will then lookup all*_status
properties of the class, query them for their status and render the return value of them into the HTTP response.All
*_status
properties can eitherNone
or atuple
with these items:An exception in case of an error, or
None
if everything is OKAn additional info dict
In case the first item is
None
, the status is interpreted asOK
. For every other value, the status is set toFAIL
. Regardless of the status, the info is always rendered in the response.Note
A word about automated testing:
When Django projects are developed, automated testing belongs to the early executed tasks in the CI/CD pipelines. This design ensures we don’t deploy anything that is “broken”, thus saving resources and ensuring systems are online until everything is ready / fixed. However, because testing is done prior to the deployment, there are no peripheral systems up & running while testing the code. Thus things like external databases, Celery workers & brokers are not running & connected until the deployment is made.
This is where this status view comes into place. As soon as the systems are deployed, the CI/CD pipeline will query this status view and ensures that it returns a
200
status code.- property celery_status¶
Checks the Celery status by checking the broker connection and pinging the workers.
Important
This status is only available when the
CELERY_APPLICATION
setting is defined.- Returns:
The broker status
- Return type:
tuple
- property command_status¶
Check the command which was used to start Django.
- Returns:
The environment status
- Return type:
tuple
- property database_status¶
Checks the database status by verifying the connection cursor and executing a
SELECT 1
on the database.- Returns:
The database status
- Return type:
tuple
- property environment_status¶
Simply gets the environment variables for debugging purposes and doesn’t do any real checking.
- Returns:
The environment variables
- Return type:
tuple
- get(request)¶
Return response to HTTP GET request.
This is basically a wrapper which calls all the
*_status
properties of this view and renders the response in a nice text view.In case all status are
OK
, the method will return a200 OK
response. However, if one status isFAIL
, the method will return a500 Internal Server Error
.- Parameters:
request (django.http.HttpRequest) – The HTTP request
- Returns:
The HTTP response
- Return type:
django.http.HttpResponse
- property settings_status¶
Simply gets the settings for debugging purposes and doesn’t do any real checking.
- Returns:
The settings
- Return type:
tuple
Generic Views¶
List Views¶
Generic list view.
- class confirm.django.views.list.ListView(**kwargs)¶
A generic Django list view which uses enhanced / generic template names to lookup the Django template.
- get_context_object_name(object_list)¶
Get the name of the item to be used in the context.
- Parameters:
object_list (list) – The object list
- Returns:
The context object name
- Return type:
str
Detail Views¶
Generic detail view.
- class confirm.django.views.detail.DetailView(**kwargs)¶
A generic Django detail view which uses enhanced / generic template names to lookup the Django template.
Create Views¶
Generic edit views.
- class confirm.django.views.create.CreateView(**kwargs)¶
A generic Django create view which uses enhanced / generic template names to lookup the Django template.
View Mixins¶
Mixins for Django views.
- class confirm.django.views.mixins.GenericTemplateNamesMixin¶
Mixin which provides custom lookup paths for Django templates.
- get_template_names()¶
Return a list of custom lookup paths for Django templates. The list will include the following paths in the following order:
The path specified in the
template_name
class property<app>/<subdir>/<doc>.html
<app>/generic/<doc>.html
generic/<doc>.html
The values are retreived this way:
app
: Eitherself.template_name_app
or auto detected from the Python modulesubdir
: Eitherself.template_name_subdir
or auto detected from the modeldoc
: Eitherself.template_name_doc
or auto detected- Returns:
The template names
- Return type:
list
- class confirm.django.views.mixins.SnakeCaseObjectNameMixin¶
Mixin which sets the context object name of a model as snake case.
- get_context_object_name(obj)¶
Get the name of the item to be used in the context.
- Parameters:
obj (django.db.models.Model) – The model object
- Returns:
The context object name
- Return type:
str