Example #1
0
 def _migrate(self, safe=True, process_to_bson=True):
     if process_to_bson:
         self._process_custom_type('bson', self, self.structure)
     self._migration.migrate(self, safe=safe)
     # reload
     old_doc = self.collection.get_from_id(self['_id'])
     if not old_doc:
         raise OperationFailure('Can not reload an unsaved document.'
                                ' %s is not found in the database' % self['_id'])
     else:
         self.update(DotedDict(old_doc))
     self._process_custom_type('python', self, self.structure)
Example #2
0
    def reload(self):
        """
        allow to refresh the document, so after using update(), it could reload
        its value from the database.

        Be careful : reload() will erase all unsaved values.

        If no _id is set in the document, a KeyError is raised.
        """
        self._process_custom_type('bson', self, self.structure)
        old_doc = self.collection.get_from_id(self['_id'])
        if not old_doc:
            raise OperationFailure('Can not reload an unsaved document.'
                                   ' %s is not found in the database' % self['_id'])
        else:
            self.update(DotedDict(old_doc))
        self._process_custom_type('python', self, self.structure)
 def __generate_skeleton(self, doc, struct, path=""):
     for key in struct:
         if type(key) is type:
             new_key = "$%s" % key.__name__
         else:
             new_key = key
         new_path = ".".join([path, new_key]).strip('.')
         #
         # Automatique generate the skeleton with NoneType
         #
         if type(key) is not type and key not in doc:
             if isinstance(struct[key], dict):
                 if type(struct[key]) is dict and self.use_dot_notation:
                     if new_path in self._i18n_namespace:
                         doc[key] = i18nDotedDict(doc.get(key, {}), self)
                     else:
                         doc[key] = DotedDict(
                             doc.get(key, {}),
                             warning=self.dot_notation_warning)
                 else:
                     if callable(struct[key]):
                         doc[key] = struct[key]()
                     else:
                         doc[key] = type(struct[key])()
             elif struct[key] is dict:
                 doc[key] = {}
             elif isinstance(struct[key], list):
                 doc[key] = type(struct[key])()
             elif isinstance(struct[key], CustomType):
                 if struct[key].init_type is not None:
                     doc[key] = struct[key].init_type()
                 else:
                     doc[key] = None
             elif struct[key] is list:
                 doc[key] = []
             elif isinstance(struct[key], tuple):
                 doc[key] = [None for _ in range(len(struct[key]))]
             else:
                 doc[key] = None
         #
         # if the value is a dict, we have a another structure to validate
         #
         if isinstance(struct[key], dict) and type(key) is not type:
             self.__generate_skeleton(doc[key], struct[key], new_path)
Example #4
0
 def __generate_doted_dict(self, doc, struct, path=""):
     for key in struct:
         #
         # Automatique generate the skeleton with NoneType
         #
         if type(key) is type:
             new_key = "$%s" % key.__name__
         else:
             new_key = key
         new_path = ".".join([path, new_key]).strip('.')
         if type(key) is not type:  # and key not in doc:
             if isinstance(struct[key], dict):
                 if type(struct[key]) is dict:
                     if new_path in self._i18n_namespace:
                         doc[key] = i18nDotedDict(doc.get(key, {}), self)
                     else:
                         doc[key] = DotedDict(doc.get(key, {}), warning=self.dot_notation_warning)
         #
         # if the value is a dict, we have a another structure to validate
         #
         if isinstance(struct[key], dict) and type(key) is not type:
             self.__generate_doted_dict(doc[key], struct[key], new_path)