예제 #1
0
    def test_get_context_with_valid_value(self):
        """Tests get_context() when a valid value is supplied."""
        book = BookFactory()
        widget = RawIdWidget(Book)

        change_route_name = admin_urlname(Book._meta, 'change')
        change_url = reverse(change_route_name, args=(book.pk, ))

        changelist_route_name = admin_urlname(Book._meta, 'changelist')
        changelist_url = reverse(changelist_route_name)

        assert widget.get_context('test-widget', str(book.pk), {}) == {
            'link_label': str(book),
            'link_title': 'Look up book',
            'link_url': change_url,
            'related_url': f'{changelist_url}?_to_field=id',
            'widget': {
                'attrs': {
                    'class': 'vUUIDField',
                },
                'is_hidden': False,
                'name': 'test-widget',
                'required': False,
                'template_name': 'admin/widgets/foreign_key_raw_id.html',
                'type': 'text',
                'value': str(book.pk),
            },
        }
예제 #2
0
class AddAccessTokenForm(forms.Form):
    """Add access token form for the admin site."""

    adviser = forms.ModelChoiceField(
        Advisor.objects.all(),
        widget=RawIdWidget(Advisor),
        label='Adviser',
        help_text='The selected adviser must have an SSO email user ID set',
    )
    expires_in_hours = forms.IntegerField(
        max_value=24 * 7,
        initial=10,
        label='Expires in (hours)',
    )

    def clean_adviser(self):
        """Validate that the adviser has an SSO email user ID."""
        adviser = self.cleaned_data['adviser']
        if not adviser.sso_email_user_id:
            raise ValidationError(NO_SSO_EMAIL_USER_ID_MESSAGE,
                                  code='no_sso_email_user_id')

        return adviser

    def save(self):
        """Add the access token to the cache."""
        token = token_urlsafe()
        adviser = self.cleaned_data['adviser']
        timeout_hours = self.cleaned_data['expires_in_hours']
        timeout = timeout_hours * 60 * 60

        add_token_data_to_cache(token, adviser.email,
                                adviser.sso_email_user_id, timeout)
        return token, timeout_hours
예제 #3
0
 def test_get_context_without_value(self):
     """Tests get_context() when no value is supplied."""
     widget = RawIdWidget(Book)
     assert widget.get_context('test-widget', None, {}) == {
         'link_title': 'Look up book',
         'related_url': '/admin/support/book/?_to_field=id',
         'widget': {
             'attrs': {
                 'class': 'vUUIDField',
             },
             'is_hidden': False,
             'name': 'test-widget',
             'required': False,
             'template_name': 'admin/widgets/foreign_key_raw_id.html',
             'type': 'text',
             'value': None,
         },
     }
예제 #4
0
    def test_get_context_with_invalid_value(self, value):
        """Tests get_context() when an invalid value is supplied."""
        widget = RawIdWidget(Book)

        changelist_route_name = admin_urlname(Book._meta, 'changelist')
        changelist_url = reverse(changelist_route_name)

        assert widget.get_context('test-widget', value, {}) == {
            'link_label': '',
            'link_title': 'Look up book',
            'link_url': '',
            'related_url': f'{changelist_url}?_to_field=id',
            'widget': {
                'attrs': {
                    'class': 'vForeignKeyRawIdAdminField',
                },
                'is_hidden': False,
                'name': 'test-widget',
                'required': False,
                'template_name': 'admin/widgets/foreign_key_raw_id.html',
                'type': 'text',
                'value': value,
            },
        }
예제 #5
0
class SelectIdsToLinkForm(forms.Form):
    """
    Form used for selecting Data Hub company id and D&B duns number for linking.
    """

    COMPANY_ALREADY_DNB_LINKED = gettext_lazy(
        'This company has already been linked with a D&B company.', )
    DUNS_NUMBER_ALREADY_LINKED = gettext_lazy(
        'This duns number has already been linked with a Data Hub company.', )

    company = forms.ModelChoiceField(
        Company.objects.all(),
        widget=RawIdWidget(Company),
        label='Data Hub Company',
    )
    duns_number = forms.CharField(
        min_length=9,
        max_length=9,
        validators=(integer_validator, ),
    )

    def clean_company(self):
        """
        Check that the company does not already have a duns number.
        """
        company = self.cleaned_data['company']
        if company.duns_number:
            raise ValidationError(self.COMPANY_ALREADY_DNB_LINKED)
        return company

    def clean_duns_number(self):
        """
        Check that the duns_number chosen has not already been linked to a Data Hub company.
        """
        duns_number = self.cleaned_data['duns_number']
        if Company.objects.filter(duns_number=duns_number).exists():
            raise ValidationError(self.DUNS_NUMBER_ALREADY_LINKED)
        return duns_number
예제 #6
0
class SelectOtherCompanyForm(forms.Form):
    """Form used for selecting a second company when merging duplicate companies."""

    BOTH_COMPANIES_ARE_THE_SAME_MSG = gettext_lazy(
        'The two companies to merge cannot be the same. Please select a different company.',
    )

    company_2 = forms.ModelChoiceField(
        Company.objects.all(),
        widget=RawIdWidget(Company),
        label='Other company',
    )

    def __init__(self, company_1, *args, **kwargs):
        """Initialises the form, saving the ID of the company already selected."""
        super().__init__(*args, **kwargs)
        self._company_1 = company_1

    def clean_company_2(self):
        """Checks that a different company than the one navigated from has been selected."""
        company_2 = self.cleaned_data['company_2']
        if company_2 == self._company_1:
            raise ValidationError(self.BOTH_COMPANIES_ARE_THE_SAME_MSG)
        return company_2