11import copy
22import numbers
3+ import warnings
34from functools import partial
45
56import pymongo
2324 OperationError ,
2425 ValidationError ,
2526)
27+ from mongoengine .pymongo_support import LEGACY_JSON_OPTIONS
2628
2729__all__ = ("BaseDocument" , "NON_FIELD_ERRORS" )
2830
2931NON_FIELD_ERRORS = "__all__"
3032
33+ try :
34+ GEOHAYSTACK = pymongo .GEOHAYSTACK
35+ except AttributeError :
36+ GEOHAYSTACK = None
37+
3138
3239class BaseDocument :
3340 # TODO simplify how `_changed_fields` is used.
@@ -439,10 +446,19 @@ def to_json(self, *args, **kwargs):
439446 Defaults to True.
440447 """
441448 use_db_field = kwargs .pop ("use_db_field" , True )
449+ if "json_options" not in kwargs :
450+ warnings .warn (
451+ "No 'json_options' are specified! Falling back to "
452+ "LEGACY_JSON_OPTIONS with uuid_representation=PYTHON_LEGACY. "
453+ "For use with other MongoDB drivers specify the UUID "
454+ "representation to use." ,
455+ DeprecationWarning ,
456+ )
457+ kwargs ["json_options" ] = LEGACY_JSON_OPTIONS
442458 return json_util .dumps (self .to_mongo (use_db_field ), * args , ** kwargs )
443459
444460 @classmethod
445- def from_json (cls , json_data , created = False ):
461+ def from_json (cls , json_data , created = False , ** kwargs ):
446462 """Converts json data to a Document instance
447463
448464 :param str json_data: The json data to load into the Document
@@ -460,7 +476,16 @@ def from_json(cls, json_data, created=False):
460476 # TODO should `created` default to False? If the object already exists
461477 # in the DB, you would likely retrieve it from MongoDB itself through
462478 # a query, not load it from JSON data.
463- return cls ._from_son (json_util .loads (json_data ), created = created )
479+ if "json_options" not in kwargs :
480+ warnings .warn (
481+ "No 'json_options' are specified! Falling back to "
482+ "LEGACY_JSON_OPTIONS with uuid_representation=PYTHON_LEGACY. "
483+ "For use with other MongoDB drivers specify the UUID "
484+ "representation to use." ,
485+ DeprecationWarning ,
486+ )
487+ kwargs ["json_options" ] = LEGACY_JSON_OPTIONS
488+ return cls ._from_son (json_util .loads (json_data , ** kwargs ), created = created )
464489
465490 def __expand_dynamic_values (self , name , value ):
466491 """Expand any dynamic values to their correct types / values."""
@@ -898,7 +923,10 @@ def _build_index_spec(cls, spec):
898923 elif key .startswith ("(" ):
899924 direction = pymongo .GEOSPHERE
900925 elif key .startswith (")" ):
901- direction = pymongo .GEOHAYSTACK
926+ try :
927+ direction = pymongo .GEOHAYSTACK
928+ except AttributeError :
929+ raise NotImplementedError
902930 elif key .startswith ("*" ):
903931 direction = pymongo .GEO2D
904932 if key .startswith (("+" , "-" , "*" , "$" , "#" , "(" , ")" )):
@@ -923,10 +951,10 @@ def _build_index_spec(cls, spec):
923951 index_list .append ((key , direction ))
924952
925953 # Don't add cls to a geo index
926- if include_cls and direction not in (
927- pymongo . GEO2D ,
928- pymongo .GEOHAYSTACK ,
929- pymongo . GEOSPHERE ,
954+ if (
955+ include_cls
956+ and direction not in ( pymongo .GEO2D , pymongo . GEOSPHERE )
957+ and ( GEOHAYSTACK is None or direction != GEOHAYSTACK )
930958 ):
931959 index_list .insert (0 , ("_cls" , 1 ))
932960
0 commit comments