Interfaces

experimental area

Tools for automating interactions.

Version: 0.1.0-x

AJAX

experimental interface

Conveniently and consistently register AJAX views across a project.

Component Reference: AJAX

interfaces

class superdjango.interfaces.ajax.interfaces.AJAX[source]

Bases: object

Capture AJAX views.

# settings.py
INSTALLED_APPS = [
    "superdjango.ajax.apps.AutoConfig",
]

# urls.py
from superdjango.ajax import ajax

urlpatterns = [
    # ...
    path('ajax/', include(ajax.get_urls())),
    # ...
]
__init__()[source]

Initialize an AJAX registry.

get_urls()[source]

Get the URLs for the site’s AJAX views.

Return type

list

register(app_name, viewset)[source]

Register an AJAX viewset.

Parameters
  • app_name (str) – The name of the app.

  • viewset (ViewSet) – The viewset class to register.

Raise

ViewAlreadyExists

CLI

experimental library

Utilities for working with command line interfaces.

Component Reference: CLI

constants

superdjango.interfaces.cli.constants.DEFAULT_OUTPUT_FORMAT = 'plain'

The default output format.

class superdjango.interfaces.cli.constants.EXIT[source]

Bases: object

Constants for standardizing shell exit codes.

superdjango.interfaces.cli.constants.EXIT_FAILURE = 1

A shortcut for a shell exit code of “failure”.

superdjango.interfaces.cli.constants.EXIT_SUCCESS = 0

A shortcut for a shell exit code of “success”.

superdjango.interfaces.cli.constants.EXIT_UNKNOWN = 99

An unknown shell exit code.

class superdjango.interfaces.cli.constants.OUTPUT_FORMAT[source]

Bases: object

The output formats supported by python-tabulate.

The MARKDOWN format is the same as pipe.

superdjango.interfaces.cli.constants.OUTPUT_FORMAT_CHOICES = ['fancy_grid', 'github', 'grid', 'html', 'jira', 'latex', 'latex_booktabs', 'latex_raw', 'markdown', 'mediawiki', 'moinmoin', 'orgtbl', 'pipe', 'plain', 'presto', 'psql', 'rst', 'simple', 'textile', 'youtrack']

Command line choices for all supported output formats.

superdjango.interfaces.cli.constants.OUTPUT_FORMAT_COMMON_CHOICES = ['html', 'markdown', 'plain', 'rst', 'simple']

Command line choices for the most common output formats.

library

class superdjango.interfaces.cli.library.Row(number=None, values=None)[source]

Bases: object

A row in tabular (command line) output.

__init__(number=None, values=None)[source]

Initialize a row.

Parameters
  • number (int) – The row number.

  • values (list) – The values included in the row.

class superdjango.interfaces.cli.library.Table(headings=None, formatting='plain', title=None)[source]

Bases: object

A table for tabular (command line) output.

__init__(headings=None, formatting='plain', title=None)[source]

Initialize a table.

Parameters
  • headings (list[str]) – A list of headings.

  • formatting (str) – The output format of the table.

  • title (str) – The title of the table.

Note

Output is generated by python-tabulate. See https://bitbucket.org/astanin/python-tabulate/

add(values)[source]

Add a row to the table.

Parameters

values (list) – The values of the row.

to_string()[source]

Get the table as string output.

Return type

str

superdjango.interfaces.cli.library.abort(code=1, message=None)[source]

Stop processing a command line script and optionally issue a message.

Parameters
  • code (int) – The exit code.

  • message (str) – The message, if any to be displayed.

Hooks

experimental interface

A simple alternative to Django’s signal handling system.

Component Reference: Hooks

library

class superdjango.interfaces.hooks.library.HookManager(module_name='hooks')[source]

Bases: object

Serves as registry and interface for hooks.

__init__(module_name='hooks')[source]

Initialize the hook manager.

Parameters

module_name – The name of the module to look for hooks.

get(name)[source]

An alias for get_hooks().

get_hooks(name)[source]

Get the hooks defined for the named hook.

Parameters

name (str) – The name of the hook.

Return type

list

Returns

A list of hooks (callables) for the given name.

load()[source]

Load hook modules if not already loaded.

register(function, hook=None, sort_order=0)[source]

Register a function as a hook.

Parameters
  • function – The function to call for the hook.

  • hook (str) – The name of the hook. Defaults to the name of the function.

  • sort_order (int) – The order in which the function should be called.

REST

experimental interface

A lightweight interface for easily registering Django REST Framework resources.

Component Reference: REST

interfaces

class superdjango.interfaces.rest.interfaces.API[source]

Bases: object

Capture REST API resources.

# settings.py
INSTALLED_APPS = [
    "superdjango.interfaces.reset.apps.AutoConfig",
]

# urls.py
from rest_framework import routers
from superdjango.interfaces.reset import api

router = routers.DefaultRouter()

urlpatterns = [
    # ...
    path('api/v1/', include(api.get_urls(router))),
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
]
__init__()[source]

Initialize self. See help(type(self)) for accurate signature.

get_urls(router, version=None)[source]

Add discovered APIs to the router.

Parameters
  • router (BaseType[BaseRouter]) – The Django REST Framework router instance.

  • version (int | str) – The API version, if any, by which to filter the URLs.

Returns

router.urls

register(app_name, rest_class)[source]

Register an API.

Parameters
  • app_name (str) – The name of the app.

  • rest_class (REST) – The REST classs to register.

Raise

ViewAlreadyExists

library

class superdjango.interfaces.rest.library.REST[source]

Bases: object

A utility class for collecting REST API information.

# <app_name>/api/serializers.py
from rest_framework import serializers
from ..models import Category, Objective

class CategorySerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = Category
        fields = (
            "label",
            "value",
        )

class ObjectiveSerializer(serializers.HyperlinkedModelSerializer):

    categories = serializers.HyperlinkedIdentityField(view_name="objectives-api-category-detail")

    client = serializers.HyperlinkedIdentityField(view_name="clients-api-client-detail")

    class Meta:
        model = Objective
        fields = (
            "annual_resolved_value",
            "cached_percentage_complete",
            "categories",
            "client",
            "description",
            "end_date",
            "percentage_complete_override",
            "priority",
            "stage",
            "start_date",
            "title",
        )

# <app_name>/api/views.py
from rest_framework import viewsets
from ..models import Category, Objective
from .serializers import CategorySerializer, ObjectiveSerializer


class CategoryViewSet(viewsets.ModelViewSet):
    queryset = Category.objects.all()
    serializer_class = CategorySerializer


class ObjectiveViewSet(viewsets.ModelViewSet):
    queryset = Objective.objects.all()
    serializer_class = ObjectiveSerializer
# <app_name>/api/__init__.py
from superdjango.interfaces.reset import api, REST
from .views import CategoryViewSet, ObjectiveViewSet

class ObjectiveREST(REST):
    # basename = "objectives-api"
    # prefix = "objectives"
    viewsets = [
        CategoryViewSet,
        ObjectiveViewSet,
    ]


api.register("objectives", ObjectiveREST)

basename and prefix are optional and default to <app_name>-api and <app_name> respectively. In the example above, category detail is available at objectives-api-category-detail and objective detail at objectives-api-objective-detail.