-
Notifications
You must be signed in to change notification settings - Fork 2.9k
7699 Add Scope to Cluster #17848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
7699 Add Scope to Cluster #17848
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
2bb49d7
7699 Add Scope to Cluster
arthanson dcb3c7c
7699 Serializer
arthanson 33b4beb
7699 filterset
arthanson 286f56b
7699 bulk_edit
arthanson 4c3d1ce
7699 bulk_import
arthanson d19cef4
7699 model_form
arthanson 7e6bb0e
7699 graphql, tables
arthanson 8a63707
7699 fixes
arthanson be59775
7699 fixes
arthanson 76e438d
7699 fixes
arthanson ee99056
7699 fixes
arthanson 071b960
7699 fix tests
arthanson 4112af5
7699 fix graphql tests for clusters reference
arthanson 65295f6
7699 fix dcim tests
arthanson 9108915
7699 fix ipam tests
arthanson c73902c
7699 fix tests
arthanson cfdab0e
7699 use mixin for model
arthanson 3525a3a
7699 change mixin name
arthanson d7b204a
7699 scope form
arthanson 8ca7cdd
7699 scope form
arthanson 277b175
7699 scoped form, fitlerset
arthanson 62358f6
7699 review changes
arthanson c75bfe1
7699 move ScopedFilterset
arthanson c500545
7699 move CachedScopeMixin
arthanson effe920
7699 review changes
arthanson 69af847
Merge branch 'feature' into 7699-cluster-location
arthanson 7fc0e4f
Merge branch 'feature' into 7699-cluster-location
arthanson 0c7710d
7699 review changes
arthanson 5c022f0
7699 refactor mixins
arthanson 88aa554
7699 _sitegroup -> _site_group
arthanson e1e6bfd
7699 update docstring
arthanson 45f29de
Merge branch 'feature' into 7699-cluster-location
jeremystretch a0c069d
Misc cleanup
jeremystretch 2185254
Merge branch 'feature' into 7699-cluster-location
jeremystretch d3946d2
Update migrations
jeremystretch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| from django import forms | ||
| from django.contrib.contenttypes.models import ContentType | ||
| from django.core.exceptions import ObjectDoesNotExist | ||
| from django.utils.translation import gettext_lazy as _ | ||
|
|
||
| from dcim.constants import LOCATION_SCOPE_TYPES | ||
| from dcim.models import Site | ||
| from utilities.forms import get_field_value | ||
| from utilities.forms.fields import ( | ||
| ContentTypeChoiceField, CSVContentTypeField, DynamicModelChoiceField, | ||
| ) | ||
| from utilities.templatetags.builtins.filters import bettertitle | ||
| from utilities.forms.widgets import HTMXSelect | ||
|
|
||
| __all__ = ( | ||
| 'ScopedBulkEditForm', | ||
| 'ScopedForm', | ||
| 'ScopedImportForm', | ||
| ) | ||
|
|
||
|
|
||
| class ScopedForm(forms.Form): | ||
| scope_type = ContentTypeChoiceField( | ||
| queryset=ContentType.objects.filter(model__in=LOCATION_SCOPE_TYPES), | ||
| widget=HTMXSelect(), | ||
| required=False, | ||
| label=_('Scope type') | ||
| ) | ||
| scope = DynamicModelChoiceField( | ||
| label=_('Scope'), | ||
| queryset=Site.objects.none(), # Initial queryset | ||
| required=False, | ||
| disabled=True, | ||
| selector=True | ||
| ) | ||
|
|
||
| def __init__(self, *args, **kwargs): | ||
| instance = kwargs.get('instance') | ||
| initial = kwargs.get('initial', {}) | ||
|
|
||
| if instance is not None and instance.scope: | ||
| initial['scope'] = instance.scope | ||
| kwargs['initial'] = initial | ||
|
|
||
| super().__init__(*args, **kwargs) | ||
| self._set_scoped_values() | ||
|
|
||
| def clean(self): | ||
| super().clean() | ||
|
|
||
| # Assign the selected scope (if any) | ||
| self.instance.scope = self.cleaned_data.get('scope') | ||
|
|
||
| def _set_scoped_values(self): | ||
| if scope_type_id := get_field_value(self, 'scope_type'): | ||
| try: | ||
| scope_type = ContentType.objects.get(pk=scope_type_id) | ||
| model = scope_type.model_class() | ||
| self.fields['scope'].queryset = model.objects.all() | ||
| self.fields['scope'].widget.attrs['selector'] = model._meta.label_lower | ||
| self.fields['scope'].disabled = False | ||
| self.fields['scope'].label = _(bettertitle(model._meta.verbose_name)) | ||
| except ObjectDoesNotExist: | ||
| pass | ||
|
|
||
| if self.instance and scope_type_id != self.instance.scope_type_id: | ||
| self.initial['scope'] = None | ||
|
|
||
|
|
||
| class ScopedBulkEditForm(forms.Form): | ||
| scope_type = ContentTypeChoiceField( | ||
| queryset=ContentType.objects.filter(model__in=LOCATION_SCOPE_TYPES), | ||
| widget=HTMXSelect(method='post', attrs={'hx-select': '#form_fields'}), | ||
| required=False, | ||
| label=_('Scope type') | ||
| ) | ||
| scope = DynamicModelChoiceField( | ||
| label=_('Scope'), | ||
| queryset=Site.objects.none(), # Initial queryset | ||
| required=False, | ||
| disabled=True, | ||
| selector=True | ||
| ) | ||
|
|
||
| def __init__(self, *args, **kwargs): | ||
| super().__init__(*args, **kwargs) | ||
|
|
||
| if scope_type_id := get_field_value(self, 'scope_type'): | ||
| try: | ||
| scope_type = ContentType.objects.get(pk=scope_type_id) | ||
| model = scope_type.model_class() | ||
| self.fields['scope'].queryset = model.objects.all() | ||
| self.fields['scope'].widget.attrs['selector'] = model._meta.label_lower | ||
| self.fields['scope'].disabled = False | ||
| self.fields['scope'].label = _(bettertitle(model._meta.verbose_name)) | ||
| except ObjectDoesNotExist: | ||
| pass | ||
|
|
||
|
|
||
| class ScopedImportForm(forms.Form): | ||
| scope_type = CSVContentTypeField( | ||
| queryset=ContentType.objects.filter(model__in=LOCATION_SCOPE_TYPES), | ||
| required=False, | ||
| label=_('Scope type (app & model)') | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.