# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from blog.models import Post,Tag
from django.shortcuts import render

# Create your views here.
from django.views import View
from django.core.paginator import Paginator
import math

class BlogView(View):
    #渲染主页面
    def get(self,request,num=1):
        num = int(num)
        #查询帖子内容
        postlist = Post.objects.all()
        #分页
        #创建分页对象
        pageobj = Paginator(postlist,1)
        #获取每页数据
        per_page_list = pageobj.page(num)
        #获取页码数(begin==当前页)
        begin = num - math.ceil(10/2)
        if begin > 1:
            begin = 1
        end = begin + 9

        if end > pageobj.num_pages:
            end = pageobj.num_pages
        if end < 10:
            begin = 1
        else:
            begin = end - 9
        page_list = range(begin,end+1)
        return render(request,'index.html',{'postlist':per_page_list,'page_list':page_list})
#帖子全文详情
class DetailView(View):
    def get(self,request,category_id):
        #由路由得到的参数都是字符串类型
        category_id = int(category_id)
        #获取贴吧对象列表
        postobj = Post.objects.filter(category_id=category_id)
        return render(request,'detailpost.html',{'postobj':postobj})
#帖子列表（通过分类）
class PostList(View):
    def get(self,request,category_id):
        #由路由得到的参数都是字符串类型
        category_id = int(category_id)
        # 获取贴吧对象列表
        postobj = Post.objects.filter(category_id=category_id)
        return render(request,'listpost.html',{'postobj':postobj})


class PostArchive(View):
    def get(self,request,year,month):
        #获取传递的参数并转换类型
        year = int(year)
        month = int(month)

        # if year == 1 and month == 1:
        # postobj = Post.objects.all().order_by('-publish_time')
        # else:
        postobj = Post.objects.filter(publish_time__year=year, publish_time__month=month)

        return render(request,'listpost.html',{'postobj':postobj})