Jump to >

djblets.webapi.managers

Managers for API-related database models.

class WebAPITokenManager[source]

Bases: django.db.models.manager.Manager

Manages WebAPIToken models.

generate_token(user, max_attempts=20, note=None, policy={}, auto_generated=False, **kwargs)[source]

Generate a WebAPIToken for a user.

This will attempt to construct a unique WebAPIToken for a user.

Since a collision is possible, it will try up to a certain number of times. If it cannot create a unique token, a WebAPITokenGenerationError will be raised.

Parameters:
  • user (django.contrib.auth.models.User) – The user who will own the token.
  • max_attempts (int, optional) – The maximum number of attempts to try to find a non-conflicting token. Defaults to 20.
  • note (unicode, optional) – A note describing the token.
  • policy (dict, optional) – The policy document describing what this token can access in the API. By default, this provides full access.
  • auto_generated (bool, optional) –

    Whether or not the token is being automatically generated.

    This parameter does not affect token generation. It’s value will solely be used in the webapi_token_created signal.

  • **kwargs (dict) – Additional keyword arguments representing fields in the token. These will be set on the token object.
Returns:

The generated API token.

Return type:

djblets.webapi.models.BaseWebAPIToken

Raises:

djblets.webapi.errors.WebAPITokenGenerationError – The token was not able to be generated after max_attempts number of collisions were hit.