def gen(request): if request.user.is_staff: if not request.POST: form = CardForm() return render(request, "card/generate.html", locals()) else: form = CardForm(request.POST) if form.is_valid(): 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('generate card'), }) else: raise PermissionDenied
def change(): id = request.args.get('id', type=int) card = Card.query.get(int(id)) form = CardForm() if form.validate_on_submit(): category = form.category_expenses.data if form.kind.data == 0 else form.category_incomes.data card.kind = form.kind.data card.price = form.price.data card.category = category card.day = form.day.data card.month = form.month.data card.year = form.year.data card.note = form.note.data db.session.commit() return redirect(url_for('main.index')) elif request.method == 'GET': form.kind.data = card.kind form.price.data = card.price form.category_expenses.data = card.category form.category_incomes.data = card.category form.day.data = card.day form.month.data = card.month form.year.data = card.year form.note.data = card.note return render_template('card/change_card.html', form=form)
def gen(request): if request.user.has_perm('app.add_card'): # 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 edit(request, id=None): if request.user.has_perm('app.change_card'): 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('edit card', id), }) else: raise PermissionDenied
def edit(request, id=None): if request.user.has_perm('app.change_card'): 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('edit card', id), }) else: raise PermissionDenied
def create(): form = CardForm() if form.validate_on_submit(): category = form.category_expenses.data if form.kind.data == 0 else form.category_incomes.data card = Card(price=form.price.data, category=category, note=form.note.data, kind=bool(form.kind.data), payer=current_user, year=form.year.data, month=form.month.data, day=form.day.data) db.session.add(card) db.session.commit() return redirect(url_for('main.index')) elif request.method == 'GET': form.day.data = datetime.utcnow().date().day form.month.data = datetime.utcnow().date().month form.year.data = datetime.utcnow().date().year return render_template('card/create_card.html', form=form)
def edit(request, id=None): if not request.user.is_staff: raise PermissionDenied else: try: card = Card.objects.get(cid=id) except ObjectDoesNotExist: return render( request, "submit.html", {"content": '<h1>Wrong card</h1><meta http-equiv="refresh" content="3; url=/">', "title": "錯誤!"}, status=404, ) if not request.POST: form = CardForm( { "name": card.name, "value": card.value, "long_desc": card.long_desc, "active": card.active, "retrieved": card.retrieved, "modified_reason": "", } ) return render(request, "card/edit.html", locals()) else: form = CardForm(request.POST) if form.is_valid(): 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.modified_reason = form.cleaned_data["modified_reason"] card.save() return render( request, "submit.html", {"content": '<h1>Submitted.</h1><meta http-equiv="refresh" content="3; url=/card/' + card.cid + '">'}, )
def gen(request): if request.user.is_staff: if not request.POST: form = CardForm() return render(request, "card/generate.html", locals()) else: form = CardForm(request.POST) if form.is_valid(): 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.modified_reason = form.cleaned_data["modified_reason"] card.save() return render( request, "submit.html", {"content": '<h1>Submitted.</h1><meta http-equiv="refresh" content="3; url=/card/' + card.cid + '">'}, ) else: raise PermissionDenied
def gen(request): if request.user.has_perm('app.add_card'): # 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