# Path: app/models.py
# class Team(models.Model):
# class Player(models.Model):
# class Card(models.Model):
#     class Meta:
# class History(models.Model):
#     class Meta:
#     def points(self):
#     def __str__(self):
#     def points_acquired(self):
#     def __str__(self):
#     def random():
#     def __str__(self):
#     def action_explain(self):
#     ACTION_CODE = {
#         1: "建立卡片",
#         2: "編輯卡片",
#         3: "關閉卡片",
#         4: "啟動卡片",
#         10: "獲得卡片",
#         0xfeed: "餵食卡片",
#     }
# 
# Path: app/staff/forms.py
# class FastSendForm(forms.Form):
#     message = forms.CharField(max_length=255, label="訊息", help_text="悄悄話", required=False)
#     point = forms.IntegerField(label="價值", help_text="點數卡的價值", initial=1)
#     player = forms.ModelChoiceField(Player.objects.all(), label="玩家")
# 
# Path: app/models.py
# class Card(models.Model):
#     def random():
#         return str(get_random_string(16))
#     cid = models.CharField(max_length=16,
#                            primary_key=True,
#                            default=random,
#                            verbose_name="Card ID",
#                            help_text="The unique ID for the points card.")
#     value = models.IntegerField(default="1",
#                                 verbose_name="Value",
#                                 help_text="The value of the points card.")
#     active = models.BooleanField(default=True, verbose_name="Active?")
#     retrieved = models.BooleanField(default=False, verbose_name="Retrieved")
#     name = models.CharField(
#         max_length=32,
#         verbose_name="Name",
#         help_text="Name of the card.",
#         blank=True)
#     long_desc = models.TextField(verbose_name="Descriptions",
#                                  help_text="Long descriptions about the card.",
#                                  blank=True)
#     issuer = models.ForeignKey(User, related_name="issued_card")
#     capturer = models.ForeignKey(Player, related_name="captured_card", null=True, blank=True)
# 
#     def __str__(self):
#         if self.active and not self.retrieved:
#             return (str(self.name) + " (" + str(self.cid) + "), "
#                     "active, " + str(self.value) + " points.")
#         elif not self.active and self.retrieved:
#             return (str(self.name) + " (" + str(self.cid) + "), "
#                     "retrieved by " + str(self.capturer) + ", " + str(self.value) + " points.")
#         else:
#             return (str(self.name) + " (" + str(self.cid) + "), "
#                     "inactive, " + str(self.value) + " points.")
# 
#     class Meta:
#         permissions = (
#             ('read_card', 'can view card detail message'),
#             ('get_card', 'can capture card'),
#             ('toggle_card', 'can active/inactive card'),
#             ('gen_card', 'can generate card within limit'),
#             ('feed_card', 'can directly send card to someone'),
#             ('view_cardlist', 'can list all card'),
# 
#             ('master', 'game master'),
#             ('worker', 'normal staff'),
#         )
# 
# class History(models.Model):
#     ACTION_CODE = {
#         1: "建立卡片",
#         2: "編輯卡片",
#         3: "關閉卡片",
#         4: "啟動卡片",
#         10: "獲得卡片",
#         0xfeed: "餵食卡片",
#     }
# 
#     no = models.AutoField(primary_key=True)
#     action = models.IntegerField(choices=list(ACTION_CODE.items()))
#     user = models.ForeignKey(User)
#     card = models.ForeignKey(Card)
#     comment = models.CharField(max_length=255)
#     date = models.DateTimeField(auto_now_add=True)
# 
#     @property
#     def action_explain(self):
#         return History.ACTION_CODE[self.action]
# 
#     class Meta:
#         permissions = (
#             ('view_history', 'can view history'),
#         )


from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect
from django.core.exceptions import PermissionDenied
from django.db import transaction, models
from app import models as data
from app.staff.forms import FastSendForm
from app.models import Card, History


@login_required
def dashboard(request):
    if request.user.has_perm('app.master'):
        # only game master can view this page
        players = data.Player.objects.all()
        cards = data.Card.objects.all()
        teams = data.Team.objects.all()
        history_entries = data.History.objects.all().order_by('-date')[:30]
        return render(request, 'staff/dashboard.html', locals())

    elif request.user.has_perm('app.worker'):
        # redirect low level staff to lite page
        return redirect('lite')

    else:
        raise PermissionDenied


@login_required
def leaderboard(request):
    if request.user.has_perm('app.master'):
        # game master can visit
        players = data.Player.objects.all()
        cards = data.Card.objects.all()
        lencards = len(cards)
        lencardsgot = len([x for x in cards if x.retrieved])
        totalpoints = data.Card.objects.filter(active=True).aggregate(models.Sum('value'))["value__sum"]
        totalpointsgot = data.Card.objects.filter(retrieved=True).aggregate(models.Sum('value'))["value__sum"]

# Path: app/models.py
# class Team(models.Model):
# class Player(models.Model):
# class Card(models.Model):
#     class Meta:
# class History(models.Model):
#     class Meta:
#     def points(self):
#     def __str__(self):
#     def points_acquired(self):
#     def __str__(self):
#     def random():
#     def __str__(self):
#     def action_explain(self):
#     ACTION_CODE = {
#         1: "建立卡片",
#         2: "編輯卡片",
#         3: "關閉卡片",
#         4: "啟動卡片",
#         10: "獲得卡片",
#         0xfeed: "餵食卡片",
#     }
# 
# Path: app/staff/forms.py
# class FastSendForm(forms.Form):
#     message = forms.CharField(max_length=255, label="訊息", help_text="悄悄話", required=False)
#     point = forms.IntegerField(label="價值", help_text="點數卡的價值", initial=1)
#     player = forms.ModelChoiceField(Player.objects.all(), label="玩家")
# 
# Path: app/models.py
# class Card(models.Model):
#     def random():
#         return str(get_random_string(16))
#     cid = models.CharField(max_length=16,
#                            primary_key=True,
#                            default=random,
#                            verbose_name="Card ID",
#                            help_text="The unique ID for the points card.")
#     value = models.IntegerField(default="1",
#                                 verbose_name="Value",
#                                 help_text="The value of the points card.")
#     active = models.BooleanField(default=True, verbose_name="Active?")
#     retrieved = models.BooleanField(default=False, verbose_name="Retrieved")
#     name = models.CharField(
#         max_length=32,
#         verbose_name="Name",
#         help_text="Name of the card.",
#         blank=True)
#     long_desc = models.TextField(verbose_name="Descriptions",
#                                  help_text="Long descriptions about the card.",
#                                  blank=True)
#     issuer = models.ForeignKey(User, related_name="issued_card")
#     capturer = models.ForeignKey(Player, related_name="captured_card", null=True, blank=True)
# 
#     def __str__(self):
#         if self.active and not self.retrieved:
#             return (str(self.name) + " (" + str(self.cid) + "), "
#                     "active, " + str(self.value) + " points.")
#         elif not self.active and self.retrieved:
#             return (str(self.name) + " (" + str(self.cid) + "), "
#                     "retrieved by " + str(self.capturer) + ", " + str(self.value) + " points.")
#         else:
#             return (str(self.name) + " (" + str(self.cid) + "), "
#                     "inactive, " + str(self.value) + " points.")
# 
#     class Meta:
#         permissions = (
#             ('read_card', 'can view card detail message'),
#             ('get_card', 'can capture card'),
#             ('toggle_card', 'can active/inactive card'),
#             ('gen_card', 'can generate card within limit'),
#             ('feed_card', 'can directly send card to someone'),
#             ('view_cardlist', 'can list all card'),
# 
#             ('master', 'game master'),
#             ('worker', 'normal staff'),
#         )
# 
# class History(models.Model):
#     ACTION_CODE = {
#         1: "建立卡片",
#         2: "編輯卡片",
#         3: "關閉卡片",
#         4: "啟動卡片",
#         10: "獲得卡片",
#         0xfeed: "餵食卡片",
#     }
# 
#     no = models.AutoField(primary_key=True)
#     action = models.IntegerField(choices=list(ACTION_CODE.items()))
#     user = models.ForeignKey(User)
#     card = models.ForeignKey(Card)
#     comment = models.CharField(max_length=255)
#     date = models.DateTimeField(auto_now_add=True)
# 
#     @property
#     def action_explain(self):
#         return History.ACTION_CODE[self.action]
# 
#     class Meta:
#         permissions = (
#             ('view_history', 'can view history'),
#         )


# Path: app/staff/views.py
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect
from django.core.exceptions import PermissionDenied
from django.db import transaction, models
from app import models as data
from app.staff.forms import FastSendForm
from app.models import Card, History



@login_required
def dashboard(request):
    if request.user.has_perm('app.master'):
        # only game master can view this page
        players = data.Player.objects.all()
        cards = data.Card.objects.all()
        teams = data.Team.objects.all()
        history_entries = data.History.objects.all().order_by('-date')[:30]
        return render(request, 'staff/dashboard.html', locals())

    elif request.user.has_perm('app.worker'):
        # redirect low level staff to lite page
        return redirect('lite')

    else:
        raise PermissionDenied


@login_required
def leaderboard(request):
    if request.user.has_perm('app.master'):
        # game master can visit
        players = data.Player.objects.all()
        cards = data.Card.objects.all()
        lencards = len(cards)
        lencardsgot = len([x for x in cards if x.retrieved])
        totalpoints = data.Card.objects.filter(active=True).aggregate(models.Sum('value'))["value__sum"]
        totalpointsgot = data.Card.objects.filter(retrieved=True).aggregate(models.Sum('value'))["value__sum"]

        teams = data.Team.objects.all().exclude(tid="zsh")