reviewboard.hostingsvcs.base.forms¶
Base hosting service configuration forms.
New in version 6.0: This replaces the old reviewboard.hostingsvcs.forms
module.
- class BaseHostingServiceAuthForm(*args, hosting_account: Optional[HostingServiceAccount] = None, **kwargs)[source]¶
Bases:
_HostingServiceSubFormMixin
,BaseRepositoryAuthSubForm
Base 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 gettext_lazy as _ from reviewboard.hostingsvcs.base.forms import BaseHostingServiceAuthForm 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.' ), }
Changed in version 6.0:
Moved from
reviewboard.hostingsvcs.forms
toreviewboard.hostingsvcs.base.forms
and renamed fromHostingServiceAuthForm
toBaseHostingServiceAuthForm
.
- __init__(*args, hosting_account: Optional[HostingServiceAccount] = None, **kwargs) None [source]¶
Initialize the authentication form.
- Parameters:
*args (
tuple
) – Additional positional arguments for the parent form.hosting_account (
reviewboard.hostingsvcs.models.HostingServiceAccount
, optional) –The hosting service account being updated, if any.
If
None
, a new one will be created.**kwargs (
dict
) – Additional keyword arguments for the parent form.
- Raises:
ValueError – One or more of the parameters are missing or not valid for the provided hosting account. Details are given in the error message.
- hosting_account: Optional[HostingServiceAccount]¶
The hosting service account being configured.
If
None
, a new one will be created when saving the form.
- get_initial_data() JSONDict [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:
- get_credentials() HostingServiceCredentials [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
BaseHostingService.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:
- save(allow_authorize: bool = True, force_authorize: bool = False, extra_authorize_kwargs: Optional[KwargsDict] = None, trust_host: bool = False, save: bool = True) HostingServiceAccount [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) – IfTrue
(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 theHostingService.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:
- Raises:
reviewboard.hostingsvcs.errors.AuthorizationError – Information needed to authorize was missing, or authorization 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.
- authorize(hosting_account: HostingServiceAccount, hosting_service_id: str, username: Optional[str] = None, local_site_name: Optional[str] = None, **kwargs) None [source]¶
Authorize the service.
- Parameters:
hosting_account (
reviewboard.hostingsvcs.models.HostingServiceAccount
) – The hosting service account.hosting_service_id (
str
) – The ID of the hosting service.username (
str
) – The username for the account.local_site_name (
str
, 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() Optional[str] [source]¶
Clean the hosting URL field.
- Returns:
A string containing the hosting URL, or
None
.- Return type:
- __annotations__ = {'hosting_account': 'Optional[HostingServiceAccount]', 'hosting_service_cls': 'Type[BaseHostingService]'}¶
- declared_fields = {'hosting_account_password': <django.forms.fields.CharField object>, 'hosting_account_two_factor_auth_code': <django.forms.fields.CharField object>, 'hosting_account_username': <django.forms.fields.CharField object>, 'hosting_url': <django.forms.fields.CharField object>}¶
- class BaseHostingServiceRepositoryForm(*args, hosting_service_cls: Type[BaseHostingService], **kwargs)[source]¶
Bases:
_HostingServiceSubFormMixin
,BaseRepositoryInfoSubForm
Base form for collecting information for a hosting service repository.
This is responsible for providing fields used to communicate with a particular hosting service, such as a registered organization name or ID on the service. There may be one global form (set in
BaseHostingService.form
or one per plan.Each field will be stored directly in
Repository.extra_data
, using the field’s name as the key.Subclasses are expected to prefix every field with the ID of the hosting service, to avoid conflicts.
Subclasses may also define a
Meta
class on the form containinghelp_texts
andlabels
attributes, mapping field names to custom help text or labels. This is useful if a hosting service has a base form for collecting details for each plan, and wants to customize the labels and help text for each subclass. For example:from django import forms from django.utils.translation import gettext_lazy as _ from reviewboard.hostingsvcs.base.forms import BaseHostingServiceRepositoryForm class MyServiceBaseForm(BaseHostingServiceRepositoryForm): myservice_owner = forms.CharField(max_length=64) class MyServiceOrgPlanForm(MyServiceBaseForm): class Meta: labels = { 'myservice_owner': _('User'), } help_texts = { 'myservice_owner': _( 'The username of the user owning the repository.' ), } class MyServicePersonalPlanForm(MyServiceBaseForm): class Meta: labels = { 'myservice_owner': _('Organization'), } help_texts = { 'myservice_owner': _( 'The ID of the organization owning the repository.' ), }
Changed in version 6.0:
Moved from
reviewboard.hostingsvcs.forms
toreviewboard.hostingsvcs.base.forms
and renamed fromHostingServiceForm
toBaseHostingServiceRepositoryForm
.
- get_initial_data() JSONDict [source]¶
Return initial data for the form.
This will load information from the repository’s
extra_data
into the form’s fields.- Returns:
Initial data for the form.
- Return type:
- __annotations__ = {}¶
- declared_fields = {}¶
- load(repository: Optional[Repository] = None, **kwargs) None [source]¶
Load information for the form.
By default, this will populate initial values returned in
get_initial_data()
. Subclasses can override this to set other fields or state as needed.- Parameters:
repository (
reviewboard.scmtools.models.Repository
, optional) –The repository being loaded.
This is scheduled to be deprecated. Subclasses should use the
repository
attribute instead.
- save(repository: Optional[Repository] = None, **kwargs) None [source]¶
Save information from the form back to the repository.
This will set each field in the repository’s
extra_data
.- Parameters:
repository (
reviewboard.scmtools.models.Repository
, optional) –The repository being loaded.
This is scheduled to be deprecated. Subclasses should use the
repository
attribute instead.