def searchsite(): query_string = request.args.get('query', '') try: results = query_search_index(query_string) posts_ids = find_posts_from_index(results) posts = Posts() posts.filter_matched(posts_ids) data = posts.to_json() except Exception as e: logging.error("error while searching {}".format(e)) data = "something went wrong while searching" return jsonify(data=data)
def main(): if users.is_current_user_admin(): if request.method == 'GET': #all entitites posts = Posts() return jsonify(posts.to_json()) elif request.method == "POST": form = PostForm() if form.validate_on_submit(): #new entity posts = Posts() categories = Categories() tags = Tags() raw_post = request.get_json() raw_category = raw_post["category"] editing_tags = raw_post["tags"] raw_summary = raw_post["summary"] tag_keys = tags.update(editing_tags) category_key = categories.update(raw_category) post_id = posts.add(raw_title=raw_post["title"], raw_body=raw_post["body"], category_key=category_key, tags_ids=tag_keys, summary=raw_summary, answers=raw_post["answers"]).id() post = BlogPost.get(post_id) if "images" in raw_post.keys() and raw_post["images"]: for img in raw_post["images"]: image_base64 = img["url"].split("base64,")[-1] mime_type = img["url"].split("base64,")[0].replace( 'data:', '').replace(';', '') image_filename = img["filename"].split("\\")[-1] if allowed_file(image_filename): image_filename = secure_filename(image_filename) post.add_blob(base64.b64decode(image_base64), image_filename, mime_type) return jsonify(post.to_json()) # Needs check else: return jsonify(msg="missing token") else: return jsonify({})
def main(): if users.is_current_user_admin(): if request.method=='GET': #all entitites posts = Posts() return jsonify(posts.to_json()) elif request.method == "POST": form = PostForm() if form.validate_on_submit(): #new entity posts = Posts() categories = Categories() tags = Tags() raw_post = request.get_json() raw_category = raw_post["category"] editing_tags = raw_post["tags"] raw_summary = raw_post["summary"] tag_keys = tags.update(editing_tags) category_key = categories.update(raw_category) post_id = posts.add(raw_title=raw_post["title"], raw_body=raw_post["body"], category_key=category_key, tags_ids=tag_keys, summary=raw_summary, answers=raw_post["answers"]).id() post = BlogPost.get(post_id) return jsonify(post.to_json()) # Needs check else: return jsonify(msg="missing token") else: return jsonify({})