Exemple #1
0
    def ListModelDefs(self, request, context):
        ystr = open('protos/modeldefs.yaml').read()
        yaml_repr = yaml.load(ystr)

        modeldefs = modeldefs_pb2.ModelDefs()

        yaml_to_grpc(yaml_repr, modeldefs)

        return modeldefs
Exemple #2
0
    def ListModelDefs(self, request, context):
        ystr = open("protos/modeldefs.yaml").read()
        yaml_repr = yaml.safe_load(ystr)

        modeldefs = modeldefs_pb2.ModelDefs()

        yaml_to_grpc(yaml_repr, modeldefs)

        REQUEST_COUNT.labels("xos-core", "Modeldefs", "ListModelDefs",
                             grpc.StatusCode.OK).inc()
        return modeldefs
Exemple #3
0
class ModelDefsService(modeldefs_pb2_grpc.modeldefsServicer,
                       XOSAPIHelperMixin):
    def __init__(self, thread_pool):
        self.thread_pool = thread_pool

    def stop(self):
        pass

    @track_request_time("Modeldefs", "ListModelDefs")
    def ListModelDefs(self, request, context):
        ystr = open("protos/modeldefs.yaml").read()
        yaml_repr = yaml.safe_load(ystr)

        modeldefs = modeldefs_pb2.ModelDefs()

        yaml_to_grpc(yaml_repr, modeldefs)

        REQUEST_COUNT.labels("xos-core", "Modeldefs", "ListModelDefs",
                             grpc.StatusCode.OK).inc()
        return modeldefs


if __name__ == "__main__":
    ystr = open("protos/modeldefs.yaml").read()
    yaml_repr = yaml.load(ystr)

    modeldefs = modeldefs_pb2.ModelDefs()
    yaml_to_grpc(yaml_repr, modeldefs)
    print(modeldefs)
Exemple #4
0
    def ListModelDefs(self, request, context):
        models = django.apps.apps.get_models()

        modeldefs = modeldefs_pb2.ModelDefs()

        response = []

        for model in models:
            # NOTE removing Django internal models
            if 'django' in model.__module__:
                continue
            if 'cors' in model.__module__:
                continue
            if 'contenttypes' in model.__module__:
                continue
            if 'core.models.journal' in model.__module__:
                continue
            if 'core.models.project' in model.__module__:
                continue

            modeldef = modeldefs.items.add()

            modeldef.name = model.__name__
            modeldef.app = self.parseModuleName(model.__module__)

            for f in model._meta.fields:
                field = modeldef.fields.add()

                field.name = f.name
                field.hint = f.help_text

                fieldtype = self.convertType(f.get_internal_type())
                if fieldtype is not None:
                    field.type = fieldtype
                else:
                    field.type = 'string'

                if not f.blank and not f.null:
                    val = field.validators.add()
                    val.name = "required"
                    val.bool_value = True

                for v in f.validators:
                    val = field.validators.add()
                    validator_name = v.__class__.__name__
                    if 'function' in validator_name:
                        validator_name = v.__name__
                    validator_name = self.convertValidator(validator_name)

                    if not validator_name:
                        continue

                    val.name = validator_name
                    if hasattr(v, 'limit_value'):
                        try:
                            val.int_value = v.limit_value
                        except TypeError:
                            val.str_value = str(v.limit_value)
                    else:
                        val.bool_value = True

                if f.is_relation and f.related_model:

                    if 'ContentType' in f.related_model.__name__:
                        # ContentType is a Django internal
                        continue

                    field.name = field.name + '_id'
                    field.relation.model = f.related_model.__name__
                    field.relation.type = self.getRelationType(f)

                    rel = modeldef.relations.add()
                    rel.model = f.related_model.__name__
                    rel.type = self.getRelationType(f)
        return modeldefs