# Path: app/models.py
# 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/player/forms.py
# class FeedForm(forms.Form):
#     card = CardCoiceField(Card.objects.filter(active=True, retrieved=False), label="Card")


from app.models import History
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
from django.shortcuts import redirect, render
from django.db import transaction
                    i.weight = 0
                else:
                    i.weight = i.points_acquired / user.player.team.points * 100
            return render(request, 'player/player.html', locals())


@login_required
def feed(request, id=None):
    if request.user.has_perm('app.feed_card'):
        try:
            user = User.objects.get(username=id)
        except ObjectDoesNotExist:
            return render(
                request, "submit.html", {
                    "content": "你是找誰？",
                    "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():
                    with transaction.atomic():
                        player = user.player
                        card = form.cleaned_data["card"]
                        card.capturer = player
                        card.retrieved = True
                        card.save()

# Path: app/models.py
# 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/player/forms.py
# class FeedForm(forms.Form):
#     card = CardCoiceField(Card.objects.filter(active=True, retrieved=False), label="Card")


# Path: app/player/views.py
from app.models import History
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
from django.shortcuts import redirect, render
from django.db import transaction

                    i.weight = 0
                else:
                    i.weight = i.points_acquired / user.player.team.points * 100
            return render(request, 'player/player.html', locals())


@login_required
def feed(request, id=None):
    if request.user.has_perm('app.feed_card'):
        try:
            user = User.objects.get(username=id)
        except ObjectDoesNotExist:
            return render(
                request, "submit.html", {
                    "content": "你是找誰？",
                    "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():
                    with transaction.atomic():
                        player = user.player
                        card = form.cleaned_data["card"]
                        card.capturer = player
                        card.retrieved = True
                        card.save()

                        record_sender = History(