def yearview(request, year):
    posts, pagedata = init()
    tot_posts = posts
    posts = posts.filter(publish__year=year)
    pagedata.update({'post_list': posts,
                     'subtitle': 'Posts for %s' % year})
    today = timezone.now().date()
    context = {
        "object_list": posts,
        "tot_object_list": tot_posts,
        "title": "List",
        "today": today,
        "aggr_data": pagedata,
    }

    return render(request, "post_list.html", context)

def monthview(request, year, month):
    posts, pagedata = init()
    tot_posts = posts
    posts = posts.filter(publish__year=year)
    posts = posts.filter(publish__month=int(month))
    pagedata.update({'post_list': posts,
                     'subtitle': 'Posts for %s %s' % (MONTH_NAMES[int(month)], year),})
    today = timezone.now().date()
    context = {
        "object_list": posts,
        "tot_object_list": tot_posts,
        "title": "List",
        # "page_request_var": page_request_var,
        "today": today,
        "aggr_data": pagedata,
    }
    return render(request, "post_list.html", context)

def tagview(request, tag):
    allposts, pagedata = init()
    posts = []
    for post in allposts:
        tags = re.split(",", post.tags.replace(" ",""))
        if tag in tags:
            posts.append(post)
    pagedata.update({'post_list': posts,
                     'subtitle': "Posts tagged '%s'" % tag,})
    today = timezone.now().date()
    context = {
        "object_list": posts,
        "tot_object_list": allposts,
        "title": "List",
        # "page_request_var": page_request_var,
        "today": today,
        "aggr_data": pagedata,
    }
    return render(request, "post_list.html", context)

def userview(request, slug=None):
    posts, pagedata = init()
    if slug:
        profile = get_object_or_404(Profile, slug=slug)
        posts = Post.objects.all().filter(user=profile.user)
    else:
        if request.user.is_authenticated:
            posts = posts.filter(user_id=request.user)

    tot_posts = posts.only('title')
    #pagedata.update({'post_list': posts,
    #                 'subtitle': 'Posts for %s' %subctg})
    #today = timezone.now().date()
    context = {
        "object_list": posts,
        "tot_object_list": tot_posts,
        "aggr_data": pagedata,
    }

    return render(request, "post_list.html", context)