repo_name
stringlengths
8
38
pr_number
int64
3
47.1k
pr_title
stringlengths
8
175
pr_description
stringlengths
2
19.8k
author
null
date_created
stringlengths
25
25
date_merged
stringlengths
25
25
filepath
stringlengths
6
136
before_content
stringlengths
54
884k
after_content
stringlengths
56
884k
pr_author
stringlengths
3
21
previous_commit
stringlengths
40
40
pr_commit
stringlengths
40
40
comment
stringlengths
2
25.4k
comment_author
stringlengths
3
29
__index_level_0__
int64
0
5.1k
airbnb/knowledge-repo
767
[KP] support more sort by & UI improvement
Description of changeset: as title Test Plan: local dev ![image](https://user-images.githubusercontent.com/64947033/224917135-549707a4-2cba-4294-8ed6-24c4d083fa98.png) Reviewers:
null
2023-03-14 06:42:17+00:00
2023-03-19 03:03:33+00:00
knowledge_repo/app/templates/index-base.html
{% extends "base.html" %} {% macro format_authors(authors) %} {% for author in authors %} <a href='/feed?authors={{ author.identifier|urlencode }}'> {{ author.format_name }} </a> {% if not loop.last %} , {% endif %} {% endfor %} {% endmacro %} {% macro pagination(max_pages=20, extremes=True, autohide=True) %} {% if feed_params %} {% set start = feed_params['start'] | default(0) %} {% set results = feed_params['results'] | default(10) %} {% set page = 1 if (start == 0) else (start//results + 1) %} {% set page_count = feed_params['page_count'] | default(1) %} {% if autohide and page_count > 1 %} {% set page_nums = pagination_pages(current_page=page, page_count=page_count, max_pages=max_pages, extremes=extremes) %} <div class='pagination-bar' role="group"> <a href="{{ "#" if (page == 1) else modify_query(start=(page-2)*results) }}" class="pagination-stepper{% if page == 1 %} disabled{% endif %}"{% if page == 1 %} onclick="return false;"{% endif %}> <i class="glyphicon glyphicon-chevron-left"></i> </a> <ul class="pagination"> {% for page_num in page_nums %} {% if loop.index0 > 0 and page_num - page_nums[loop.index0 - 1] > 1 %} <li class="disabled"><a>&middot;&middot;&middot;</a></li> {% endif %} <li {% if page == page_num %}class="active"{% endif %}><a href= "{{ modify_query(start=(page_num-1)*results) }}"> {{ page_num }} </a></li> {% endfor %} </ul> <a href="{{ "#" if (page == page_count) else modify_query(start=page*results) }}" class="pagination-stepper{% if page == page_count %} disabled{% endif %}"{% if page == page_count %} onclick="return false;"{% endif %}> <i class="glyphicon glyphicon-chevron-right"></i> </a> </div> {% endif %} {% endif %} {% endmacro %} {% macro page_sizer() %} {% if feed_params %} {% set results = feed_params['results'] | default(10) %} <ul class="pagination"> <li {% if results == 5 %}class="active"{% endif %}> <a href="/feed?results=5" title="Show 5 items per page" aria-current="true">5</a> </li> <li {% if results == 10 %}class="active"{% endif %}> <a href="/feed?results=10" title="Show 10 items per page" aria-current="true">10</a> </li> <li {% if results == 20 %}class="active"{% endif %}> <a href="/feed?results=20" title="Show 20 items per page" aria-current="true">20</a> </li> <span class="text" style="display:inline-flex;text-align:justify;align-items:center;line-height:35px;"> &nbsp;per page</span> </ul> {% endif %} {% endmacro %} {% block content %} <!-- Index rendering mode switch --> <div class="row"> <div class="col-md-5"> <div class="btn-group btn-group-justified index-view-btn-group" role="group"> <a href="/feed" class="btn btn-default btn-card no-underline" role="button"> <i class="glyphicon glyphicon-post-org glyphicon-align-justify"></i> <span class="index-view-name"> Card </span> </a> <a href="/table" class="btn btn-default btn-table no-underline" role="button"> <i class="glyphicon glyphicon-post-org glyphicon-th"></i> <span class="index-view-name"> Table </span> </a> <a href="/cluster" class="btn btn-default btn-cluster no-underline" role="button"> <i class="glyphicon glyphicon-post-org glyphicon-th-list"></i> <span class="index-view-name"> Cluster </span> </a> </div> </div> <div class="pull-right visible-md-block visible-lg-block"> {{ page_sizer() }} </div> </div> <!-- Container for index items --> <div class="col-12"> {% block inner_content %} {% endblock %} </div> {# Show pagination at bottom of page unless showing clusters. #} {% if request.endpoint != 'index.render_cluster' %} {{ pagination(max_pages=10) }} {% endif %} {% endblock %}
{% extends "base.html" %} {% macro format_authors(authors) %} {% for author in authors %} <a href='/feed?authors={{ author.identifier|urlencode }}'> {{ author.format_name }} </a> {% if not loop.last %} , {% endif %} {% endfor %} {% endmacro %} {% macro pagination(max_pages=20, extremes=True, autohide=True) %} {% if feed_params %} {% set start = feed_params['start'] | default(0) %} {% set results = feed_params['results'] | default(10) %} {% set page = 1 if (start == 0) else (start//results + 1) %} {% set page_count = feed_params['page_count'] | default(1) %} {% if autohide and page_count > 1 %} {% set page_nums = pagination_pages(current_page=page, page_count=page_count, max_pages=max_pages, extremes=extremes) %} <div class='pagination-bar' role="group"> <a href="{{ " #" if (page==1) else modify_query(start=(page-2)*results) }}" class="pagination-stepper{% if page == 1 %} disabled{% endif %}" {% if page==1 %} onclick="return false;" {% endif %}> <i class="glyphicon glyphicon-chevron-left"></i> </a> <ul class="pagination"> {% for page_num in page_nums %} {% if loop.index0 > 0 and page_num - page_nums[loop.index0 - 1] > 1 %} <li class="disabled"><a>&middot;&middot;&middot;</a></li> {% endif %} <li {% if page==page_num %}class="active" {% endif %}><a href="{{ modify_query(start=(page_num-1)*results) }}"> {{ page_num }} </a></li> {% endfor %} </ul> <a href="{{ " #" if (page==page_count) else modify_query(start=page*results) }}" class="pagination-stepper{% if page == page_count %} disabled{% endif %}" {% if page==page_count %} onclick="return false;" {% endif %}> <i class="glyphicon glyphicon-chevron-right"></i> </a> </div> {% endif %} {% endif %} {% endmacro %} {% macro page_sizer() %} {% if feed_params %} {% set results = feed_params['results'] | default(10) %} <ul class="pagination"> <li {% if results==5 %}class="active" {% endif %}> <a href="{{ modify_query(results=5) }}" title="Show 5 items per page" aria-current="true">5</a> </li> <li {% if results==10 %}class="active" {% endif %}> <a href="{{ modify_query(results=10) }}" title="Show 10 items per page" aria-current="true">10</a> </li> <li {% if results==20 %}class="active" {% endif %}> <a href="{{ modify_query(results=20) }}" title="Show 20 items per page" aria-current="true">20</a> </li> <span class="text" style="display:inline-flex;text-align:justify;align-items:center;line-height:35px;"> &nbsp;per page</span> </ul> {% endif %} {% endmacro %} {% macro sort_filter() %} {% if feed_params and get_current_path() == 'feed' %} {% set tab = feed_params['tab'] | default('Newest') %} <!-- Index rendering mode switch --> <div class="row"> <div class="col-md-11"> <a href="/feed?tab=Newest" class="btn btn-default btn-card no-underline {% if tab == 'Newest' %} active {% endif %}" role="button"> <span class="index-view-name"> Newest </span> </a> <a href="/feed?tab=Frequent" class="btn btn-default btn-table no-underline {% if tab == 'Frequent' %} active {% endif %} " role="button"> <span class="index-view-name"> Frequent </span> </a> <a href="/feed?tab=Vote" class="btn btn-default btn-cluster no-underline {% if tab == 'Vote' %} active {% endif %}" role="button"> <span class="index-view-name"> Vote(s) </span> </a> <!-- <a class="btn btn-default btn-card no-underline" id="toggle-button" role="button" aria-expanded="false" aria-controls="uql-form"> <svg class="justify-content: center; display: flex" width="20" height="20" viewBox="0 -5 20 20"> <path d="M2 4h14v2H2V4Zm2 4h10v2H4V8Zm8 4H6v2h6v-2Z" fill="#06c6b6"></path> </svg> Filter </a> --> <!-- </div> --> </div> </div> {% endif %} {% endmacro %} {% block panel_left %} <div class="sidebar2 homepage-side-panel"> <h2>Menu</h2> <ul> <li> <a href="/feed" role="button" class="btn btn-default btn-card no-underline"> <i class="glyphicon glyphicon-pencil"></i> <span class="index-view-name"> Home </span> </a> </li> <li> <a href="/table" role="button" class="btn btn-default btn-table no-underline"> <i class="glyphicon glyphicon-th"></i> <span class="index-view-name"> Table </span> </a> </li> <li> <a href="/cluster" role="button" class="btn btn-default btn-cluster no-underline"> <i class="glyphicon glyphicon-th-list"></i> <span class="index-view-name"> Cluster </span> </a> </li> </ul> </div> {% endblock %} {% block content %} <div data-controller="se-uql" data-se-uql-id="" data-se-uql-sanitize-tag-query="false"> <div class="row"> <div class="col-md-6"> {{ sort_filter() }} </div> <div class="pull-right visible-md-block visible-lg-block"> {{ page_sizer() }} </div> </div> <!-- <div class="row"> <div class="col-md-6"> <form class="form-group form-base" id="uql-form"> <fieldset class="fieldset"> <legend class="legend font-style">Tagged with:</legend> <div class="radio-inline radio-cluster"> <input class="radio" type="radio" name="tagModeId" value="Watched"> <label class="feed-tldr font-style">My subscribed tags</label> </div> <div class="margin-left: 0px"> <div class="radio-inline radio-cluster"> <input class="radio" type="radio" name="tagModeId" value="Watched"> <label class="feed-tldr font-style">The following tags</label> <input type="text" autocomplete="off" tabindex="0" aria-autocomplete="list" aria-haspopup="listbox" role="combobox" aria-expanded="false" placeholder="e.g. python" style="width: 100px;"> </div> </div> </fieldset> <div> </div> <button class="btn btn-default btn-card no-underline" style="justify-content: flex-end;" type="button" onclick="applySelection()">Apply</button> <button class="btn btn-default btn-card no-underline" style="justify-content: flex-end;" type="reset" id="toggle-button">Cancel</button> </form> </div> </div> --> </div> <div class="col-12"> {% block inner_content %} {% endblock %} </div> {# Show pagination at bottom of page unless showing clusters. #} {% if request.endpoint != 'index.render_cluster' %} {{ pagination(max_pages=10) }} {% endif %} <script type="text/javascript"> // const form = document.querySelector("#uql-form"); // const toggleButton = document.querySelector("#toggle-button"); // // Initially hide the form // form.style.display = "none"; // // Attach an event listener to the toggle button // toggleButton.addEventListener("click", () => { // if (form.style.display === "none") { // form.style.display = "block"; // toggleButton.setAttribute("aria-expanded", true); // } else { // form.style.display = "none"; // toggleButton.setAttribute("aria-expanded", false); // } // }); </script> {% endblock %}
mengting1010
9a3fac2c2e8cdd295b53fe38a446d535e4f2bcb5
0d7aba9ddd3336f00f42000a5c2f17ad76ef570d
The above code and some code below will be uncommented in the next PR
mengting1010
0
airbnb/knowledge-repo
735
[kp] fix md data error and ipynb upload
Description of changeset: fix md data error and ipynb upload Test Plan: [x] local test Reviewers: @csharplus @mengting1010
null
2023-01-29 09:14:49+00:00
2023-02-02 07:41:26+00:00
knowledge_repo/app/routes/editor.py
from .. import permissions from ..index import update_index from ..models import Comment, PageView, Post, PostAuthorAssoc from ..proxies import current_repo, current_user, db_session, s3_client, notion_client from ..utils.emails import ( send_review_email, send_reviewer_request_email, ) from ..utils.image import ( is_allowed_image_format, is_pdf, pdf_page_to_png, ) from ..utils.shared import get_blueprint from datetime import datetime from flask import ( current_app, render_template, request, send_from_directory, url_for, ) from knowledge_repo.post import KnowledgePost from sqlalchemy import or_ from urllib.parse import unquote from werkzeug.utils import secure_filename import json import logging import os from knowledge_repo.utils.s3 import put_object_to_s3 import nbformat from nbconvert import HTMLExporter import io from knowledge_repo.constants import AWS_S3_BUCKET from knowledge_repo.utils.notion import create_page logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) blueprint = get_blueprint("editor", __name__) def get_warning_msg(msg): return json.dumps({"msg": msg, "success": False}) def get_error_msg(msg): return json.dumps({"error_msg": msg, "success": False}) # TODO: These functions have not been fully married # to the KnowledgePost API # Currently, backended by Post objects but partially # implemented on KnowledgePost API # TODO: Deprecate this route in favour of integrating editing # links into primary index pages and user pages @blueprint.route("/webposts", methods=["GET"]) @PageView.logged @permissions.post_edit.require() def gitless_drafts(): """Render the gitless posts that a user has created in table form Editors can see all the posts created via Gitless_Editing """ prefixes = current_app.config.get("WEB_EDITOR_PREFIXES", []) if prefixes == []: raise Exception("Web editing is not configured") query = db_session.query(Post) if prefixes is not None: query = query.filter(or_(*[Post.path.like(p + "%") for p in prefixes])) if current_user.identifier not in current_repo.config.editors: query = query.outerjoin( PostAuthorAssoc, PostAuthorAssoc.post_id == Post.id ).filter(PostAuthorAssoc.user_id == current_user.id) return render_template("web_posts.html", posts=query.all()) @blueprint.route("/edit") @blueprint.route("/edit/<path:path>", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def editor(path=None): """Render the web post editor, either with the default values or if the post already exists, with what has been saved""" prefixes = current_app.config.get("WEB_EDITOR_PREFIXES", None) if prefixes is not None: assert path is None or any( path.startswith(prefix) for prefix in prefixes ), "Editing this post online is not permitted by server configuration." # set defaults data = { "title": None, "status": current_repo.PostStatus.DRAFT.value, "markdown": request.args.get("markdown"), "thumbnail": "", "can_approve": 0, "username": current_user.identifier, "created_at": datetime.now(), "updated_at": datetime.now(), "authors": [current_user.identifier], "comments": [], "tldr": request.args.get("tldr"), } if path is not None and path in current_repo: kp = current_repo.post(path) data.update(kp.headers) data["status"] = kp.status.value data["path"] = path data["markdown"] = kp.read(images=False, headers=False) # retrieve reviews post = db_session.query(Post).filter(Post.path == path).first() if post: # post may have not been indexed yet data["comments"] = ( db_session.query(Comment) .filter(Comment.post_id == post.id) .filter(Comment.type == "review") .all() ) if ( current_user.identifier not in data["authors"] or current_user.identifier in current_repo.config.editors ): data["can_approve"] = 1 data["created_at"] = data["created_at"] data["updated_at"] = data["updated_at"] data["authors"] = json.dumps(data.get("authors")) data["tags"] = json.dumps(data.get("tags", [])) logger.info(data) if "proxy" in data or request.args.get("proxy", False): return render_template("post_editor_proxy.html", **data) if "ipynb" in data or request.args.get("ipynb", False): data["ipynb"] = True return render_template("post_editor_ipynb.html", **data) return render_template("post_editor_markdown.html", **data) @blueprint.route("/ajax/editor/save", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def save_post(): """Save the post""" data = request.get_json() path = data["path"] prefixes = current_app.config["WEB_EDITOR_PREFIXES"] if prefixes == []: raise Exception("Web editing is not configured") if prefixes is not None: if not any([path.startswith(prefix) for prefix in prefixes]): return get_warning_msg(f"Your post path must begin with one of {prefixes}") # TODO better handling of overwriting kp = None if path in current_repo: kp = current_repo.post(path) if ( current_user.identifier not in kp.headers["authors"] and current_user.identifier not in current_repo.config.editors ): return get_warning_msg( f"Post with path {path} already exists and you are not " "an author!\nPlease try a different path" ) # create the knowledge post kp = kp or KnowledgePost(path=path) headers = {} headers["created_at"] = datetime.strptime(data["created_at"], "%Y-%m-%d").date() headers["updated_at"] = datetime.strptime(data["updated_at"], "%Y-%m-%d").date() headers["title"] = data["title"] headers["path"] = data["path"] # TODO: thumbnail header not working currently, as feed image set # with kp method not based on header headers["thumbnail"] = data.get("feed_image", "") headers["authors"] = [auth.strip() for auth in data["author"]] headers["tldr"] = data["tldr"] headers["tags"] = [tag.strip() for tag in data.get("tags", [])] if "proxy" in data: headers["proxy"] = data["proxy"] if "ipynb" in data: headers["ipynb"] = data["ipynb"] if ( data.get("file_name", None) is not None and data.get("file_data", None) is not None ): # save file to local env with open(data["file_name"], "w") as text_file: text_file.write(data["file_data"]) # add to repo current_repo.save(data["file_name"], path) response = s3_upload(data["file_name"], data["file_data"]) if response is None: error_msg = "ERROR during upload file to s3" logger.error(error_msg) return get_error_msg(error_msg) else: headers["display_link"] = response else: headers["display_link"] = data["display_link"] kp.write(unquote(data["markdown"]), headers=headers) # add to repo current_repo.add(kp, update=True, message=headers["title"]) # THIS IS DANGEROUS # add into notion database if "ipynb" in data: create_page(notion_client=notion_client, database_id=current_app.config.get("NOTION_DATABASE_ID", ""), params=headers) update_index() return json.dumps({"path": path}) @blueprint.route("/ajax/editor/submit", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def submit_for_review(): """Submit post and if there are reviewers assigned, email them""" path = request.args.get("path", None) data = request.get_json() current_repo.submit(path) # email the reviewers reviewers = data.get("post_reviewers", None) if reviewers: for r in reviewers.split(","): send_reviewer_request_email(path=path, reviewer=r) update_index() return "OK" @blueprint.route("/ajax/editor/publish", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def publish_post(): """Publish the post by changing the status""" path = request.args.get("path", None) if path not in current_repo: return get_warning_msg(f"Unable to retrieve post with path = {path}!") current_repo.publish(path) update_index(check_timeouts=False) return "OK" @blueprint.route("/ajax/editor/unpublish", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def unpublish_post(): """Unpublish the post""" path = request.args.get("path", None) if path not in current_repo: return get_warning_msg(f"Unable to retrieve post with path = {path}!") current_repo.unpublish(path) update_index(check_timeouts=False) return "OK" @blueprint.route("/ajax/editor/accept", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def accept(): """Accept the post""" path = request.args.get("path", None) if path not in current_repo: return get_warning_msg(f"Unable to retrieve post with path = {path}!") current_repo.accept(path) update_index() return "OK" @blueprint.route("/ajax/editor/delete", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def delete_post(): """Delete a post""" path = request.args.get("path", None) if path not in current_repo: return get_warning_msg(f"Unable to retrieve post with path = {path}!") kp = current_repo.post(path) if current_user.identifier not in kp.headers["authors"]: return get_warning_msg("You can only delete a post where you are an author!") current_repo.remove(path) update_index(check_timeouts=False) return "OK" @blueprint.route("/ajax/editor/review", methods=["POST", "DELETE"]) @PageView.logged @permissions.post_edit.require() def review_comment(): """ Saves a review and sends an email that the post has been reviewed to the author of the post or deletes a submitted review """ if request.method == "POST": path = request.args.get("path", None) post_id = db_session.query(Post).filter(Post.path == path).first().id comment = Comment() comment.text = request.get_json()["text"] comment.user_id = current_user.id comment.post_id = post_id comment.type = "review" db_session.add(comment) db_session.commit() send_review_email( path=path, commenter=current_user.identifier, comment_text=comment.text ) elif request.method == "DELETE": comment = Comment.query.get(int(request.args.get("comment_id", ""))) if comment and current_user.id == comment.user_id: db_session.delete(comment) db_session.commit() return "OK" def s3_upload(file_name, file_data): """Upload file(s) to AWS s3 path and return the display link in the response""" if file_name is None or file_data is None or file_data is "": return get_warning_msg(f"File name is empty. Please re-upload!") response = put_object_to_s3(s3_client, file_data, AWS_S3_BUCKET, file_name) # create a html version of this file if ".ipynb" in file_name: with io.StringIO(file_data) as f: nb = nbformat.read(f, as_version=4) # export to html html_exporter = HTMLExporter() (html_data, resources) = html_exporter.from_notebook_node(nb) html_file_name = file_name.replace(".ipynb", ".html") response = put_object_to_s3( s3_client, html_data, AWS_S3_BUCKET, html_file_name, "text/html", ) if response: display_link = "https://s3.us-west-2.amazonaws.com/{0}/{1}".format( AWS_S3_BUCKET, html_file_name ) # todo: make s3 region name be configurable return display_link return None # DEPRECATED @blueprint.route("/file_upload", methods=["POST", "GET"]) @PageView.logged @permissions.post_edit.require() def file_upload(): """ Uploads images dropped on the web editor's markdown box to static/images and notifies editors by email """ upload_folder = "images" title = request.form["title"] files = request.files uploadedFiles = [] if files: for img_file in files.values(): filename = secure_filename(title + "_" + img_file.filename).lower() dst_folder = os.path.join(current_app.static_folder, upload_folder) if is_allowed_image_format(img_file): try: img_file.save(os.path.join(dst_folder, filename)) send_from_directory(dst_folder, filename) uploadedFiles += [ url_for( "static", filename=os.path.join(upload_folder, filename) ) ] except Exception as e: error_msg = f"ERROR during image upload: {e}" logger.error(error_msg) return get_error_msg(error_msg) elif is_pdf(filename): from PyPDF2 import PdfFileReader try: src_pdf = PdfFileReader(img_file) filename = os.path.splitext(filename)[0] num_pages = src_pdf.getNumPages() for page_num in range(num_pages): page_png = pdf_page_to_png(src_pdf, page_num) page_name = "{filename}_{page_num}.jpg".format(**locals()) page_png.save(filename=os.path.join(dst_folder, page_name)) uploadedFiles += [ url_for( "static", filename=os.path.join(upload_folder, page_name), ) ] except Exception as e: error_msg = f"ERROR during pdf upload: {e}" logger.error(error_msg) return get_error_msg(error_msg) return json.dumps({"links": uploadedFiles, "success": True})
from .. import permissions from ..index import update_index from ..models import Comment, PageView, Post, PostAuthorAssoc from ..proxies import current_repo, current_user, db_session, s3_client, notion_client from ..utils.emails import ( send_review_email, send_reviewer_request_email, ) from ..utils.image import ( is_allowed_image_format, is_pdf, pdf_page_to_png, ) from ..utils.shared import get_blueprint from datetime import datetime from flask import ( current_app, render_template, request, send_from_directory, url_for, ) from knowledge_repo.post import KnowledgePost from sqlalchemy import or_ from urllib.parse import unquote from werkzeug.utils import secure_filename import json import logging import os from knowledge_repo.utils.s3 import put_object_to_s3 import nbformat from nbconvert import HTMLExporter import io from knowledge_repo.constants import AWS_S3_BUCKET from knowledge_repo.utils.notion import create_page logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) blueprint = get_blueprint("editor", __name__) def get_warning_msg(msg): return json.dumps({"msg": msg, "success": False}) def get_error_msg(msg): return json.dumps({"error_msg": msg, "success": False}) # TODO: These functions have not been fully married # to the KnowledgePost API # Currently, backended by Post objects but partially # implemented on KnowledgePost API # TODO: Deprecate this route in favour of integrating editing # links into primary index pages and user pages @blueprint.route("/webposts", methods=["GET"]) @PageView.logged @permissions.post_edit.require() def gitless_drafts(): """Render the gitless posts that a user has created in table form Editors can see all the posts created via Gitless_Editing """ prefixes = current_app.config.get("WEB_EDITOR_PREFIXES", []) if prefixes == []: raise Exception("Web editing is not configured") query = db_session.query(Post) if prefixes is not None: query = query.filter(or_(*[Post.path.like(p + "%") for p in prefixes])) if current_user.identifier not in current_repo.config.editors: query = query.outerjoin( PostAuthorAssoc, PostAuthorAssoc.post_id == Post.id ).filter(PostAuthorAssoc.user_id == current_user.id) return render_template("web_posts.html", posts=query.all()) @blueprint.route("/edit") @blueprint.route("/edit/<path:path>", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def editor(path=None): """Render the web post editor, either with the default values or if the post already exists, with what has been saved""" prefixes = current_app.config.get("WEB_EDITOR_PREFIXES", None) if prefixes is not None: assert path is None or any( path.startswith(prefix) for prefix in prefixes ), "Editing this post online is not permitted by server configuration." # set defaults data = { "title": None, "status": current_repo.PostStatus.DRAFT.value, "markdown": request.args.get("markdown"), "thumbnail": "", "can_approve": 0, "username": current_user.identifier, "created_at": datetime.now(), "updated_at": datetime.now(), "authors": [current_user.identifier], "comments": [], "tldr": request.args.get("tldr"), } if path is not None and path in current_repo: kp = current_repo.post(path) data.update(kp.headers) data["status"] = kp.status.value data["path"] = path data["markdown"] = kp.read(images=False, headers=False) # retrieve reviews post = db_session.query(Post).filter(Post.path == path).first() if post: # post may have not been indexed yet data["comments"] = ( db_session.query(Comment) .filter(Comment.post_id == post.id) .filter(Comment.type == "review") .all() ) if ( current_user.identifier not in data["authors"] or current_user.identifier in current_repo.config.editors ): data["can_approve"] = 1 data["created_at"] = data["created_at"] data["updated_at"] = data["updated_at"] data["authors"] = json.dumps(data.get("authors")) data["tags"] = json.dumps(data.get("tags", [])) logger.info(data) if "proxy" in data or request.args.get("proxy", False): return render_template("post_editor_proxy.html", **data) if "ipynb" in data or request.args.get("ipynb", False): data["ipynb"] = True return render_template("post_editor_ipynb.html", **data) return render_template("post_editor_markdown.html", **data) @blueprint.route("/ajax/editor/save", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def save_post(): """Save the post""" data = request.get_json() path = data["path"] prefixes = current_app.config["WEB_EDITOR_PREFIXES"] if prefixes == []: raise Exception("Web editing is not configured") if prefixes is not None: if not any([path.startswith(prefix) for prefix in prefixes]): return get_warning_msg(f"Your post path must begin with one of {prefixes}") # TODO better handling of overwriting kp = None if path in current_repo: kp = current_repo.post(path) if ( current_user.identifier not in kp.headers["authors"] and current_user.identifier not in current_repo.config.editors ): return get_warning_msg( f"Post with path {path} already exists and you are not " "an author!\nPlease try a different path" ) # create the knowledge post kp = kp or KnowledgePost(path=path) headers = {} headers["created_at"] = datetime.strptime(data["created_at"], "%Y-%m-%d").date() headers["updated_at"] = datetime.strptime(data["updated_at"], "%Y-%m-%d").date() headers["title"] = data["title"] headers["path"] = data["path"] # TODO: thumbnail header not working currently, as feed image set # with kp method not based on header headers["thumbnail"] = data.get("feed_image", "") headers["authors"] = [auth.strip() for auth in data["author"]] headers["tldr"] = data["tldr"] headers["tags"] = [tag.strip() for tag in data.get("tags", [])] if "proxy" in data: headers["proxy"] = data["proxy"] if "ipynb" in data: headers["ipynb"] = data["ipynb"] if ( data.get("file_name", None) is not None and data.get("file_data", None) is not None ): # save file to local env with open(data["file_name"], "w") as text_file: text_file.write(data["file_data"]) # add to repo kp = current_repo.save(data["file_name"], path) # upload to s3 response = s3_upload(data["file_name"], path, data["file_data"]) if response is None: error_msg = "ERROR during upload file to s3" logger.error(error_msg) return get_error_msg(error_msg) else: headers["display_link"] = response else: headers["display_link"] = data["display_link"] # generate dummp md for post redirect if "ipynb" not in data: kp.write(unquote(data["markdown"]), headers=headers) # add to repo current_repo.add(kp, update=True, message=headers["title"]) # THIS IS DANGEROUS # add into notion database if "ipynb" in data: create_page(notion_client=notion_client, database_id=current_app.config.get("NOTION_DATABASE_ID", ""), params=headers) update_index() return json.dumps({"path": path}) @blueprint.route("/ajax/editor/submit", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def submit_for_review(): """Submit post and if there are reviewers assigned, email them""" path = request.args.get("path", None) data = request.get_json() current_repo.submit(path) # email the reviewers reviewers = data.get("post_reviewers", None) if reviewers: for r in reviewers.split(","): send_reviewer_request_email(path=path, reviewer=r) update_index() return "OK" @blueprint.route("/ajax/editor/publish", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def publish_post(): """Publish the post by changing the status""" path = request.args.get("path", None) if path not in current_repo: return get_warning_msg(f"Unable to retrieve post with path = {path}!") current_repo.publish(path) update_index(check_timeouts=False) return "OK" @blueprint.route("/ajax/editor/unpublish", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def unpublish_post(): """Unpublish the post""" path = request.args.get("path", None) if path not in current_repo: return get_warning_msg(f"Unable to retrieve post with path = {path}!") current_repo.unpublish(path) update_index(check_timeouts=False) return "OK" @blueprint.route("/ajax/editor/accept", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def accept(): """Accept the post""" path = request.args.get("path", None) if path not in current_repo: return get_warning_msg(f"Unable to retrieve post with path = {path}!") current_repo.accept(path) update_index() return "OK" @blueprint.route("/ajax/editor/delete", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def delete_post(): """Delete a post""" path = request.args.get("path", None) if path not in current_repo: return get_warning_msg(f"Unable to retrieve post with path = {path}!") kp = current_repo.post(path) if current_user.identifier not in kp.headers["authors"]: return get_warning_msg("You can only delete a post where you are an author!") current_repo.remove(path) update_index(check_timeouts=False) return "OK" @blueprint.route("/ajax/editor/review", methods=["POST", "DELETE"]) @PageView.logged @permissions.post_edit.require() def review_comment(): """ Saves a review and sends an email that the post has been reviewed to the author of the post or deletes a submitted review """ if request.method == "POST": path = request.args.get("path", None) post_id = db_session.query(Post).filter(Post.path == path).first().id comment = Comment() comment.text = request.get_json()["text"] comment.user_id = current_user.id comment.post_id = post_id comment.type = "review" db_session.add(comment) db_session.commit() send_review_email( path=path, commenter=current_user.identifier, comment_text=comment.text ) elif request.method == "DELETE": comment = Comment.query.get(int(request.args.get("comment_id", ""))) if comment and current_user.id == comment.user_id: db_session.delete(comment) db_session.commit() return "OK" def s3_upload(file_name, path, file_data): """Upload file(s) to AWS s3 path and return the display link in the response""" if file_name is None or file_data is None or file_data == "": return get_warning_msg(f"File name is empty. Please re-upload!") response = put_object_to_s3(s3_client, file_data, AWS_S3_BUCKET, file_name) # create a html version of this file if ".ipynb" in file_name: with io.StringIO(file_data) as f: nb = nbformat.read(f, as_version=4) # export to html html_exporter = HTMLExporter() (html_data, resources) = html_exporter.from_notebook_node(nb) html_file_name = file_name.replace(".ipynb", ".html") response = put_object_to_s3( s3_client, html_data, AWS_S3_BUCKET, os.path.join(path + '.kp/' + html_file_name), "text/html", ) if response: display_link = "https://s3.us-west-2.amazonaws.com/{0}/{1}".format( AWS_S3_BUCKET, html_file_name ) # todo: make s3 region name be configurable return display_link return None # DEPRECATED @blueprint.route("/file_upload", methods=["POST", "GET"]) @PageView.logged @permissions.post_edit.require() def file_upload(): """ Uploads images dropped on the web editor's markdown box to static/images and notifies editors by email """ upload_folder = "images" title = request.form["title"] files = request.files uploadedFiles = [] if files: for img_file in files.values(): filename = secure_filename(title + "_" + img_file.filename).lower() dst_folder = os.path.join(current_app.static_folder, upload_folder) if is_allowed_image_format(img_file): try: img_file.save(os.path.join(dst_folder, filename)) send_from_directory(dst_folder, filename) uploadedFiles += [ url_for( "static", filename=os.path.join(upload_folder, filename) ) ] except Exception as e: error_msg = f"ERROR during image upload: {e}" logger.error(error_msg) return get_error_msg(error_msg) elif is_pdf(filename): from PyPDF2 import PdfFileReader try: src_pdf = PdfFileReader(img_file) filename = os.path.splitext(filename)[0] num_pages = src_pdf.getNumPages() for page_num in range(num_pages): page_png = pdf_page_to_png(src_pdf, page_num) page_name = "{filename}_{page_num}.jpg".format(**locals()) page_png.save(filename=os.path.join(dst_folder, page_name)) uploadedFiles += [ url_for( "static", filename=os.path.join(upload_folder, page_name), ) ] except Exception as e: error_msg = f"ERROR during pdf upload: {e}" logger.error(error_msg) return get_error_msg(error_msg) return json.dumps({"links": uploadedFiles, "success": True})
JJJ000
43635f53c93ceb0f1568c9fdda796e88a80bb2d0
1070404b926ac5f1e1c27a246d4fac95d3cd3518
Use `os.path.join()` as explained in: https://www.geeksforgeeks.org/python-os-path-join-method/ ?
csharplus
1
airbnb/knowledge-repo
725
update notion db id
Description of changeset: as title Test Plan: local dev Reviewers: @csharplus @JJJ000
null
2023-01-20 01:59:00+00:00
2023-01-21 19:36:20+00:00
knowledge_repo/utils/notion.py
from notion_client import Client, AsyncClient import logging from notion_client import APIResponseError from knowledge_repo.constants import KP_EDIT_PROD_LINK logger = logging.getLogger(__name__) def get_notion_client(auth): """Get a notion synchronous client for notion synchronous operations :param auth: Bearer token for authentication :return: a notion client for notion sync operations """ return Client(auth=auth) def get_notion_async_client(auth): """Get a notion asynchronous client for notion asynchronous operations :param auth: Bearer token for authentication :return: a notion async client for notion async operations """ return AsyncClient(auth=auth) def query_page(notion_client, page_id): """Retrieve a Page object using the page ID specified :param notion_client: a notion client :param pag_id: Identifier for a Notion page :return: page object if found, else False """ try: logger.info(notion_client.pages.retrieve(page_id)) except APIResponseError as error: logging.error(error) return False def create_page(notion_client, params): """Create a new page in the specified database :param notion_client: a notion client :param params: property values of this page. :return: True if page was created, else False """ name = params.get("title", None) description = params.get("tldr", "") tags = [{"name": t} for t in params.get("tags", [])] path = params.get("path", "") if len(path) > 0: post_link = "/".join([KP_EDIT_PROD_LINK, path]) logger.info(post_link) else: post_link = "" file_link = params.get("display_link", "") if name is None: logger.error("Page Name is Empty") return False try: notion_client.pages.create( parent={ "type": "database_id", "database_id": "85db4c309b6d4d00a6a1df19d56496b6", }, properties={ "Name": {"title": [{"text": {"content": name}}]}, "Description": {"rich_text": [{"text": {"content": description}}]}, "Tags": {"multi_select": tags}, "Knowledge Repo Link": { "rich_text": [ {"text": {"content": post_link, "link": {"url": post_link}}}, ] }, "Original File Link": {"rich_text": [{"text": {"content": file_link}}]}, }, ) except APIResponseError as error: logging.error(error) return False return True
from notion_client import Client, AsyncClient import logging from notion_client import APIResponseError from knowledge_repo.constants import KP_EDIT_PROD_LINK logger = logging.getLogger(__name__) def get_notion_client(auth): """Get a notion synchronous client for notion synchronous operations :param auth: Bearer token for authentication :return: a notion client for notion sync operations """ return Client(auth=auth) def get_notion_async_client(auth): """Get a notion asynchronous client for notion asynchronous operations :param auth: Bearer token for authentication :return: a notion async client for notion async operations """ return AsyncClient(auth=auth) def query_page(notion_client, page_id): """Retrieve a Page object using the page ID specified :param notion_client: a notion client :param pag_id: Identifier for a Notion page :return: page object if found, else False """ try: logger.info(notion_client.pages.retrieve(page_id)) except APIResponseError as error: logging.error(error) return False def create_page(notion_client, database_id, params): """Create a new page in the specified database :param notion_client: a notion client :param params: property values of this page. :return: True if page was created, else False """ name = params.get("title", None) description = params.get("tldr", "") tags = [{"name": t} for t in params.get("tags", [])] path = params.get("path", "") if len(path) > 0: post_link = "/".join([KP_EDIT_PROD_LINK, path]) logger.info(post_link) else: post_link = "" file_link = params.get("display_link", "") if name is None: logger.error("Page Name is Empty") return False try: notion_client.pages.create( parent={ "type": "database_id", "database_id": database_id, }, properties={ "Name": {"title": [{"text": {"content": name}}]}, "Description": {"rich_text": [{"text": {"content": description}}]}, "Tags": {"multi_select": tags}, "Knowledge Repo Link": { "rich_text": [ {"text": {"content": post_link, "link": {"url": post_link}}}, ] }, "Original File Link": {"rich_text": [{"text": {"content": file_link}}]}, }, ) except APIResponseError as error: logging.error(error) return False return True
mengting1010
582fc923aff96253440a15de493f950f8195cca6
58155530b5fe639d54f6797cd6e8b55ac4a753fe
we probably need to add it to config
JJJ000
2
airbnb/knowledge-repo
725
update notion db id
Description of changeset: as title Test Plan: local dev Reviewers: @csharplus @JJJ000
null
2023-01-20 01:59:00+00:00
2023-01-21 19:36:20+00:00
knowledge_repo/utils/notion.py
from notion_client import Client, AsyncClient import logging from notion_client import APIResponseError from knowledge_repo.constants import KP_EDIT_PROD_LINK logger = logging.getLogger(__name__) def get_notion_client(auth): """Get a notion synchronous client for notion synchronous operations :param auth: Bearer token for authentication :return: a notion client for notion sync operations """ return Client(auth=auth) def get_notion_async_client(auth): """Get a notion asynchronous client for notion asynchronous operations :param auth: Bearer token for authentication :return: a notion async client for notion async operations """ return AsyncClient(auth=auth) def query_page(notion_client, page_id): """Retrieve a Page object using the page ID specified :param notion_client: a notion client :param pag_id: Identifier for a Notion page :return: page object if found, else False """ try: logger.info(notion_client.pages.retrieve(page_id)) except APIResponseError as error: logging.error(error) return False def create_page(notion_client, params): """Create a new page in the specified database :param notion_client: a notion client :param params: property values of this page. :return: True if page was created, else False """ name = params.get("title", None) description = params.get("tldr", "") tags = [{"name": t} for t in params.get("tags", [])] path = params.get("path", "") if len(path) > 0: post_link = "/".join([KP_EDIT_PROD_LINK, path]) logger.info(post_link) else: post_link = "" file_link = params.get("display_link", "") if name is None: logger.error("Page Name is Empty") return False try: notion_client.pages.create( parent={ "type": "database_id", "database_id": "85db4c309b6d4d00a6a1df19d56496b6", }, properties={ "Name": {"title": [{"text": {"content": name}}]}, "Description": {"rich_text": [{"text": {"content": description}}]}, "Tags": {"multi_select": tags}, "Knowledge Repo Link": { "rich_text": [ {"text": {"content": post_link, "link": {"url": post_link}}}, ] }, "Original File Link": {"rich_text": [{"text": {"content": file_link}}]}, }, ) except APIResponseError as error: logging.error(error) return False return True
from notion_client import Client, AsyncClient import logging from notion_client import APIResponseError from knowledge_repo.constants import KP_EDIT_PROD_LINK logger = logging.getLogger(__name__) def get_notion_client(auth): """Get a notion synchronous client for notion synchronous operations :param auth: Bearer token for authentication :return: a notion client for notion sync operations """ return Client(auth=auth) def get_notion_async_client(auth): """Get a notion asynchronous client for notion asynchronous operations :param auth: Bearer token for authentication :return: a notion async client for notion async operations """ return AsyncClient(auth=auth) def query_page(notion_client, page_id): """Retrieve a Page object using the page ID specified :param notion_client: a notion client :param pag_id: Identifier for a Notion page :return: page object if found, else False """ try: logger.info(notion_client.pages.retrieve(page_id)) except APIResponseError as error: logging.error(error) return False def create_page(notion_client, database_id, params): """Create a new page in the specified database :param notion_client: a notion client :param params: property values of this page. :return: True if page was created, else False """ name = params.get("title", None) description = params.get("tldr", "") tags = [{"name": t} for t in params.get("tags", [])] path = params.get("path", "") if len(path) > 0: post_link = "/".join([KP_EDIT_PROD_LINK, path]) logger.info(post_link) else: post_link = "" file_link = params.get("display_link", "") if name is None: logger.error("Page Name is Empty") return False try: notion_client.pages.create( parent={ "type": "database_id", "database_id": database_id, }, properties={ "Name": {"title": [{"text": {"content": name}}]}, "Description": {"rich_text": [{"text": {"content": description}}]}, "Tags": {"multi_select": tags}, "Knowledge Repo Link": { "rich_text": [ {"text": {"content": post_link, "link": {"url": post_link}}}, ] }, "Original File Link": {"rich_text": [{"text": {"content": file_link}}]}, }, ) except APIResponseError as error: logging.error(error) return False return True
mengting1010
582fc923aff96253440a15de493f950f8195cca6
58155530b5fe639d54f6797cd6e8b55ac4a753fe
Updated, PTAL again. Thanks!
mengting1010
3
airbnb/knowledge-repo
706
[kp] update s3 repo
Description of changeset: update s3 repo Test Plan: [x] CI Reviewers: @csharplus @mengting1010
null
2023-01-02 01:35:26+00:00
2023-01-06 03:37:10+00:00
requirements.txt
boto3==1.26.37 botocore==1.29.37 cooked_input flask==2.1.2 Flask-Migrate gitdb gitpython==3.1.30 tabulate==0.8.9 pyyaml markdown==3.3.4 pygments==2.10.0 pyyaml flask_login==0.6.1 flask_principal flask_mail gunicorn inflection pillow psycopg2 nbformat nbconvert[execute] traitlets ldap3 requests requests_oauthlib weasyprint jinja2>=2.7,<=3.0.3 werkzeug>=1.0,<=2.0.3 multiprocess importlib-metadata==4.13.0 sqlalchemy==1.4.37 weasyprint==54.3
boto3==1.26.37 botocore==1.29.37 cooked_input flask==2.1.2 Flask-Migrate gitdb gitpython==3.1.30 tabulate==0.8.9 pyyaml markdown==3.3.4 pygments==2.10.0 pyyaml flask_login==0.6.1 flask_principal flask_mail gunicorn inflection pillow psycopg2 nbformat nbconvert[execute] traitlets ldap3 requests requests_oauthlib weasyprint jinja2>=2.7,<=3.0.3 werkzeug>=1.0,<=2.0.3 multiprocess importlib-metadata==4.13.0 sqlalchemy==1.4.37 weasyprint==54.3 s3path==0.3.4
JJJ000
c255ede148aef3f804a293972a21b9d7b2419326
00d51151f35a0dccf7dae17812331fdc0065f1ca
Please lock the version of the new library to avoid unexpected breaks when the library updates in the future.
csharplus
4
airbnb/knowledge-repo
704
Add Notion Integration
Description of changeset: as title. Test Plan: local dev Reviewers:
null
2022-12-31 20:38:03+00:00
2023-01-11 04:00:33+00:00
requirements.txt
boto3==1.26.37 botocore==1.29.37 cooked_input flask==2.1.2 Flask-Migrate gitdb gitpython==3.1.30 tabulate==0.8.9 pyyaml markdown==3.3.4 pygments==2.10.0 pyyaml flask_login==0.6.1 flask_principal flask_mail gunicorn inflection pillow psycopg2 nbformat nbconvert[execute] traitlets ldap3 requests requests_oauthlib weasyprint jinja2>=2.7,<=3.0.3 werkzeug>=1.0,<=2.0.3 multiprocess importlib-metadata==4.13.0 sqlalchemy==1.4.37 weasyprint==54.3 s3path==0.3.4
boto3==1.26.37 botocore==1.29.37 cooked_input flask==2.1.2 Flask-Migrate gitdb gitpython==3.1.30 tabulate==0.8.9 pyyaml markdown==3.3.4 pygments==2.10.0 pyyaml flask_login==0.6.1 flask_principal flask_mail gunicorn inflection pillow psycopg2 nbformat nbconvert[execute] traitlets ldap3 requests requests_oauthlib weasyprint jinja2>=2.7,<=3.0.3 werkzeug>=1.0,<=2.0.3 multiprocess importlib-metadata==4.13.0 sqlalchemy==1.4.37 weasyprint==54.3 s3path==0.3.4 notion-client==2.0.0
mengting1010
a875df6b4cc47024d8b3133776c7c6e8213f9daa
1ad529a84dcf923fdde97a3b7e804936f1d14007
Please add the current version number of `notion-client` as well to avoid future break changes from this library.
csharplus
5
airbnb/knowledge-repo
698
Update Jupyter Notebook Upload Related
Description of changeset: - Integrate with S3 client - upload Jupyter Notebook to s3 when saving the post - export a html version of Jupyter Notebook and upload to s3 Test Plan: local dev Reviewers: @csharplus @JJJ000
null
2022-12-28 23:02:53+00:00
2022-12-29 07:10:50+00:00
knowledge_repo/app/routes/editor.py
from .. import permissions from ..index import update_index from ..models import Comment, PageView, Post, PostAuthorAssoc from ..proxies import current_repo, current_user, db_session from ..utils.emails import ( send_review_email, send_reviewer_request_email, ) from ..utils.image import ( is_allowed_image_format, is_pdf, pdf_page_to_png, ) from ..utils.shared import get_blueprint from datetime import datetime from flask import ( current_app, render_template, request, send_from_directory, url_for, ) from knowledge_repo.post import KnowledgePost from sqlalchemy import or_ from urllib.parse import unquote from werkzeug.utils import secure_filename import json import logging import os logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) blueprint = get_blueprint("editor", __name__) def get_warning_msg(msg): return json.dumps({'msg': msg, 'success': False}) def get_error_msg(msg): return json.dumps({'error_msg': msg, 'success': False}) # TODO: These functions have not been fully married # to the KnowledgePost API # Currently, backended by Post objects but partially # implemented on KnowledgePost API # TODO: Deprecate this route in favour of integrating editing # links into primary index pages and user pages @blueprint.route('/webposts', methods=['GET']) @PageView.logged @permissions.post_edit.require() def gitless_drafts(): """ Render the gitless posts that a user has created in table form Editors can see all the posts created via Gitless_Editing """ prefixes = current_app.config.get('WEB_EDITOR_PREFIXES', []) if prefixes == []: raise Exception('Web editing is not configured') query = (db_session.query(Post)) if prefixes is not None: query = query.filter(or_(*[Post.path.like(p + '%') for p in prefixes])) if current_user.identifier not in current_repo.config.editors: query = (query.outerjoin( PostAuthorAssoc, PostAuthorAssoc.post_id == Post.id) .filter(PostAuthorAssoc.user_id == current_user.id)) return render_template('web_posts.html', posts=query.all()) @blueprint.route('/edit') @blueprint.route('/edit/<path:path>', methods=['GET', 'POST']) @PageView.logged @permissions.post_edit.require() def editor(path=None): """ Render the web post editor, either with the default values or if the post already exists, with what has been saved """ prefixes = current_app.config.get('WEB_EDITOR_PREFIXES', None) if prefixes is not None: assert ( path is None or any(path.startswith(prefix) for prefix in prefixes) ), 'Editing this post online is not permitted by server configuration.' # set defaults data = {'title': None, 'status': current_repo.PostStatus.DRAFT.value, 'markdown': request.args.get('markdown'), 'thumbnail': '', 'can_approve': 0, 'username': current_user.identifier, 'created_at': datetime.now(), 'updated_at': datetime.now(), 'authors': [current_user.identifier], 'comments': [], 'tldr': request.args.get('tldr'), } if path is not None and path in current_repo: kp = current_repo.post(path) data.update(kp.headers) data['status'] = kp.status.value data['path'] = path data['markdown'] = kp.read(images=False, headers=False) # retrieve reviews post = db_session.query(Post).filter(Post.path == path).first() if post: # post may have not been indexed yet data['comments'] = (db_session.query(Comment) .filter(Comment.post_id == post.id) .filter(Comment.type == 'review') .all()) if current_user.identifier not in data['authors'] \ or current_user.identifier in current_repo.config.editors: data['can_approve'] = 1 data['created_at'] = data['created_at'] data['updated_at'] = data['updated_at'] data['authors'] = json.dumps(data.get('authors')) data['tags'] = json.dumps(data.get('tags', [])) if "proxy" in data or request.args.get("proxy", False): return render_template("post_editor_proxy.html", **data) if "ipynb" in data or request.args.get("ipynb", False): return render_template("post_editor_ipynb.html", **data) return render_template("post_editor_markdown.html", **data) @blueprint.route('/ajax/editor/save', methods=['GET', 'POST']) @PageView.logged @permissions.post_edit.require() def save_post(): """ Save the post """ data = request.get_json() path = data['path'] prefixes = current_app.config['WEB_EDITOR_PREFIXES'] if prefixes == []: raise Exception('Web editing is not configured') if prefixes is not None: if not any([path.startswith(prefix) for prefix in prefixes]): return get_warning_msg( f'Your post path must begin with one of {prefixes}') # TODO better handling of overwriting kp = None if path in current_repo: kp = current_repo.post(path) if current_user.identifier not in kp.headers['authors'] \ and current_user.identifier not in current_repo.config.editors: return get_warning_msg( f'Post with path {path} already exists and you are not ' 'an author!\nPlease try a different path') # create the knowledge post kp = kp or KnowledgePost(path=path) headers = {} headers['created_at'] = datetime.strptime( data['created_at'], '%Y-%m-%d').date() headers['updated_at'] = datetime.strptime( data['updated_at'], '%Y-%m-%d').date() headers['title'] = data['title'] headers['path'] = data['path'] # TODO: thumbnail header not working currently, as feed image set # with kp method not based on header headers['thumbnail'] = data.get('feed_image', '') headers['authors'] = [auth.strip() for auth in data['author']] headers['tldr'] = data['tldr'] headers['tags'] = [tag.strip() for tag in data.get('tags', [])] if 'proxy' in data: headers['proxy'] = data['proxy'] if "ipynb" in data: headers["ipynb"] = data["ipynb"] kp.write(unquote(data['markdown']), headers=headers) # add to repo current_repo.add( kp, update=True, message=headers['title']) # THIS IS DANGEROUS update_index() return json.dumps({'path': path}) @blueprint.route('/ajax/editor/submit', methods=['GET', 'POST']) @PageView.logged @permissions.post_edit.require() def submit_for_review(): """ Submit post and if there are reviewers assigned, email them""" path = request.args.get('path', None) data = request.get_json() current_repo.submit(path) # email the reviewers reviewers = data.get('post_reviewers', None) if reviewers: for r in reviewers.split(','): send_reviewer_request_email(path=path, reviewer=r) update_index() return 'OK' @blueprint.route('/ajax/editor/publish', methods=['GET', 'POST']) @PageView.logged @permissions.post_edit.require() def publish_post(): """ Publish the post by changing the status """ path = request.args.get('path', None) if path not in current_repo: return get_warning_msg(f'Unable to retrieve post with path = {path}!') current_repo.publish(path) update_index(check_timeouts=False) return 'OK' @blueprint.route('/ajax/editor/unpublish', methods=['GET', 'POST']) @PageView.logged @permissions.post_edit.require() def unpublish_post(): """ Unpublish the post """ path = request.args.get('path', None) if path not in current_repo: return get_warning_msg(f'Unable to retrieve post with path = {path}!') current_repo.unpublish(path) update_index(check_timeouts=False) return 'OK' @blueprint.route('/ajax/editor/accept', methods=['GET', 'POST']) @PageView.logged @permissions.post_edit.require() def accept(): """ Accept the post """ path = request.args.get('path', None) if path not in current_repo: return get_warning_msg(f'Unable to retrieve post with path = {path}!') current_repo.accept(path) update_index() return 'OK' @blueprint.route('/ajax/editor/delete', methods=['GET', 'POST']) @PageView.logged @permissions.post_edit.require() def delete_post(): """ Delete a post """ path = request.args.get('path', None) if path not in current_repo: return get_warning_msg(f'Unable to retrieve post with path = {path}!') kp = current_repo.post(path) if current_user.identifier not in kp.headers['authors']: return get_warning_msg( 'You can only delete a post where you are an author!') current_repo.remove(path) update_index(check_timeouts=False) return 'OK' @blueprint.route('/ajax/editor/review', methods=['POST', 'DELETE']) @PageView.logged @permissions.post_edit.require() def review_comment(): """ Saves a review and sends an email that the post has been reviewed to the author of the post or deletes a submitted review """ if request.method == 'POST': path = request.args.get('path', None) post_id = db_session.query(Post).filter(Post.path == path).first().id comment = Comment() comment.text = request.get_json()['text'] comment.user_id = current_user.id comment.post_id = post_id comment.type = 'review' db_session.add(comment) db_session.commit() send_review_email(path=path, commenter=current_user.identifier, comment_text=comment.text) elif request.method == 'DELETE': comment = Comment.query.get(int(request.args.get('comment_id', ''))) if comment and current_user.id == comment.user_id: db_session.delete(comment) db_session.commit() return 'OK' @blueprint.route("/ajax/editor/s3_upload", methods=["POST", "GET"]) @PageView.logged @permissions.post_edit.require() def s3_upload(): """Upload file(s) to AWS s3 path and return the display link in the response""" if request.method == "POST": data = request.get_json() file_name = data.get("file_name", None) object_name = os.path.basename(file_name.replace("\\", "/")) logger.info("file_name: {0} & object_name: {1}".format(file_name, object_name)) if file_name is None: return get_warning_msg(f"File name is empty. Please re-upload!") bucket = data.get("bucket", "www.knowledge-repo.com") response = True # todo: replace it with real s3 upload if response: display_link = "https://s3.us-west-2.amazonaws.com/{0}/{1}".format( bucket, object_name ) # todo: make s3 region name be configurable return json.dumps({"display_link": display_link, "success": True}) error_msg = "ERROR during upload file to s3" logger.error(error_msg) return get_error_msg(error_msg) return "OK" # DEPRECATED @blueprint.route('/file_upload', methods=['POST', 'GET']) @PageView.logged @permissions.post_edit.require() def file_upload(): """ Uploads images dropped on the web editor's markdown box to static/images and notifies editors by email """ upload_folder = 'images' title = request.form['title'] files = request.files uploadedFiles = [] if files: for img_file in files.values(): filename = secure_filename(title + '_' + img_file.filename).lower() dst_folder = os.path.join(current_app.static_folder, upload_folder) if is_allowed_image_format(img_file): try: img_file.save(os.path.join(dst_folder, filename)) send_from_directory(dst_folder, filename) uploadedFiles += [url_for('static', filename=os.path.join( upload_folder, filename))] except Exception as e: error_msg = f'ERROR during image upload: {e}' logger.error(error_msg) return get_error_msg(error_msg) elif is_pdf(filename): from PyPDF2 import PdfFileReader try: src_pdf = PdfFileReader(img_file) filename = os.path.splitext(filename)[0] num_pages = src_pdf.getNumPages() for page_num in range(num_pages): page_png = pdf_page_to_png(src_pdf, page_num) page_name = '{filename}_{page_num}.jpg'.format( **locals()) page_png.save(filename=os.path.join( dst_folder, page_name)) uploadedFiles += [url_for( 'static', filename=os.path.join( upload_folder, page_name))] except Exception as e: error_msg = f'ERROR during pdf upload: {e}' logger.error(error_msg) return get_error_msg(error_msg) return json.dumps({'links': uploadedFiles, 'success': True})
from .. import permissions from ..index import update_index from ..models import Comment, PageView, Post, PostAuthorAssoc from ..proxies import current_repo, current_user, db_session from ..utils.emails import ( send_review_email, send_reviewer_request_email, ) from ..utils.image import ( is_allowed_image_format, is_pdf, pdf_page_to_png, ) from ..utils.shared import get_blueprint from datetime import datetime from flask import ( current_app, render_template, request, send_from_directory, url_for, ) from knowledge_repo.post import KnowledgePost from sqlalchemy import or_ from urllib.parse import unquote from werkzeug.utils import secure_filename import json import logging import os from knowledge_repo.utils.s3 import get_s3_client, put_object_to_s3 import nbformat from nbconvert import HTMLExporter import io from knowledge_repo.constants import AWS_S3_BUCKET logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) blueprint = get_blueprint("editor", __name__) s3_client = get_s3_client("", "", "us-west-2") def get_warning_msg(msg): return json.dumps({"msg": msg, "success": False}) def get_error_msg(msg): return json.dumps({"error_msg": msg, "success": False}) # TODO: These functions have not been fully married # to the KnowledgePost API # Currently, backended by Post objects but partially # implemented on KnowledgePost API # TODO: Deprecate this route in favour of integrating editing # links into primary index pages and user pages @blueprint.route("/webposts", methods=["GET"]) @PageView.logged @permissions.post_edit.require() def gitless_drafts(): """Render the gitless posts that a user has created in table form Editors can see all the posts created via Gitless_Editing """ prefixes = current_app.config.get("WEB_EDITOR_PREFIXES", []) if prefixes == []: raise Exception("Web editing is not configured") query = db_session.query(Post) if prefixes is not None: query = query.filter(or_(*[Post.path.like(p + "%") for p in prefixes])) if current_user.identifier not in current_repo.config.editors: query = query.outerjoin( PostAuthorAssoc, PostAuthorAssoc.post_id == Post.id ).filter(PostAuthorAssoc.user_id == current_user.id) return render_template("web_posts.html", posts=query.all()) @blueprint.route("/edit") @blueprint.route("/edit/<path:path>", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def editor(path=None): """Render the web post editor, either with the default values or if the post already exists, with what has been saved""" prefixes = current_app.config.get("WEB_EDITOR_PREFIXES", None) if prefixes is not None: assert path is None or any( path.startswith(prefix) for prefix in prefixes ), "Editing this post online is not permitted by server configuration." # set defaults data = { "title": None, "status": current_repo.PostStatus.DRAFT.value, "markdown": request.args.get("markdown"), "thumbnail": "", "can_approve": 0, "username": current_user.identifier, "created_at": datetime.now(), "updated_at": datetime.now(), "authors": [current_user.identifier], "comments": [], "tldr": request.args.get("tldr"), } if path is not None and path in current_repo: kp = current_repo.post(path) data.update(kp.headers) data["status"] = kp.status.value data["path"] = path data["markdown"] = kp.read(images=False, headers=False) # retrieve reviews post = db_session.query(Post).filter(Post.path == path).first() if post: # post may have not been indexed yet data["comments"] = ( db_session.query(Comment) .filter(Comment.post_id == post.id) .filter(Comment.type == "review") .all() ) if ( current_user.identifier not in data["authors"] or current_user.identifier in current_repo.config.editors ): data["can_approve"] = 1 data["created_at"] = data["created_at"] data["updated_at"] = data["updated_at"] data["authors"] = json.dumps(data.get("authors")) data["tags"] = json.dumps(data.get("tags", [])) logger.info(data) if "proxy" in data or request.args.get("proxy", False): return render_template("post_editor_proxy.html", **data) if "ipynb" in data or request.args.get("ipynb", False): return render_template("post_editor_ipynb.html", **data) return render_template("post_editor_markdown.html", **data) @blueprint.route("/ajax/editor/save", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def save_post(): """Save the post""" data = request.get_json() path = data["path"] prefixes = current_app.config["WEB_EDITOR_PREFIXES"] if prefixes == []: raise Exception("Web editing is not configured") if prefixes is not None: if not any([path.startswith(prefix) for prefix in prefixes]): return get_warning_msg(f"Your post path must begin with one of {prefixes}") # TODO better handling of overwriting kp = None if path in current_repo: kp = current_repo.post(path) if ( current_user.identifier not in kp.headers["authors"] and current_user.identifier not in current_repo.config.editors ): return get_warning_msg( f"Post with path {path} already exists and you are not " "an author!\nPlease try a different path" ) # create the knowledge post kp = kp or KnowledgePost(path=path) headers = {} headers["created_at"] = datetime.strptime(data["created_at"], "%Y-%m-%d").date() headers["updated_at"] = datetime.strptime(data["updated_at"], "%Y-%m-%d").date() headers["title"] = data["title"] headers["path"] = data["path"] # TODO: thumbnail header not working currently, as feed image set # with kp method not based on header headers["thumbnail"] = data.get("feed_image", "") headers["authors"] = [auth.strip() for auth in data["author"]] headers["tldr"] = data["tldr"] headers["tags"] = [tag.strip() for tag in data.get("tags", [])] if "proxy" in data: headers["proxy"] = data["proxy"] if "ipynb" in data: headers["ipynb"] = data["ipynb"] if ( data.get("file_name", None) is not None and data.get("file_data", None) is not None ): response = s3_upload(data["file_name"], data["file_data"]) if response is None: error_msg = "ERROR during upload file to s3" logger.error(error_msg) return get_error_msg(error_msg) else: headers["display_link"] = response else: headers["display_link"] = data["display_link"] kp.write(unquote(data["markdown"]), headers=headers) # add to repo current_repo.add(kp, update=True, message=headers["title"]) # THIS IS DANGEROUS update_index() return json.dumps({"path": path}) @blueprint.route("/ajax/editor/submit", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def submit_for_review(): """Submit post and if there are reviewers assigned, email them""" path = request.args.get("path", None) data = request.get_json() current_repo.submit(path) # email the reviewers reviewers = data.get("post_reviewers", None) if reviewers: for r in reviewers.split(","): send_reviewer_request_email(path=path, reviewer=r) update_index() return "OK" @blueprint.route("/ajax/editor/publish", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def publish_post(): """Publish the post by changing the status""" path = request.args.get("path", None) if path not in current_repo: return get_warning_msg(f"Unable to retrieve post with path = {path}!") current_repo.publish(path) update_index(check_timeouts=False) return "OK" @blueprint.route("/ajax/editor/unpublish", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def unpublish_post(): """Unpublish the post""" path = request.args.get("path", None) if path not in current_repo: return get_warning_msg(f"Unable to retrieve post with path = {path}!") current_repo.unpublish(path) update_index(check_timeouts=False) return "OK" @blueprint.route("/ajax/editor/accept", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def accept(): """Accept the post""" path = request.args.get("path", None) if path not in current_repo: return get_warning_msg(f"Unable to retrieve post with path = {path}!") current_repo.accept(path) update_index() return "OK" @blueprint.route("/ajax/editor/delete", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def delete_post(): """Delete a post""" path = request.args.get("path", None) if path not in current_repo: return get_warning_msg(f"Unable to retrieve post with path = {path}!") kp = current_repo.post(path) if current_user.identifier not in kp.headers["authors"]: return get_warning_msg("You can only delete a post where you are an author!") current_repo.remove(path) update_index(check_timeouts=False) return "OK" @blueprint.route("/ajax/editor/review", methods=["POST", "DELETE"]) @PageView.logged @permissions.post_edit.require() def review_comment(): """ Saves a review and sends an email that the post has been reviewed to the author of the post or deletes a submitted review """ if request.method == "POST": path = request.args.get("path", None) post_id = db_session.query(Post).filter(Post.path == path).first().id comment = Comment() comment.text = request.get_json()["text"] comment.user_id = current_user.id comment.post_id = post_id comment.type = "review" db_session.add(comment) db_session.commit() send_review_email( path=path, commenter=current_user.identifier, comment_text=comment.text ) elif request.method == "DELETE": comment = Comment.query.get(int(request.args.get("comment_id", ""))) if comment and current_user.id == comment.user_id: db_session.delete(comment) db_session.commit() return "OK" def s3_upload(file_name, file_data): """Upload file(s) to AWS s3 path and return the display link in the response""" if file_name is None or file_data is None or file_data is "": return get_warning_msg(f"File name is empty. Please re-upload!") response = put_object_to_s3(s3_client, file_data, AWS_S3_BUCKET, file_name) # create a html version of this file if ".ipynb" in file_name: with io.StringIO(file_data) as f: nb = nbformat.read(f, as_version=4) # export to html html_exporter = HTMLExporter() (html_data, resources) = html_exporter.from_notebook_node(nb) html_file_name = file_name.replace(".ipynb", ".html") response = put_object_to_s3( s3_client, html_data, AWS_S3_BUCKET, html_file_name, "text/html", ) if response: display_link = "https://s3.us-west-2.amazonaws.com/{0}/{1}".format( AWS_S3_BUCKET, html_file_name ) # todo: make s3 region name be configurable return display_link return None # DEPRECATED @blueprint.route("/file_upload", methods=["POST", "GET"]) @PageView.logged @permissions.post_edit.require() def file_upload(): """ Uploads images dropped on the web editor's markdown box to static/images and notifies editors by email """ upload_folder = "images" title = request.form["title"] files = request.files uploadedFiles = [] if files: for img_file in files.values(): filename = secure_filename(title + "_" + img_file.filename).lower() dst_folder = os.path.join(current_app.static_folder, upload_folder) if is_allowed_image_format(img_file): try: img_file.save(os.path.join(dst_folder, filename)) send_from_directory(dst_folder, filename) uploadedFiles += [ url_for( "static", filename=os.path.join(upload_folder, filename) ) ] except Exception as e: error_msg = f"ERROR during image upload: {e}" logger.error(error_msg) return get_error_msg(error_msg) elif is_pdf(filename): from PyPDF2 import PdfFileReader try: src_pdf = PdfFileReader(img_file) filename = os.path.splitext(filename)[0] num_pages = src_pdf.getNumPages() for page_num in range(num_pages): page_png = pdf_page_to_png(src_pdf, page_num) page_name = "{filename}_{page_num}.jpg".format(**locals()) page_png.save(filename=os.path.join(dst_folder, page_name)) uploadedFiles += [ url_for( "static", filename=os.path.join(upload_folder, page_name), ) ] except Exception as e: error_msg = f"ERROR during pdf upload: {e}" logger.error(error_msg) return get_error_msg(error_msg) return json.dumps({"links": uploadedFiles, "success": True})
mengting1010
451577868d66570a463260c67dff7034214beafd
6edad5351bf3f4f0abc457b6d9532ca25c62c952
You got Lint error in CI: ``` knowledge_repo/app/routes/editor.py:198:57: E502 the backslash is redundant between brackets Error: Process completed with exit code 1. ```
csharplus
6
airbnb/knowledge-repo
698
Update Jupyter Notebook Upload Related
Description of changeset: - Integrate with S3 client - upload Jupyter Notebook to s3 when saving the post - export a html version of Jupyter Notebook and upload to s3 Test Plan: local dev Reviewers: @csharplus @JJJ000
null
2022-12-28 23:02:53+00:00
2022-12-29 07:10:50+00:00
knowledge_repo/app/routes/editor.py
from .. import permissions from ..index import update_index from ..models import Comment, PageView, Post, PostAuthorAssoc from ..proxies import current_repo, current_user, db_session from ..utils.emails import ( send_review_email, send_reviewer_request_email, ) from ..utils.image import ( is_allowed_image_format, is_pdf, pdf_page_to_png, ) from ..utils.shared import get_blueprint from datetime import datetime from flask import ( current_app, render_template, request, send_from_directory, url_for, ) from knowledge_repo.post import KnowledgePost from sqlalchemy import or_ from urllib.parse import unquote from werkzeug.utils import secure_filename import json import logging import os logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) blueprint = get_blueprint("editor", __name__) def get_warning_msg(msg): return json.dumps({'msg': msg, 'success': False}) def get_error_msg(msg): return json.dumps({'error_msg': msg, 'success': False}) # TODO: These functions have not been fully married # to the KnowledgePost API # Currently, backended by Post objects but partially # implemented on KnowledgePost API # TODO: Deprecate this route in favour of integrating editing # links into primary index pages and user pages @blueprint.route('/webposts', methods=['GET']) @PageView.logged @permissions.post_edit.require() def gitless_drafts(): """ Render the gitless posts that a user has created in table form Editors can see all the posts created via Gitless_Editing """ prefixes = current_app.config.get('WEB_EDITOR_PREFIXES', []) if prefixes == []: raise Exception('Web editing is not configured') query = (db_session.query(Post)) if prefixes is not None: query = query.filter(or_(*[Post.path.like(p + '%') for p in prefixes])) if current_user.identifier not in current_repo.config.editors: query = (query.outerjoin( PostAuthorAssoc, PostAuthorAssoc.post_id == Post.id) .filter(PostAuthorAssoc.user_id == current_user.id)) return render_template('web_posts.html', posts=query.all()) @blueprint.route('/edit') @blueprint.route('/edit/<path:path>', methods=['GET', 'POST']) @PageView.logged @permissions.post_edit.require() def editor(path=None): """ Render the web post editor, either with the default values or if the post already exists, with what has been saved """ prefixes = current_app.config.get('WEB_EDITOR_PREFIXES', None) if prefixes is not None: assert ( path is None or any(path.startswith(prefix) for prefix in prefixes) ), 'Editing this post online is not permitted by server configuration.' # set defaults data = {'title': None, 'status': current_repo.PostStatus.DRAFT.value, 'markdown': request.args.get('markdown'), 'thumbnail': '', 'can_approve': 0, 'username': current_user.identifier, 'created_at': datetime.now(), 'updated_at': datetime.now(), 'authors': [current_user.identifier], 'comments': [], 'tldr': request.args.get('tldr'), } if path is not None and path in current_repo: kp = current_repo.post(path) data.update(kp.headers) data['status'] = kp.status.value data['path'] = path data['markdown'] = kp.read(images=False, headers=False) # retrieve reviews post = db_session.query(Post).filter(Post.path == path).first() if post: # post may have not been indexed yet data['comments'] = (db_session.query(Comment) .filter(Comment.post_id == post.id) .filter(Comment.type == 'review') .all()) if current_user.identifier not in data['authors'] \ or current_user.identifier in current_repo.config.editors: data['can_approve'] = 1 data['created_at'] = data['created_at'] data['updated_at'] = data['updated_at'] data['authors'] = json.dumps(data.get('authors')) data['tags'] = json.dumps(data.get('tags', [])) if "proxy" in data or request.args.get("proxy", False): return render_template("post_editor_proxy.html", **data) if "ipynb" in data or request.args.get("ipynb", False): return render_template("post_editor_ipynb.html", **data) return render_template("post_editor_markdown.html", **data) @blueprint.route('/ajax/editor/save', methods=['GET', 'POST']) @PageView.logged @permissions.post_edit.require() def save_post(): """ Save the post """ data = request.get_json() path = data['path'] prefixes = current_app.config['WEB_EDITOR_PREFIXES'] if prefixes == []: raise Exception('Web editing is not configured') if prefixes is not None: if not any([path.startswith(prefix) for prefix in prefixes]): return get_warning_msg( f'Your post path must begin with one of {prefixes}') # TODO better handling of overwriting kp = None if path in current_repo: kp = current_repo.post(path) if current_user.identifier not in kp.headers['authors'] \ and current_user.identifier not in current_repo.config.editors: return get_warning_msg( f'Post with path {path} already exists and you are not ' 'an author!\nPlease try a different path') # create the knowledge post kp = kp or KnowledgePost(path=path) headers = {} headers['created_at'] = datetime.strptime( data['created_at'], '%Y-%m-%d').date() headers['updated_at'] = datetime.strptime( data['updated_at'], '%Y-%m-%d').date() headers['title'] = data['title'] headers['path'] = data['path'] # TODO: thumbnail header not working currently, as feed image set # with kp method not based on header headers['thumbnail'] = data.get('feed_image', '') headers['authors'] = [auth.strip() for auth in data['author']] headers['tldr'] = data['tldr'] headers['tags'] = [tag.strip() for tag in data.get('tags', [])] if 'proxy' in data: headers['proxy'] = data['proxy'] if "ipynb" in data: headers["ipynb"] = data["ipynb"] kp.write(unquote(data['markdown']), headers=headers) # add to repo current_repo.add( kp, update=True, message=headers['title']) # THIS IS DANGEROUS update_index() return json.dumps({'path': path}) @blueprint.route('/ajax/editor/submit', methods=['GET', 'POST']) @PageView.logged @permissions.post_edit.require() def submit_for_review(): """ Submit post and if there are reviewers assigned, email them""" path = request.args.get('path', None) data = request.get_json() current_repo.submit(path) # email the reviewers reviewers = data.get('post_reviewers', None) if reviewers: for r in reviewers.split(','): send_reviewer_request_email(path=path, reviewer=r) update_index() return 'OK' @blueprint.route('/ajax/editor/publish', methods=['GET', 'POST']) @PageView.logged @permissions.post_edit.require() def publish_post(): """ Publish the post by changing the status """ path = request.args.get('path', None) if path not in current_repo: return get_warning_msg(f'Unable to retrieve post with path = {path}!') current_repo.publish(path) update_index(check_timeouts=False) return 'OK' @blueprint.route('/ajax/editor/unpublish', methods=['GET', 'POST']) @PageView.logged @permissions.post_edit.require() def unpublish_post(): """ Unpublish the post """ path = request.args.get('path', None) if path not in current_repo: return get_warning_msg(f'Unable to retrieve post with path = {path}!') current_repo.unpublish(path) update_index(check_timeouts=False) return 'OK' @blueprint.route('/ajax/editor/accept', methods=['GET', 'POST']) @PageView.logged @permissions.post_edit.require() def accept(): """ Accept the post """ path = request.args.get('path', None) if path not in current_repo: return get_warning_msg(f'Unable to retrieve post with path = {path}!') current_repo.accept(path) update_index() return 'OK' @blueprint.route('/ajax/editor/delete', methods=['GET', 'POST']) @PageView.logged @permissions.post_edit.require() def delete_post(): """ Delete a post """ path = request.args.get('path', None) if path not in current_repo: return get_warning_msg(f'Unable to retrieve post with path = {path}!') kp = current_repo.post(path) if current_user.identifier not in kp.headers['authors']: return get_warning_msg( 'You can only delete a post where you are an author!') current_repo.remove(path) update_index(check_timeouts=False) return 'OK' @blueprint.route('/ajax/editor/review', methods=['POST', 'DELETE']) @PageView.logged @permissions.post_edit.require() def review_comment(): """ Saves a review and sends an email that the post has been reviewed to the author of the post or deletes a submitted review """ if request.method == 'POST': path = request.args.get('path', None) post_id = db_session.query(Post).filter(Post.path == path).first().id comment = Comment() comment.text = request.get_json()['text'] comment.user_id = current_user.id comment.post_id = post_id comment.type = 'review' db_session.add(comment) db_session.commit() send_review_email(path=path, commenter=current_user.identifier, comment_text=comment.text) elif request.method == 'DELETE': comment = Comment.query.get(int(request.args.get('comment_id', ''))) if comment and current_user.id == comment.user_id: db_session.delete(comment) db_session.commit() return 'OK' @blueprint.route("/ajax/editor/s3_upload", methods=["POST", "GET"]) @PageView.logged @permissions.post_edit.require() def s3_upload(): """Upload file(s) to AWS s3 path and return the display link in the response""" if request.method == "POST": data = request.get_json() file_name = data.get("file_name", None) object_name = os.path.basename(file_name.replace("\\", "/")) logger.info("file_name: {0} & object_name: {1}".format(file_name, object_name)) if file_name is None: return get_warning_msg(f"File name is empty. Please re-upload!") bucket = data.get("bucket", "www.knowledge-repo.com") response = True # todo: replace it with real s3 upload if response: display_link = "https://s3.us-west-2.amazonaws.com/{0}/{1}".format( bucket, object_name ) # todo: make s3 region name be configurable return json.dumps({"display_link": display_link, "success": True}) error_msg = "ERROR during upload file to s3" logger.error(error_msg) return get_error_msg(error_msg) return "OK" # DEPRECATED @blueprint.route('/file_upload', methods=['POST', 'GET']) @PageView.logged @permissions.post_edit.require() def file_upload(): """ Uploads images dropped on the web editor's markdown box to static/images and notifies editors by email """ upload_folder = 'images' title = request.form['title'] files = request.files uploadedFiles = [] if files: for img_file in files.values(): filename = secure_filename(title + '_' + img_file.filename).lower() dst_folder = os.path.join(current_app.static_folder, upload_folder) if is_allowed_image_format(img_file): try: img_file.save(os.path.join(dst_folder, filename)) send_from_directory(dst_folder, filename) uploadedFiles += [url_for('static', filename=os.path.join( upload_folder, filename))] except Exception as e: error_msg = f'ERROR during image upload: {e}' logger.error(error_msg) return get_error_msg(error_msg) elif is_pdf(filename): from PyPDF2 import PdfFileReader try: src_pdf = PdfFileReader(img_file) filename = os.path.splitext(filename)[0] num_pages = src_pdf.getNumPages() for page_num in range(num_pages): page_png = pdf_page_to_png(src_pdf, page_num) page_name = '{filename}_{page_num}.jpg'.format( **locals()) page_png.save(filename=os.path.join( dst_folder, page_name)) uploadedFiles += [url_for( 'static', filename=os.path.join( upload_folder, page_name))] except Exception as e: error_msg = f'ERROR during pdf upload: {e}' logger.error(error_msg) return get_error_msg(error_msg) return json.dumps({'links': uploadedFiles, 'success': True})
from .. import permissions from ..index import update_index from ..models import Comment, PageView, Post, PostAuthorAssoc from ..proxies import current_repo, current_user, db_session from ..utils.emails import ( send_review_email, send_reviewer_request_email, ) from ..utils.image import ( is_allowed_image_format, is_pdf, pdf_page_to_png, ) from ..utils.shared import get_blueprint from datetime import datetime from flask import ( current_app, render_template, request, send_from_directory, url_for, ) from knowledge_repo.post import KnowledgePost from sqlalchemy import or_ from urllib.parse import unquote from werkzeug.utils import secure_filename import json import logging import os from knowledge_repo.utils.s3 import get_s3_client, put_object_to_s3 import nbformat from nbconvert import HTMLExporter import io from knowledge_repo.constants import AWS_S3_BUCKET logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) blueprint = get_blueprint("editor", __name__) s3_client = get_s3_client("", "", "us-west-2") def get_warning_msg(msg): return json.dumps({"msg": msg, "success": False}) def get_error_msg(msg): return json.dumps({"error_msg": msg, "success": False}) # TODO: These functions have not been fully married # to the KnowledgePost API # Currently, backended by Post objects but partially # implemented on KnowledgePost API # TODO: Deprecate this route in favour of integrating editing # links into primary index pages and user pages @blueprint.route("/webposts", methods=["GET"]) @PageView.logged @permissions.post_edit.require() def gitless_drafts(): """Render the gitless posts that a user has created in table form Editors can see all the posts created via Gitless_Editing """ prefixes = current_app.config.get("WEB_EDITOR_PREFIXES", []) if prefixes == []: raise Exception("Web editing is not configured") query = db_session.query(Post) if prefixes is not None: query = query.filter(or_(*[Post.path.like(p + "%") for p in prefixes])) if current_user.identifier not in current_repo.config.editors: query = query.outerjoin( PostAuthorAssoc, PostAuthorAssoc.post_id == Post.id ).filter(PostAuthorAssoc.user_id == current_user.id) return render_template("web_posts.html", posts=query.all()) @blueprint.route("/edit") @blueprint.route("/edit/<path:path>", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def editor(path=None): """Render the web post editor, either with the default values or if the post already exists, with what has been saved""" prefixes = current_app.config.get("WEB_EDITOR_PREFIXES", None) if prefixes is not None: assert path is None or any( path.startswith(prefix) for prefix in prefixes ), "Editing this post online is not permitted by server configuration." # set defaults data = { "title": None, "status": current_repo.PostStatus.DRAFT.value, "markdown": request.args.get("markdown"), "thumbnail": "", "can_approve": 0, "username": current_user.identifier, "created_at": datetime.now(), "updated_at": datetime.now(), "authors": [current_user.identifier], "comments": [], "tldr": request.args.get("tldr"), } if path is not None and path in current_repo: kp = current_repo.post(path) data.update(kp.headers) data["status"] = kp.status.value data["path"] = path data["markdown"] = kp.read(images=False, headers=False) # retrieve reviews post = db_session.query(Post).filter(Post.path == path).first() if post: # post may have not been indexed yet data["comments"] = ( db_session.query(Comment) .filter(Comment.post_id == post.id) .filter(Comment.type == "review") .all() ) if ( current_user.identifier not in data["authors"] or current_user.identifier in current_repo.config.editors ): data["can_approve"] = 1 data["created_at"] = data["created_at"] data["updated_at"] = data["updated_at"] data["authors"] = json.dumps(data.get("authors")) data["tags"] = json.dumps(data.get("tags", [])) logger.info(data) if "proxy" in data or request.args.get("proxy", False): return render_template("post_editor_proxy.html", **data) if "ipynb" in data or request.args.get("ipynb", False): return render_template("post_editor_ipynb.html", **data) return render_template("post_editor_markdown.html", **data) @blueprint.route("/ajax/editor/save", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def save_post(): """Save the post""" data = request.get_json() path = data["path"] prefixes = current_app.config["WEB_EDITOR_PREFIXES"] if prefixes == []: raise Exception("Web editing is not configured") if prefixes is not None: if not any([path.startswith(prefix) for prefix in prefixes]): return get_warning_msg(f"Your post path must begin with one of {prefixes}") # TODO better handling of overwriting kp = None if path in current_repo: kp = current_repo.post(path) if ( current_user.identifier not in kp.headers["authors"] and current_user.identifier not in current_repo.config.editors ): return get_warning_msg( f"Post with path {path} already exists and you are not " "an author!\nPlease try a different path" ) # create the knowledge post kp = kp or KnowledgePost(path=path) headers = {} headers["created_at"] = datetime.strptime(data["created_at"], "%Y-%m-%d").date() headers["updated_at"] = datetime.strptime(data["updated_at"], "%Y-%m-%d").date() headers["title"] = data["title"] headers["path"] = data["path"] # TODO: thumbnail header not working currently, as feed image set # with kp method not based on header headers["thumbnail"] = data.get("feed_image", "") headers["authors"] = [auth.strip() for auth in data["author"]] headers["tldr"] = data["tldr"] headers["tags"] = [tag.strip() for tag in data.get("tags", [])] if "proxy" in data: headers["proxy"] = data["proxy"] if "ipynb" in data: headers["ipynb"] = data["ipynb"] if ( data.get("file_name", None) is not None and data.get("file_data", None) is not None ): response = s3_upload(data["file_name"], data["file_data"]) if response is None: error_msg = "ERROR during upload file to s3" logger.error(error_msg) return get_error_msg(error_msg) else: headers["display_link"] = response else: headers["display_link"] = data["display_link"] kp.write(unquote(data["markdown"]), headers=headers) # add to repo current_repo.add(kp, update=True, message=headers["title"]) # THIS IS DANGEROUS update_index() return json.dumps({"path": path}) @blueprint.route("/ajax/editor/submit", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def submit_for_review(): """Submit post and if there are reviewers assigned, email them""" path = request.args.get("path", None) data = request.get_json() current_repo.submit(path) # email the reviewers reviewers = data.get("post_reviewers", None) if reviewers: for r in reviewers.split(","): send_reviewer_request_email(path=path, reviewer=r) update_index() return "OK" @blueprint.route("/ajax/editor/publish", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def publish_post(): """Publish the post by changing the status""" path = request.args.get("path", None) if path not in current_repo: return get_warning_msg(f"Unable to retrieve post with path = {path}!") current_repo.publish(path) update_index(check_timeouts=False) return "OK" @blueprint.route("/ajax/editor/unpublish", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def unpublish_post(): """Unpublish the post""" path = request.args.get("path", None) if path not in current_repo: return get_warning_msg(f"Unable to retrieve post with path = {path}!") current_repo.unpublish(path) update_index(check_timeouts=False) return "OK" @blueprint.route("/ajax/editor/accept", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def accept(): """Accept the post""" path = request.args.get("path", None) if path not in current_repo: return get_warning_msg(f"Unable to retrieve post with path = {path}!") current_repo.accept(path) update_index() return "OK" @blueprint.route("/ajax/editor/delete", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def delete_post(): """Delete a post""" path = request.args.get("path", None) if path not in current_repo: return get_warning_msg(f"Unable to retrieve post with path = {path}!") kp = current_repo.post(path) if current_user.identifier not in kp.headers["authors"]: return get_warning_msg("You can only delete a post where you are an author!") current_repo.remove(path) update_index(check_timeouts=False) return "OK" @blueprint.route("/ajax/editor/review", methods=["POST", "DELETE"]) @PageView.logged @permissions.post_edit.require() def review_comment(): """ Saves a review and sends an email that the post has been reviewed to the author of the post or deletes a submitted review """ if request.method == "POST": path = request.args.get("path", None) post_id = db_session.query(Post).filter(Post.path == path).first().id comment = Comment() comment.text = request.get_json()["text"] comment.user_id = current_user.id comment.post_id = post_id comment.type = "review" db_session.add(comment) db_session.commit() send_review_email( path=path, commenter=current_user.identifier, comment_text=comment.text ) elif request.method == "DELETE": comment = Comment.query.get(int(request.args.get("comment_id", ""))) if comment and current_user.id == comment.user_id: db_session.delete(comment) db_session.commit() return "OK" def s3_upload(file_name, file_data): """Upload file(s) to AWS s3 path and return the display link in the response""" if file_name is None or file_data is None or file_data is "": return get_warning_msg(f"File name is empty. Please re-upload!") response = put_object_to_s3(s3_client, file_data, AWS_S3_BUCKET, file_name) # create a html version of this file if ".ipynb" in file_name: with io.StringIO(file_data) as f: nb = nbformat.read(f, as_version=4) # export to html html_exporter = HTMLExporter() (html_data, resources) = html_exporter.from_notebook_node(nb) html_file_name = file_name.replace(".ipynb", ".html") response = put_object_to_s3( s3_client, html_data, AWS_S3_BUCKET, html_file_name, "text/html", ) if response: display_link = "https://s3.us-west-2.amazonaws.com/{0}/{1}".format( AWS_S3_BUCKET, html_file_name ) # todo: make s3 region name be configurable return display_link return None # DEPRECATED @blueprint.route("/file_upload", methods=["POST", "GET"]) @PageView.logged @permissions.post_edit.require() def file_upload(): """ Uploads images dropped on the web editor's markdown box to static/images and notifies editors by email """ upload_folder = "images" title = request.form["title"] files = request.files uploadedFiles = [] if files: for img_file in files.values(): filename = secure_filename(title + "_" + img_file.filename).lower() dst_folder = os.path.join(current_app.static_folder, upload_folder) if is_allowed_image_format(img_file): try: img_file.save(os.path.join(dst_folder, filename)) send_from_directory(dst_folder, filename) uploadedFiles += [ url_for( "static", filename=os.path.join(upload_folder, filename) ) ] except Exception as e: error_msg = f"ERROR during image upload: {e}" logger.error(error_msg) return get_error_msg(error_msg) elif is_pdf(filename): from PyPDF2 import PdfFileReader try: src_pdf = PdfFileReader(img_file) filename = os.path.splitext(filename)[0] num_pages = src_pdf.getNumPages() for page_num in range(num_pages): page_png = pdf_page_to_png(src_pdf, page_num) page_name = "{filename}_{page_num}.jpg".format(**locals()) page_png.save(filename=os.path.join(dst_folder, page_name)) uploadedFiles += [ url_for( "static", filename=os.path.join(upload_folder, page_name), ) ] except Exception as e: error_msg = f"ERROR during pdf upload: {e}" logger.error(error_msg) return get_error_msg(error_msg) return json.dumps({"links": uploadedFiles, "success": True})
mengting1010
451577868d66570a463260c67dff7034214beafd
6edad5351bf3f4f0abc457b6d9532ca25c62c952
Same lint issue here.
csharplus
7
airbnb/knowledge-repo
698
Update Jupyter Notebook Upload Related
Description of changeset: - Integrate with S3 client - upload Jupyter Notebook to s3 when saving the post - export a html version of Jupyter Notebook and upload to s3 Test Plan: local dev Reviewers: @csharplus @JJJ000
null
2022-12-28 23:02:53+00:00
2022-12-29 07:10:50+00:00
knowledge_repo/app/routes/editor.py
from .. import permissions from ..index import update_index from ..models import Comment, PageView, Post, PostAuthorAssoc from ..proxies import current_repo, current_user, db_session from ..utils.emails import ( send_review_email, send_reviewer_request_email, ) from ..utils.image import ( is_allowed_image_format, is_pdf, pdf_page_to_png, ) from ..utils.shared import get_blueprint from datetime import datetime from flask import ( current_app, render_template, request, send_from_directory, url_for, ) from knowledge_repo.post import KnowledgePost from sqlalchemy import or_ from urllib.parse import unquote from werkzeug.utils import secure_filename import json import logging import os logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) blueprint = get_blueprint("editor", __name__) def get_warning_msg(msg): return json.dumps({'msg': msg, 'success': False}) def get_error_msg(msg): return json.dumps({'error_msg': msg, 'success': False}) # TODO: These functions have not been fully married # to the KnowledgePost API # Currently, backended by Post objects but partially # implemented on KnowledgePost API # TODO: Deprecate this route in favour of integrating editing # links into primary index pages and user pages @blueprint.route('/webposts', methods=['GET']) @PageView.logged @permissions.post_edit.require() def gitless_drafts(): """ Render the gitless posts that a user has created in table form Editors can see all the posts created via Gitless_Editing """ prefixes = current_app.config.get('WEB_EDITOR_PREFIXES', []) if prefixes == []: raise Exception('Web editing is not configured') query = (db_session.query(Post)) if prefixes is not None: query = query.filter(or_(*[Post.path.like(p + '%') for p in prefixes])) if current_user.identifier not in current_repo.config.editors: query = (query.outerjoin( PostAuthorAssoc, PostAuthorAssoc.post_id == Post.id) .filter(PostAuthorAssoc.user_id == current_user.id)) return render_template('web_posts.html', posts=query.all()) @blueprint.route('/edit') @blueprint.route('/edit/<path:path>', methods=['GET', 'POST']) @PageView.logged @permissions.post_edit.require() def editor(path=None): """ Render the web post editor, either with the default values or if the post already exists, with what has been saved """ prefixes = current_app.config.get('WEB_EDITOR_PREFIXES', None) if prefixes is not None: assert ( path is None or any(path.startswith(prefix) for prefix in prefixes) ), 'Editing this post online is not permitted by server configuration.' # set defaults data = {'title': None, 'status': current_repo.PostStatus.DRAFT.value, 'markdown': request.args.get('markdown'), 'thumbnail': '', 'can_approve': 0, 'username': current_user.identifier, 'created_at': datetime.now(), 'updated_at': datetime.now(), 'authors': [current_user.identifier], 'comments': [], 'tldr': request.args.get('tldr'), } if path is not None and path in current_repo: kp = current_repo.post(path) data.update(kp.headers) data['status'] = kp.status.value data['path'] = path data['markdown'] = kp.read(images=False, headers=False) # retrieve reviews post = db_session.query(Post).filter(Post.path == path).first() if post: # post may have not been indexed yet data['comments'] = (db_session.query(Comment) .filter(Comment.post_id == post.id) .filter(Comment.type == 'review') .all()) if current_user.identifier not in data['authors'] \ or current_user.identifier in current_repo.config.editors: data['can_approve'] = 1 data['created_at'] = data['created_at'] data['updated_at'] = data['updated_at'] data['authors'] = json.dumps(data.get('authors')) data['tags'] = json.dumps(data.get('tags', [])) if "proxy" in data or request.args.get("proxy", False): return render_template("post_editor_proxy.html", **data) if "ipynb" in data or request.args.get("ipynb", False): return render_template("post_editor_ipynb.html", **data) return render_template("post_editor_markdown.html", **data) @blueprint.route('/ajax/editor/save', methods=['GET', 'POST']) @PageView.logged @permissions.post_edit.require() def save_post(): """ Save the post """ data = request.get_json() path = data['path'] prefixes = current_app.config['WEB_EDITOR_PREFIXES'] if prefixes == []: raise Exception('Web editing is not configured') if prefixes is not None: if not any([path.startswith(prefix) for prefix in prefixes]): return get_warning_msg( f'Your post path must begin with one of {prefixes}') # TODO better handling of overwriting kp = None if path in current_repo: kp = current_repo.post(path) if current_user.identifier not in kp.headers['authors'] \ and current_user.identifier not in current_repo.config.editors: return get_warning_msg( f'Post with path {path} already exists and you are not ' 'an author!\nPlease try a different path') # create the knowledge post kp = kp or KnowledgePost(path=path) headers = {} headers['created_at'] = datetime.strptime( data['created_at'], '%Y-%m-%d').date() headers['updated_at'] = datetime.strptime( data['updated_at'], '%Y-%m-%d').date() headers['title'] = data['title'] headers['path'] = data['path'] # TODO: thumbnail header not working currently, as feed image set # with kp method not based on header headers['thumbnail'] = data.get('feed_image', '') headers['authors'] = [auth.strip() for auth in data['author']] headers['tldr'] = data['tldr'] headers['tags'] = [tag.strip() for tag in data.get('tags', [])] if 'proxy' in data: headers['proxy'] = data['proxy'] if "ipynb" in data: headers["ipynb"] = data["ipynb"] kp.write(unquote(data['markdown']), headers=headers) # add to repo current_repo.add( kp, update=True, message=headers['title']) # THIS IS DANGEROUS update_index() return json.dumps({'path': path}) @blueprint.route('/ajax/editor/submit', methods=['GET', 'POST']) @PageView.logged @permissions.post_edit.require() def submit_for_review(): """ Submit post and if there are reviewers assigned, email them""" path = request.args.get('path', None) data = request.get_json() current_repo.submit(path) # email the reviewers reviewers = data.get('post_reviewers', None) if reviewers: for r in reviewers.split(','): send_reviewer_request_email(path=path, reviewer=r) update_index() return 'OK' @blueprint.route('/ajax/editor/publish', methods=['GET', 'POST']) @PageView.logged @permissions.post_edit.require() def publish_post(): """ Publish the post by changing the status """ path = request.args.get('path', None) if path not in current_repo: return get_warning_msg(f'Unable to retrieve post with path = {path}!') current_repo.publish(path) update_index(check_timeouts=False) return 'OK' @blueprint.route('/ajax/editor/unpublish', methods=['GET', 'POST']) @PageView.logged @permissions.post_edit.require() def unpublish_post(): """ Unpublish the post """ path = request.args.get('path', None) if path not in current_repo: return get_warning_msg(f'Unable to retrieve post with path = {path}!') current_repo.unpublish(path) update_index(check_timeouts=False) return 'OK' @blueprint.route('/ajax/editor/accept', methods=['GET', 'POST']) @PageView.logged @permissions.post_edit.require() def accept(): """ Accept the post """ path = request.args.get('path', None) if path not in current_repo: return get_warning_msg(f'Unable to retrieve post with path = {path}!') current_repo.accept(path) update_index() return 'OK' @blueprint.route('/ajax/editor/delete', methods=['GET', 'POST']) @PageView.logged @permissions.post_edit.require() def delete_post(): """ Delete a post """ path = request.args.get('path', None) if path not in current_repo: return get_warning_msg(f'Unable to retrieve post with path = {path}!') kp = current_repo.post(path) if current_user.identifier not in kp.headers['authors']: return get_warning_msg( 'You can only delete a post where you are an author!') current_repo.remove(path) update_index(check_timeouts=False) return 'OK' @blueprint.route('/ajax/editor/review', methods=['POST', 'DELETE']) @PageView.logged @permissions.post_edit.require() def review_comment(): """ Saves a review and sends an email that the post has been reviewed to the author of the post or deletes a submitted review """ if request.method == 'POST': path = request.args.get('path', None) post_id = db_session.query(Post).filter(Post.path == path).first().id comment = Comment() comment.text = request.get_json()['text'] comment.user_id = current_user.id comment.post_id = post_id comment.type = 'review' db_session.add(comment) db_session.commit() send_review_email(path=path, commenter=current_user.identifier, comment_text=comment.text) elif request.method == 'DELETE': comment = Comment.query.get(int(request.args.get('comment_id', ''))) if comment and current_user.id == comment.user_id: db_session.delete(comment) db_session.commit() return 'OK' @blueprint.route("/ajax/editor/s3_upload", methods=["POST", "GET"]) @PageView.logged @permissions.post_edit.require() def s3_upload(): """Upload file(s) to AWS s3 path and return the display link in the response""" if request.method == "POST": data = request.get_json() file_name = data.get("file_name", None) object_name = os.path.basename(file_name.replace("\\", "/")) logger.info("file_name: {0} & object_name: {1}".format(file_name, object_name)) if file_name is None: return get_warning_msg(f"File name is empty. Please re-upload!") bucket = data.get("bucket", "www.knowledge-repo.com") response = True # todo: replace it with real s3 upload if response: display_link = "https://s3.us-west-2.amazonaws.com/{0}/{1}".format( bucket, object_name ) # todo: make s3 region name be configurable return json.dumps({"display_link": display_link, "success": True}) error_msg = "ERROR during upload file to s3" logger.error(error_msg) return get_error_msg(error_msg) return "OK" # DEPRECATED @blueprint.route('/file_upload', methods=['POST', 'GET']) @PageView.logged @permissions.post_edit.require() def file_upload(): """ Uploads images dropped on the web editor's markdown box to static/images and notifies editors by email """ upload_folder = 'images' title = request.form['title'] files = request.files uploadedFiles = [] if files: for img_file in files.values(): filename = secure_filename(title + '_' + img_file.filename).lower() dst_folder = os.path.join(current_app.static_folder, upload_folder) if is_allowed_image_format(img_file): try: img_file.save(os.path.join(dst_folder, filename)) send_from_directory(dst_folder, filename) uploadedFiles += [url_for('static', filename=os.path.join( upload_folder, filename))] except Exception as e: error_msg = f'ERROR during image upload: {e}' logger.error(error_msg) return get_error_msg(error_msg) elif is_pdf(filename): from PyPDF2 import PdfFileReader try: src_pdf = PdfFileReader(img_file) filename = os.path.splitext(filename)[0] num_pages = src_pdf.getNumPages() for page_num in range(num_pages): page_png = pdf_page_to_png(src_pdf, page_num) page_name = '{filename}_{page_num}.jpg'.format( **locals()) page_png.save(filename=os.path.join( dst_folder, page_name)) uploadedFiles += [url_for( 'static', filename=os.path.join( upload_folder, page_name))] except Exception as e: error_msg = f'ERROR during pdf upload: {e}' logger.error(error_msg) return get_error_msg(error_msg) return json.dumps({'links': uploadedFiles, 'success': True})
from .. import permissions from ..index import update_index from ..models import Comment, PageView, Post, PostAuthorAssoc from ..proxies import current_repo, current_user, db_session from ..utils.emails import ( send_review_email, send_reviewer_request_email, ) from ..utils.image import ( is_allowed_image_format, is_pdf, pdf_page_to_png, ) from ..utils.shared import get_blueprint from datetime import datetime from flask import ( current_app, render_template, request, send_from_directory, url_for, ) from knowledge_repo.post import KnowledgePost from sqlalchemy import or_ from urllib.parse import unquote from werkzeug.utils import secure_filename import json import logging import os from knowledge_repo.utils.s3 import get_s3_client, put_object_to_s3 import nbformat from nbconvert import HTMLExporter import io from knowledge_repo.constants import AWS_S3_BUCKET logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) blueprint = get_blueprint("editor", __name__) s3_client = get_s3_client("", "", "us-west-2") def get_warning_msg(msg): return json.dumps({"msg": msg, "success": False}) def get_error_msg(msg): return json.dumps({"error_msg": msg, "success": False}) # TODO: These functions have not been fully married # to the KnowledgePost API # Currently, backended by Post objects but partially # implemented on KnowledgePost API # TODO: Deprecate this route in favour of integrating editing # links into primary index pages and user pages @blueprint.route("/webposts", methods=["GET"]) @PageView.logged @permissions.post_edit.require() def gitless_drafts(): """Render the gitless posts that a user has created in table form Editors can see all the posts created via Gitless_Editing """ prefixes = current_app.config.get("WEB_EDITOR_PREFIXES", []) if prefixes == []: raise Exception("Web editing is not configured") query = db_session.query(Post) if prefixes is not None: query = query.filter(or_(*[Post.path.like(p + "%") for p in prefixes])) if current_user.identifier not in current_repo.config.editors: query = query.outerjoin( PostAuthorAssoc, PostAuthorAssoc.post_id == Post.id ).filter(PostAuthorAssoc.user_id == current_user.id) return render_template("web_posts.html", posts=query.all()) @blueprint.route("/edit") @blueprint.route("/edit/<path:path>", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def editor(path=None): """Render the web post editor, either with the default values or if the post already exists, with what has been saved""" prefixes = current_app.config.get("WEB_EDITOR_PREFIXES", None) if prefixes is not None: assert path is None or any( path.startswith(prefix) for prefix in prefixes ), "Editing this post online is not permitted by server configuration." # set defaults data = { "title": None, "status": current_repo.PostStatus.DRAFT.value, "markdown": request.args.get("markdown"), "thumbnail": "", "can_approve": 0, "username": current_user.identifier, "created_at": datetime.now(), "updated_at": datetime.now(), "authors": [current_user.identifier], "comments": [], "tldr": request.args.get("tldr"), } if path is not None and path in current_repo: kp = current_repo.post(path) data.update(kp.headers) data["status"] = kp.status.value data["path"] = path data["markdown"] = kp.read(images=False, headers=False) # retrieve reviews post = db_session.query(Post).filter(Post.path == path).first() if post: # post may have not been indexed yet data["comments"] = ( db_session.query(Comment) .filter(Comment.post_id == post.id) .filter(Comment.type == "review") .all() ) if ( current_user.identifier not in data["authors"] or current_user.identifier in current_repo.config.editors ): data["can_approve"] = 1 data["created_at"] = data["created_at"] data["updated_at"] = data["updated_at"] data["authors"] = json.dumps(data.get("authors")) data["tags"] = json.dumps(data.get("tags", [])) logger.info(data) if "proxy" in data or request.args.get("proxy", False): return render_template("post_editor_proxy.html", **data) if "ipynb" in data or request.args.get("ipynb", False): return render_template("post_editor_ipynb.html", **data) return render_template("post_editor_markdown.html", **data) @blueprint.route("/ajax/editor/save", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def save_post(): """Save the post""" data = request.get_json() path = data["path"] prefixes = current_app.config["WEB_EDITOR_PREFIXES"] if prefixes == []: raise Exception("Web editing is not configured") if prefixes is not None: if not any([path.startswith(prefix) for prefix in prefixes]): return get_warning_msg(f"Your post path must begin with one of {prefixes}") # TODO better handling of overwriting kp = None if path in current_repo: kp = current_repo.post(path) if ( current_user.identifier not in kp.headers["authors"] and current_user.identifier not in current_repo.config.editors ): return get_warning_msg( f"Post with path {path} already exists and you are not " "an author!\nPlease try a different path" ) # create the knowledge post kp = kp or KnowledgePost(path=path) headers = {} headers["created_at"] = datetime.strptime(data["created_at"], "%Y-%m-%d").date() headers["updated_at"] = datetime.strptime(data["updated_at"], "%Y-%m-%d").date() headers["title"] = data["title"] headers["path"] = data["path"] # TODO: thumbnail header not working currently, as feed image set # with kp method not based on header headers["thumbnail"] = data.get("feed_image", "") headers["authors"] = [auth.strip() for auth in data["author"]] headers["tldr"] = data["tldr"] headers["tags"] = [tag.strip() for tag in data.get("tags", [])] if "proxy" in data: headers["proxy"] = data["proxy"] if "ipynb" in data: headers["ipynb"] = data["ipynb"] if ( data.get("file_name", None) is not None and data.get("file_data", None) is not None ): response = s3_upload(data["file_name"], data["file_data"]) if response is None: error_msg = "ERROR during upload file to s3" logger.error(error_msg) return get_error_msg(error_msg) else: headers["display_link"] = response else: headers["display_link"] = data["display_link"] kp.write(unquote(data["markdown"]), headers=headers) # add to repo current_repo.add(kp, update=True, message=headers["title"]) # THIS IS DANGEROUS update_index() return json.dumps({"path": path}) @blueprint.route("/ajax/editor/submit", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def submit_for_review(): """Submit post and if there are reviewers assigned, email them""" path = request.args.get("path", None) data = request.get_json() current_repo.submit(path) # email the reviewers reviewers = data.get("post_reviewers", None) if reviewers: for r in reviewers.split(","): send_reviewer_request_email(path=path, reviewer=r) update_index() return "OK" @blueprint.route("/ajax/editor/publish", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def publish_post(): """Publish the post by changing the status""" path = request.args.get("path", None) if path not in current_repo: return get_warning_msg(f"Unable to retrieve post with path = {path}!") current_repo.publish(path) update_index(check_timeouts=False) return "OK" @blueprint.route("/ajax/editor/unpublish", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def unpublish_post(): """Unpublish the post""" path = request.args.get("path", None) if path not in current_repo: return get_warning_msg(f"Unable to retrieve post with path = {path}!") current_repo.unpublish(path) update_index(check_timeouts=False) return "OK" @blueprint.route("/ajax/editor/accept", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def accept(): """Accept the post""" path = request.args.get("path", None) if path not in current_repo: return get_warning_msg(f"Unable to retrieve post with path = {path}!") current_repo.accept(path) update_index() return "OK" @blueprint.route("/ajax/editor/delete", methods=["GET", "POST"]) @PageView.logged @permissions.post_edit.require() def delete_post(): """Delete a post""" path = request.args.get("path", None) if path not in current_repo: return get_warning_msg(f"Unable to retrieve post with path = {path}!") kp = current_repo.post(path) if current_user.identifier not in kp.headers["authors"]: return get_warning_msg("You can only delete a post where you are an author!") current_repo.remove(path) update_index(check_timeouts=False) return "OK" @blueprint.route("/ajax/editor/review", methods=["POST", "DELETE"]) @PageView.logged @permissions.post_edit.require() def review_comment(): """ Saves a review and sends an email that the post has been reviewed to the author of the post or deletes a submitted review """ if request.method == "POST": path = request.args.get("path", None) post_id = db_session.query(Post).filter(Post.path == path).first().id comment = Comment() comment.text = request.get_json()["text"] comment.user_id = current_user.id comment.post_id = post_id comment.type = "review" db_session.add(comment) db_session.commit() send_review_email( path=path, commenter=current_user.identifier, comment_text=comment.text ) elif request.method == "DELETE": comment = Comment.query.get(int(request.args.get("comment_id", ""))) if comment and current_user.id == comment.user_id: db_session.delete(comment) db_session.commit() return "OK" def s3_upload(file_name, file_data): """Upload file(s) to AWS s3 path and return the display link in the response""" if file_name is None or file_data is None or file_data is "": return get_warning_msg(f"File name is empty. Please re-upload!") response = put_object_to_s3(s3_client, file_data, AWS_S3_BUCKET, file_name) # create a html version of this file if ".ipynb" in file_name: with io.StringIO(file_data) as f: nb = nbformat.read(f, as_version=4) # export to html html_exporter = HTMLExporter() (html_data, resources) = html_exporter.from_notebook_node(nb) html_file_name = file_name.replace(".ipynb", ".html") response = put_object_to_s3( s3_client, html_data, AWS_S3_BUCKET, html_file_name, "text/html", ) if response: display_link = "https://s3.us-west-2.amazonaws.com/{0}/{1}".format( AWS_S3_BUCKET, html_file_name ) # todo: make s3 region name be configurable return display_link return None # DEPRECATED @blueprint.route("/file_upload", methods=["POST", "GET"]) @PageView.logged @permissions.post_edit.require() def file_upload(): """ Uploads images dropped on the web editor's markdown box to static/images and notifies editors by email """ upload_folder = "images" title = request.form["title"] files = request.files uploadedFiles = [] if files: for img_file in files.values(): filename = secure_filename(title + "_" + img_file.filename).lower() dst_folder = os.path.join(current_app.static_folder, upload_folder) if is_allowed_image_format(img_file): try: img_file.save(os.path.join(dst_folder, filename)) send_from_directory(dst_folder, filename) uploadedFiles += [ url_for( "static", filename=os.path.join(upload_folder, filename) ) ] except Exception as e: error_msg = f"ERROR during image upload: {e}" logger.error(error_msg) return get_error_msg(error_msg) elif is_pdf(filename): from PyPDF2 import PdfFileReader try: src_pdf = PdfFileReader(img_file) filename = os.path.splitext(filename)[0] num_pages = src_pdf.getNumPages() for page_num in range(num_pages): page_png = pdf_page_to_png(src_pdf, page_num) page_name = "{filename}_{page_num}.jpg".format(**locals()) page_png.save(filename=os.path.join(dst_folder, page_name)) uploadedFiles += [ url_for( "static", filename=os.path.join(upload_folder, page_name), ) ] except Exception as e: error_msg = f"ERROR during pdf upload: {e}" logger.error(error_msg) return get_error_msg(error_msg) return json.dumps({"links": uploadedFiles, "success": True})
mengting1010
451577868d66570a463260c67dff7034214beafd
6edad5351bf3f4f0abc457b6d9532ca25c62c952
Same lint issue here.
csharplus
8
airbnb/knowledge-repo
697
Enable Jupyter Notebook Upload Post Editor
Description of changeset: - Enable jupyter notebook upload post editor ![image](https://user-images.githubusercontent.com/64947033/209613316-41bf9581-2f77-4dc4-8f79-afb17f578269.png) - This pr only enables the ability to upload and render a html version of jupyter notebook (if it is a ipynb file, s3 will download the file to your local instead of displaying it. todo: will try to fix it in next pr) ![image](https://user-images.githubusercontent.com/64947033/209613457-13c64da9-f6e6-4cf8-ab97-c7b6e95ed7ba.png) Test Plan: Local Dev Reviewers: @csharplus @JJJ000
null
2022-12-27 04:44:23+00:00
2022-12-27 20:24:27+00:00
docker/config.py
from datetime import timedelta # --------------------------------------------------- # Host configuration # --------------------------------------------------- # The server name is used by Flask to limit access to the # served content to request to a particular domain. It # is also used by some authentication providers (in particular # OAuth providers) to advertise callback providers. If # not provided, it is assumed in these contexts to be # 'localhost:7000'. Be sure to specify this before deploying # into production. SERVER_NAME = "localhost:7001" # The knowledge repository uses the secret key to sign user # sessions. If not specified, a unique secret key will be # generated every time the server starts up. If hosting # in a multi-server environment, or you want sessions # to persist accross server restarts, set this to something # static. SECRET_KEY = None # Set DEPLOY_HTTPS to True if you want to enable encrypted # communication with Flask. When enabled, you must provide # your ssl certificate, which consists of a .crt and .key # file. # Note: Even if you set DEPLOY_HTTPS to True, you still need # to set the port to 443 manually. DEPLOY_HTTPS = False SSL_CERT = {"cert": "/path/to/cert", "key": "/path/to/key"} # --------------------------------------------------- # Debug configuration # --------------------------------------------------- DEBUG = False # --------------------------------------------------- # Database configuration # --------------------------------------------------- SQLALCHEMY_DATABASE_URI = ( "postgresql+psycopg2://knowledge_repo:password@localhost:5432/knowledge_repo" ) # If you are using a MySQL database, you must specify the URI as # demonstrated below. # SQLALCHEMY_DATABASE_URI = 'mysql://username:password@hostname/database' SQLALCHEMY_ECHO = False SQLALCHEMY_TRACK_MODIFICATIONS = False # Should the database tables be automatically created DB_AUTO_CREATE = True # Should the database be automatically migrated when updates exist # Note: This is True by default if this configuration is not applied, # under the assumption that if you are concerned about this file # you are probably interested in minimising risk to stability and handling # database upgrades manually. Manual database migrations can be # performed using `knowledge_repo --repo <> db_upgrade ...`. DB_AUTO_UPGRADE = False # --------------------------------------------------- # Authentication configuration # --------------------------------------------------- # Authentication providers allow users to sign into the Knowledge Repo # in a variety of different ways. You can create your own subclass of # `KnowledgeAuthProvider` and add either the instance or identifier # used for that class below. # By default, the knowledge repo offers: # ['debug', 'oauth2', 'bitbucket', 'github', 'google', 'ldap'] AUTH_PROVIDERS = ["google"] OAUTH_GOOGLE_CLIENT_ID = "<client id>" OAUTH_GOOGLE_CLIENT_SECRET = "<client secret>" # If you are going to use a OAuth provider, you will need to specify client ids # and private tokens. This can be done by instantiating instances of # `OAuth2Provider` and adding them to the above list, or by specifying OAuth # connection properties as demonstrated below for the GitHub authenticator. # OAUTH_GITHUB_CLIENT_ID = '<client id>' # OAUTH_GITHUB_CLIENT_SECRET = '<client secret>' # To configure a generic OAuth provider that is not one of the presets # provided, you may use the provider 'oauth2' which creates an empty, # unconfigured OAuth2Provider. You must then override its configuration. # For example, for a self-managed Gitlab CE instance at gitlab.example.com: # OAUTH_OAUTH2_BASE_URL = 'https://gitlab.example.com/api/v4/' # OAUTH_OAUTH2_AUTHORIZATION_URL = 'https://gitlab.example.com/oauth/authorize' # OAUTH_OAUTH2_TOKEN_URL = 'https://gitlab.example.com/oauth/token' # OAUTH_OAUTH2_AUTO_REFRESH_URL = 'https://gitlab.example.com/oauth/token' # OAUTH_OAUTH2_SCOPES = 'api' # OAUTH_OAUTH2_USER_INFO_ENDPOINT = 'user' # OAUTH_OAUTH2_USER_INFO_MAPPING = { # 'identifier': 'username', # 'name': 'name', # 'avatar_uri': 'avatar_url' # } # OAUTH_OAUTH2_VERIFY_SSL_CERTS = '/path/to/certs/my.ca-bundle' # OAUTH_OAUTH2_CLIENT_ID = '<client id>' # OAUTH_OAUTH2_CLIENT_SECRET = '<client secret>' # The configuration OAUTH_<name>_VERIFY_SSL_CERTS is what is passed to the # 'verify' parameter in the Requests module, and can be used to disable # HTTPS verification (not recommended) or provide a custom CA bundle. See: # http://docs.python-requests.org/en/master/user/advanced/#ssl-cert-verification # You may also override the .validate() method of a KnowledgeAuthProvider # to perform an additional validation step before authenticating a user. # The following example checks whether a user has access to the git remote # of the local Knowledge Repository: # def OAUTH_OAUTH2_VALIDATE(provider, user): # # if provider.app.repository.git_has_remote: # # url_parts = ( # provider.app.repository.git_remote.url.split(':') # ) # # url_subparts = url_parts[1].split('/') # # if url_parts[0] == "[email protected]": # git_project = ( # url_subparts[0] + "%2F" + url_subparts[1].split('.')[0]) # elif ( # url_parts[0] == "https" # and url_subparts[2] == "gitlab.example.com" # ): # git_project = ( # url_subparts[3] + "%2F" + url_subparts[4].split('.')[0]) # else: # provider.app.logger.warning( # "User validation failed: unexpected git remote url [" # + provider.app.repository.git_remote.url + "]") # return False # # user_validate_url = provider.base_url + "projects/" + git_project # # resp = provider.oauth_client.get( # user_validate_url, # verify=OAUTH_OAUTH2_VERIFY_HTTPS) # # if resp.status_code == 200: # return True # else: # provider.app.logger.warning( # "User validation failed: validation URL [" # + user_validate_url + "] returned HTTP status [" # + str(resp.status_code) + "]") # You can also forgo a fully-fledged sign in process for users by hosting the # knowledge repository behind a proxy server that pre-authenticates users, and # adds the appropriate user identifier to the http headers of the request. If # enabled below, then they take precedence over any other forms of # authentication. If the call to `AUTH_MAP_REQUEST_HEADERS` results in a null # user identifier, then the authentication flow will fall back to use any of # the providers specified above. AUTH_USE_REQUEST_HEADERS = False # If using headers to authenticate, the following function should be # implemented to transform a dictionary of headers into a dictionary of user # attributes. Currently only 'identifier', 'avatar_uri', 'name' and 'email' # are supported. If this method returns `None`, or `identifier` is not # supplied, then the authorization flow will fall back to other authentication # methods. def AUTH_MAP_REQUEST_HEADERS(headers): return { # 'identifier': None, # 'avatar_uri': None, # 'name': None, # 'email': None } # The following AUTH_USER_IDENTIFIER* configuration keys are deprecated and # will be removed in v0.9. AUTH_USER_IDENTIFIER_REQUEST_HEADER = None def AUTH_USER_IDENTIFIER_REQUEST_HEADER_MAPPING(identifier): return identifier # If the server desires to modify the attributes of the `User` object # associated with users logged in via any of the above authentication # providers, it can do so via this configuration key. This function will be # run once at user login (if using an `AuthenticationProvider`, and then at # most once during any caching lifetime period (as specified below). Note that # attributes collected via `AuthenticationProvider`s will not be updated # after initial login (user must relogin in order to reset those attributes). def AUTH_USER_ATTRIBUTE_SETTER(user): return user # The time to wait before re-checking user attributes with the above function # for users logged in via request headers. AUTH_USER_ATTRIBUTE_CACHE_LIFETIME = 24 * 60 * 60 # 1 day # Once a user is logged in via an authentication provider, they will remain # logged in via the use of cookies. By default, this cookie will last one year. # This is managed by `flask_login`, but is copied here for convenience. # For other options regarding sessions, please refer to: # https://flask-login.readthedocs.io/en/latest/#cookie-settings REMEMBER_COOKIE_DURATION = timedelta(days=365) # --------------------------------------------------- # LDAP configuration # --------------------------------------------------- # When using an LDAP server for user verification, you need to configure # the location of the server, and the directory structure used by your # organization. # Currently the port and protocol must both be included in the server address LDAP_SERVER = "ldap://127.0.0.1:389" # When entering this, note the "{0}" which denotes where the user_id # is inserted. LDAP_USERDN_SCHEMA = "cn={user_id},ou=people,dc=planetexpress,dc=com" # --------------------------------------------------- # Policy configuration # --------------------------------------------------- # This section configures various policy related to access control. # Should anonymous users be able to view the post indices POLICY_ANONYMOUS_VIEW_INDEX = True # Should anonymous users be able to view the content of posts POLICY_ANONYMOUS_VIEW_POST = True # Should anonymous users be able to view overall statistics POLICY_ANONYMOUS_VIEW_STATS = True # Should anonymous users be able to view tag pages POLICY_ANONYMOUS_VIEW_TAGS = True # Should anonymous users be able to download posts (or their source) POLICY_ANONYMOUS_DOWNLOADS = False # --------------------------------------------------- # Repository configuration # --------------------------------------------------- # You may specify a function `prepare_repo` which configures # the repository upon which this server is running. This # takes place after the repository has been instantiated # and before the server is able to serve requests. It is # possible to do anything to the repository, including # substituting the repository for another one. # By default, repositories manage their own configurations, # but this can be risky as they may run arbitrary python code, # which opens a vector for malicious users to compromise # the server. If you want to avoid this risk, pass # the '--safe' (TODO!) option to `knowledge_repo` config and # manually configure the repository here. # For example, if your server instance is sitting atop # a meta-repository, it may make sense to update the meta-repository # configuration with that of one of its children. def prepare_repo(repo): return repo # --------------------------------------------------- # Repository Indexing configuration # --------------------------------------------------- # The Knowedge Repo updates the index of available posts on a regular basis. # If the database is not thread-safe (i.e. in the case of SQLite), then the # index will be updated on the main thread before every request that is more # than `INDEX_INTERVAL` seconds after the last sync completed. Otherwise, # indexing will occur every `INDEX_INTERVAL` seconds after the previous sync. # Syncing is designed to be compatible with multiple instances of the Knowledge # Repo connected to the same database, accross multiple machines and/or # processes; and so a global indexing lock is employed. When a sync begins, # a sync lock is put in place and the responsible process is considered to be # the primary agent responsible for syncing until its last update is longer # than`INDEXING_TIMEOUT` seconds, whereby the lock is ceded to the next # requesting process. Note that `INDEXING_TIMEOUT` must be larger than # `INDEXING_INTERVAL` or strange things might begin to happen. INDEXING_INTERVAL = 5 * 60 # 5 minutes INDEXING_TIMEOUT = 10 * 60 # 10 minutes # Whether an index operation should update repositories INDEXING_UPDATES_REPOSITORIES = True # Whether repositories should be updated even without a sync lock (in which # case the repositories will be updated on the sync timers, even if the # relevant process/thread does not have a lock on updating the index). This is # useful in context of multiple Knowledge Repo servers working together to # serve the repositories across multiple machines, which each require # repository syncing. Disable this if (for some reason) you have multiple # Knowledge Repo servers running on the same machine, and you want to avoid # potential clashes. This key is ignored if `INDEXING_UPDATES_REPOSITORIES` is # False INDEXING_UPDATES_REPOSITORIES_WITHOUT_LOCK = True # In some cases you may want to disable indexing entirely, which is currently # only ever used by the Knowledge Post previewer. Disabling the index means # that posts will not be discoverable, but if know the path in the repository # you can view the post with a direct link. INDEXING_ENABLED = True # --------------------------------------------------- # Flask Mail Configuration # Refer to https://pythonhosted.org/flask-mail/ # Unless specified, upstream defaults are used as indicated # provided that MAIL_SERVER is defined. # --------------------------------------------------- # MAIL_SERVER = 'localhost' # default = 'localhost' # MAIL_PORT = 25 # default = 25 # MAIL_USE_TLS = False # default = False # MAIL_USE_SSL = False # default = False # MAIL_DEBUG = False # default = app.debug # MAIL_USERNAME = None # default = None # MAIL_PASSWORD = None # default = None # MAIL_DEFAULT_SENDER = None # default = None # MAIL_MAX_EMAILS = None # default = None # MAIL_SUPPRESS_SEND = False # default = app.testing # MAIL_ASCII_ATTACHMENTS = False # default = False # # Detailed integration procedure with SendGrid is available at: # https://sendgrid.com/blog/sending-emails-from-python-flask-applications-with-twilio-sendgrid/ # -------------------------------------------------- # Web Editor Configuration # -------------------------------------------------- # The web editor can be limited to editing posts under # a limited set of parent directories by setting # WEB_EDITOR_PREFIXES to a list of supported path prefixes. # e.g. ['webposts', 'projects'] WEB_EDITOR_PREFIXES = ["webposts"] # --------------------------------------------------- # Tag configuration # --------------------------------------------------- # Posts with certain tags can be excluded from showing up # in the app. This can be useful for security purposes EXCLUDED_TAGS = ["private"] # ------------- # Collapse Code as Default Display Option # ------------- COLLAPSE_CODE_DEFAULT = False # ------------- # AWS related settings # ------------- S3_AWS_ACCESS_KEY_ID = "" S3_AWS_SECRET_ACCESS_KEY = "" S3_AWS_REGION_NAME = ""
from datetime import timedelta # --------------------------------------------------- # Host configuration # --------------------------------------------------- # The server name is used by Flask to limit access to the # served content to request to a particular domain. It # is also used by some authentication providers (in particular # OAuth providers) to advertise callback providers. If # not provided, it is assumed in these contexts to be # 'localhost:7000'. Be sure to specify this before deploying # into production. SERVER_NAME = "localhost:7001" # The knowledge repository uses the secret key to sign user # sessions. If not specified, a unique secret key will be # generated every time the server starts up. If hosting # in a multi-server environment, or you want sessions # to persist accross server restarts, set this to something # static. SECRET_KEY = None # Set DEPLOY_HTTPS to True if you want to enable encrypted # communication with Flask. When enabled, you must provide # your ssl certificate, which consists of a .crt and .key # file. # Note: Even if you set DEPLOY_HTTPS to True, you still need # to set the port to 443 manually. DEPLOY_HTTPS = False SSL_CERT = {"cert": "/path/to/cert", "key": "/path/to/key"} # --------------------------------------------------- # Debug configuration # --------------------------------------------------- DEBUG = False # --------------------------------------------------- # Database configuration # --------------------------------------------------- SQLALCHEMY_DATABASE_URI = ( "postgresql+psycopg2://knowledge_repo:password@localhost:5432/knowledge_repo" ) # If you are using a MySQL database, you must specify the URI as # demonstrated below. # SQLALCHEMY_DATABASE_URI = 'mysql://username:password@hostname/database' SQLALCHEMY_ECHO = False SQLALCHEMY_TRACK_MODIFICATIONS = False # Should the database tables be automatically created DB_AUTO_CREATE = True # Should the database be automatically migrated when updates exist # Note: This is True by default if this configuration is not applied, # under the assumption that if you are concerned about this file # you are probably interested in minimising risk to stability and handling # database upgrades manually. Manual database migrations can be # performed using `knowledge_repo --repo <> db_upgrade ...`. DB_AUTO_UPGRADE = False # --------------------------------------------------- # Authentication configuration # --------------------------------------------------- # Authentication providers allow users to sign into the Knowledge Repo # in a variety of different ways. You can create your own subclass of # `KnowledgeAuthProvider` and add either the instance or identifier # used for that class below. # By default, the knowledge repo offers: # ['debug', 'oauth2', 'bitbucket', 'github', 'google', 'ldap'] AUTH_PROVIDERS = ["google"] OAUTH_GOOGLE_CLIENT_ID = "<client id>" OAUTH_GOOGLE_CLIENT_SECRET = "<client secret>" # If you are going to use a OAuth provider, you will need to specify client ids # and private tokens. This can be done by instantiating instances of # `OAuth2Provider` and adding them to the above list, or by specifying OAuth # connection properties as demonstrated below for the GitHub authenticator. # OAUTH_GITHUB_CLIENT_ID = '<client id>' # OAUTH_GITHUB_CLIENT_SECRET = '<client secret>' # To configure a generic OAuth provider that is not one of the presets # provided, you may use the provider 'oauth2' which creates an empty, # unconfigured OAuth2Provider. You must then override its configuration. # For example, for a self-managed Gitlab CE instance at gitlab.example.com: # OAUTH_OAUTH2_BASE_URL = 'https://gitlab.example.com/api/v4/' # OAUTH_OAUTH2_AUTHORIZATION_URL = 'https://gitlab.example.com/oauth/authorize' # OAUTH_OAUTH2_TOKEN_URL = 'https://gitlab.example.com/oauth/token' # OAUTH_OAUTH2_AUTO_REFRESH_URL = 'https://gitlab.example.com/oauth/token' # OAUTH_OAUTH2_SCOPES = 'api' # OAUTH_OAUTH2_USER_INFO_ENDPOINT = 'user' # OAUTH_OAUTH2_USER_INFO_MAPPING = { # 'identifier': 'username', # 'name': 'name', # 'avatar_uri': 'avatar_url' # } # OAUTH_OAUTH2_VERIFY_SSL_CERTS = '/path/to/certs/my.ca-bundle' # OAUTH_OAUTH2_CLIENT_ID = '<client id>' # OAUTH_OAUTH2_CLIENT_SECRET = '<client secret>' # The configuration OAUTH_<name>_VERIFY_SSL_CERTS is what is passed to the # 'verify' parameter in the Requests module, and can be used to disable # HTTPS verification (not recommended) or provide a custom CA bundle. See: # http://docs.python-requests.org/en/master/user/advanced/#ssl-cert-verification # You may also override the .validate() method of a KnowledgeAuthProvider # to perform an additional validation step before authenticating a user. # The following example checks whether a user has access to the git remote # of the local Knowledge Repository: # def OAUTH_OAUTH2_VALIDATE(provider, user): # # if provider.app.repository.git_has_remote: # # url_parts = ( # provider.app.repository.git_remote.url.split(':') # ) # # url_subparts = url_parts[1].split('/') # # if url_parts[0] == "[email protected]": # git_project = ( # url_subparts[0] + "%2F" + url_subparts[1].split('.')[0]) # elif ( # url_parts[0] == "https" # and url_subparts[2] == "gitlab.example.com" # ): # git_project = ( # url_subparts[3] + "%2F" + url_subparts[4].split('.')[0]) # else: # provider.app.logger.warning( # "User validation failed: unexpected git remote url [" # + provider.app.repository.git_remote.url + "]") # return False # # user_validate_url = provider.base_url + "projects/" + git_project # # resp = provider.oauth_client.get( # user_validate_url, # verify=OAUTH_OAUTH2_VERIFY_HTTPS) # # if resp.status_code == 200: # return True # else: # provider.app.logger.warning( # "User validation failed: validation URL [" # + user_validate_url + "] returned HTTP status [" # + str(resp.status_code) + "]") # You can also forgo a fully-fledged sign in process for users by hosting the # knowledge repository behind a proxy server that pre-authenticates users, and # adds the appropriate user identifier to the http headers of the request. If # enabled below, then they take precedence over any other forms of # authentication. If the call to `AUTH_MAP_REQUEST_HEADERS` results in a null # user identifier, then the authentication flow will fall back to use any of # the providers specified above. AUTH_USE_REQUEST_HEADERS = False # If using headers to authenticate, the following function should be # implemented to transform a dictionary of headers into a dictionary of user # attributes. Currently only 'identifier', 'avatar_uri', 'name' and 'email' # are supported. If this method returns `None`, or `identifier` is not # supplied, then the authorization flow will fall back to other authentication # methods. def AUTH_MAP_REQUEST_HEADERS(headers): return { # 'identifier': None, # 'avatar_uri': None, # 'name': None, # 'email': None } # The following AUTH_USER_IDENTIFIER* configuration keys are deprecated and # will be removed in v0.9. AUTH_USER_IDENTIFIER_REQUEST_HEADER = None def AUTH_USER_IDENTIFIER_REQUEST_HEADER_MAPPING(identifier): return identifier # If the server desires to modify the attributes of the `User` object # associated with users logged in via any of the above authentication # providers, it can do so via this configuration key. This function will be # run once at user login (if using an `AuthenticationProvider`, and then at # most once during any caching lifetime period (as specified below). Note that # attributes collected via `AuthenticationProvider`s will not be updated # after initial login (user must relogin in order to reset those attributes). def AUTH_USER_ATTRIBUTE_SETTER(user): return user # The time to wait before re-checking user attributes with the above function # for users logged in via request headers. AUTH_USER_ATTRIBUTE_CACHE_LIFETIME = 24 * 60 * 60 # 1 day # Once a user is logged in via an authentication provider, they will remain # logged in via the use of cookies. By default, this cookie will last one year. # This is managed by `flask_login`, but is copied here for convenience. # For other options regarding sessions, please refer to: # https://flask-login.readthedocs.io/en/latest/#cookie-settings REMEMBER_COOKIE_DURATION = timedelta(days=365) # --------------------------------------------------- # LDAP configuration # --------------------------------------------------- # When using an LDAP server for user verification, you need to configure # the location of the server, and the directory structure used by your # organization. # Currently the port and protocol must both be included in the server address LDAP_SERVER = "ldap://127.0.0.1:389" # When entering this, note the "{0}" which denotes where the user_id # is inserted. LDAP_USERDN_SCHEMA = "cn={user_id},ou=people,dc=planetexpress,dc=com" # --------------------------------------------------- # Policy configuration # --------------------------------------------------- # This section configures various policy related to access control. # Should anonymous users be able to view the post indices POLICY_ANONYMOUS_VIEW_INDEX = True # Should anonymous users be able to view the content of posts POLICY_ANONYMOUS_VIEW_POST = True # Should anonymous users be able to view overall statistics POLICY_ANONYMOUS_VIEW_STATS = True # Should anonymous users be able to view tag pages POLICY_ANONYMOUS_VIEW_TAGS = True # Should anonymous users be able to download posts (or their source) POLICY_ANONYMOUS_DOWNLOADS = False # --------------------------------------------------- # Repository configuration # --------------------------------------------------- # You may specify a function `prepare_repo` which configures # the repository upon which this server is running. This # takes place after the repository has been instantiated # and before the server is able to serve requests. It is # possible to do anything to the repository, including # substituting the repository for another one. # By default, repositories manage their own configurations, # but this can be risky as they may run arbitrary python code, # which opens a vector for malicious users to compromise # the server. If you want to avoid this risk, pass # the '--safe' (TODO!) option to `knowledge_repo` config and # manually configure the repository here. # For example, if your server instance is sitting atop # a meta-repository, it may make sense to update the meta-repository # configuration with that of one of its children. def prepare_repo(repo): return repo # --------------------------------------------------- # Repository Indexing configuration # --------------------------------------------------- # The Knowedge Repo updates the index of available posts on a regular basis. # If the database is not thread-safe (i.e. in the case of SQLite), then the # index will be updated on the main thread before every request that is more # than `INDEX_INTERVAL` seconds after the last sync completed. Otherwise, # indexing will occur every `INDEX_INTERVAL` seconds after the previous sync. # Syncing is designed to be compatible with multiple instances of the Knowledge # Repo connected to the same database, accross multiple machines and/or # processes; and so a global indexing lock is employed. When a sync begins, # a sync lock is put in place and the responsible process is considered to be # the primary agent responsible for syncing until its last update is longer # than`INDEXING_TIMEOUT` seconds, whereby the lock is ceded to the next # requesting process. Note that `INDEXING_TIMEOUT` must be larger than # `INDEXING_INTERVAL` or strange things might begin to happen. INDEXING_INTERVAL = 5 * 60 # 5 minutes INDEXING_TIMEOUT = 10 * 60 # 10 minutes # Whether an index operation should update repositories INDEXING_UPDATES_REPOSITORIES = True # Whether repositories should be updated even without a sync lock (in which # case the repositories will be updated on the sync timers, even if the # relevant process/thread does not have a lock on updating the index). This is # useful in context of multiple Knowledge Repo servers working together to # serve the repositories across multiple machines, which each require # repository syncing. Disable this if (for some reason) you have multiple # Knowledge Repo servers running on the same machine, and you want to avoid # potential clashes. This key is ignored if `INDEXING_UPDATES_REPOSITORIES` is # False INDEXING_UPDATES_REPOSITORIES_WITHOUT_LOCK = True # In some cases you may want to disable indexing entirely, which is currently # only ever used by the Knowledge Post previewer. Disabling the index means # that posts will not be discoverable, but if know the path in the repository # you can view the post with a direct link. INDEXING_ENABLED = True # --------------------------------------------------- # Flask Mail Configuration # Refer to https://pythonhosted.org/flask-mail/ # Unless specified, upstream defaults are used as indicated # provided that MAIL_SERVER is defined. # --------------------------------------------------- # MAIL_SERVER = 'localhost' # default = 'localhost' # MAIL_PORT = 25 # default = 25 # MAIL_USE_TLS = False # default = False # MAIL_USE_SSL = False # default = False # MAIL_DEBUG = False # default = app.debug # MAIL_USERNAME = None # default = None # MAIL_PASSWORD = None # default = None # MAIL_DEFAULT_SENDER = None # default = None # MAIL_MAX_EMAILS = None # default = None # MAIL_SUPPRESS_SEND = False # default = app.testing # MAIL_ASCII_ATTACHMENTS = False # default = False # # Detailed integration procedure with SendGrid is available at: # https://sendgrid.com/blog/sending-emails-from-python-flask-applications-with-twilio-sendgrid/ # -------------------------------------------------- # Web Editor Configuration # -------------------------------------------------- # The web editor can be limited to editing posts under # a limited set of parent directories by setting # WEB_EDITOR_PREFIXES to a list of supported path prefixes. # e.g. ['webposts', 'projects'] WEB_EDITOR_PREFIXES = ["webposts"] # --------------------------------------------------- # Tag configuration # --------------------------------------------------- # Posts with certain tags can be excluded from showing up # in the app. This can be useful for security purposes EXCLUDED_TAGS = ["private"] # ------------- # Collapse Code as Default Display Option # ------------- COLLAPSE_CODE_DEFAULT = False # ------------- # AWS related settings # ------------- S3_AWS_ACCESS_KEY_ID = "" S3_AWS_SECRET_ACCESS_KEY = "" S3_AWS_REGION_NAME = "us-west-2"
mengting1010
b15c759e7d17b6a52c502b8bfdc174181da83789
451577868d66570a463260c67dff7034214beafd
this could cause security issue, you might need to remove it from your commit.
JJJ000
9
README.md exists but content is empty. Use the Edit dataset card button to edit it.
Downloads last month
53
Edit dataset card