def cleaner(): logger.info('Start cleaner!') published_posts = Post.select().where(Post.published_at != None) for post in published_posts: os.remove(post.file_path) logger.info(f'File {post.file_path} was removed') Post.update(file_deleted=True).where(Post.id == post.id).execute()
def create_post(post_in: PostIn): author = get_user(user_name=post_in.author) if author: post = Post(author=post_in.author, title=post_in.title, body=post_in.body) try: post.save() return post except: return False
def post_exists(url): exists = Post.select().where(Post.url == url).exists() if exists: logger.info(f'Post exists: {url}') else: logger.info(f'Found new post: {url}') return exists
def delete_comments_by_post(post_id: str): post = Post.objects(pk=post_id) if post: comments = Comment.objects(post_id=post_id) for comment in comments: comment.delete() return True return False
def publish(): logger.info('Start to publish') bot = init_bot() unpublished_posts = Post.select().where( Post.published_at == None).order_by(Post.created_at.desc()).limit(1) for post in unpublished_posts: logger.info(f'Publish {post.title}') msg = prepare_message(post) bot.send_message(chat_id=settings.tg_chat_id, text=msg, parse_mode='Markdown') bot.send_audio(chat_id=settings.tg_chat_id, audio=open(post.file_path, 'rb'), timeout=300, title=post.title) Post.update(published_at=datetime.now(timezone.utc)).where( Post.id == post.id).execute() logger.info(f'Post {post.title} successfully published!')
def submit_post(): req_data = get_post_data_from_req(request) post = Post.submit_post(current_user, req_data.get("text"), req_data.get("title")) if isinstance(post, str): return transitify({"error": post, "error_type": "create-post"}) post_id = req_data.get('parent', Post.root_post_id()) relation = Relation.link_posts(post_id, post, current_user) if isinstance(relation, str): return transitify({"error": relation, "error_type": "link-posts"}) app_state = {"success": "posted successfully"} if req_data.get('current_post') and req_data.get('ask_for'): page = req_data.get( "page", math.ceil(float(total_actions(post_id)) / float(ACTIONS_PER_PAGE))) app_state.update( handle_asks(req_data.get('current_post'), req_data.get('ask_for'), page)) return transitify(app_state)
def submit_post(): req_data = get_post_data_from_req(request) post = Post.submit_post(current_user, req_data.get("text"), req_data.get("title")) if isinstance(post, basestring): return transitify({"error": post, "error_type": "create-post"}) post_id = req_data.get('parent', Post.root_post_id()) relation = Relation.link_posts(post_id, post, current_user) if isinstance(relation, basestring): return transitify({"error": relation, "error_type": "link-posts"}) app_state = {"success": "posted successfully"} if req_data.get('current_post') and req_data.get('ask_for'): page = req_data.get("page", math.ceil(float(total_actions(post_id)) / float(ACTIONS_PER_PAGE))) app_state.update(handle_asks(req_data.get('current_post'), req_data.get('ask_for'), page)) return transitify(app_state)
def create_or_get_post(user_id, title, text): session = Session() post = session.query(Post).filter_by(title=title).first() if not post: post = Post(title=title, text=text, user_id=user_id) session.add(post) session.commit() post_id = post.id session.close() return post_id
def get_post_id_from_text(s): if is_number(s): return int(s) rt = route_from(s, method="GET") if rt[0] == 'views.render_post': return rt[1]['post_id'] elif rt[0] == 'views.post_page': try: return Post.query.filter(Post.url==rt[1]['post_url']) \ .with_entities(Post.id).one()[0] except NoResultFound: return None elif rt[0] == 'views.index': return Post.root_post_id()
def get_post_id_from_text(s): if is_number(s): return int(s) rt = route_from(s, method="GET") if rt[0] == 'views.render_post': return rt[1]['post_id'] elif rt[0] == 'views.post_page': try: return Post.query.filter(Post.url==rt[1]['post_url']) \ .with_entities(Post.id).one()[0] except NoResultFound: return None elif rt[0] == 'views.index': return Post.root_post_id()
def index(): app_state = {"user": writable_current_user()} return render_post(Post.root_post_id())
def index(): app_state = {"user": writable_current_user()} return render_post(Post.root_post_id())
def save_post(self, model: models.Post): Post.create(**model.dict())
def delete_post(post_id: str): post = Post.objects(pk=post_id).first() if post: post.delete() return True return False
def get_post(post_id): try: post = Post.objects(pk=post_id).first() return post except: return False
def get_posts_by_author(author: str): if User.objects(user_name=author).first(): posts = Post.objects(author=author) return posts return False