Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Verified Commit 06f2bb73 authored by Vincent SIMONIN's avatar Vincent SIMONIN
Browse files

Add some search and filtering improvment

* Add some default columns to table
* Update filtrering field in forms
parent 6c142146
No related branches found
No related tags found
1 merge request!21Add some search and filtering improvment
Pipeline #70047 passed
......@@ -5,7 +5,7 @@ class NetBoxRpsConfig(PluginConfig):
name = 'netbox_rps_plugin'
verbose_name = 'NetBox RPS'
description = 'A Netbox plugin to add RPS resources'
version = '0.8.1'
version = '0.8.2'
author = "Vincent Simonin"
author_email = "vincent.simonin@ext.ec.europa.eu"
base_url = 'rps'
......
from netbox.filtersets import NetBoxModelFilterSet
from .models import Mapping, HttpHeader
from .models import Mapping, HttpHeader, AuthenticationChoices
from django.db.models import Q
import django_filters
class MappingFilterSet(NetBoxModelFilterSet):
class Meta:
model = Mapping
fields = ('id', 'authentication', 'source', 'target', 'Comment')
fields = ('id', 'authentication', 'source', 'target', 'Comment', 'webdav', 'testingpage')
def search(self, queryset, name, value):
return queryset.filter( Q(source__icontains=value) | Q(target__icontains=value)
| Q(authentication__icontains=value) | Q(Comment__icontains=value))
if not value.strip():
return queryset
return queryset.filter(
Q(source__icontains=value) |
Q(target__icontains=value) |
Q(Comment__icontains=value)
)
class HttpHeaderFilterSet(NetBoxModelFilterSet):
class Meta:
model = HttpHeader
fields = ('id', 'name', 'value', 'apply_to')
fields = ('id', 'name', 'value', 'apply_to', 'mapping')
def search(self, queryset, name, value):
"""Perform the filtered search."""
if not value.strip():
return queryset
qs_filter = (
Q(name__icontains=value)
return queryset.filter(
Q(name__icontains=value) |
Q(value__icontains=value)
)
return queryset.filter(qs_filter)
from django import forms
from django.utils.translation import gettext as _
from netbox.forms import NetBoxModelForm, NetBoxModelFilterSetForm, NetBoxModelImportForm
from .models import Mapping, AuthenticationChoices, HttpHeader
from .models import Mapping, AuthenticationChoices, HttpHeader, ApplyToChoices
from utilities.forms.fields import DynamicModelMultipleChoiceField, TagFilterField
from utilities.forms import BOOLEAN_WITH_BLANK_CHOICES, add_blank_choice
class MappingForm(NetBoxModelForm):
......@@ -22,10 +24,19 @@ class MappingForm(NetBoxModelForm):
class MappingFilterForm(NetBoxModelFilterSetForm):
model = Mapping
source = forms.CharField(max_length=120, min_length=1, required=False, label='Source URL')
target = forms.CharField(max_length=120, min_length=1, required=False, label='Target URL')
authentication = forms.MultipleChoiceField(
choices=AuthenticationChoices,
required=False,
)
webdav = forms.BooleanField(
required=False,
widget=forms.Select(choices=BOOLEAN_WITH_BLANK_CHOICES)
)
testingpage = forms.CharField(max_length=120, min_length=1, required=False, label='Testing URL')
Comment = forms.CharField(max_length=500, min_length=1, required=False, label='Comment')
tag = TagFilterField(model)
class MappingImportForm(NetBoxModelImportForm):
......@@ -48,3 +59,15 @@ class HttpHeaderForm(NetBoxModelForm):
'value': 'Value',
'apply_to': 'Apply to',
}
class HttpHeaderFilterForm(NetBoxModelFilterSetForm):
model = HttpHeader
name = forms.CharField(max_length=120, min_length=1, required=False, label='Header name')
value = forms.CharField(max_length=256, min_length=1, required=False, label='Header value')
apply_to = forms.ChoiceField(choices=add_blank_choice(ApplyToChoices), required=False, label='Apply to')
mapping = DynamicModelMultipleChoiceField(
queryset=Mapping.objects.all(),
required=False,
label=_('Mapping')
)
tag = TagFilterField(model)
from netbox.search import SearchIndex, register_search
from .models import Mapping
from .models import Mapping, HttpHeader
@register_search
......@@ -13,3 +12,11 @@ class MappingIndex(SearchIndex):
('Comment', 500),
)
@register_search
class HttpHeaderIndex(SearchIndex):
model = HttpHeader
fields = (
('name', 120),
('value', 256),
('apply_to', 8)
)
......@@ -12,14 +12,15 @@ class MappingTable(NetBoxTable):
url_params={'mapping_id': 'pk'},
verbose_name='HTTP Headers count'
)
tags = columns.TagColumn()
class Meta(NetBoxTable.Meta):
model = Mapping
fields = (
'pk', 'id', 'source', 'target', 'authentication', 'testingpage', 'webdav', 'Comment', 'httpheader_count'
'pk', 'id', 'source', 'target', 'authentication', 'testingpage', 'webdav', 'Comment', 'httpheader_count', 'tags', 'created', 'last_updated',
)
default_columns = (
'source', 'target', 'authentication', 'Comment', 'httpheader_count'
'source', 'target', 'authentication', 'webdav', 'httpheader_count'
)
class HttpHeaderTable(NetBoxTable):
......@@ -30,8 +31,8 @@ class HttpHeaderTable(NetBoxTable):
class Meta(NetBoxTable.Meta):
model = HttpHeader
fields = (
'pk', 'id', 'name', 'value', 'apply_to', 'mapping'
'pk', 'id', 'mapping', 'name', 'value', 'apply_to', 'tags', 'created', 'last_updated',
)
default_columns = (
'name', 'value', 'apply_to'
'mapping', 'name', 'value', 'apply_to'
)
{% extends 'generic/object.html' %}
{% load helpers %}
{% load render_table from django_tables2 %}
{% load perms %}
{% block extra_controls %}
<a href="{% url 'plugins:netbox_rps_plugin:httpheader_add' %}?mapping={{ object.pk }}&return_url={{ object.get_absolute_url }}" class="btn btn-sm btn-primary">
......
......@@ -66,7 +66,7 @@ class HttpHeaderListView(generic.ObjectListView):
queryset = models.HttpHeader.objects.all()
table = tables.HttpHeaderTable
filterset = filtersets.HttpHeaderFilterSet
filterset_form = forms.HttpHeaderForm
filterset_form = forms.HttpHeaderFilterForm
class HttpHeaderEditView(generic.ObjectEditView):
......
......@@ -2,7 +2,7 @@ from setuptools import find_packages, setup
setup(
name='netbox_rps_plugin',
version='0.8.1',
version='0.8.2',
description='A Netbox plugin to add RPS resources',
install_requires=[],
packages=find_packages(),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment