from django.shortcuts import render
from .models import Article
from .models import Category, Banner, Article, Tag, Link
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

# Create your views here.


from django.http import HttpResponse

def hello(request):
    sitename = "zhdya's django home!"
    url = "zhdya.okay686.cn"

    myinfo = {
        'name': 'zhdya',
        'sex': 'man',
        'hobies': 'talk is cheap, show me the code!!',
        'address': 'JSPX'
    }

    content = {
        'myinfo':myinfo
    }
    return render(request, 'index.html', content)


def global_variable(request):
    allcategory = Category.objects.all()
    remen = Article.objects.filter(tui__id=1)[:4]
    tags = Tag.objects.all()
    return locals()


def index(request):
    ##轮播图
    banner = Banner.objects.filter(is_active=True)[0:4] #查询所有幻灯图数据，并进行切片
    tui = Article.objects.filter(tui__id=1)[:3]  # 查询推荐位ID为1的文章
    allarticle = Article.objects.all().order_by('-id')[0:3]
    hot = Article.objects.all().order_by('?')[:3]  # 随机推荐
    # hot = Article.objects.filter(tui__id=3)[:3]   #通过推荐进行查询，以推荐ID是3为例
    # hot = Article.objects.all().order_by('views')[:3]#通过浏览数进行排序
    link = Link.objects.all()
    return render(request, 'index.html', locals())
    ##首页推荐阅读
    # tui = Article.objects.filter(tui__id=1)[:3]  #查询推荐位ID为1的文章
    # context = {
    #     'tui': tui,
    # }
    ##首页最新文章
    # allarticle = Article.objects.all().order_by('-id')[0:3]
    # context = {
    #     'allarticle': allarticle,
    # }
    ##热门文章排行
    #hot = Article.objects.all().order_by('?')[:3] #随机推荐
    #hot = Article.objects.filter(tui__id=3)[:3]   #通过推荐进行查询，以推荐ID是3为例
    # hot = Article.objects.all().order_by('views')[:3]#通过浏览数进行排序
    # context = {
    #     'hot': hot,
    # }
    ##右侧热门推荐
    # remen = Article.objects.filter(tui__id=1)[:5]
    # context = {
    #     'remen':remen,
    # }
    ##右侧所有标签
    # tags = Tag.objects.all()
    # context = {
    #     'tags': tags,
    # }
    ##友情链接
    # link = Link.objects.all()
    # context = {
    #         'link':link,
    #     }

#列表页
def list(request,lid):
    list = Article.objects.filter(category_id=lid)
    cname = Category.objects.get(id=lid)
    page = request.GET.get('page')
    paginator = Paginator(list, 5)
    try:
        list = paginator.page(page)#获取当前页码的记录
    except PageNotAnInteger:
        list = paginator.page(1)#如果用户输入的页码不是整数时,显示第1页的内容
    except EmptyPage:
        list = paginator.page(paginator.num_pages)#如果用户输入的页数不在系统的页码列表中时,显示最后一页的内容
    return render(request, 'list.html', locals())


#内容页
def show(request,sid):
    show = Article.objects.get(id=sid)
    hot = Article.objects.all().order_by('?')[:10]
    previous_blog = Article.objects.filter(created_time__gt=show.created_time,category=show.category.id).first()
    netx_blog = Article.objects.filter(created_time__lt=show.created_time,category=show.category.id).last()
    show.views = show.views + 1
    show.save()
    return render(request, 'show.html', locals())

#标签页
def tag(request, tag):
    list = Article.objects.filter(tags__name=tag)
    tname = Tag.objects.get(name=tag)
    page = request.GET.get('page')
    paginator = Paginator(list, 5)
    try:
        list = paginator.page(page)  # 获取当前页码的记录
    except PageNotAnInteger:
        list = paginator.page(1)  # 如果用户输入的页码不是整数时,显示第1页的内容
    except EmptyPage:
        list = paginator.page(paginator.num_pages)  # 如果用户输入的页数不在系统的页码列表中时,显示最后一页的内容
    return render(request, 'tags.html', locals())

# 搜索页
def search(request):
    ss=request.GET.get('search')
    list = Article.objects.filter(title__contains=ss)
    page = request.GET.get('page')
    paginator = Paginator(list, 10)
    try:
        list = paginator.page(page)  # 获取当前页码的记录
    except PageNotAnInteger:
        list = paginator.page(1)  # 如果用户输入的页码不是整数时,显示第1页的内容
    except EmptyPage:
        list = paginator.page(paginator.num_pages)  # 如果用户输入的页数不在系统的页码列表中时,显示最后一页的内容
    return render(request, 'search.html', locals())


# 关于我们
def about(request):
    return render(request, 'page.html',locals())