Пример #1
0
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)
Пример #2
0
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)