Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 602c24b0d6b8e125039cd56ae14ab84719ec6f71 @davedash committed Mar 5, 2010
@@ -0,0 +1,4 @@
+*.egg
+dist/
+*.egg-info
+build/
@@ -0,0 +1,27 @@
+Copyright (c) 2009, Dave Dash
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ 3. Neither the name of fixture-magic nor the names of its contributors may
+ be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,2 @@
+include LICENSE
+include README.rst
@@ -0,0 +1,48 @@
+============
+Requirements
+============
+
+This package requires:
+
+ * Python 2.6
+ * Django
+
+
+Installation
+------------
+
+You can get fixture-magic from pypi with: ::
+
+ pip install django-fixture-magic
+
+The development version can be installed with: ::
+
+ pip install -e git://github.com/davedash/django-fixture-magic#egg=django_fixture_magic
+
+fixture-magic adds two commands to ``manage.py`` therefore you should add it to
+your ``INSTALLED_APPS`` in ``settings.py``: ::
+
+ INSTALLED_APPS = (
+ ...
+ 'fixture_magic',
+ ...
+ )
+
+Usage
+-----
+
+There are two commands. ``dump_object`` returns the json representation of
+a specific object as well as all its dependencies (as defined by ForeignKeys).
+
+ ./manage.py dump_object APP.MODEL PK1 PK2 PK3 ... > my_new_fixture.json
+
+You can now safely load ``my_new_fixture.json`` in a test without foreign key i
+errors.
+
+The second command is ``merge_fixtures``. This command takes several fixture
+files and does a simple de-dupe operation (based on model and pk) and returns a
+clean json file. This is helpful if you have multiple json fixtures that might
+have redundant data.
+
+ ./manage.py merge_fixtures fixture1.json fixture2.json fixture3.json ... \
+ > all_my_fixtures.json
No changes.
No changes.
@@ -0,0 +1,36 @@
+from django.core.management.base import BaseCommand
+from django.db.models import loading
+from django.core.serializers import serialize
+from django.db import models
+
+
+def get_fields(obj):
+ try:
+ return obj._meta.fields
+ except AttributeError:
+ return []
+
+
+class Command(BaseCommand):
+ help = ('Dump specific objects from the database into JSON that you can '
+ 'use in a fixture')
+ args = "[object_class id ...]"
+
+ def handle(self, object_class, *ids, **options):
+ (app_label, model_name) = object_class.split('.')
+ dump_me = loading.get_model(app_label, model_name)
+ obj = dump_me.objects.filter(id__in=[int(i) for i in ids])
+ serialize_me = list(obj)
+ index = 0
+
+ while index < len(serialize_me):
+ for field in get_fields(serialize_me[index]):
+ if isinstance(field, models.ForeignKey):
+ serialize_me.append(
+ serialize_me[index].__getattribute__(field.name))
+
+ index = index + 1
+
+ serialize_me.reverse()
+ print serialize('json', [o for o in serialize_me if o is not None],
@mccammos
mccammos Mar 5, 2010

Looks like we need to create a subclass of DjangoJSONEncoder in order to handle our custom fields:

http://code.djangoproject.com/browser/django/tags/releases/1.1.1/django/core/serializers/json.py#L44

+ indent=4)
@@ -0,0 +1,26 @@
+import json
+
+from django.core.management.base import BaseCommand
+
+class Command(BaseCommand):
+ help = ('Merge a series of fixtures and remove duplicates.')
+ args = '[file ...]'
+
+ def handle(self, *files, **options):
+ """
+ Load a bunch of json files. Store the pk/model in a seen dictionary.
+ Add all the unseen objects into output.
+ """
+ output = []
+ seen = {}
+
+ for f in files:
+ f = file(f)
+ data = json.loads(f.read())
+ for object in data:
+ key = '%s|%s' % (object['model'], object['pk'])
+ if key not in seen:
+ seen[key] = 1
+ output.append(object)
+
+ print json.dumps(output, indent=4)
@@ -0,0 +1,26 @@
+from setuptools import setup, find_packages
+
+
+setup(
+ name='django-fixture-magic',
+ version='0.0.1',
+ description='A few extra management tools to handle fixtures.',
+ long_description=open('README.rst').read(),
+ author='Dave Dash',
+ author_email='dd+pypi@davedash.com',
+ url='http://github.com/davedash/django-fixture-magic',
+ license='BSD',
+ packages=find_packages(),
+ include_package_data=True,
+ classifiers=[
+ 'Development Status :: 4 - Beta',
+ 'Environment :: Web Environment',
+ 'Framework :: Django',
+ 'Intended Audience :: Developers',
+ 'License :: OSI Approved :: BSD License',
+ 'Operating System :: OS Independent',
+ 'Programming Language :: Python',
+ 'Topic :: Software Development :: Libraries :: Python Modules',
+ ],
+ )
+

0 comments on commit 602c24b

Please sign in to comment.