Commits

Luke Plant committed aedd91d

Moved functionality from create_anonymizers command to the introspection module, to make it more testable

Comments (0)

Files changed (2)

anonymizer/introspect.py

 import re
 
 from django.db.models import EmailField
+from django.db.models.loading import get_models
 
 field_replacers = {
     'AutoField': None,
     return class_template % {'modelname':model.__name__,
                              'attributes': "\n".join(attributes) }
 
+
+def create_anonymizers_module(app):
+    model_names = []
+    imports = []
+    output = []
+    output.append("")
+    imports.append("from anonymizer import Anonymizer")
+    for model in get_models(app):
+        model_names.append(model.__name__)
+        output.append(create_anonymizer(model))
+
+    imports.insert(0, "from %s import %s" % (app.__name__, ", ".join(model_names)))
+
+    return "\n".join(imports) + "\n".join(output)

anonymizer/management/commands/create_anonymizers.py

 import sys
 import os.path
 
-from django.db.models.loading import get_models
 from django.core.exceptions import ImproperlyConfigured
 from django.core.management.base import AppCommand, CommandError
 from django.utils import importlib
 
-from anonymizer import Anonymizer
 from anonymizer import introspect
 
+
 class Command(AppCommand):
 
     def handle_app(self, app, **options):
         if os.path.exists(path):
             raise CommandError("File '%s' already exists." % path)
 
-        model_names = []
-        imports = []
-        output = []
-        output.append("")
-        imports.append("from anonymizer import Anonymizer")
-        for model in get_models(app):
-            model_names.append(model.__name__)
-            output.append(introspect.create_anonymizer(model))
-
-        imports.insert(0, "from %s import %s" % (app.__name__, ", ".join(model_names)))
+        module = introspect.create_anonymizers_module(app)
 
         with open(path, "w") as fd:
-            fd.write("\n".join(imports) + "\n".join(output))
+            fd.write(module)