Exemple #1
0
    def handle(self, dump_name, *pks, **options):
        # Get the primary object
        dump_settings = settings.CUSTOM_DUMPS[dump_name]
        (app_label, model_name) = dump_settings['primary'].split('.')
        dump_me = loading.get_model(app_label, model_name)
        objs = dump_me.objects.filter(pk__in=[int(i) for i in pks])
        for obj in objs:
            # get the dependent objects and add to serialize list
            for dep in dump_settings['dependents']:
                try:
                    thing = Variable("thing.%s" % dep).resolve({'thing': obj})
                    add_to_serialize_list([thing])
                except VariableDoesNotExist:
                    sys.stderr.write('%s not found' % dep)

            if not dump_settings['dependents']:
                add_to_serialize_list([obj])

        serialize_fully()
        data = serialize('json', [o for o in serialize_me if o is not None])

        data = reorder_json(json.loads(data),
                            dump_settings.get('order', []),
                            ordering_cond=dump_settings.get('order_cond', {}))

        print json.dumps(data, indent=4)
    def handle(self, dump_name, *pks, **options):
        # Get the primary object
        dump_settings = settings.CUSTOM_DUMPS[dump_name]
        (app_label, model_name) = dump_settings['primary'].split('.')
        dump_me = loading.get_model(app_label, model_name)
        objs = dump_me.objects.filter(pk__in=[int(i) for i in pks])
        for obj in objs:
            # get the dependent objects and add to serialize list
            for dep in dump_settings['dependents']:
                try:
                    thing = Variable("thing.%s" % dep).resolve({'thing': obj})
                    add_to_serialize_list([thing])
                except VariableDoesNotExist:
                    sys.stderr.write('%s not found' % dep)

            if not dump_settings['dependents']:
                add_to_serialize_list([obj])

        serialize_fully()
        data = serialize('json', [o for o in serialize_me if o is not None])

        data = reorder_json(json.loads(data), dump_settings.get('order', []),
                ordering_cond=dump_settings.get('order_cond',{}))

        print json.dumps(data, indent=4)
Exemple #3
0
 def test_reorder_json(self):
     """Test basic ordering of JSON/python object."""
     input_json = [
         {
             'model': 'f'
         },
         {
             'model': 'x'
         },
     ]
     expected = [{'model': 'x'}, {'model': 'f'}]
     self.assertEqual(expected, reorder_json(input_json, models=['x', 'f']))
    def handle(self, dump_name, pk, **options):
        # Get the primary object
        dump_settings = settings.CUSTOM_DUMPS[dump_name]
        (app_label, model_name) = dump_settings['primary'].split('.')
        dump_me = loading.get_model(app_label, model_name)
        obj = dump_me.objects.get(pk=pk)

        # get the dependent objects and add to serialize list
        for dep in dump_settings['dependents']:
            thing = Variable("thing.%s" % dep).resolve({'thing': obj})
            add_to_serialize_list([thing])

        serialize_fully()
        data = serialize('json', [o for o in serialize_me if o is not None])

        data = reorder_json(json.loads(data), dump_settings.get('order', []))

        print json.dumps(data, indent=4)
Exemple #5
0
    def handle(self, dump_name, *ids, **options):
        # Get the primary object
        dump_settings = settings.CUSTOM_DUMPS[dump_name]
        (app_label, model_name) = dump_settings['primary'].split('.')
        dump_me = loading.get_model(app_label, model_name)
        if ids[0] == '*':
            objs = dump_me.objects.all()
        else:
            try:
                objs = dump_me.objects.filter(pk__in=[int(i) for i in ids])
            except ValueError:
                # We might have primary keys that are longs...
                try:
                    objs = dump_me.objects.filter(
                        pk__in=[long(i) for i in ids])
                except ValueError:
                    # or json objects for better filter control
                    try:
                        objs = dump_me.objects.filter(**json.loads(ids[0]))
                    except ValueError:
                        # Finally, we might have primary keys that are strings...
                        objs = dump_me.objects.filter(pk__in=ids)

        for obj in objs:
            # get the dependent objects and add to serialize list
            for dep in dump_settings['dependents']:
                try:
                    thing = Variable("thing.%s" % dep).resolve({'thing': obj})
                    add_to_serialize_list([thing])
                except VariableDoesNotExist:
                    sys.stderr.write('%s not found' % dep)

            if not dump_settings['dependents']:
                add_to_serialize_list([obj])

        serialize_fully()
        data = serialize('json', [o for o in serialize_me if o is not None])

        data = reorder_json(json.loads(data),
                            dump_settings.get('order', []),
                            ordering_cond=dump_settings.get('order_cond', {}))

        print json.dumps(data, indent=4)
    def handle(self, dump_name, *ids, **options):
        # Get the primary object
        dump_settings = settings.CUSTOM_DUMPS[dump_name]
        (app_label, model_name) = dump_settings["primary"].split(".")
        dump_me = loading.get_model(app_label, model_name)
        if ids[0] == "*":
            objs = dump_me.objects.all()
        else:
            try:
                objs = dump_me.objects.filter(pk__in=[int(i) for i in ids])
            except ValueError:
                # We might have primary keys that are longs...
                try:
                    objs = dump_me.objects.filter(pk__in=[long(i) for i in ids])
                except ValueError:
                    # or json objects for better filter control
                    try:
                        objs = dump_me.objects.filter(**json.loads(ids[0]))
                    except ValueError:
                        # Finally, we might have primary keys that are strings...
                        objs = dump_me.objects.filter(pk__in=ids)

        for obj in objs:
            # get the dependent objects and add to serialize list
            for dep in dump_settings["dependents"]:
                try:
                    thing = Variable("thing.%s" % dep).resolve({"thing": obj})
                    add_to_serialize_list([thing])
                except VariableDoesNotExist:
                    sys.stderr.write("%s not found" % dep)

            if not dump_settings["dependents"]:
                add_to_serialize_list([obj])

        serialize_fully()
        data = serialize("json", [o for o in serialize_me if o is not None])

        data = reorder_json(
            json.loads(data), dump_settings.get("order", []), ordering_cond=dump_settings.get("order_cond", {})
        )

        print json.dumps(data, indent=4)
    def handle(self, *args, **options):
        # Get the primary object
        dump_name = options["dump_name"]
        pks = options["pk"]
        dump_settings = settings.CUSTOM_DUMPS[dump_name]
        (app_label, model_name) = dump_settings["primary"].split(".")
        include_primary = dump_settings.get("include_primary", False)
        dump_me = loading.get_model(app_label, model_name)
        objs = dump_me.objects.filter(pk__in=[int(i) for i in pks])
        for obj in objs.all():
            # get the dependent objects and add to serialize list
            for dep in dump_settings["dependents"]:
                try:
                    thing = Variable("thing.%s" % dep).resolve({"thing": obj})
                    if hasattr(
                            thing,
                            "all"):  # Related managers can't be iterated over
                        thing = thing.all()
                    add_to_serialize_list([thing])
                except VariableDoesNotExist:
                    sys.stderr.write("%s not found" % dep)

            if include_primary or not dump_settings["dependents"]:
                add_to_serialize_list([obj])

        serialize_fully()
        data = serialize(
            "json",
            [o for o in serialize_me if o is not None],
            indent=4,
            use_natural_foreign_keys=options.get("natural", False),
            use_natural_primary_keys=options.get("natural", False),
        )

        data = reorder_json(
            json.loads(data),
            dump_settings.get("order", []),
            ordering_cond=dump_settings.get("order_cond", {}),
        )

        print(json.dumps(data, indent=4))
    def handle(self, *args, **options):
        serialize_me = []
        seen = set()
        # Get the primary object
        dump_name = options['dump_name']
        pks = options['pk']
        dump_settings = settings.CUSTOM_DUMPS[dump_name]
        app_label, model_name, *manager_method = dump_settings[
            'primary'].split('.')
        include_primary = dump_settings.get("include_primary", False)

        default_manager = loading.get_model(app_label, model_name).objects
        if manager_method:
            queryset = getattr(default_manager, manager_method[0])()
        else:
            queryset = default_manager.all()
        if pks:
            queryset = queryset.filter(pk__in=pks)

        deps = dump_settings.get('dependents', [])
        for obj in queryset:
            # get the dependent objects and add to serialize list
            for dep in deps:
                process_dep(obj, dep, serialize_me, seen)
            if include_primary or not deps:
                add_to_serialize_list([obj], serialize_me, seen)

        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),
            use_natural_primary_keys=options.get('natural', False),
        )

        data = reorder_json(json.loads(data),
                            dump_settings.get('order', []),
                            ordering_cond=dump_settings.get('order_cond', {}))

        self.stdout.write(json.dumps(data, indent=4))
    def handle(self, *args, **options):
        # Get the primary object
        dump_name = options['dump_name']
        pks = options['pk']
        dump_settings = settings.CUSTOM_DUMPS[dump_name]
        (app_label, model_name) = dump_settings['primary'].split('.')
        include_primary = dump_settings.get("include_primary", False)
        dump_me = loading.get_model(app_label, model_name)
        objs = dump_me.objects.filter(pk__in=[int(i) for i in pks])
        for obj in objs.all():
            # get the dependent objects and add to serialize list
            for dep in dump_settings['dependents']:
                try:
                    thing = Variable("thing.%s" % dep).resolve({'thing': obj})
                    if hasattr(thing, 'all'):  # Related managers can't be iterated over
                        thing = thing.all()
                    add_to_serialize_list([thing])
                except VariableDoesNotExist:
                    sys.stderr.write('%s not found' % dep)

            if include_primary or not dump_settings['dependents']:
                add_to_serialize_list([obj])

        serialize_fully()
        data = serialize('json', [o for o in serialize_me if o is not None],
                         indent=4,
                         use_natural_foreign_keys=options.get('natural', False),
                         use_natural_primary_keys=options.get('natural', False),
                         )

        data = reorder_json(
            json.loads(data),
            dump_settings.get('order', []),
            ordering_cond=dump_settings.get('order_cond', {})
        )

        print(json.dumps(data, indent=4))
    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:
            # verify input is valid
            try:
                (app_label, model_name) = options['model'].split('.')
            except AttributeError:
                raise CommandError("Specify model as `appname.modelname")
            query = options['query']
            ids = options['ids']

            if options['order']:
                options['order'] = json.loads(options['order'])

            ignore = []
            if options['ignore']:
                ignore = json.loads(options['ignore'])

            related_ignore = []
            if options['related_ignore']:
                related_ignore = json.loads(options['related_ignore'])

            if ids and query:
                raise CommandError(error_text % 'either use query or id list, not both')
            if not (ids or query):
                raise CommandError(error_text % 'must pass list of --ids or a json --query')
        except IndexError:
            raise CommandError(error_text % 'No object_class or filter clause supplied.')
        except ValueError as e:
            raise CommandError(
                error_text %
                "object_class must be provided in the following format: app_name.model_name"
            )
        except AssertionError:
            raise CommandError(error_text % 'No filter argument supplied.')

        dump_me = loading.get_model(app_label, model_name)
        if query:
            objs = dump_me.objects.filter(**json.loads(query))
        else:
            if ids[0] == '*':
                objs = dump_me.objects.all()
            else:
                try:
                    parsers = int, long, str
                except NameError:
                    parsers = int, str
                for parser in parsers:
                    try:
                        objs = dump_me.objects.filter(pk__in=map(parser, ids))
                    except ValueError:
                        pass
                    else:
                        break
                else:
                    objs = []

        if options.get('kitchensink'):
            fields = get_all_related_objects(dump_me)

            # if there are no related ignores for this model include the field
            # or if this related field is not in the related ignores include the field
            related_fields = [rel.get_accessor_name() for rel in fields if
                              model_name not in related_ignore or
                              rel.name not in related_ignore[model_name]]

            for obj in objs:
                for rel in related_fields:
                    try:
                        if hasattr(getattr(obj, rel), 'all'):
                            add_to_serialize_list(getattr(obj, rel).all(),
                                                  serialize_me, seen, ignore=ignore)
                        else:
                            add_to_serialize_list([getattr(obj, rel)],
                                                  serialize_me, seen, ignore=ignore)
                    except FieldError:
                        pass
                    except ObjectDoesNotExist:
                        pass

        add_to_serialize_list(objs, serialize_me, seen, prepend=True, ignore=ignore)

        if options.get('follow_fk', True):
            serialize_fully(serialize_me, seen, ignore)
        else:
            # reverse list to match output of serializez_fully
            serialize_me.reverse()

        natural_foreign = (options.get('natural', False) or
                           options.get('natural_foreign', False))
        natural_primary = (options.get('natural', False) or
                           options.get('natural_primary', False))

        data = serialize(options.get('format', 'json'),
                         [o for o in serialize_me if o is not None],
                         indent=4,
                         use_natural_foreign_keys=natural_foreign,
                         use_natural_primary_keys=natural_primary)

        data = reorder_json(
            json.loads(data),
            options.get('order', []),
        )

        self.stdout.write(json.dumps(data, indent=4))
Exemple #11
0
    def handle(self, fixture, *models, **options):
        output = reorder_json(json.loads(file(fixture).read()), models)

        print json.dumps(output, indent=4)
 def test_reorder_json(self):
     """Test basic ordering of JSON/python object."""
     input_json = [{'model': 'f'}, {'model': 'x'},]
     expected = [{'model': 'x'}, {'model': 'f'}]
     self.assertEqual(expected, reorder_json(input_json, models=['x', 'f'])
     )
Exemple #13
0
def dump_object(model,
                query,
                ids,
                order=[],
                ignore=[],
                related_ignore={},
                additional_serialization_objects_fnc=None,
                format='json',
                kitchensink=True,
                follow_fk=True,
                natural=False,
                natural_foreign=False,
                natural_primary=False):
    serialize_me = []
    seen = set()
    error_text = ('Error\n')

    try:
        # verify input is valid
        try:
            (app_label, model_name) = model.split('.')
        except AttributeError:
            raise CommandError("Specify model as `appname.modelname")

        if ids and query:
            raise CommandError(error_text % 'either use query or id list, not both')
        if not (ids or query):
            raise CommandError(error_text % 'must pass list of --ids or a json --query')
    except IndexError:
        raise CommandError(error_text % 'No object_class or filter clause supplied.')
    except ValueError as e:
        raise CommandError(
            error_text %
            "object_class must be provided in the following format: app_name.model_name"
        )
    except AssertionError:
        raise CommandError(error_text % 'No filter argument supplied.')

    dump_me = loading.get_model(app_label, model_name)
    if query:
        objs = dump_me.objects.filter(**json.loads(query))
    else:
        if ids[0] == '*':
            objs = dump_me.objects.all()
        else:
            try:
                parsers = int, long, str
            except NameError:
                parsers = int, str
            for parser in parsers:
                try:
                    objs = dump_me.objects.filter(pk__in=map(parser, ids))
                except ValueError:
                    pass
                else:
                    break

    if kitchensink:
        fields = get_all_related_objects(dump_me)

        # if there are no related ignores for this model include the field
        # or if this related field is not in the related ignores include the field
        related_fields = [rel.get_accessor_name() for rel in fields if
                          model_name not in related_ignore or
                          rel.name not in related_ignore[model_name]]

        generic_foreign_key_fields = get_all_generic_foreign_keys(dump_me)

        for field in generic_foreign_key_fields:
            if model_name not in related_ignore or field.name not in related_ignore[model_name]:
                related_fields.append(field.name)
        for obj in objs:
            for rel in related_fields:
                try:
                    if hasattr(getattr(obj, rel), 'all'):
                        add_to_serialize_list(getattr(obj, rel).all(), serialize_me, seen, ignore=ignore)
                    else:
                        add_to_serialize_list([getattr(obj, rel)], serialize_me, seen, ignore=ignore)

                        # allow user to add additional data apart from standard foreign keys
                        if additional_serialization_objects_fnc and \
                                callable(additional_serialization_objects_fnc):
                            extra_objs = additional_serialization_objects_fnc(getattr(obj, rel))
                            if extra_objs:
                                add_to_serialize_list(extra_objs, serialize_me, seen, ignore=ignore)

                except FieldError:
                    pass
                except ObjectDoesNotExist:
                    pass

    add_to_serialize_list(objs, serialize_me, seen, prepend=True, ignore=ignore)

    if follow_fk:
        serialize_fully(serialize_me, seen, ignore, additional_serialization_objects_fnc)
    else:
        # reverse list to match output of serializez_fully
        serialize_me.reverse()

    natural_foreign = (natural or
                       natural_foreign)
    natural_primary = (natural or
                       natural_primary)

    if format:
        if format == "json":
            data = CustomJsonSerializer().serialize(
                [o for o in serialize_me if o is not None],
                indent=4,
                use_natural_foreign_keys=natural_foreign,
                use_natural_primary_keys=natural_primary)

        else:
            data = serialize(format,
                             [o for o in serialize_me if o is not None],
                             indent=4,
                             use_natural_foreign_keys=natural_foreign,
                             use_natural_primary_keys=natural_primary)

        data = reorder_json(
            json.loads(data),
            order,
        )

        return json.dumps(data, indent=4)

    # return unserialized objs
    return [o for o in serialize_me if o is not None]
    def handle(self, fixture, *models, **options):
        output = reorder_json(json.loads(file(fixture).read()), models)

        print json.dumps(output, indent=4)
    def handle(self, *args, **options):
        error_text = ('%s\nTry calling dump_object with --help argument or ' +
                      'use the following arguments:\n %s' % self.args)
        try:
            #verify input is valid
            try:
                (app_label, model_name) = options['model'].split('.')
            except AttributeError:
                raise CommandError("Specify model as `appname.modelname")
            query = options['query']
            ids = options['ids']
            config_name = options['config_name']

            if ids and query:
                raise CommandError(error_text %
                                   'either use query or id list, not both')
            if not (ids or query):
                raise CommandError(error_text %
                                   'must pass list of --ids or a json --query')
        except IndexError:
            raise CommandError(error_text %
                               'No object_class or filter clause supplied.')
        except ValueError:
            raise CommandError(error_text %
                               ("object_class must be provided in"
                                " the following format: app_name.model_name"))
        except AssertionError:
            raise CommandError(error_text % 'No filter argument supplied.')

        dump_me = loading.get_model(app_label, model_name)
        if query:
            objs = dump_me.objects.filter(**json.loads(query))
        else:
            if ids[0] == '*':
                objs = dump_me.objects.all()
            else:
                try:
                    parsers = int, long, str
                except NameError:
                    parsers = int, str
                for parser in parsers:
                    try:
                        objs = dump_me.objects.filter(pk__in=map(parser, ids))
                    except ValueError:
                        pass
                    else:
                        break

        if options.get('kitchensink'):
            if django.VERSION >= (1, 8):
                fields = (
                    f for f in dump_me._meta.get_fields()
                    if (f.one_to_many or f.one_to_one) and f.auto_created)
            else:
                fields = dump_me._meta.get_all_related_objects()

            related_fields = [rel.get_accessor_name() for rel in fields]

            for obj in objs:
                for rel in related_fields:
                    try:
                        if hasattr(getattr(obj, rel), 'all'):
                            add_to_serialize_list(getattr(obj, rel).all())
                        else:
                            add_to_serialize_list([getattr(obj, rel)])
                    except FieldError:
                        pass
                    except ObjectDoesNotExist:
                        pass

        try:
            dump_settings = settings.CUSTOM_DUMPS[config_name]
        except Exception:
            dump_settings = None

        add_to_serialize_list(objs)
        serialize_fully()
        data = serialize(
            options.get('format',
                        'json'), [o for o in serialize_me if o is not None],
            indent=4,
            use_natural_foreign_keys=options.get('natural', False),
            use_natural_primary_keys=options.get('natural', False))

        if dump_settings and dump_settings.get('order', []):
            data = reorder_json(json.loads(data),
                                dump_settings.get('order', []),
                                ordering_cond=dump_settings.get(
                                    'order_cond', {}))
        self.stdout.write(json.dumps(data))
        # Clear the list. Useful for when calling multiple dump_object commands with a single execution of django
        del serialize_me[:]
        seen.clear()