def parse_batch(self, requestArray): ''' request = { 'route':'/......', 'data':{......} } ''' ret = [] try: for request in requestArray: mapped = self.map_request(request['route']) if mapped: ret.append( mapped(__fetch_values=self.values_proxy( request['data'] if 'data' in request else {}), __channel='batch')) else: ret.append(Res(-20001, route=request['route'])) return Res(0, batch=ret) except: return Res( -1, 'Could not complete the request due to error. Please check whether batch argument is in the right format.' )
def getDocuments(uID=None, status='active', start='0', end='50'): try: start = int(start) end = int(end) assert start <= end except: return Res(**{'code': -1, 'message': 'Invalid start / end'}) archived = False if status == 'active': archived = False elif status == 'archived': archived = True else: archived = None r = [] for x in core.GetDocuments(uID=uID, archived=archived, start=start, end=end): Q = dict(x.to_mongo()) Q.pop('_id') r.append(Q) return Res(**{'code': 0, 'result': r})
def GetResourceGroupByID(uID, resID): r = core.GetResourceGroupByID(uID, resID) if r: return Res( **{ 'code': 0, 'message': 'Successfully obtained resource group info.', 'resourceGroup': { 'resID': r.resID, 'name': r.name, 'uID': r.uID } }) return { 'code': -303, 'message': f'ResourceGroup "{str(resID)}" does not exist.' }
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 search(term, start=0, limit=100): only_uuid = False only_userid = False only_name = False if term: if term[0] == '#': term = term[1:] only_userid = True elif term[0] == '!': term = term[1:] only_name = True elif term[0] == '$': term = term[1:] only_uuid = True ret = [] for user in core.userlib.search(term, only_name=only_name, only_userid=only_userid, only_uuid=only_uuid): ret.append(user.uuid) return Res(0, results=ret[start:limit])
def unfollow(uuid, target): return Res(core.userlib.unfollow(uuid, target))
def post_get_stream(uuid): # TODO: Add limit stream = core.postlib.get_stream(uuid) return Res(0, stream=stream)
def check_email(email): return Res(0, exist=core.authlib.get_if_email_exists(email=email))
def remoteLoginRequest(): r = core.NewRemoteLogin() if r: return Res(**{'code': 0, 'rID': r}) return Res(**{'code': -1, 'message': 'Could not initiate'})
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})
def deleteDocumentByID(docID): if os.path.exists(filestore.getStorageLocation(docID)) and os.path.isfile( filestore.getStorageLocation(docID)): os.remove(filestore.getStorageLocation(docID)) return Res(**{'code': 0, 'result': core.DeleteDocs(docID)})
def get_beta(uuid): return Res(0, status=core.userlib.get_beta_status(uuid))
def set_beta(uuid, status): r = core.userlib.set_beta_status(uuid, status) if status and r == 0: core.email.send_beta_join_alert(uuid) return Res(r)
def unpin(uuid, target): return Res(core.userlib.unpin(uuid, target))
def delete_post(uuid, postid): post = core.postlib.get_post_by_postid(postid) if post.uuid.lower() == uuid: post.delete() return Res(0) return Res(-120)
def getHashTags(uID): r = core.GetUserHashTags(uID) return Res(**{'code': 0, 'result': r})
def get_following(uuid, target=None, start=0, limit=100): if not target: target = uuid return Res(0, following=core.userlib.get_following_by_uuid( uuid=target)[start:limit])
def get_pinned(uuid, start=0, limit=100): return Res( 0, pinned=core.userlib.get_pinned_by_uuid(uuid=uuid)[start:limit])
def get_followers(uuid, target=None, start=0, limit=100): if not target: target = uuid return Res(0, followers=core.userlib.get_followers_by_uuid(uuid))
def register(uID, name, password): return Res(**core.NewUser(uID, name, password))
def is_following(uuid, target): return Res(0, following=core.userlib.is_following(uuid, target))
def getDocumentsByResourceGroup(uID, resID): return Res(**{ 'code': 0, 'documents': core.GetDocumentsByResourceGroup(uID, resID) })
def is_follower(uuid, target): return Res(0, follower=core.userlib.is_following(target, uuid))
def rejectRemoteLogin(rID): core.RejectRemoteLogin(rID) return Res(**{'code': 0, 'message': 'Request rejected.'})
def is_pinned(uuid, target): return Res(0, pinned=core.userlib.is_pinned(uuid, target))
def check_scope(uuid, token): return Res(0, scope=core.authlib.get_token_scope(uuid, token))
def post_get_user_post(uuid, target): stream = core.postlib.get_user_stream(uuid, target) return Res(0, stream=stream)