Commits

Konstantine Rybnikov committed d51d964 Draft

Add some initial code.

  • Participants
  • Parent commits 7f5aa7f

Comments (0)

Files changed (6)

 syntax: glob
 docs/_build/
+*.pyc

File django_pyfixture/__init__.py

+# -*- coding: utf-8 -*-
+
+from .django_pyfixture import PyFixtureBase, load, PyFixtureTestCase
+from .django_pyfixture import load_py_fixtures_by_names
+from .django_pyfixture import PyFixtureTestCase

File django_pyfixture/django_pyfixture.py

+# -*- coding: utf-8 -*-
+
+from importlib import import_module
+from inspect import getmembers
+from inspect import isclass
+
+from django.conf import settings
+
+__version__ = '0.0.1'
+
+
+class PyFixtureBase(object):
+    def load_data(self):
+        raise NotImplementedError
+
+
+class PyFixtureTestCase(object):
+    """Mixin for your base test case."""
+
+    def setUp(self):
+        super(PyFixtureTestCase, self).setUp()
+
+    def tearDown(self):
+        super(PyFixtureTestCase, self).tearDown()
+
+
+def load_py_fixtures_from_test_case(test_case):
+    if hasattr(test_case, 'py_fixtures'):
+        if len(test_case.py_fixtures):
+            load_py_fixtures_by_names(test_case.py_fixtures)
+
+
+def load_py_fixtures_by_names(fixture_names):
+    """Do the actual loading work."""
+    # import logging
+
+    loaded = []
+    for fixture_name in fixture_names:
+        fixture_classes = list(find_fixture_classes(fixture_name))
+        # logging.debug(u"Loading fixture: %s" % fixture_name)
+        for fixture_class in fixture_classes:
+            # fire
+            # logging.debug(u"- loading func %s" % fixture_class.__name__)
+            fixture = fixture_class()
+            fixture.load_data()
+            loaded.append(fixture_name)
+    return loaded
+
+
+def find_fixture_classes(fixture_name):
+    installed_apps = settings.INSTALLED_APPS
+    for app_name in installed_apps:
+        try:
+            fixture_module_name = app_name + '.fixtures.' + fixture_name
+            fixture_module = import_module(fixture_module_name)
+            for member_name, member in getmembers(fixture_module):
+                if (isclass(member)
+                    and issubclass(member, PyFixtureBase)
+                    and member is not PyFixtureBase):
+                    #
+                    yield member
+        except ImportError:
+            pass

File django_pyfixture/management/commands/loaddata_py.py

+# -*- coding: utf-8 -*-
+
+from django.core.management.base import BaseCommand
+# from django.core.management.base import CommandError
+from django_pyfixture import load_py_fixtures_by_names
+
+
+class Command(BaseCommand):
+    """Command that gives you an ability to load fixtures written in python.
+
+    Just put your fixtures into regular `app/fixtures/foo.py` file,
+    then add them into 
+    """
+    args = '<fixture_name fixture_name ...>'
+    help = 'Loads/executes py-fixtures'
+
+    def handle(self, *args, **options):
+        loaded = load_py_fixtures_by_names(args)
+        if loaded:
+            self.stdout.write(u'Successfully loaded "%s"\n' % ', '.join(loaded))
+        else:
+            self.stdout.write(u'No fixtures for "%s" found\n' % ', '.join(args))

File django_pyfixture/models.py

+# -*- coding: utf-8 -*-

File docs/index.rst

             # do something with foo here
             Foo.objects.all()
 
+7.  To load some data from terminal use
+
+.. code-block:: bash
+
+    python loaddata_py foo
+
 Indices and tables
 ==================
 
 * :ref:`genindex`
 * :ref:`modindex`
 * :ref:`search`
-