def doc_write(docID=None, uID=None, token=None): if docID: d = core.GetDocByDocID(docID) if d: # if d.accessLevel == 'public': # return { # 'code': 0, # 'message': 'Public Document' # } # else: # Not public - auth authresult = doc_access_v_token(uID, token) if authresult['code'] != 0: return authresult # Check permission if str(d.owner.lower()) == str(uID).lower(): return {'code': 0, 'message': 'Document owner'} else: for x in d.policies: if str(x.uID).lower() == str( uID).lower() and x.write == True: return {'code': 0, 'message': 'Policy allowed'} return { 'code': -400, 'message': 'You do not have the right to make changes to this document.' } return {'code': -301, 'message': 'Document does not exist'}
def shareDocument(uID, targetUID, docID, read='true', write='false'): d = core.GetDocByDocID(docID) read = True if read == 'true' else False write = True if write == 'true' else False if d: try: # Try if the policy for that user exists for x in range(len(d.policies) - 1, -1, -1): if str(d.policies[x].uID).lower() == targetUID.lower(): d.policies.pop(x) if read or write: d.policies.append(Policy(uID=targetUID, read=read, write=write)) d.save() return Res( **{ 'code': 0, 'result': { 'targetUID': targetUID, 'read': read, 'write': write } }) except Exception as e: print(e) return Res(**{'code': -1, 'message': 'Error'}) return Res(**{'code': -1, 'message': 'Error'})
def getDocumentByDocumentID(docID): r = core.GetDocByDocID(docID) if r: r = r.to_mongo() r.pop('_id') else: r = {} return Res(**{'code': 0, 'result': r})
def getDocumentAccessToken(docID): r = core.GetDocByDocID(docID) if r: auth = core.GetAuthCode(docID) if auth: return Res(**{'code': 0, 'auth': auth}) return GeneralErrorHandler(-1, 'Could not get auth.') else: return GeneralErrorHandler(-301, 'Document does not exist')
def GetFilePreview(docID, auth=None, path=None): if core.ValidatePermission(docID, auth): doc = core.GetDocByDocID(docID) extension = doc.fileName.rsplit('.', 1)[-1].lower() MIME = None if extension in EXTENSION_MIME: MIME = EXTENSION_MIME[extension] return send_file(filestore.getStorageLocation(docID), mimetype=MIME) # Attempts to re-authorize return redirect(FRONTEND_ROOT + '/view?docID=' + docID)
def addDocumentsToResourceGroup(uID, resID, docID): # Check if resGroup exists if not core.GetResourceGroupByID(uID, resID): return Res(**{ 'code': -303, 'message': 'Resource group does not exist' }) if not core.GetDocByDocID(docID): return Res(**{'code': -301, 'message': 'Document does not exist!'}) r = core.AddDocumentToResourceGroup(uID, resID, docID) if r: return Res(**{'code': 0, 'message': 'Success'}) return Res(**{'code': -1, 'message': 'Failed to add document.'})
def getPreviewLink(docID): r = core.GetDocByDocID(docID) if r: # redAddr = r.fileName redAddr = secure_filename(r.name + '.' + r.fileName.rsplit('.', 1)[-1].lower()) auth = core.GetAuthCode(docID) if auth: return Res( **{ 'code': 0, 'link': '/preview/' + redAddr + '?auth=' + auth + '&docID=' + docID }) return GeneralErrorHandler(-1, 'Could not get auth.') else: return GeneralErrorHandler(-301, 'Document does not exist')
def editDocumentByID(docID, properties, elToken=None, uID=None): doc = core.GetDocByDocID(docID) if not doc: return GeneralErrorHandler(-301, 'Document does not exist') try: properties = json.loads(properties) except: return GeneralErrorHandler(-1, 'properties JSON parse error') if not isinstance(properties, dict): return GeneralErrorHandler(-1, 'properties should be a dictionary.') if 'fileName' in properties: return GeneralErrorHandler( -1, "field fileName is protected and can not be changed over the API.") success = [] failed = [] for prop in properties: if prop in doc: setattr(doc, prop, properties[prop]) success.append(prop) else: failed.append(prop) if 'docID' in properties: try: if filestore.getStorageLocation(docID): os.rename(filestore.getStorageLocation(docID), filestore.newStorageLocation(properties['docID'])) else: raise Exception('Could not save the file') except: return GeneralErrorHandler(-1, 'Failed to move the file') # Also, change the DocumentProperties d = core.GetAllDocumentProperties(docID=docID) for x in d: try: x.docID = properties['docID'] except: print('Warn: Could not change docID in DocumentProperties.') try: doc.save() try: for x in d: x.save() except: print('Warn: Could not save changed DocumentProperties.') except: if 'docID' in properties: try: # Rollback os.rename(filestore.getStorageLocation(properties['docID']), filestore.newStorageLocation(docID)) except: pass return GeneralErrorHandler(-302, 'Failed to save to the database.') return Res(**{'code': 0, 'success': success, 'failed': failed})