from django.shortcuts import render

from . import reddit

# Create your views here.
def index(request, subreddit=""):
    if subreddit == "":
        subreddit = "all"

    if subreddit == "all" or "+" in subreddit:
        print_subreddit = True
    else:
        print_subreddit = False

    after = request.GET.get('after', None)
    nsfw = 'nsfw' in request.GET
    posts, last = list(reddit.get_posts(subreddit, after, nsfw))

    context = {
        'posts' : posts,
        'print_subreddit' : print_subreddit,
        'last' : last,
    }

    return render(request, 'index.html', context)

def about(request):
    return render(request, 'about.html', {})

from django.shortcuts import render
from django.http import JsonResponse

from . import reddit

# Create your views here.
def index(request, subreddit=""):
    if subreddit == "":
        subreddit = "all"

    if subreddit == "all" or "+" in subreddit:
        print_subreddit = True
    else:
        print_subreddit = False

    after = request.GET.get('after', None)
    # default set is html (None)
    content_type = request.GET.get('content_type', None)
    nsfw = 'nsfw' in request.GET
    posts, last = list(reddit.get_posts(subreddit, after, nsfw))

    context = {
        'posts' : posts,
        'print_subreddit' : print_subreddit,
        'last' : last,
    }

    if content_type == 'json':
        # make Subreddit and Reddiator serilazible
        for post in posts:
            post['subreddit'] = str(post['subreddit'])
            post['author'] = str(post['author'])
        return JsonResponse(context)

    # normal http response
    return render(request, 'index.html', context)

def about(request):
    return render(request, 'about.html', {})

add json support