diff --git a/lostplaces/lostplaces_app/urls.py b/lostplaces/lostplaces_app/urls.py index 4e6fc00..78af2df 100644 --- a/lostplaces/lostplaces_app/urls.py +++ b/lostplaces/lostplaces_app/urls.py @@ -1,7 +1,7 @@ from django.urls import path from .views import ( hello_world, - VoucherVerify, + VoucherVerifyView, place_detail_view, place_list_view, SignUpView, @@ -11,9 +11,8 @@ from .views import ( urlpatterns = [ 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('voucher//', VoucherVerify.as_view(), name='voucher_verify'), - path('voucher/', VoucherVerify.as_view(), name='enter_voucher'), path('place//', place_detail_view, name='place_detail'), path('place/create/', PlaceCreateView.as_view(), name='place_create'), path('place/update//', PlaceUpdateView.as_view(), name='place_edit'), diff --git a/lostplaces/lostplaces_app/views.py b/lostplaces/lostplaces_app/views.py index e5a9da3..2920e70 100644 --- a/lostplaces/lostplaces_app/views.py +++ b/lostplaces/lostplaces_app/views.py @@ -14,16 +14,31 @@ from .models import Place, PlaceImage, Voucher # Create your views here. -class VoucherVerify(View): +class VoucherVerifyView(View): formclass = VoucherVerifyForm - voucher_form = VoucherVerifyForm() - success_url = reverse_lazy('signin') fields = ['code'] def get(self, request, *args, **kwargs): voucher_form = VoucherVerifyForm() 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): form_class = ExplorerCreationForm success_url = reverse_lazy('login')