Managers

Managers have the task of managing access to resources.

Note

Managers are modelled after Django’s ORM Managers.

For example, to access a group of fictional User resources you would use:

# Simple user of a manager
users = User.objects.all()

Here you access the objects manager on the User resource. The objects manager is known as the ‘default’ manager. Additional managers may also by provided. For example:

class User(Resource):
    ... define fields...

    # Note you need to explicitly define the 'objects' default
    # manager when you add custom managers
    objects = Manager()

    # Now add some custom managers
    active_users = Manager(filter=lambda u: u.is_active)
    inactive_users = Manager(filter=lambda u: not u.is_active)
    super_users = Manager(filter=lambda u: u.is_super_user)

Now you can use statements such as:

awesome_users = User.super_users.all()
total_active_users = User.active_users.count()

You can also extend the Manager class to provide both additional functionality and greater intelligence. For example:

class UserManager(Manager):

    def count(self):
        # Pull the count from the server rather than pulling all
        # users then counting them.
        json = self.api.get('/users/total_count')
        return json['total']

Or perhaps you want be able to perform custom actions on groups of Resources:

class LightManager(manager):

    def turn_on(self):
        for light in self.all():
            light.on = True
            light.save()

Todo

Implement support for pagination of resources

class repose.managers.Manager(decoders=None, results_endpoint=None, filter=None)

The base Manager class

api

Api

The Api instance

decoders

list[Decoder]

The decoders used to decode list data

model

Resource

The Resource class to be managed

results

list

The results as loaded from the API

results_endpoint

list

The results to be used to fetch results

__init__(decoders=None, results_endpoint=None, filter=None)

Initialise the Manager

Parameters:
  • decoders (list[Decoder]) – The decoders used to decode list data
  • results_endpoint (str) – The results to be used to fetch results. Defaults to Meta.endpoint_list
  • filter (callable) – The filter function to be applied to the results. Will be passed a single result and must return True/False if the result should be included/excluded in the results respectively.
all()

Return all results

count()

Return the total number of results

Returns:int

Note

This is a naive implementation of count() which simply retrieves all results and counts them. You should consider overriding this (as demoed above) if dealing with non-trivial numbers of results.

get(**endpoint_params)

Get a single resource

Parameters:endpoint_params (dict) – Parameters which should be used to format the Meta.endpoint string.
Returns:
Return type:Resource
get_decoders()

Return the decoders to be used for decoding list data

Returns:Manager.decoders by default
Return type:list[Decoder]
get_results_endpoint()

Get the results endpoint

Returns:results_endpoint as passed to __init__() or Meta.endpoint_list.
Return type:str