from app.models import Card, History, is_player
from app.player.forms import FeedForm
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist, PermissionDenied
from django.shortcuts import redirect, render


def player(request, id=None):
    if not request.user.is_authenticated():
        return redirect("login", id)
    else:
        if not id == request.user.username:
            # rediect logged-in user to /player/<username>
            return redirect('player data', request.user.username)
        if not is_player(request.user):
            # ban non player
            raise PermissionDenied
        else:
            user = request.user
            records = History.objects.filter(user=user)
            return render(request, 'player/player.html',
                          {"user": user, "records": records})

@login_required
def feed(request, id=None):
    if not request.user.is_staff:
        raise PermissionDenied
    else:
        try:
            user = User.objects.get(username=id)
        except ObjectDoesNotExist:
            return render(
                request, "submit.html", {
                    "content":("<h1>Wrong user</h1>"
                               "<meta http-equiv=\"refresh\" content=\"3; url=/\">"),
                    "title":"錯誤！"}, status=404)

        if not is_player(user):
            return render(
                request, "submit.html", {
                    "content":("<h1>Wrong user</h1>"
                               "<meta http-equiv=\"refresh\" content=\"3; url=/\">"),
                    "title":"錯誤！"}, status=404)
        else:
            if not request.POST:
                form = FeedForm()
                return render(request, "player/feed.html", locals())
            else:
                form = FeedForm(request.POST)
                if form.is_valid():
                    player = user.player
                    card = form.cleaned_data["card"]
                    player.captured_card.add(card)
                    player.save()
                    card.retrieved = True
                    card.save()
                    record = History(action=0xfeed, user=request.user, card=card,
                                     comment="給" + user.get_full_name() + " (" + user.username + ")")
                    record.save()
                    card.save()

                    return render(
                        request, "submit.html", {
                            "content":("<h1>Submitted.</h1>"
                                       "<meta http-equiv=\"refresh\" content=\"3; "
                                       "url=\"/\">")})
                else:
                    return render(request, "player/feed.html", locals())

@@ -917,16 +917,17 @@
 ords%7D)%0A%0A
+%0A
 @login_r
@@ -1237,115 +1237,15 @@
 nt%22:
-(%22%3Ch1%3EWrong user%3C/h1%3E%22%0A                               %22%3Cmeta http-equiv=%5C%22refresh%5C%22 content=%5C%223; url=/%5C%22%3E%22)
+ %22%E4%BD%A0%E6%98%AF%E8%AA%B0%EF%BC%9F%22
 ,%0A  
@@ -1262,32 +1262,33 @@
         %22title%22:
+ 
 %22%E9%8C%AF%E8%AA%A4%EF%BC%81%22%7D, status=4
@@ -1427,115 +1427,25 @@
 nt%22:
-(%22%3Ch1%3EWrong user%3C/h1%3E%22%0A                               %22%3Cmeta http-equiv=%5C%22refresh%5C%22 content=%5C%223; url=/%5C%22%3E%22)
+ %22%E5%B7%A5%E4%BD%9C%E4%BA%BA%E5%93%A1%E7%9A%84%E4%B8%96%E7%95%8C%E4%BD%A0%E6%98%AF%E7%9C%8B%E4%B8%8D%E5%88%B0%E7%9A%84!%22
 ,%0A  
@@ -1470,16 +1470,17 @@
 %22title%22:
+ 
 %22%E9%8C%AF%E8%AA%A4%EF%BC%81%22%7D, 
@@ -2363,139 +2363,108 @@
    %22
-content%22:(%22%3Ch1%3ESubmitted.%3C/h1%3E%22%0A                                       %22%3Cmeta http-equiv=%5C%22refresh%5C%22 content=%5C%223; %22%0A               
+title%22: %22%E6%88%90%E5%8A%9F%E7%99%BC%E9%80%81%22,%0A                            %22content%22: %22%E4%BD%A0%E9%80%81%E7%B5%A6 %25d %E4%B8%80%E5%BC%B5%E5%8D%A1%E7%89%87%22 %25 player.user.get_full_name(),%0A
     
@@ -2487,21 +2487,8 @@
     
-%22url=%5C%22/%5C%22%3E%22)
 %7D)%0A 

fix error message at player page