Build a PyMongo index spec from a MongoEngine index spec.
(cls, spec)
| 880 | |
| 881 | @classmethod |
| 882 | def _build_index_spec(cls, spec): |
| 883 | """Build a PyMongo index spec from a MongoEngine index spec.""" |
| 884 | if isinstance(spec, str): |
| 885 | spec = {"fields": [spec]} |
| 886 | elif isinstance(spec, (list, tuple)): |
| 887 | spec = {"fields": list(spec)} |
| 888 | elif isinstance(spec, dict): |
| 889 | spec = dict(spec) |
| 890 | |
| 891 | index_list = [] |
| 892 | direction = None |
| 893 | |
| 894 | # Check to see if we need to include _cls |
| 895 | allow_inheritance = cls._meta.get("allow_inheritance") |
| 896 | include_cls = ( |
| 897 | allow_inheritance |
| 898 | and not spec.get("sparse", False) |
| 899 | and spec.get("cls", True) |
| 900 | and "_cls" not in spec["fields"] |
| 901 | ) |
| 902 | |
| 903 | # 733: don't include cls if index_cls is False unless there is an explicit cls with the index |
| 904 | include_cls = include_cls and ( |
| 905 | spec.get("cls", False) or cls._meta.get("index_cls", True) |
| 906 | ) |
| 907 | if "cls" in spec: |
| 908 | spec.pop("cls") |
| 909 | for key in spec["fields"]: |
| 910 | # If inherited spec continue |
| 911 | if isinstance(key, (list, tuple)): |
| 912 | continue |
| 913 | |
| 914 | # ASCENDING from + |
| 915 | # DESCENDING from - |
| 916 | # TEXT from $ |
| 917 | # HASHED from # |
| 918 | # GEOSPHERE from ( |
| 919 | # GEOHAYSTACK from ) |
| 920 | # GEO2D from * |
| 921 | direction = pymongo.ASCENDING |
| 922 | if key.startswith("-"): |
| 923 | direction = pymongo.DESCENDING |
| 924 | elif key.startswith("$"): |
| 925 | direction = pymongo.TEXT |
| 926 | elif key.startswith("#"): |
| 927 | direction = pymongo.HASHED |
| 928 | elif key.startswith("("): |
| 929 | direction = pymongo.GEOSPHERE |
| 930 | elif key.startswith(")"): |
| 931 | try: |
| 932 | direction = pymongo.GEOHAYSTACK |
| 933 | except AttributeError: |
| 934 | raise NotImplementedError |
| 935 | elif key.startswith("*"): |
| 936 | direction = pymongo.GEO2D |
| 937 | if key.startswith(("+", "-", "*", "$", "#", "(", ")")): |
| 938 | key = key[1:] |
| 939 |
no test coverage detected