def getFile(_id): """ Retrieve the data from the GenericFile collection for the given _id or db_ref Returns the data in Binary. If you know it's a txt file and want a string, convert with str() Maybe this will be automatically deref'd most of the time? Only if they're mongoengine ReferenceFields... """ try: _id = _id.id except AttributeError: pass f = GenericFile.objects(__raw__={'_id': _id}) # yes it's '_id' here but just 'id' below, gofigure return _try0_dict_key(f, 'file', 'id', _id, None, dict_key='data')
def addFile(data=None, filename=None): """ Put the file data into the GenericFile collection, return the _id for use as an id or ReferenceField. If a filename kwarg is given, read data from the file. If a data kwarg is given or data is the 1st arg, store the data. If both or neither is given, raise an error. """ if filename is not None: if data is not None: raise ValueError('both filename and data kwargs given. can only use one.') else: with open(filename, 'r') as file: # do we need 'b' for binary? data = file.read() # is this blocking? might not always get everything at once?! elif data is None: raise ValueError('neither filename or data kwargs given. need one.') f = GenericFile(data=data) f.save() f.reload() # to fetch generated id return f.to_dbref()