def dashboard(request): if user_permission(request.user) < 2: raise PermissionDenied if user_permission(request.user) < 3: return redirect('lite') 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())
def card(request, id=None): if user_permission(request.user) < 2: raise PermissionDenied else: try: card = Card.objects.get(cid=id) except ObjectDoesNotExist: return CardNotFound(request) retriever = card.capturer host = request.META['HTTP_HOST'] permission = user_permission(request.user) return render(request, "card/card.html", locals())
def gen(request): if user_permission(request.user) == 3: # only teamleader can use if not request.POST: form = CardForm() return render(request, "card/generate.html", locals()) else: form = CardForm(request.POST) if form.is_valid(): with transaction.atomic(): card = Card() card.name = form.cleaned_data["name"] card.value = form.cleaned_data["value"] card.long_desc = form.cleaned_data["long_desc"] card.active = form.cleaned_data["active"] card.issuer = request.user card.save() record = History(action=1, user=request.user, card=card) record.save() return render( request, "submit.html", { "success": True, "title": "一張卡片就此誕生!", "content": "生成了一張卡片 %s 含有 %d 點" % (card.name, card.value), "next_page": reverse('view card', args=[card.cid]), }) else: return render(request, "submit.html", { "success": False, "title": "產生卡片失敗", "content": "要不要去戳戳系統管理員呢?" "(如果是POST奇怪的資料,可能會收到彈力繩喔ˊ_>ˋ)" }) else: raise PermissionDenied
def leaderboard(request): if user_permission(request.user) < 2: raise PermissionDenied if user_permission(request.user) < 3: return redirect('lite') 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") sorted_teams = list(teams) sorted_teams.sort(key=lambda x: x.points, reverse=True) history_entries = data.History.objects.all().order_by('-date')[:30] return render(request, 'staff/leaderboard.html', locals())
def get(request, id=None): if user_permission(request.user) == 0: # Anonymous User return render( request, "submit.html", { "success": False, "content": "你可能需要先掃描一下識別證上的 QR_Code 來登入系統", "title": "未登入!"}, status=404) elif user_permission(request.user) == 1: # player try: card = Card.objects.get(cid=id) except ObjectDoesNotExist: return CardNotFound(request) if not card.retrieved and card.active: # Add points with transaction.atomic(): player = request.user.player player.captured_card.add(card) player.save() card.retrieved = True card.save() record = History(action=10, user=request.user, card=card) record.save() abscardvalue = abs(card.value) return render( request, "card/get.html", locals()) else: return render( request, "submit.html", { "success": False, "title": "卡片已被捕獲", "content": "這張卡片已經被使用過囉,何不換張卡片呢?", }) elif user_permission(request.user) > 1: # worker and teamleader return redirect('view card', card.cid) else: raise PermissionDenied
def edit(request, id=None): if user_permission(request.user) < 3: raise PermissionDenied else: try: card = Card.objects.get(cid=id) except ObjectDoesNotExist: return CardNotFound(request) if not request.POST: form = CardForm( {"name": card.name, "value": card.value, "long_desc": card.long_desc, "active": card.active, "retrieved": card.retrieved}) return render(request, "card/edit.html", locals()) else: form = CardForm(request.POST) if form.is_valid(): action = 2 if card.active is not form.cleaned_data["active"]: if form.cleaned_data["active"]: action = 4 else: action = 3 with transaction.atomic(): card.name = form.cleaned_data["name"] card.value = form.cleaned_data["value"] card.long_desc = form.cleaned_data["long_desc"] card.active = form.cleaned_data["active"] card.save() record = History(action=action, user=request.user, card=card) record.save() return render( request, "submit.html", { "success": True, "title": "成功編輯", "content": "成功編輯卡片 %s" % card.name, "next_page": reverse('view card', args=[card.cid]), }) else: # invalid value in form return render( request, "submit.html", { "success": True, "title": "編輯失敗", "next_page": reverse('home'), })
def gift(request): if user_permission(request.user) < 3: raise PermissionDenied 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 if present == "": present = '祝福' with transaction.atomic(): 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": "要不要去戳戳系統管理員呢?" "(如果是POST奇怪的資料,可能會收到彈力繩喔ˊ_>ˋ)" })
def lite(request, tt=None): denomination = [32, 64, 128, 256, -128, -32, -64] if user_permission(request.user) < 2: raise PermissionDenied if tt is not None: try: tt = int(tt) except: return render( request, "submit.html", { "success": False, "title": "發送卡片失敗", "content": "我幫你綁好繩子了," "你要自己跳還是我推你跳呢?(本繩載重20g)"}) if tt not in range(0, len(denomination)): return render( request, "submit.html", { "success": False, "title": "發送卡片失敗", "content": "要不要去戳戳系統管理員呢?" "(如果是POST奇怪的資料,可能會收到彈力繩喔ˊ_>ˋ)" }) with transaction.atomic(): card = Card() present = request.user.first_name if present == "": present = '祝福' card.name = "來自 %s 的%s" % (request.user.last_name, present) card.value = denomination[tt] card.active = True card.retrieved = False card.issuer = request.user card.save() record = History(action=1, user=request.user, card=card) record.save() return redirect('view card', card.cid) else: return render(request, 'staff/lite.html',locals())