Example #1
0
    def handle(self, *args, **options):
        if (len(args) == 0):
            raise CommandError("Please specify model class name.")

        model_path = args[0]
        model_id = options["id"]
        if model_id:
            Model = resolve_model(model_path)

            try:
                data = Model.objects.get(pk=model_id)
                # logging.info("Retrieved '%s' entry with primary key: '%s'" % (model_path, model_id))

                serialized_data = serializers.serialize("python",
                                                        [data])[0]["fields"]
                serialized_data["id"] = model_id
                logging.debug("Serialized data: '%s'" % serialized_data)
                print json.dumps(serialized_data)

            except Model.DoesNotExist:
                logging.error(
                    "Could not find '%s' entry with primary key: '%s'" %
                    (model_path, model_id))
                sys.exit(1)

        else:
            raise CommandError("Please specify --id to fetch model.")
Example #2
0
    def handle(self, *args, **kwargs):
        if len(args) != 2:
            raise CommandError("Wrong number of args specified")

        model_class = resolve_model(args[0])
        model_id = args[1]
        newdata = json.loads(kwargs['json_data'])

        model = model_class.objects.get(id=model_id)

        for attr, value in newdata.iteritems():
            setattr(model, attr, value)

        model.save()
    def countmodels(self, modelpath):
        '''
        Return the number of instances of a particular model that exist on
        this distributed server.
        '''

        model = resolve_model(modelpath)

        code = '''
        from %(module_path)s import %(model_name)s
        count = %(model_name)s.objects.count()
        ''' % {"model_name": model.__name__, "module_path": model.__module__}

        return self.runcode(code)["count"]
Example #4
0
    def handle(self, *args, **kwargs):
        if len(args) != 2:
            raise CommandError("Wrong number of args specified")

        model_class = resolve_model(args[0])
        model_id = args[1]
        newdata = json.loads(kwargs['json_data'])

        model = model_class.objects.get(id=model_id)

        for attr, value in newdata.iteritems():
            setattr(model, attr, value)

        model.save()
    def countmodels(self, modelpath):
        '''
        Return the number of instances of a particular model that exist on
        this distributed server.
        '''

        model = resolve_model(modelpath)

        code = '''
        from %(module_path)s import %(model_name)s
        count = %(model_name)s.objects.count()
        ''' % {
            "model_name": model.__name__,
            "module_path": model.__module__
        }

        return self.runcode(code)["count"]
Example #6
0
    def handle(self, *args, **options):

        if len(args) != 1:
            raise CommandError("No args specified")
        else:
            if not options["json_data"]:
                raise CommandError(
                    "Please specify input data as a json string")

            try:
                model = resolve_model(args[0])

                # Reading the json data string into a map
                data_map = json.loads(options["json_data"])

                entity_ids = []

                for i in range(int(options["count"])):

                    # Incorporate the iteration number into any fields that need it
                    model_data = {}
                    for key, val in data_map.items():
                        if "%d" in val or "%s" in val:
                            val = val % i
                        model_data[key] = val

                    # Constructing an entity from the Model
                    entity = model(**model_data)
                    entity.full_clean()
                    entity.save()

                    entity_ids.append(entity.id)

                # Printing out the id of the entity
                sys.stdout.write("%s\n" % (",".join(entity_ids)))
                sys.exit(0)
            except:
                raise
Example #7
0
    def handle(self, *args, **options):

        if len(args) != 1:
            raise CommandError("No args specified")
        else:
            if not options["json_data"]:
                raise CommandError("Please specify input data as a json string")

            try:
                model = resolve_model(args[0])

                # Reading the json data string into a map
                data_map = json.loads(options["json_data"])

                entity_ids = []

                for i in range(int(options["count"])):

                    # Incorporate the iteration number into any fields that need it
                    model_data = {}
                    for key, val in data_map.items():
                        if "%d" in val or "%s" in val:
                            val = val % i
                        model_data[key] = val

                    # Constructing an entity from the Model
                    entity = model(**model_data)
                    entity.full_clean()
                    entity.save()

                    entity_ids.append(entity.id)

                # Printing out the id of the entity
                sys.stdout.write("%s\n" % (",".join(entity_ids)))
                sys.exit(0)
            except:
                raise
Example #8
0
    def handle(self, *args, **options):
        if (len(args) == 0):
            raise CommandError("Please specify model class name.")

        model_path = args[0]
        model_id = options["id"]
        if model_id:
            Model = resolve_model(model_path)
            
            try:
                data = Model.objects.get(pk=model_id)
                # logging.info("Retrieved '%s' entry with primary key: '%s'" % (model_path, model_id))

                serialized_data = serializers.serialize("python", [data])[0]["fields"]
                serialized_data["id"] = model_id
                logging.debug("Serialized data: '%s'" % serialized_data)
                print json.dumps(serialized_data, default=dthandler)

            except Model.DoesNotExist:
                logging.error("Could not find '%s' entry with primary key: '%s'" % (model_path, model_id))
                sys.exit(1)
            
        else:
            raise CommandError("Please specify --id to fetch model.")