Example #1
0
 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)
Example #2
0
 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)
Example #3
0
 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()]
     })
Example #4
0
 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))
Example #5
0
 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
     ))
Example #6
0
 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)
Example #7
0
 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()
Example #8
0
 def all_comments():
     return api_jsonify({
         'comments': {
             'list': [
                 x.to_dict() for x in Comment.query.order_by(
                     Comment.id.desc()).paginate().items
             ]
         }
     })
Example #9
0
 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})
Example #10
0
 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)]
         }
     })
Example #11
0
 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()
         ]
     })
Example #12
0
 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
     })
Example #13
0
 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)
Example #14
0
 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)
Example #15
0
 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())
Example #16
0
 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})
Example #17
0
    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)
Example #18
0
 def put():
     option.set_option(Config.captcha, request.json['enabled'])
     return api_jsonify()
Example #19
0
 def put():
     if 'text' in request.json:
         AboutPage.query.one().text = request.json['text']
         db.session.commit()
     return api_jsonify()
Example #20
0
 def get():
     page: AboutPage = AboutPage.query.one()
     return api_jsonify({
         'text': page.text,
         'comment_loc': page.comment_loc()
     })
Example #21
0
 def get():
     return api_jsonify(payload=BlogMeta.load_from_options().to_dict())
Example #22
0
 def get_comment_by_id(loc, comment_id):
     return api_jsonify(payload=Comment.query.filter_by(
         id=comment_id).first_or_404().to_dict())
Example #23
0
 def toggle_comments_in_loc(loc):
     CommentToggle.set_state(loc, request.json.get('enabled', False))
     db.session.commit()
     return api_jsonify()