Пример #1
0
    def dump_tree(cls, root=None, up_to=None, to_camel=True):
        """Dump a tree or subtree for serialization rendering all
        fieldnames as camelCase by default.

        Extension of django-treebeard.treebeard.mp_tree `dump_bulk` for
        finer-grained control over the initial queryset and resulting value.
        """
        if up_to and up_to not in constants.CTS_URN_NODES:
            raise ValueError(f"Invalid CTS node identifier for: {up_to}")

        # NOTE: This filters the queryset using path__startswith,
        # because the default `get_tree(parent=root)` uses `self.is_leaf
        # and the current bulk ingestion into ATLAS does not populate
        # `numchild`.
        qs = cls._get_serializable_model().get_tree()
        if root:
            qs = qs.filter(
                path__startswith=root.path,
                # depth__gte=parent.depth
            ).order_by("path")
        if up_to:
            depth = constants.CTS_URN_DEPTHS[up_to]
            qs = qs.exclude(depth__gt=depth)

        tree, index = [], {}
        for pyobj in serializers.serialize("python", qs):
            fields = pyobj["fields"]
            path = fields["path"]
            depth = int(len(path) / cls.steplen)
            del fields["depth"]
            del fields["path"]
            del fields["numchild"]

            metadata = fields["metadata"]
            if to_camel:
                fields = camelize(fields)
                metadata = camelize(metadata)
            fields.update({"metadata": metadata})

            newobj = {"data": fields}

            if (not root and depth == 1) or (root and len(path) == len(root.path)):
                tree.append(newobj)
            else:
                parentpath = cls._get_basepath(path, depth - 1)
                parentobj = index[parentpath]
                if "children" not in parentobj:
                    parentobj["children"] = []
                parentobj["children"].append(newobj)
            index[path] = newobj
        return tree
Пример #2
0
 def serialize(errors):
     if isinstance(errors, dict):
         if errors.get("__all__", False):
             errors["non_field_errors"] = errors.pop("__all__")
         return camelize(errors)
     elif isinstance(errors, list):
         return {"nonFieldErrors": errors}
     raise WrongUsage("`errors` must be list or dict!")
Пример #3
0
 def resolve_metadata(obj, *args, **kwargs):
     metadata = obj.metadata
     work = obj.get_parent()
     text_group = work.get_parent()
     metadata.update({
         "work_label": work.label,
         "text_group_label": text_group.label,
         "lang": metadata["lang"],
         "human_lang": hookset.get_human_lang(metadata["lang"]),
     })
     return camelize(metadata)
Пример #4
0
    def __init__(self, error_field=None, message=None, errors=None):
        field = error_field
        if message is None:
            message = self.default_message

        if errors is not None:
            if isinstance(errors, dict):
                if errors.get("__all__", False):
                    errors["non_field_errors"] = errors.pop("__all__")
                return camelize(errors)
            elif isinstance(errors, list):
                return {"nonFieldErrors": errors}

        super().__init__(message)
Пример #5
0
def get_errors(errors):
    error_list = list()
    errors = camelize(errors)
    for key, value in errors.items():
        error_list.append(ErrorType(field=key, messages=value[0]))
    return error_list
Пример #6
0
 def from_errors(cls, errors):
     data = camelize(
         errors) if graphene_settings.CAMELCASE_ERRORS else errors
     return [cls(field=key, messages=value) for key, value in data.items()]
Пример #7
0
 def resolve_data(obj, *args, **kwargs):
     return camelize(obj.data)
Пример #8
0
 def resolve_metadata(obj, info, *args, **kwargs):
     # TODO: make generic.GenericScalar derived class
     # that automatically camelizes data
     return camelize(obj.metadata)
Пример #9
0
 def resolve_metadata(obj, *args, **kwargs):
     metadata = obj.metadata
     return camelize(metadata)