Пример #1
0
 def __to_elastic_dict__(self):
     d = {}
     for k, v in self.__fields_values__.items():
         if v is not None:
             value = v
             if isinstance(v, datetime.datetime):
                 value = v.isoformat()
             if self.__fields__[k].is_encrypted:
                 conn = ElasticOSMConnection()
                 if conn.encryption_key is None:
                     raise ElasticOSMException(
                             'Cannot encrypt field without encryption key'
                             )
                 cryptographer = Cryptographer(
                                     key_string=conn.encryption_key
                                     )
                 value = cryptographer.encrypt(v)
             d[k] = value
     d['id'] = self.id
     return d
Пример #2
0
 def from_pyes_model(cls, pyes_model):
     from elasticosm.models.registry import ModelRegistry
     registry = ModelRegistry()
     class_type = registry.model_registry[pyes_model._meta.type]
     module = import_module(class_type.__module__)
     _class = getattr(module, class_type.__name__)
     instance = _class()
     for k, v in pyes_model.items():
         value = v
         if k != 'id':
             if instance.__fields__[k].is_encrypted:
                 conn = ElasticOSMConnection()
                 if conn.encryption_key is None:
                     raise ElasticOSMException(
                             'Cannot decrypt field without encryption key'
                             )
                 cryptographer = Cryptographer(
                                     key_string=conn.encryption_key
                                     )
                 value = unicode(cryptographer.decrypt(v))
         instance.__setattr__(k, value)
     return instance