def export_ary_fixture(): for model in apps.get_app_config('ary').get_models(): label = model._meta.label if label in IGNORE_MODELS: continue objs = model.objects.all() add_to_serialize_list(objs) serialize_fully() data = serialize('json', sorted( [o for o in serialize_me if o is not None], key=lambda x: (f'{x._meta.app_label}.{x._meta.model_name}', x.pk), ), indent=2, use_natural_foreign_keys=False, use_natural_primary_keys=False) del serialize_me[:] seen.clear() return data
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'] 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() elif 'limit:' in ids[0]: try: limit = int(ids[0].replace('limit:', '')) objs = dump_me.objects.all()[:limit] except: raise CommandError( 'Recheck the command it should be limit:N, N is the limit.' ) 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'): fields = get_all_related_objects(dump_me) 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 add_to_serialize_list(objs) if options.get('follow_fk', True): serialize_fully() 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)) self.stdout.write( 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)) # Clear the list. Useful for when calling multiple # dump_object commands with a single execution of django del serialize_me[:] seen.clear()
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"] 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"): fields = get_all_related_objects(dump_me) 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 if options["count"] is not None: objs = objs.all()[: options["count"]] add_to_serialize_list(objs) if options.get("follow_fk", True): serialize_fully() 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 ) self.stdout.write( 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, ) ) # Clear the list. Useful for when calling multiple # dump_object commands with a single execution of django del serialize_me[:] seen.clear()
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"] 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 add_to_serialize_list(objs) serialize_fully() self.stdout.write( 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), ) ) # Clear the list. Useful for when calling multiple dump_object commands with a single execution of django del serialize_me[:] seen.clear()
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'] 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'): related_fields = [rel.get_accessor_name() for rel in dump_me._meta.get_all_related_objects()] 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 add_to_serialize_list(objs) serialize_fully() self.stdout.write(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))) # Clear the list. Useful for when calling multiple dump_object commands with a single execution of django del serialize_me[:] seen.clear()
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()