# Path: src/presenters/models.py
# class Platform(models.Model):
#     name = models.CharField(max_length=50)
#     url = models.CharField(max_length=50)
#     intro = models.TextField()
#     logo = models.ImageField(upload_to='platform_logos', blank=True)
# 
#     login_param = models.TextField()
#     fetch_status = models.BooleanField(default=False)
# 
#     def __unicode__(self):
#         return self.name
# 
# class Presenter(models.Model):
#     game = models.ForeignKey('Game', default=get_default_game)
#     platform = models.ForeignKey('Platform')
#     id_in_platform = models.IntegerField(default=0)
#     tag = models.ManyToManyField('Tag', blank=True)
#     nickname = models.CharField(max_length=50)
#     introduce = models.TextField()
#     room_url = models.CharField(max_length=255, blank=True)
#     gender = models.CharField(max_length=1, choices=GENDER_CHOICES, default='M')
#     avatar_url = models.CharField(max_length=255, blank=True)
# 
#     join_date = models.DateField(auto_now_add=True)
# 
#     invalid = models.BooleanField(default=True)
# 
#     def __unicode__(self):
#         return self.nickname
# 
# class Game(models.Model):
#     name = models.CharField(max_length=50)
# 
#     def __unicode__(self):
#         return self.name


from django.shortcuts import render
from django.shortcuts import get_object_or_404
from django.http import HttpResponse, Http404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.db.models import Count
from .models import Platform, Presenter, Game
from haystack.query import SearchQuerySet
import urllib, urllib2, cookielib, json
import time
import logging
        presenter_list = presenter_list.filter(game__name=game_name)

    # 根据用户输入的查询条件过滤
    q = request.GET.get('q')
    if q:
        sqs = SearchQuerySet().auto_query(q)
        q_result = []
        for item in sqs:
            if item.model_name == 'presenter':
                q_result.append(item.pk)
        presenter_list = presenter_list.filter(pk__in=q_result)

    # 分页
    per_page = 10;
    paginator = Paginator(presenter_list, per_page)
    page_number = request.GET.get('page')   # start from 1
    if not page_number:
        page_number = 1

    try:
        presenter_list = paginator.page(page_number)
    except PageNotAnInteger:
        page_number = 1
        presenter_list = paginator.page(1)
    except EmptyPage:
        page_number = paginator.num_pages
        presenter_list = paginator.page(paginator.num_pages)

    # 直播平台的分组信息
    platform_list = Platform.objects.annotate(num_presenters=Count('presenter')).order_by('-num_presenters')

# Path: src/presenters/models.py
# class Platform(models.Model):
#     name = models.CharField(max_length=50)
#     url = models.CharField(max_length=50)
#     intro = models.TextField()
#     logo = models.ImageField(upload_to='platform_logos', blank=True)
# 
#     login_param = models.TextField()
#     fetch_status = models.BooleanField(default=False)
# 
#     def __unicode__(self):
#         return self.name
# 
# class Presenter(models.Model):
#     game = models.ForeignKey('Game', default=get_default_game)
#     platform = models.ForeignKey('Platform')
#     id_in_platform = models.IntegerField(default=0)
#     tag = models.ManyToManyField('Tag', blank=True)
#     nickname = models.CharField(max_length=50)
#     introduce = models.TextField()
#     room_url = models.CharField(max_length=255, blank=True)
#     gender = models.CharField(max_length=1, choices=GENDER_CHOICES, default='M')
#     avatar_url = models.CharField(max_length=255, blank=True)
# 
#     join_date = models.DateField(auto_now_add=True)
# 
#     invalid = models.BooleanField(default=True)
# 
#     def __unicode__(self):
#         return self.nickname
# 
# class Game(models.Model):
#     name = models.CharField(max_length=50)
# 
#     def __unicode__(self):
#         return self.name


# Path: src/presenters/views.py
from django.shortcuts import render
from django.shortcuts import get_object_or_404
from django.http import HttpResponse, Http404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.db.models import Count
from .models import Platform, Presenter, Game
from haystack.query import SearchQuerySet
import urllib, urllib2, cookielib, json
import time
import logging

        presenter_list = presenter_list.filter(game__name=game_name)

    # 根据用户输入的查询条件过滤
    q = request.GET.get('q')
    if q:
        sqs = SearchQuerySet().auto_query(q)
        q_result = []
        for item in sqs:
            if item.model_name == 'presenter':
                q_result.append(item.pk)
        presenter_list = presenter_list.filter(pk__in=q_result)

    # 分页
    per_page = 10;
    paginator = Paginator(presenter_list, per_page)
    page_number = request.GET.get('page')   # start from 1
    if not page_number:
        page_number = 1

    try:
        presenter_list = paginator.page(page_number)
    except PageNotAnInteger:
        page_number = 1
        presenter_list = paginator.page(1)
    except EmptyPage:
        page_number = paginator.num_pages
        presenter_list = paginator.page(paginator.num_pages)

    # 直播平台的分组信息
    platform_list = Platform.objects.annotate(num_presenters=Count('presenter')).order_by('-num_presenters')

    game_list = Game.objects.annotate(num_presenters=Count('presenter'))