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 containinghelp_texts
andlabels
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
andhosting_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
, andtwo_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={})[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. IfFalse
, 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.
Returns: The updated or created hosting service account.
Return type: reviewboard.hostingsvcs.models.HostingServiceAccount
Raises: reviewboard.hostingsvcs.errors.AuthorizationError
– Information needed to authorize was missing, or authorziation failed.reviewboard.hostingsvcs.errors.TwoFactorAuthCodeRequiredError
– A two-factor authentication code is required to authorize the account. A code will need to be provided to the form.
- allow_authorize (bool, optional) – If
-