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_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 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 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)