def discussion_list(request, community_id):
    try:
        page = int(request.GET.get("page"))
        start = (page - 1) * 5
        community = CommunityCommunities.objects.get(id=community_id)
        discussions = CommunityDiscussions.objects.filter(community=community).order_by("-date_reply")[
            start : start + 5
        ]
        total = CommunityDiscussions.objects.filter(community=community).count()
        views_connect = view_counter_store()

        data = {"pager": get_pager(total, 5, page, 5), "discussions": list()}
        for discussion in discussions:
            views_object = views_connect.get_item("discussion", str(discussion.id))
            if views_object is None:
                views = 0
            else:
                views = views_object["views"]
            data["discussions"].append(
                {
                    "url": reverse("community_discussion_view", args=[discussion.id]),
                    "subject": discussion.subject,
                    "replies": CommunityDiscussionReplies.objects.filter(discussion=discussion).count(),
                    "views": views,
                    "date_create": "{dt:%b}. {dt.day}, {dt.year}".format(dt=discussion.date_create),
                    "first_name": discussion.user.first_name,
                    "last_name": discussion.user.last_name,
                    "avatar": reverse("user_photo", args=[discussion.user.id]),
                }
            )
    except Exception as e:
        data = {"Error": e}

    return HttpResponse(json.dumps(data), content_type="application/json")
def community(request, community_id):
    """
    Returns a single community page.
    :param request: Request object.
    :param community_id: The machine name of the community.
    :return: The Community page.
    """
    views_connect = view_counter_store()
    page = request.GET.get("page", "")
    if page.isdigit() and int(page) > 0:
        page = int(page)
    else:
        page = 1
    start = (page - 1) * 5

    community = CommunityCommunities.objects.get(id=community_id)
    facilitator = CommunityUsers.objects.select_related().filter(facilitator=True, community=community)
    users = CommunityUsers.objects.filter(community=community)
    discussions = CommunityDiscussions.objects.filter(community=community).order_by("-date_reply")[start : start + 5]
    total = CommunityDiscussions.objects.filter(community=community).count()
    # mems = CommunityUsers.objects.select_related().filter(user=request.user, community=community)
    resources = CommunityResources.objects.filter(community=community)
    courses = CommunityCourses.objects.filter(community=community)

    for d in discussions:
        d.replies = CommunityDiscussionReplies.objects.filter(discussion=d).count()
        views = views_connect.get_item("discussion", str(d.id))
        if views is None:
            d.views = 0
        else:
            d.views = views["views"]

    trending = get_trending(community_id)

    data = {
        "community": community,
        "facilitator": facilitator[0] if len(facilitator) else None,
        "discussions": discussions,
        "trending": trending,
        "users": users,
        "resources": resources,
        "courses": courses,
        # "mem": mems[0] if mems.count() else None,
        "pager": get_pager(total, 5, page, 5),
        "total_discussions": total,
    }

    return render_to_response("communities/community.html", data)