voucher_form.code won't match voucher.code :-(

This commit is contained in:
Marcus Scholz 2020-08-01 21:40:11 +02:00
parent cd0df727cc
commit 8cbb1baf27
2 changed files with 20 additions and 6 deletions

View File

@ -1,7 +1,7 @@
from django.urls import path from django.urls import path
from .views import ( from .views import (
hello_world, hello_world,
VoucherVerify, VoucherVerifyView,
place_detail_view, place_detail_view,
place_list_view, place_list_view,
SignUpView, SignUpView,
@ -11,9 +11,8 @@ from .views import (
urlpatterns = [ urlpatterns = [
path('hello_world/', hello_world), # You know what this is :P path('hello_world/', hello_world), # You know what this is :P
path('voucher/', VoucherVerifyView.as_view(), name='enter_voucher'),
path('signup/', SignUpView.as_view(), name='signup'), path('signup/', SignUpView.as_view(), name='signup'),
path('voucher/<slug:code>/', VoucherVerify.as_view(), name='voucher_verify'),
path('voucher/', VoucherVerify.as_view(), name='enter_voucher'),
path('place/<int:pk>/', place_detail_view, name='place_detail'), path('place/<int:pk>/', place_detail_view, name='place_detail'),
path('place/create/', PlaceCreateView.as_view(), name='place_create'), path('place/create/', PlaceCreateView.as_view(), name='place_create'),
path('place/update/<int:pk>/', PlaceUpdateView.as_view(), name='place_edit'), path('place/update/<int:pk>/', PlaceUpdateView.as_view(), name='place_edit'),

View File

@ -14,16 +14,31 @@ from .models import Place, PlaceImage, Voucher
# Create your views here. # Create your views here.
class VoucherVerify(View): class VoucherVerifyView(View):
formclass = VoucherVerifyForm formclass = VoucherVerifyForm
voucher_form = VoucherVerifyForm()
success_url = reverse_lazy('signin')
fields = ['code'] fields = ['code']
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
voucher_form = VoucherVerifyForm() voucher_form = VoucherVerifyForm()
return render(request, 'voucher-verify.html', {'voucher_form': voucher_form}) return render(request, 'voucher-verify.html', {'voucher_form': voucher_form})
def post(self, request, *args, **kwargs):
voucher_form = VoucherVerifyForm(request.POST)
if voucher_form.is_valid():
# Compare voucher from form with available vouchers in Voucher.
for voucher in Voucher.objects.all():
print(voucher.code)
if voucher.code == voucher_form.cleaned_data.get('code'):
kwargs_to_pass = {
'voucher_pk': voucher.pk
}
return redirect(reverse_lazy('signup'), kwargs=kwargs_to_pass)
else:
return redirect(reverse_lazy('enter_voucher'))
else:
return redirect(reverse_lazy('enter_voucher'))
class SignUpView(CreateView): class SignUpView(CreateView):
form_class = ExplorerCreationForm form_class = ExplorerCreationForm
success_url = reverse_lazy('login') success_url = reverse_lazy('login')