from django.contrib.auth.decorators import login_required
from django.shortcuts import render
from django.core.exceptions import PermissionDenied

from app import models as data
from app.staff.forms import FastSendForm
from app.models import Card, History


@login_required
def dashboard(request):
    if not request.user.is_staff:
        raise PermissionDenied
    players = data.Player.objects.all()
    cards = data.Card.objects.all()
    teams = data.Team.objects.all()
    history_entries = data.History.objects.all()
    return render(request, 'staff/dashboard.html', locals())


@login_required
def gift(request):
    if request.method == 'GET':
        return render(request, 'staff/gift.html', {"form": FastSendForm()})
    else:
        form = FastSendForm(request.POST)
        if form.is_valid():
            player = form.cleaned_data['player']
            card = Card()
            # hard code
            present = request.user.first_name
            card.name = "來自 %s 的 %s" % (request.user.last_name, present)
            card.value = form.cleaned_data['point']
            card.comment = form.cleaned_data['message']
            card.active = True
            card.retrieved = True
            card.issuer = request.user
            card.capturer = player
            card.save()
            record_reciever = History(
                action=0xfeed, user=player.user, card=card,
                comment="從 %s 收到一張卡片" % request.user.get_full_name())
            record_reciever.save()
            record_sender = History(
                action=0xfeed, user=request.user, card=card,
                comment="給了 %s (%s)" % (player.user.get_full_name(), player.user.username))
            record_sender.save()
            return render(
                request, "submit.html", {
                    "success": True,
                    "title": "成功發送卡片",
                    "content": "成功將卡片送給 %s 了！" % player.user.get_full_name(),
                })
        else:
            return render(
                request, "submit.html", {
                    "success": False,
                    "title": "發送卡片失敗",
                    "content": "要不要去戳戳系統管理員呢？"
                })

@@ -138,16 +138,50 @@
 onDenied
+%0Afrom django.db import transaction
 %0A%0Afrom a
@@ -982,16 +982,114 @@
 st_name%0A
+            if present:%0A                present = '%E7%A5%9D%E7%A6%8F'%0A            with transaction.atomic():%0A    
         
@@ -1161,24 +1161,28 @@
             
+ 
+   
 card.value =
@@ -1209,16 +1209,20 @@
 point'%5D%0A
+    
         
@@ -1277,24 +1277,28 @@
             
+   
+ 
 card.active 
@@ -1296,32 +1296,36 @@
 d.active = True%0A
+    
             card
@@ -1350,24 +1350,28 @@
             
+    
 card.issuer 
@@ -1393,24 +1393,28 @@
             
+ 
+   
 card.capture
@@ -1432,24 +1432,28 @@
             
+    
 card.save()%0A
@@ -1456,32 +1456,36 @@
 e()%0A            
+   
+ 
 record_reciever 
@@ -1487,32 +1487,36 @@
 ever = History(%0A
+    
                 
@@ -1567,32 +1567,36 @@
                 
+    
 comment=%22%E5%BE%9E %25s %E6%94%B6%E5%88%B0
@@ -1637,32 +1637,36 @@
 ())%0A            
+ 
+   
 record_reciever.
@@ -1664,32 +1664,36 @@
 reciever.save()%0A
+    
             reco
@@ -1721,32 +1721,36 @@
                 
+   
+ 
 action=0xfeed, u
@@ -1770,32 +1770,36 @@
 ser, card=card,%0A
+    
                 
@@ -1874,16 +1874,20 @@
 rname))%0A
+    
         
@@ -2313,16 +2313,16 @@
 %E9%80%81%E5%8D%A1%E7%89%87%E5%A4%B1%E6%95%97%22,%0A
-
         
@@ -2360,16 +2360,67 @@
 %E7%B5%B1%E7%AE%A1%E7%90%86%E5%93%A1%E5%91%A2%EF%BC%9F%22%0A
+                    %22(%E5%A6%82%E6%9E%9C%E6%98%AFPOST%E5%A5%87%E6%80%AA%E7%9A%84%E8%B3%87%E6%96%99%EF%BC%8C%E5%8F%AF%E8%83%BD%E6%9C%83%E6%94%B6%E5%88%B0%E5%BD%88%E5%8A%9B%E7%B9%A9%E5%96%94%CB%8A_%3E%CB%8B)%22%0A
         

use transaction at dashboard