def post_new(request): # get -> form # post -> save form = PostModelForm() new_post = None msg = Msg(MSG_TYPE_WARNING, BLOG_WARNING_OOOPS, None) service = PostService() if request.method == 'POST': try: new_post, form = post_form(request, service) if not form.is_valid(): msg.msg = BLOG_POST_INVALID else: new_post = service.save(new_post) request.session['msg'] = { 'type': MSG_TYPE_SUCCESS, 'title': BLOG_SUCCESS_WELL_DONE, 'msg': BLOG_POST_SAVED } return redirect('blog:post_edit', id=new_post.id) except BusinessException as be: # invalid post # form.is_valid() msg.msg = '%s' % be context_response = { RESPONSE_FORM: msg, RESPONSE_FORM: form, RESPONSE_POST: new_post, } return render(request, blog_themed_view(VIEW_TPL_POST_NEW), context_response)
def post(self): """ Creer un nouveau post """ user_id = get_jwt_identity() post = PostService.create_post(request.form["title"], request.form["body"], user_id) if post is not None: return redirect(f'/posts/{post.id}') else: return str(), 400
def get(self): """ Obtenir tous les posts """ posts = PostService.get_all_post() post_schema = PostSchema(exclude=["comments"]) return render_template('posts.html', title="Articles", posts=post_schema.dump(posts, many=True), current_connected_user=connected_user( get_jwt_identity()))
def get(self): """ Page d'accueil """ posts = PostService.get_all_post() post_schema = PostSchema(exclude=["comments"]) return render_template('home.html', title="Accueil", posts=post_schema.dump(posts, many=True), current_connected_user=connected_user( get_jwt_identity()))
def delete(self, post_id): """ Supprimer un post """ user_id = get_jwt_identity() try: post = PostService.delete_post(post_id, user_id) return "OK", 200 except ResourceNotFound as e: return str(), 204 except UnauthorizedUser as e: return str(), 401
def post_edit(request, id): # get -> form # post -> save form = PostModelForm() edit_post = None msg = Msg(MSG_TYPE_WARNING, BLOG_WARNING_OOOPS, None) if RESPONSE_MSG in request.session: msg = Msg(**request.session[RESPONSE_MSG]) del request.session[RESPONSE_MSG] service = PostService() if request.method == 'POST': try: edit_post, form = post_form(request, service, id=id) if not form.is_valid(): msg.msg = BLOG_POST_INVALID else: edit_post = service.save(edit_post) msg = Msg(MSG_TYPE_SUCCESS, BLOG_SUCCESS_WELL_DONE, BLOG_POST_SAVED) except BusinessException as be: # invalid post # not form.is_valid() msg.msg = '%s' % be elif request.method == 'GET': try: edit_post, form = post_form(request, service, id=id) except BusinessException as be: msg = warning_msg_from_business_exception(be) context_response = { RESPONSE_MSG: msg, RESPONSE_FORM: form, RESPONSE_POST: edit_post, } return render(request, blog_themed_view(VIEW_TPL_POST_EDIT), context_response)
def get(self, post_id): """ Obtenir un post à partir de son id. """ try: post = PostService.get_post_by_id(post_id) post_schema = PostSchema() return render_template('post.html', title=post.title, post=post_schema.dump(post), current_connected_user=connected_user( get_jwt_identity())) except ResourceNotFound as e: return render_template('404.html', title="Article non trouvé", current_connected_user=connected_user( get_jwt_identity())), 404
def put(self, post_id): """ Mettre à jour un post """ user_id = get_jwt_identity() try: content = request.json if content is None: return str(), 400 new_title = content['title'] new_body = content['body'] post = PostService.update_post_by_id(new_title, new_body, post_id, user_id) post_schema = PostSchema() return jsonify(post_schema.dump(post)) except ResourceNotFound as e: return str(), 404 except UnauthorizedUser as e: return str(), 401
def post_view(request, slug): msg = None post = None service = PostService() comments = list() if request.method == 'GET': try: post = service.get_by_slug(slug) except BusinessException as be: msg = warning_msg_from_business_exception(be) except Exception as e: msg = danger_msg(e) else: post_id = request.POST[FORM_POST_ID] comment_form = CommentModelForm(request.POST) try: comment = comment_form.save(commit=False) service.publish_comment(post_id, comment) msg = Msg(MSG_TYPE_SUCCESS, BLOG_COMMENT_PUBLISHED, '') post = service.get_by_id(int(post_id)) except BusinessException as be: msg = warning_msg_from_business_exception(be) except Exception as e: msg = danger_msg(e) try: if post not in (None,): comments = service.list_post_comments(post.id) except Exception: comments = None comment_form = CommentModelForm() context_response = { RESPONSE_MSG: msg, RESPONSE_POST: post, RESPONSE_COMMENTS: comments, RESPONSE_COMMENT_FORM: comment_form } return render(request, blog_themed_view(VIEW_TPL_POST_VIEW), context_response)
def grpc_handlers(server): post_pb2_grpc.add_PostControllerServicer_to_server( PostService.as_servicer(), server)
def post_listing(request, by=None, what=None, title=None, sub_title=None, template=None, url_listing=None): """ Listing posts by criteria :param request: :param by: home|search|author|date :param what: criteria for listing :param title: title of results page :param sub_title: sub-title || :param template: template for listing results :return: response with rendered template with(out) results defined by criteria """ q = None msg = None posts = [] paginator = None listing_title = title listing_sub_title = sub_title if url_listing in (None,): url_listing = '/' service = PostService() page = 1 if RESPONSE_PAGE in request.GET: page = int(request.GET[RESPONSE_PAGE]) try: if by == 'home': paginator, posts = service.list_all_published(page=page) url_listing = reverse('blog:home') elif by == 'date': paginator, posts = service.list_all_published_by_date(date=what, page=page) url_listing = reverse('blog:post_list_by_date', kwargs={'year': what.year, 'month': what.month, 'day': what.day}) elif by == 'author': paginator, posts = service.list_all_published_by_author(author=what, page=page) url_listing = reverse('blog:post_list_by_author', kwargs={'author': what}) elif by == 'search': q = what paginator, posts = service.search_by_author_or_title(q=q, page=page) url_listing = reverse('blog:home') if len(posts) == 0: raise BusinessException(BLOG_POSTS_NOT_FOUND_BY_AUTHOR_OR_TITLE_S_ % q, '') elif by == 'my-posts': paginator, posts = service.list_all_author_posts(author=what, page=page) url_listing = reverse('blog:my_posts') if len(posts) == 0: raise BusinessException(BLOG_POSTS_NOT_FOUND_WITH_AUTHOR_USERNAME_S_ % what, '') except BusinessException as be: msg = warning_msg_from_business_exception(be) except Exception as e: msg = danger_msg(e) # TODO: redir to 500 error page if paginator not in (None,) and paginator.num_pages > 1: listing_sub_title = BLOG_PAGE_X_OF_Y % (page, paginator.num_pages) context_response = { RESPONSE_LISTING_SUB_TITLE: listing_sub_title, RESPONSE_LISTING_TITLE: listing_title, RESPONSE_URL_LISTING: url_listing, RESPONSE_PAGINATOR: paginator, RESPONSE_POSTS: posts, RESPONSE_PAGE: page, RESPONSE_WHAT: what, RESPONSE_MSG: msg, RESPONSE_BY_: by, RESPONSE_Q: q } return render(request, blog_themed_view(template), context_response)