def share_p(): ''' generates a token that can be used to grant access to a project ''' if not authenticator.is_auth(flask.session): return flask.jsonify(status="auth", message="User is not authenticated") # request fields: target, access, project_id, document_id req = json.loads(flask.request.form['request']) try: # create a token for the given document and email the recipient if req['record']['project_id'] is None: raise query.QueryException("Required parameter project_id not provided") result = query.add_token(db(), authenticator.user_id(flask.session), req['record']['project_id'], req['record']['access'], document_id=None) # send email if config.EMAIL: # not yet implemented msg = flask_mail.Message("Hello", sender="*****@*****.**", recipients=[req['record']['target']]) mail.send(msg) return flask.jsonify(status="success", token=result) except query.QueryException as ex: return flask.jsonify(status="error", message=ex.message)
def search_rated(): if not authenticator.is_auth(flask.session): return flask.jsonify(status="auth", message="User is not authenticated") project_id = flask.request.form['project_id'] if flask.request.form['project_id'] is None: raise query.QueryException("Required parameter project_id not provided") return flask.jsonify(status="success", q='Top Rated', documents=query.summary(query.search_rated(db(), authenticator.user_id(flask.session), project_id)))
def get_data(category): if not authenticator.is_auth(flask.session): return flask.jsonify(status="auth", message="User is not authenticated") try: # project level if category == 'projects': if 'message' in flask.session: message=flask.session['message'] del flask.session['message'] return flask.jsonify( message=message, username=authenticator.username(flask.session), projects=query.summary(query.projects(db(), authenticator.user_id(flask.session)).all()), shared=query.summary(query.shared_projects(db(), authenticator.user_id(flask.session))) ) else: return flask.jsonify( username=authenticator.username(flask.session), projects=query.summary(query.projects(db(), authenticator.user_id(flask.session)).all()), shared=query.summary(query.shared_projects(db(), authenticator.user_id(flask.session))) ) if category == 'documents': if flask.request.args.get('project_id') is None: raise query.QueryException("Required parameter project_id not provided") return flask.jsonify(username=authenticator.username(flask.session), documents=query.documents(db(), authenticator.user_id(flask.session), flask.request.args.get('project_id'))) if category == 'shares': if flask.request.args.get('project_id') is None: raise query.QueryException("Required parameter project_id not provided") return flask.jsonify(status="success", username=authenticator.username(flask.session), shares=query.detail(query.shares(db(), authenticator.user_id(flask.session), flask.request.args.get('project_id')))) # document level if category == 'document': if flask.request.args.get('project_id') is None: raise query.QueryException("Required parameter project_id not provided") if flask.request.args.get('document_id') is None: raise query.QueryException("Required parameter document_id not provided") document = query.document(db(), authenticator.user_id(flask.session), flask.request.args.get('project_id'), flask.request.args.get('document_id')) return flask.jsonify(username=authenticator.username(flask.session), document=document.detail()) if category == 'folder': if flask.request.args.get('project_id') is None: raise query.QueryException("Required parameter project_id not provided") if flask.request.args.get('document_id') is None: raise query.QueryException("Required parameter document_id not provided") folder = query.folder(db(), authenticator.user_id(flask.session), flask.request.args.get('project_id'), flask.request.args.get('document_id')) children = query.children(db(), folder) return flask.jsonify(username=authenticator.username(flask.session), document=folder.detail(), children=query.detail(children)) if category == 'attachment': if flask.request.args.get('project_id') is None: raise query.QueryException("Required parameter project_id not provided") if flask.request.args.get('id') is None: raise query.QueryException("Required parameter id not provided") result = query.attachment(db(), authenticator.user_id(flask.session), flask.request.args.get('project_id'), flask.request.args.get('id')) # post-processing if flask.request.args.get('resize') is not None: # image resize im = Image.open(result['filename']) f = float(flask.request.args.get('resize')) (width, height) = int(im.width * f), int(im.height * f) im = im.resize((width, height)) with io.BytesIO() as output: im.save(output, format=result['name'].split('.')[-1]) response = flask.make_response(output.getvalue()) else: response = flask.make_response(open(result['filename'], 'rb').read()) content_type = mimetypes.MimeTypes().guess_type(result['name'])[0] response.headers['Content-Type'] = content_type or 'application/octet-stream' response.headers['Content-Disposition'] = 'inline; filename="{}"'.format(result["name"].replace('"', '')) # TODO encode name return response except query.QueryException as ex: return flask.jsonify(status="error", message="Request failed: {}".format(ex.message))