Skip to content

Commit

Permalink
Convert globals to local params
Browse files Browse the repository at this point in the history
  • Loading branch information
achembarpu committed Aug 3, 2018
1 parent f6db2e7 commit d99fcc9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
17 changes: 8 additions & 9 deletions fixture_magic/management/commands/custom_dump.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import print_function

import sys

try:
import json
except ImportError:
Expand All @@ -18,12 +16,11 @@
from fixture_magic.utils import (
add_to_serialize_list,
reorder_json,
serialize_me,
serialize_fully
)


def process_dep(parent, dep):
def process_dep(parent, dep, serialize_me, seen):
parts = dep.split('.')
current = parts.pop(0)
remain = '.'.join(parts)
Expand All @@ -37,11 +34,11 @@ def process_dep(parent, dep):
children = thing.all()
else:
children = [thing]
add_to_serialize_list(children)
add_to_serialize_list(children, serialize_me, seen)

if remain:
for child in children:
process_dep(child, remain)
process_dep(child, remain, serialize_me, seen)


class Command(BaseCommand):
Expand All @@ -54,6 +51,8 @@ def add_arguments(self, parser):
help='Use natural keys if they are available.')

def handle(self, *args, **options):
serialize_me = []
seen = set()
# Get the primary object
dump_name = options['dump_name']
pks = options['pk']
Expand All @@ -66,11 +65,11 @@ def handle(self, *args, **options):
for obj in objs.all():
# get the dependent objects and add to serialize list
for dep in deps:
process_dep(obj, dep)
process_dep(obj, dep, serialize_me, seen)
if include_primary or not deps:
add_to_serialize_list([obj])
add_to_serialize_list([obj], serialize_me, seen)

serialize_fully()
serialize_fully(serialize_me, seen)
data = serialize('json', [o for o in serialize_me if o is not None],
indent=4,
use_natural_foreign_keys=options.get('natural', False),
Expand Down
18 changes: 7 additions & 11 deletions fixture_magic/management/commands/dump_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
from django.apps import apps as loading
import json

from fixture_magic.utils import (add_to_serialize_list, serialize_me, seen,
serialize_fully)
from fixture_magic.utils import (add_to_serialize_list, serialize_fully)


class Command(BaseCommand):
Expand Down Expand Up @@ -65,6 +64,8 @@ def add_arguments(self, parser):
)

def handle(self, *args, **options):
serialize_me = []
seen = set()
error_text = ('%s\nTry calling dump_object with --help argument or ' +
'use the following arguments:\n %s' % self.args)
try:
Expand Down Expand Up @@ -117,18 +118,18 @@ def handle(self, *args, **options):
for rel in related_fields:
try:
if hasattr(getattr(obj, rel), 'all'):
add_to_serialize_list(getattr(obj, rel).all())
add_to_serialize_list(getattr(obj, rel).all(), serialize_me, seen)
else:
add_to_serialize_list([getattr(obj, rel)])
add_to_serialize_list([getattr(obj, rel)], serialize_me, seen)
except FieldError:
pass
except ObjectDoesNotExist:
pass

add_to_serialize_list(objs)
add_to_serialize_list(objs, serialize_me, seen)

if options.get('follow_fk', True):
serialize_fully()
serialize_fully(serialize_me, seen)
else:
# reverse list to match output of serializez_fully
serialize_me.reverse()
Expand All @@ -143,8 +144,3 @@ def handle(self, *args, **options):
indent=4,
use_natural_foreign_keys=natural_foreign,
use_natural_primary_keys=natural_primary))

# Clear the list. Useful for when calling multiple
# dump_object commands with a single execution of django
del serialize_me[:]
seen.clear()
14 changes: 7 additions & 7 deletions fixture_magic/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from django.db import models

serialize_me = []
seen = {}


def reorder_json(data, models, ordering_cond=None):
"""Reorders JSON (actually a list of model dicts).
Expand Down Expand Up @@ -45,21 +42,24 @@ def get_fields(obj):
return []


def serialize_fully():
def serialize_fully(serialize_me, seen):
index = 0

while index < len(serialize_me):
for field in get_fields(serialize_me[index]):
if isinstance(field, models.ForeignKey):
add_to_serialize_list(
[serialize_me[index].__getattribute__(field.name)])
[serialize_me[index].__getattribute__(field.name)],
serialize_me,
seen
)

index += 1

serialize_me.reverse()


def add_to_serialize_list(objs):
def add_to_serialize_list(objs, serialize_me, seen):
for obj in objs:
if obj is None:
continue
Expand All @@ -76,4 +76,4 @@ def add_to_serialize_list(objs):

if key not in seen:
serialize_me.append(obj)
seen[key] = 1
seen.add(key)

0 comments on commit d99fcc9

Please sign in to comment.