def _validate_doc_data(self, data, doc=None): if 'validate' in self.params and self.params['validate'] == '0': validate=False else: validate=True # NOTE: Get Base object! By John Doe base=self.context.get_base() # NOTE: Parse JSON object! By John Doe from ..lib import utils document=utils.json2object(data) from liblightbase.lbdoc.metadata import DocumentMetadata import datetime if doc is not None: dt_idx=document.get('_metadata', { }).get('dt_idx', None) if dt_idx and not isinstance(dt_idx, datetime.datetime): dt_idx=datetime.datetime.strptime(dt_idx, '%d/%m/%Y %H:%M:%S') # NOTE: Build Metadata! By John Doe _metadata=DocumentMetadata(**dict( id_doc=doc.id_doc, dt_doc=doc.dt_doc, dt_last_up=datetime.datetime.now(), dt_idx=dt_idx, dt_del=doc.dt_del )) else: # NOTE: SELECT next id from sequence! By John Doe doc_id=self.context.entity.next_id() # NOTE: Build Metadata! By John Doe now=datetime.datetime.now() _metadata=DocumentMetadata(doc_id, now, now) (document, # NOTE: Document it self! By John Doe reldata, # NOTE: Relational data! By John Doe files, # NOTE: All existent files within document! By John Doe cfiles # NOTE: All non-existent (will be created) files within # document! By John Doe )=base.validate(document, _metadata, validate) # NOTE: Normalize relational data! By John Doe [self._fix_matrix(reldata[field]) for field in reldata if\ isinstance(reldata[field], Matrix)] data={} # NOTE: Build database object! By John Doe data['document']=document data['__files__']=files data.update(_metadata.__dict__) data.update(reldata) return data
def dict2document(base, dictobj, metaclass=None): """ Convert a dictionary object to BaseMetaClass object. @param base: Base object. @param dictobj: dictionary object. @param metaclass: GroupMetaClass in question. """ kwargs = {} if metaclass is None: metaclass = base.metaclass() if dictobj.get('_metadata'): kwargs['_metadata'] = DocumentMetadata(**dictobj.pop('_metadata')) for member in dictobj: struct = base.get_struct(member) if struct.is_field: kwargs[member] = dictobj[member] elif struct.is_group: if struct.metadata.multivalued: meta_object = [] for element in dictobj[member]: meta_inner_object = dict2document( base=base, dictobj=element, metaclass=base.metaclass(struct.metadata.name)) meta_object.append(meta_inner_object) else: meta_object = dict2document(base=base, dictobj=dictobj[member], metaclass=base.metaclass( struct.metadata.name)) kwargs[member] = meta_object return metaclass(**kwargs)
def validate_patch_data(cls, data, member, whole_doc=True): validate = True if 'validate' in data and data['validate'] == '0': validate = False data.pop('validate') if 'value' in data: # NOTE: Recupera a estrutura (json) da base no formato dict. # Get Base object (CustomView <- DocumentContextFactory <- # CustomContextFactory <- BaseMemory <- get_base()) base = cls.get_base() # Parse JSON object if not whole_doc: document = member.document updated_partial_document = utils.json2object(data['value']) # update member.document with new data utils.DocumentPatchUtils().update_dict(document, updated_partial_document) else: document = utils.json2object(data['value']) data.pop('value') dt_idx = document.get('_metadata', {}).get('dt_idx', None) if dt_idx and not isinstance(dt_idx, datetime.datetime): dt_idx = datetime.datetime.strptime( dt_idx, '%d/%m/%Y %H:%M:%S') # Build Metadata _metadata = DocumentMetadata(**dict( id_doc=member.id_doc, dt_doc=member.dt_doc, dt_last_up=datetime.datetime.now(), dt_idx=dt_idx, dt_del=member.dt_del )) (document, # Document it self. reldata, # Relational data. files, # All existent files within document. cfiles # All non-existent (will be created) files within document. ) = base.validate(document, _metadata, validate) # Normalize relational data [fix_matrix(reldata[field]) for field in reldata if isinstance(reldata[field], Matrix)] # Build database object data['document'] = document data['__files__'] = files data.update(_metadata.__dict__) data.update(reldata) return data
def validate_put_data(cls, data, member): if 'value' in data: # Get Base object base = cls.get_base() # Parse JSON object document = utils.json2object(data['value']) data.pop('value') dt_idx = document.get('_metadata', { })\ .get('dt_idx', None) if dt_idx and not isinstance(dt_idx, datetime.datetime): dt_idx = datetime.datetime.strptime(dt_idx, '%d/%m/%Y %H:%M:%S') # Build Metadata _metadata = DocumentMetadata(**dict(id_doc=member.id_doc, dt_doc=member.dt_doc, dt_last_up=datetime.datetime.now(), dt_idx=dt_idx, dt_del=member.dt_del)) ( document, # Document it self. reldata, # Relational data. files, # All existent files within document. cfiles # All non-existent (will be created) files within document. ) = base.validate(document, _metadata) # Normalize relational data [ fix_matrix(reldata[field]) for field in reldata if isinstance(reldata[field], Matrix) ] # Build database object data['document'] = document data['__files__'] = files data.update(_metadata.__dict__) data.update(reldata) return data
def validate_post_data(cls, data): validate = True if 'validate' in data and data['validate'] == '0': validate = False if 'value' in data: # Get Base object base = cls.get_base() # Parse JSON object document = utils.json2object(data['value']) # SELECT next id from sequence id = cls.context.entity.next_id() # Build Metadata now = datetime.datetime.now() _metadata = DocumentMetadata(id, now, now) ( document, # Document it self. reldata, # Relational data. files, # All existent files within document. cfiles # All non-existent (will be created) files within document. ) = base.validate(document, _metadata, validate) # Normlize relational data [ fix_matrix(reldata[field]) for field in reldata if isinstance(reldata[field], Matrix) ] # Build database object data['document'] = document data['__files__'] = files data.update(_metadata.__dict__) data.update(reldata) return data