from django.shortcuts import render, redirect
from django.http import HttpResponse
from .models import Article
from django.contrib.auth.decorators import login_required
from . import forms

def Articles(request):
    articles = Article.objects.all().order_by('date')
    return render(request, 'articles/article_list.html', {'articles': articles})


def article_detail(request, slug):
    # return  HttpResponse(slug)
    article = Article.objects.get(slug=slug)
    return render(request, 'articles/article_details.html', {'article': article})


@login_required(login_url="/accounts/login")
def article_create(request):
    if request.method == 'POST':
        form = forms.CreateArticle(request.POST, request.FILES)
        if form.is_valid():
            #save article to DB
            instance = form.save(commit=False)
            instance.author = request.user
            instance.save( )
            return redirect ('articles:list')
    else:
        form = forms.CreateArticle()
    return render(request, 'articles/article_create.html', {'form':form})

[((18, 1, 18, 44), 'django.contrib.auth.decorators.login_required', 'login_required', (), '', False, 'from django.contrib.auth.decorators import login_required\n'), ((9, 11, 9, 80), 'django.shortcuts.render', 'render', ({(9, 18, 9, 25): 'request', (9, 27, 9, 55): '"""articles/article_list.html"""', (9, 57, 9, 79): "{'articles': articles}"}, {}), "(request, 'articles/article_list.html', {'articles': articles})", False, 'from django.shortcuts import render, redirect\n'), ((15, 11, 15, 81), 'django.shortcuts.render', 'render', ({(15, 18, 15, 25): 'request', (15, 27, 15, 58): '"""articles/article_details.html"""', (15, 60, 15, 80): "{'article': article}"}, {}), "(request, 'articles/article_details.html', {'article': article})", False, 'from django.shortcuts import render, redirect\n'), ((30, 11, 30, 73), 'django.shortcuts.render', 'render', ({(30, 18, 30, 25): 'request', (30, 27, 30, 57): '"""articles/article_create.html"""', (30, 59, 30, 72): "{'form': form}"}, {}), "(request, 'articles/article_create.html', {'form': form})", False, 'from django.shortcuts import render, redirect\n'), ((27, 19, 27, 45), 'django.shortcuts.redirect', 'redirect', ({(27, 29, 27, 44): '"""articles:list"""'}, {}), "('articles:list')", False, 'from django.shortcuts import render, redirect\n')]