Repository: Onemind-Services-LLC/netbox-secrets (language: python)

Issue:
[Bug]: API get /secret-roles/?name=XXX doesn't work
### NetBox Secrets plugin version

v1.8.2

### NetBox version

v3.5.2

### Steps to Reproduce

GET with any name filter always fails. For example:

```
curl -X GET "https://netbox/api/plugins/secrets/secret-roles/?name=XXX" -H "Authorization: Token XXX" -H "Accept: application/json; indent=4" -H "Content-Type: application/json"
{
    "name": [
        "“XXX” is not a valid value."
```

Using id as filter works.

### Expected Behavior

Get specified role.

### Observed Behavior

`"“XXX” is not a valid value."` error.
Fix secret role filterset
# Pull Request

## Description

- Fix secret role filter sets

## Close: #68 


Discussion / hints:

Fix patch:
diff --git a/netbox_secrets/filtersets.py b/netbox_secrets/filtersets.py
index 84b8aa8..1410665 100644
--- a/netbox_secrets/filtersets.py
+++ b/netbox_secrets/filtersets.py
@@ -23,7 +23,7 @@ class SecretRoleFilterSet(NetBoxModelFilterSet):
         method='search',
         label='Search',
     )
-    name = django_filters.ModelMultipleChoiceFilter(queryset=SecretRole.objects.all(), field_name='name')
+    name = MultiValueCharFilter(lookup_expr='iexact')
 
     class Meta:
         model = SecretRole
diff --git a/netbox_secrets/forms/filterset.py b/netbox_secrets/forms/filterset.py
index a5e54a6..5842c36 100644
--- a/netbox_secrets/forms/filterset.py
+++ b/netbox_secrets/forms/filterset.py
@@ -1,13 +1,13 @@
 from django import forms
 from django.contrib.contenttypes.models import ContentType
 from django.utils.translation import gettext as _
+
 from netbox.forms import NetBoxModelFilterSetForm
 from utilities.forms.fields import (
     ContentTypeMultipleChoiceField,
     DynamicModelMultipleChoiceField,
     TagFilterField,
 )
-
 from ..constants import *
 from ..models import Secret, SecretRole
 
@@ -20,7 +20,7 @@
 class SecretRoleFilterForm(NetBoxModelFilterSetForm):
     model = SecretRole
     q = forms.CharField(required=False, label=_('Search'))
-    name = DynamicModelMultipleChoiceField(queryset=SecretRole.objects.all(), required=False)
+    id = DynamicModelMultipleChoiceField(queryset=SecretRole.objects.all(), required=False, label=_('Roles'))
 
 
 class SecretFilterForm(NetBoxModelFilterSetForm):
diff --git a/netbox_secrets/migrations/0007_secret__object_repr_secret_comments_and_more.py b/netbox_secrets/migrations/0007_secret__object_repr_secret_comments_and_more.py
index 00e2e8b..e16976d 100644
--- a/netbox_secrets/migrations/0007_secret__object_repr_secret_comments_and_more.py
+++ b/netbox_secrets/migrations/0007_secret__object_repr_secret_comments_and_more.py
@@ -1,10 +1,21 @@
 # Generated by Django 4.1.9 on 2023-05-07 18:36
 
+from django.contrib.contenttypes.models import ContentType
 from django.db import migrations, models
 
 
-class Migration(migrations.Migration):
+def copy_assigned_object(apps, schema_editor):
+    Secret = apps.get_model('netbox_secrets', 'Secret')
+
+    for secret in Secret.objects.all():
+        content_type = ContentType.objects.get(id=secret.assigned_object_type_id)
+        Model = apps.get_model(content_type.app_label, str(content_type.model).capitalize())
+        object = Model.objects.filter(id=secret.assigned_object_id).first()
+        secret._object_repr = object.name
+        secret.save()
 
+
+class Migration(migrations.Migration):
     dependencies = [
         ('netbox_secrets', '0006_alter_userkey_created_alter_userkey_last_updated'),
     ]
@@ -30,4 +41,5 @@ class Migration(migrations.Migration):
             name='comments',
             field=models.TextField(blank=True),
         ),
+        migrations.RunPython(copy_assigned_object)
     ]
diff --git a/setup.py b/setup.py
index 3b50ca8..bc39dec 100644
--- a/setup.py
+++ b/setup.py
@@ -2,7 +2,7 @@
 
 setup(
     name='netbox-secrets',
-    version='1.8.2',
+    version='1.8.3',
     description='Netbox Secrets',
     long_description='A Secret store for NetBox',
     url='https://github.com/Onemind-Services-LLC/netbox-secrets/',


Test patch:
diff --git a/netbox_secrets/tests/test_filters.py b/netbox_secrets/tests/test_filters.py
index 1d241dc..c4d1a4c 100644
--- a/netbox_secrets/tests/test_filters.py
+++ b/netbox_secrets/tests/test_filters.py
@@ -1,9 +1,9 @@
-from dcim.models import Device, DeviceRole, DeviceType, Manufacturer, Site
 from django.test import TestCase
-from virtualization.models import Cluster, ClusterType, VirtualMachine
 
+from dcim.models import Device, DeviceRole, DeviceType, Manufacturer, Site
 from netbox_secrets.filtersets import *
 from netbox_secrets.models import Secret, SecretRole
+from virtualization.models import Cluster, ClusterType, VirtualMachine
 
 
 class SecretRoleTestCase(TestCase):
@@ -25,7 +25,7 @@ def test_id(self):
 
     def test_name(self):
         name = SecretRole.objects.all()
-        params = {'name': [name[0].pk, name[1].pk]}
+        params = {'name': [name[0].name, name[1].name]}
         self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
 
     def test_slug(self):