Jeremy Stretch 1 год назад
Родитель
Сommit
6d65d92c38

+ 3 - 3
netbox/ipam/api/serializers_/vlans.py

@@ -121,7 +121,7 @@ class VLANTranslationRuleSerializer(NetBoxModelSerializer):
 
     class Meta:
         model = VLANTranslationRule
-        fields = ['id', 'policy', 'local_vid', 'remote_vid']
+        fields = ['id', 'url', 'display', 'policy', 'local_vid', 'remote_vid', 'description']
 
 
 class VLANTranslationPolicySerializer(NetBoxModelSerializer):
@@ -129,5 +129,5 @@ class VLANTranslationPolicySerializer(NetBoxModelSerializer):
 
     class Meta:
         model = VLANTranslationPolicy
-        fields = ['id', 'url', 'name', 'description', 'display', 'rules']
-        brief_fields = ('id', 'url', 'name', 'description', 'display')
+        fields = ['id', 'url', 'display', 'name', 'description', 'display', 'rules']
+        brief_fields = ('id', 'url', 'display', 'name', 'description')

+ 6 - 0
netbox/ipam/forms/bulk_import.py

@@ -487,6 +487,12 @@ class VLANTranslationPolicyImportForm(NetBoxModelImportForm):
 
 
 class VLANTranslationRuleImportForm(NetBoxModelImportForm):
+    policy = CSVModelChoiceField(
+        label=_('Policy'),
+        queryset=VLANTranslationPolicy.objects.all(),
+        to_field_name='name',
+        help_text=_('VLAN translation policy')
+    )
 
     class Meta:
         model = VLANTranslationRule

+ 2 - 0
netbox/ipam/models/vlans.py

@@ -379,6 +379,8 @@ class VLANTranslationRule(NetBoxModel):
         'ipam.VLANTranslationPolicy',
     )
 
+    clone_fields = ['policy']
+
     class Meta:
         verbose_name = _('VLAN translation rule')
         ordering = ('policy', 'local_vid',)

+ 7 - 2
netbox/ipam/tables/vlans.py

@@ -231,6 +231,11 @@ class VLANTranslationPolicyTable(NetBoxTable):
         verbose_name=_('Name'),
         linkify=True
     )
+    rule_count = columns.LinkedCountColumn(
+        viewname='ipam:vlantranslationrule_list',
+        url_params={'policy_id': 'pk'},
+        verbose_name=_('Rules')
+    )
     description = tables.Column(
         verbose_name=_('Description'),
     )
@@ -241,9 +246,9 @@ class VLANTranslationPolicyTable(NetBoxTable):
     class Meta(NetBoxTable.Meta):
         model = VLANTranslationPolicy
         fields = (
-            'pk', 'id', 'name', 'description', 'tags', 'created', 'last_updated',
+            'pk', 'id', 'name', 'rule_count', 'description', 'tags', 'created', 'last_updated',
         )
-        default_columns = ('pk', 'name', 'description')
+        default_columns = ('pk', 'name', 'rule_count', 'description')
 
 
 class VLANTranslationRuleTable(NetBoxTable):

+ 7 - 7
netbox/ipam/tests/test_views.py

@@ -973,16 +973,16 @@ class VLANTranslationRuleTestCase(ViewTestCases.PrimaryObjectViewTestCase):
 
         cls.csv_data = (
             "policy,local_vid,remote_vid",
-            f"{vlan_translation_policies[0].pk},103,203",
-            f"{vlan_translation_policies[0].pk},104,204",
-            f"{vlan_translation_policies[1].pk},105,205",
+            f"{vlan_translation_policies[0].name},103,203",
+            f"{vlan_translation_policies[0].name},104,204",
+            f"{vlan_translation_policies[1].name},105,205",
         )
 
         cls.csv_update_data = (
-            "id,policy,local_vid,remote_vid",
-            f"{vlan_translation_rules[0].pk},{vlan_translation_policies[1].pk},105,205",
-            f"{vlan_translation_rules[1].pk},{vlan_translation_policies[1].pk},106,206",
-            f"{vlan_translation_rules[2].pk},{vlan_translation_policies[0].pk},107,207",
+            "id,local_vid,remote_vid",
+            f"{vlan_translation_rules[0].pk},105,205",
+            f"{vlan_translation_rules[1].pk},106,206",
+            f"{vlan_translation_rules[2].pk},107,207",
         )
 
         cls.bulk_edit_data = {

+ 3 - 1
netbox/ipam/views.py

@@ -1050,7 +1050,9 @@ class VLANGroupVLANsView(generic.ObjectChildrenView):
 
 @register_model_view(VLANTranslationPolicy, 'list', path='', detail=False)
 class VLANTranslationPolicyListView(generic.ObjectListView):
-    queryset = VLANTranslationPolicy.objects.all()
+    queryset = VLANTranslationPolicy.objects.annotate(
+        rule_count=count_related(VLANTranslationRule, 'policy'),
+    )
     filterset = filtersets.VLANTranslationPolicyFilterSet
     filterset_form = forms.VLANTranslationPolicyFilterForm
     table = tables.VLANTranslationPolicyTable

+ 10 - 0
netbox/templates/ipam/vlantranslationpolicy.html

@@ -18,6 +18,16 @@
             <th scope="row">{% trans "Description" %}</th>
             <td>{{ object.description|placeholder }}</td>
           </tr>
+          <tr>
+            <th scope="row">{% trans "Rules" %}</th>
+            <td>
+              {% if object.rules.count %}
+                <a href="{% url 'ipam:vlantranslationrule_list' %}?policy_id={{ object.pk }}">{{ object.rules.count }}</a>
+              {% else %}
+                0
+              {% endif %}
+            </td>
+          </tr>
         </table>
       </div>
       {% plugin_left_page object %}