from django.shortcuts import render, redirect
from .models import Post
from .forms import ScheduleForm
from django.core.paginator import Paginator

# Create your views here.
def view_schedule(request):
    all_posts = Post.objects.all().order_by('pub_date')
    page = int(request.GET.get('p', 1))
    pagenator = Paginator(all_posts, 5)
    posts = pagenator.get_page(page)
    return render(request, 'schedule/view_schedule.html', {'posts': posts})

def write_schedule(request):
    if request.method == "POST":
        form = ScheduleForm(request.POST)

        if form.is_valid():
            # form의 모든 validators 호출 유효성 검증 수행
            # user_id = request.session.get('user')
            # user = User.objects.get(pk=user_id)

            schedule = Post()
            schedule.title = form.cleaned_data['title']
            # # 검증에 성공한 값들은 사전타입으로 제공 (form.cleaned_data)
            # # 검증에 실패시 form.error 에 오류 정보를 저장
            schedule.username = form.cleaned_data['username']
            schedule.pub_date = form.cleaned_data['pub_date']
            schedule.save()

            return redirect('schedule:view_schedule')

    else:
        form = ScheduleForm()

    return render(request, 'schedule/write_schedule.html', {'form': form})

def delete(request, posts_id):
    post = Post.objects.get(id=posts_id)
    post.delete()
    posts = Post.objects.all().order_by('-id')
    return render(request, 'schedule/view_schedule.html', {'posts': posts})
[((10, 16, 10, 39), 'django.core.paginator.Paginator', 'Paginator', ({(10, 26, 10, 35): 'all_posts', (10, 37, 10, 38): '5'}, {}), '(all_posts, 5)', False, 'from django.core.paginator import Paginator\n'), ((12, 11, 12, 75), 'django.shortcuts.render', 'render', ({(12, 18, 12, 25): 'request', (12, 27, 12, 56): '"""schedule/view_schedule.html"""', (12, 58, 12, 74): "{'posts': posts}"}, {}), "(request, 'schedule/view_schedule.html', {'posts': posts})", False, 'from django.shortcuts import render, redirect\n'), ((36, 11, 36, 74), 'django.shortcuts.render', 'render', ({(36, 18, 36, 25): 'request', (36, 27, 36, 57): '"""schedule/write_schedule.html"""', (36, 59, 36, 73): "{'form': form}"}, {}), "(request, 'schedule/write_schedule.html', {'form': form})", False, 'from django.shortcuts import render, redirect\n'), ((42, 11, 42, 75), 'django.shortcuts.render', 'render', ({(42, 18, 42, 25): 'request', (42, 27, 42, 56): '"""schedule/view_schedule.html"""', (42, 58, 42, 74): "{'posts': posts}"}, {}), "(request, 'schedule/view_schedule.html', {'posts': posts})", False, 'from django.shortcuts import render, redirect\n'), ((31, 19, 31, 53), 'django.shortcuts.redirect', 'redirect', ({(31, 28, 31, 52): '"""schedule:view_schedule"""'}, {}), "('schedule:view_schedule')", False, 'from django.shortcuts import render, redirect\n')]