Skip to content
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

Devise a mechanism for referencing all user-facing models #13427

Closed
jeremystretch opened this issue Aug 9, 2023 · 1 comment
Closed

Devise a mechanism for referencing all user-facing models #13427

jeremystretch opened this issue Aug 9, 2023 · 1 comment
Assignees
Labels
status: accepted This issue has been accepted for implementation type: housekeeping Changes to the application which do not directly impact the end user
Milestone

Comments

@jeremystretch
Copy link
Member

jeremystretch commented Aug 9, 2023

Proposed Changes

Devise and implement a mechanism for filtering the content types for all user-facing models in NetBox. This includes essentially all of the models which can be manipulated directly via the UI or API, and excludes those "behind the scenes" models which cannot.

Justification

There are several instances where we currently work around this limitation. Below are some that I have identified; there are likely others to address as well.

  1. Retrieving content types for e.g. the object list dashboard widget:
# extras/dashboard/widgets.py
def get_content_type_labels():
    return [
        (content_type_identifier(ct), content_type_name(ct))
        for ct in ContentType.objects.filter(
            FeatureQuery('export_templates').get_query() | Q(app_label='extras', model='objectchange') |
            Q(app_label='extras', model='configcontext')
        ).order_by('app_label', 'model')
    ]
  1. Limiting object_type choices on CustomField import:
# extras/forms/bulk_import.py
class CustomFieldImportForm(CSVModelForm):
    ...
    object_type = CSVContentTypeField(
        label=_('Object type'),
        queryset=ContentType.objects.all(),
        limit_choices_to=FeatureQuery('custom_fields'),  # Should be generic
        required=False,
        help_text=_("Object type (for object or multi-object fields)")
    )
  1. Filtering for SavedFilter objects:
# extras/forms/filtersets.py
class SavedFilterFilterForm(SavedFiltersMixin, FilterForm):
    content_types = ContentTypeMultipleChoiceField(
        label=_('Content types'),
        queryset=ContentType.objects.filter(FeatureQuery('export_templates').get_query()),  # Should be generic
        required=False
    )
  1. Limiting object_type choices on CustomField edit:
# extras/forms/model_forms.py
class CustomFieldForm(BootstrapMixin, forms.ModelForm):
    object_type = ContentTypeChoiceField(
        label=_('Object type'),
        queryset=ContentType.objects.all(),
        # TODO: Come up with a canonical way to register suitable models
        limit_choices_to=FeatureQuery('webhooks').get_query() | Q(app_label='auth', model__in=['user', 'group']),
        required=False,
        help_text=_("Type of the related object (for object/multi-object fields only)")
    )
@jeremystretch jeremystretch added type: housekeeping Changes to the application which do not directly impact the end user status: under review Further discussion is needed to determine this issue's scope and/or implementation labels Aug 9, 2023
@jeremystretch jeremystretch added status: accepted This issue has been accepted for implementation and removed status: under review Further discussion is needed to determine this issue's scope and/or implementation labels Aug 29, 2023
@jeremystretch jeremystretch added this to the v4.0 milestone Aug 29, 2023
@jeremystretch jeremystretch modified the milestones: v4.0, v3.7 Sep 25, 2023
@jeremystretch jeremystretch self-assigned this Oct 17, 2023
jeremystretch added a commit that referenced this issue Oct 31, 2023
@jeremystretch
Copy link
Member Author

Another instance where this is needed: Populating model classes in ConfigTemplate context.

# extras/models/configs.py
# Populate the default template context with NetBox model classes, namespaced by app
# TODO: Devise a canonical mechanism for identifying the models to include (see #13427)
for app, model_names in registry['model_features']['custom_fields'].items():
    _context.setdefault(app, {})
    for model_name in model_names:
        model = apps.get_registered_model(app, model_name)
        _context[app][model.__name__] = model

jeremystretch added a commit that referenced this issue Nov 3, 2023
* Initial work on #13427

* Clarify documentation

* Reference public models registry when populating models for ConfigTemplate context
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: accepted This issue has been accepted for implementation type: housekeeping Changes to the application which do not directly impact the end user
Projects
None yet
Development

No branches or pull requests

1 participant