def get(idx: int): post: Post = Post.query.filter_by(id=idx).one_or_none() if post is None: abort(404) ret = post.to_dict() ret.update({'comment_loc': post.comment_loc()}) return api_jsonify(ret)
def remove_comment_by_id(loc, comment_id): try: db.session.delete(Comment.query.filter_by(id=comment_id).one()) db.session.commit() return api_jsonify() except NoResultFound: abort(404)
def search(): if 'q' not in request.args: query_str = '' else: query_str = request.args['q'] return api_jsonify({ 'tags': [x.name for x in Tag.query.filter(Tag.name.contains(query_str)).all()] })
def get(category: str): model_category = None try: model_category = Category.query.filter_by(name=category).one() except NoResultFound: abort(404) return api_jsonify( dict((str(x.id), x.name) for x in model_category.posts))
def get(tag: str): try: model_tag = Tag.query.filter_by(name=tag).one() except NoResultFound: abort(404) return api_jsonify(dict( (str(x.id), x.name) for x in model_tag.posts ))
def set_spam(loc, comment_id): try: comment: Comment = Comment.query.filter_by(id=comment_id).one() comment.is_spam = request.json['isSpam'] db.session.commit() return api_jsonify() except NoResultFound: abort(404)
def put(): meta_dict = BlogMeta.load_from_options().to_dict() meta_dict.update(request.json) log.debug(meta_dict) print(meta_dict) BlogMeta.from_dict(meta_dict).write_to_options() db.session.commit() return api_jsonify()
def all_comments(): return api_jsonify({ 'comments': { 'list': [ x.to_dict() for x in Comment.query.order_by( Comment.id.desc()).paginate().items ] } })
def put(idx: int): req = request.json post = Post.query.filter_by(id=idx).one_or_none() if post is None: abort(404) post.update_from_dict(req) db.session.add(post) db.session.commit() return api_jsonify({'id': idx})
def get_comments_in_loc(loc): state = CommentToggle.get_state(loc) if state is None: abort(404) return api_jsonify({ 'comments': { 'enabled': state, 'list': [x.to_dict() for x in Comment.query.filter_by(loc=loc)] } })
def search(): if 'q' not in request.args: query_str = '' else: query_str = request.args['q'] return api_jsonify({ 'categories': [ x.name for x in Category.query.filter( Category.name.contains(query_str)).all() ] })
def post(): req = request.json new_post = None try: new_post = Post.from_dict(req) new_post.updated = datetime.utcnow() except NoResultFound as e: abort(400, str(e)) db.session.add(new_post) db.session.commit() return api_jsonify({ 'id': new_post.id })
def get(): if not option.get_option(Config.captcha): abort(404) answer = ''.join([choice(alphabet) for i in range(4)]) captcha = ImageCaptcha().generate_image(answer) buf = BytesIO() captcha.save(buf, format='GIF') b64_payload = b64encode(buf.getvalue()) payload = { 'image': 'data:image/gif;base64,' + b64_payload.decode('ascii'), 'answer_enc': jwt_encode(interval=timedelta(minutes=5), data={'answer': answer}) } return api_jsonify(payload)
def search(): if 'q' not in request.args: query_str = '' else: query_str = request.args['q'] rows = (Post.query .filter(Post.name.contains(query_str) | Post.text.contains(query_str)) .order_by(Post.id.desc()).paginate().items) post_list = [(str(x.id), x.to_excerpt_dict()) for x in rows] post_list.sort(key=lambda x: int(x[0])) post_dict = {} for x in post_list: post_dict[x[0]] = x[1] return api_jsonify(post_dict)
def post_comment_in_loc(loc): state = CommentToggle.get_state(loc) if state is None: abort(404) elif not state: abort(403, 'comments are disabled') new_comment = Comment(username=request.json['username'], email=request.json.get('email', ''), text=request.json['text'], loc=loc) db.session.add(new_comment) # TODO: Akismet db.session.commit() return api_jsonify(new_comment.to_dict())
def delete(idx: int): post: Post = Post.query.filter_by(id=idx).one_or_none() if post is None: abort(404) # also delete the related category & tags; store a copy first category = copy(post.category) tags = copy(post.tags) loc = post.comment_loc() db.session.delete(post) db.session.flush() if not category.posts: db.session.delete(category) for tag in tags: if not tag.posts: db.session.delete(tag) Comment.query.filter_by(loc=loc).delete() db.session.commit() return api_jsonify({'id': idx})
def post(): try: option.get_option(Site.title) abort(410) except NotInitializedError as e: # database not set up properly, ok to do OOBE # set up database from models.AboutPage import AboutPage # add options option.set_option(Config.username, request.json['username']) option.set_option(Config.passwordHash, generate_password_hash(request.json['password'])) option.set_option(Config.captcha, request.json['captcha']) BlogMeta.from_dict(request.json['blogMeta']).write_to_options() # noinspection PyArgumentList db.session.add(AboutPage(text=request.json['aboutPage']['text'])) db.session.commit() return api_jsonify(payload=None)
def put(): option.set_option(Config.captcha, request.json['enabled']) return api_jsonify()
def put(): if 'text' in request.json: AboutPage.query.one().text = request.json['text'] db.session.commit() return api_jsonify()
def get(): page: AboutPage = AboutPage.query.one() return api_jsonify({ 'text': page.text, 'comment_loc': page.comment_loc() })
def get(): return api_jsonify(payload=BlogMeta.load_from_options().to_dict())
def get_comment_by_id(loc, comment_id): return api_jsonify(payload=Comment.query.filter_by( id=comment_id).first_or_404().to_dict())
def toggle_comments_in_loc(loc): CommentToggle.set_state(loc, request.json.get('enabled', False)) db.session.commit() return api_jsonify()