Szafran
Szafran

Reputation: 51

Two views with only one diffrent line of code

I have two views with only one diffrent line of code, rest is same in both views. How to prevent repetition of code? Can i delete tag_view and add optional parameter as kwarg to index and depending on it return view without specific tag or with tag? Is it how it should work? My views below.

def index(request):
    post_form = AddPostForm(request.POST or None, instance=request.user)
    comment_form = AddCommentForm(request.POST or None, instance=request.user)
    if request.method == 'POST':
        # FORMULARZ DODAWANIA POSTU
        if post_form.is_valid():
            post_form.save()
            create_notifications(post_form.instance)

        # FORMULARZ DODAWANIA KOMENTARZA
        if comment_form.is_valid():
            comment_form.save()

    (posts, comments) = serve_post_and_comments_except_blocked(request)

    return render(request, 'mikroblog/index.html', {'posts': posts, 'comments': comments,
                                                    'post_form': post_form, 'comment_form': comment_form})


def tag_view(request, tag):
    post_form = AddPostForm(request.POST or None, instance=request.user)
    comment_form = AddCommentForm(request.POST or None, instance=request.user)
    if request.method == 'POST':
        # FORMULARZ DODAWANIA POSTU
        if post_form.is_valid():
            post_form.save()
            create_notifications(post_form.instance)

        # FORMULARZ DODAWANIA KOMENTARZA
        if comment_form.is_valid():
            comment_form.save()

    (posts, comments) = serve_post_and_comments_except_blocked(request)
    posts.filter(content_post__contains=tag)
    actual_tag = tag

    return render(request, 'mikroblog/tag.html', {'posts': posts, 'actual_tag': actual_tag, 'comments': comments,
                                                  'post_form': post_form, 'comment_form': comment_form})

Upvotes: 0

Views: 45

Answers (1)

Glx
Glx

Reputation: 104

I'd define an helper function like this (not tested):

def view_common(url, request, tag=None):
    post_form = AddPostForm(request.POST or None, instance=request.user)
    comment_form = AddCommentForm(request.POST or None, instance=request.user)
    if request.method == 'POST':
        # FORMULARZ DODAWANIA POSTU
        if post_form.is_valid():
            post_form.save()
            create_notifications(post_form.instance)

        # FORMULARZ DODAWANIA KOMENTARZA
        if comment_form.is_valid():
            comment_form.save()

    (posts, comments) = serve_post_and_comments_except_blocked(request)
    posts.filter(content_post__contains=tag)

    parameters = {'posts': posts, 'comments': comments,'post_form': post_form, 'comment_form': comment_form}

    if tag:
        parameters['actual_tag'] = tag

    return render(request, url, parameters)

and then redefine your 2 functions like this:

def tag_view(request, tag):
    return view_common('mikroblog/tag.html', request, tag=tag)

def index(request):
    return view_common('mikroblog/index.html', request)

Hope this helps.

Upvotes: 1

Related Questions