Просмотр исходного кода

Introduced a custom model_to_dict()

Jeremy Stretch 6 лет назад
Родитель
Сommit
e01c984c01
1 измененных файлов с 19 добавлено и 0 удалено
  1. 19 0
      netbox/utilities/testing.py

+ 19 - 0
netbox/utilities/testing.py

@@ -2,6 +2,7 @@ import logging
 from contextlib import contextmanager
 from contextlib import contextmanager
 
 
 from django.contrib.auth.models import Permission, User
 from django.contrib.auth.models import Permission, User
+from django.forms.models import model_to_dict as _model_to_dict
 from django.test import Client, TestCase as _TestCase
 from django.test import Client, TestCase as _TestCase
 from rest_framework.test import APIClient
 from rest_framework.test import APIClient
 
 
@@ -69,6 +70,24 @@ class APITestCase(TestCase):
         self.header = {'HTTP_AUTHORIZATION': 'Token {}'.format(self.token.key)}
         self.header = {'HTTP_AUTHORIZATION': 'Token {}'.format(self.token.key)}
 
 
 
 
+def model_to_dict(instance, fields=None, exclude=None):
+    """
+    Customized wrapper for Django's built-in model_to_dict(). Does the following:
+      - Excludes the instance ID field
+      - Convert any assigned tags to a comma-separated string
+    """
+    _exclude = ['id']
+    if exclude is not None:
+        _exclude += exclude
+
+    model_dict = _model_to_dict(instance, fields=fields, exclude=_exclude)
+
+    if 'tags' in model_dict:
+        model_dict['tags'] = ','.join(sorted([tag.name for tag in model_dict['tags']]))
+
+    return model_dict
+
+
 def create_test_user(username='testuser', permissions=list()):
 def create_test_user(username='testuser', permissions=list()):
     """
     """
     Create a User with the given permissions.
     Create a User with the given permissions.