Jump to >

reviewboard.hostingsvcs.forms

class HostingServiceAuthForm(data=None, hosting_service_cls=None, hosting_account=None, local_site=None, *args, **kwargs)[source]

Bases: django.forms.forms.Form

Form for handling authentication information for a hosting account.

This takes care of collecting additional details needed for authenticating an account, including that information with the account credentials (if needed by the hosting service).

By default, this will retain the existing username, password, and two-factor auth fields. Those can be replaced, but the field names should remain the same.

Unlike HostingServiceForm, field names on this class do not need to include a service-specific prefix, as they will not conflict with other forms. The field names will be used for the data storage. How a subclass chooses to name these fields is up to them.

Subclasses can define a Meta class on the form containing help_texts and labels attributes, mapping field names to custom help text or labels. This is useful for providing more specific instructions for setting authentication data for a given service without having to override the built-in fields. For example:

from django.utils.translation import ugettext_lazy as _
from reviewboard.hostingsvcs.forms import HostingServiceAuthForm


class MyAuthForm(HostingServiceAuthForm):
    class Meta:
        labels = {
            'hosting_account_username': 'API Access ID',
            'hosting_account_password': 'API Secret Key',
        }

        help_texts = {
            'hosting_account_username': _(
                'Access ID used for the API. This can be found in '
                'your FooService account settings.'
            ),
            'hosting_account_password': _(
                'Secret key used for the API. This can be found in '
                'your FooService account settings.'
            ),
        }
__init__(data=None, hosting_service_cls=None, hosting_account=None, local_site=None, *args, **kwargs)[source]

Initialize the authentication form.

Parameters:
  • data (dict, optional) – Data posted for the form.
  • hosting_service_cls (type) –

    The hosting service class (subclass of HostingService) that works with this form.

    This must be provided, or an assertion error will be raised.

  • hosting_account (reviewboard.hostingsvcs.models.HostingServiceAccount, optional) – The hosting service account being updated, if any. If None, a new one will be created.
  • local_site (reviewboard.site.models.LocalSite, optional) – The Local Site used for accounts on the service.
  • *args (tuple) – Extra positional arguments for the form.
  • **kwargs (dict) – Extra keyword arguments for the form.
Raises:

ValueError – One or more of the paramters are missing or not valid for the provided hosting account. Details are given in the error message.

get_initial_data()[source]

Return initial data for the form, based on the hosting account.

This will return initial data for the fields, generally pulled from the hosting account. This will be used when relinking a hosting account that’s no longer authorized.

Generally, sensitive information, like passwords, should not be provided.

By default, the username and hosting_url fields will have data provided. Subclasses can override this to present more initial data.

This is only called if the form was provided a hosting account during construction.

Returns:Initial data for the form.
Return type:dict
get_credentials()[source]

Return credentials from the form.

This should return the data that will be stored along with the HostingServiceAccount.

The username, password, and two_factor_auth_code values are treated specially during the creation and authentication of the account, and should be provided for most standard hosting services.

All values will be provided to HostingService.authenticate, which will be responsible for making use of these values and storing them on the account.

Subclasses should call the parent method and use their results as a base, if they reuse any of the built-in fields.

Returns:A dictionary of credentials used to authenticate the account and talk to the API.
Return type:dict
save(allow_authorize=True, force_authorize=False, extra_authorize_kwargs=None, trust_host=False, save=True)[source]

Save the hosting account and authorize against the service.

This will create or update a hosting account, based on the information provided in the form and to this method.

is_valid() must be called prior to saving.

Parameters:
  • allow_authorize (bool, optional) – If True (the default), the account will be authorized against the hosting service. If False, only the database entry for the account will be affected.
  • force_authorize (bool, optional) – Force the account to be re-authorized, if already authorized.
  • extra_authorize_kwargs (dict, optional) – Additional keyword arguments to provide for the HostingService.authorize() call.
  • trust_host (bool, optional) – Whether to trust the given host, even if the linked certificate is invalid or self-signed.
  • save (bool, optional) –

    Whether or not the created account should be saved.

    This is intended to be used by subclasses who want to add additional data to the resulting hosting account before saving.

    If this is False, the caller must ensure the resulting hosting account is saved.

Returns:

The updated or created hosting service account.

Return type:

reviewboard.hostingsvcs.models.HostingServiceAccount

Raises:
authorize(hosting_account, hosting_service_id, username=None, local_site_name=None, **kwargs)[source]

Authorize the service.

Parameters:
  • hosting_account (reviewboard.hostingsvcs.models.HostingServiceAccount) – The hosting service account.
  • hosting_service_id (unicode) – The ID of the hosting service.
  • username (unicode) – The username for the account.
  • local_site_name (unicode, optional) – The Local Site name, if any, that the account should be bound to.
  • **kwargs (dict) – Keyword arguments to pass into the service authorize function.
clean_hosting_url()[source]

Clean the hosting URL field.

Returns:A string containing the hosting URL, or None.
Return type:unicode
class HostingServiceForm(data=None, files=None, auto_id=u'id_%s', prefix=None, initial=None, error_class=<class 'django.forms.util.ErrorList'>, label_suffix=None, empty_permitted=False)[source]

Bases: django.forms.forms.Form

load(repository)[source]
save(repository, *args, **kwargs)[source]