def loginUser(request): # checks if a user has inputted in the search field in the navbar if 'search_filter' in request.GET: return index(request) context = {} user = request.user if user.is_authenticated: request.content_params return redirect('/user/account') if request.POST: form = AccountAuthenticationForm(request.POST) if form.is_valid(): email = request.POST['email'] password = request.POST['password'] user = authenticate(email=email, password=password) if user: login(request, user) try: if request.session['redirect'] == None: return redirect('/') else: return redirect(str(request.session['redirect'])) except: return redirect('/') else: form = AccountAuthenticationForm() context['loginForm'] = form # print(form) return render(request, 'user/login.html', context)
def editProfile(request): # checks if a user has inputted in the search field in the navbar if 'search_filter' in request.GET: return index(request) if request.POST: form = EditProfileForm(request.POST, instance=request.user) if form.is_valid(): user = User.objects.get(pk=request.user.pk) if user: user.__setUserAttributes__(form) user.save() login(request, user) return redirect('/user/account') else: form = EditProfileForm( initial={ 'firstName': request.user.firstName, 'lastName': request.user.lastName, 'imgURL': request.user.imgURL, 'phoneNumber': request.user.phoneNumber, 'password': request.user.password }) context = {'editProfileForm': form} return render(request, 'user/account/profile/profile_edit.html', context)
def getSearchHistory(request): # checks if a user has inputted in the search field in the navbar if 'search_filter' in request.GET: return index(request) try: context = {'sh': __getSearchHistory(request)} except KeyError: context = {'sh': {}} return render(request, 'user/account/searchhistory/searchhistory_page.html', context)
def addToCart(request, id): # checks if a user has inputted in the search field in the navbar if 'search_filter' in request.GET: return index(request) product = Product.objects.get(id=id) # Guard against default cookies without cart. # If cart hasn't been created in the cookies before, we do it here __cartExists(request) request.session['cart'][str(id)] = product.productToDict(id) request.session.modified = True messages.success(request, 'Added to cart') return redirect('/')
def reviewOrder(request): # checks if a user has inputted in the search field in the navbar if 'search_filter' in request.GET: return index(request) context = {} context['cart'] = request.session['cart'] sa = ShippingAddress.objects.get(user_id=request.user.pk) context['sa'] = sa pm = PaymentMethod.objects.get(user_id=request.user.pk) context['pm'] = pm context['total'] = request.session['total'] return render(request, 'cart/review_order.html', context)
def removeFromCart(request, id): # checks if a user has inputted in the search field in the navbar if 'search_filter' in request.GET: return index(request) # This guard should prevent crash context = __cartExists(request) # Removing the certain product from the cart del request.session['cart'][str(id)] # Committing the changes request.session.modified = True # dictionary ret value to return context = {'cart': request.session['cart']} # Message displayed when item removed messages.warning(request, 'Item removed from cart') return redirect('/cart')
def updatePaymentMethod(request): # checks if a user has inputted in the search field in the navbar if 'search_filter' in request.GET: return index(request) # Try to find a BillingAddress that exists exists context = {} try: pm = PaymentMethod.objects.get(user_id=request.user.id) except PaymentMethod.DoesNotExist: pm = PaymentMethod() if request.POST: form = PaymentMethodForm(request.POST) if form.is_valid(): # cardNumber = form.cleaned_data.get('cardNumber') # form.cleanCardNumber() # pm.validateAttributes(request, form) pm.setPaymentMethodAttributes(request, form) pm.save() else: messages.warning(request, 'Not valid Payment info') try: if request.session['redirect'] == None: return redirect('/user/account') else: return redirect(str(request.session['redirect'])) except: return render( request, 'user/account/paymentmethod/paymentmethod_update.html', context) form = PaymentMethodForm( initial={ 'nameOnCard': pm.nameOnCard, 'cardNumber': pm.cardNumber, 'cardExpiry': pm.cardExpiry, 'cvc': pm.cvc }) context['updatePaymentMethod'] = form return render(request, 'user/account/paymentmethod/paymentmethod_update.html', context)
def getPayment(request): # checks if a user has inputted in the search field in the navbar if 'search_filter' in request.GET: return index(request) request.session['redirect'] = '/cart/checkout/payment' request.session.modified = True context = {} try: pm = PaymentMethod.objects.get(user_id=request.user.pk) context['hasPaymentMethod'] = True context['pm'] = pm context['cart'] = request.session['cart'] context['total'] = request.session['total'] except: context['cart'] = request.session['cart'] context['total'] = request.session['total'] messages.warning(request, 'Missing Payment') context['hasPaymentMethod'] = False return render(request, 'cart/checkout_payment.html', context) return render(request, 'cart/checkout_payment.html', context)
def registerUser(request): # checks if a user has inputted in the search field in the navbar if 'search_filter' in request.GET: return index(request) context = {} if request.POST: form = RegistrationForm(request.POST) if form.is_valid(): form.save() email = form.cleaned_data.get('email') raw_password = form.cleaned_data.get('password1') user = authenticate(email=email, password=raw_password) login(request, user) return redirect('/user/account') else: context['registerForm'] = form else: form = RegistrationForm() context['registerForm'] = form return render(request, 'user/register.html', context)
def checkout(request): # checks if a user has inputted in the search field in the navbar if 'search_filter' in request.GET: return index(request) request.session['redirect'] = '/cart/checkout/shipping' request.session.modified = True try: if request.session['cart'] == {}: return getCart(request) else: context = {} if request.user.is_authenticated: return __getCheckoutDetails(request) else: form = AccountAuthenticationForm(request.POST) context['loginForm'] = form # redirect to cart when logging in from checkout request.session['redirect'] = '/cart' request.session.modified = True return render(request, 'user/login.html', context) except: return redirect('/')
def confirmOrder(request): # checks if a user has inputted in the search field in the navbar if 'search_filter' in request.GET: return index(request) pm = PaymentMethod.objects.get(user_id=request.user.pk) sa = ShippingAddress.objects.get(user_id=request.user.pk) context = {} context['cart'] = request.session['cart'] context['total'] = request.session['total'] # Add to database. # credit cart cardNumber = { 'cardNumber': str(pm.getCardNumber), 'name': str(pm.nameOnCard) } context['cardNumber'] = cardNumber # Address address = { 'address': sa.address1, 'city': sa.city, 'country': sa.country, 'region': sa.region, 'postal': sa.postalCode } context['address'] = address # Orders cartList = [] for item in context['cart']: cartList.append(context['cart'][item]['id']) context['cartList'] = cartList # save to database orderModel = orders() orderModel.setOrderAttirbutes(request, context) orderModel.save() # clear cart del request.session['cart'] del request.session['total'] request.session.modified = True return render(request, 'cart/confirm_order.html', context)
def getProfile(request): # checks if a user has inputted in the search field in the navbar if 'search_filter' in request.GET: return index(request) request.session['redirect'] = None # TODO: return the payment-method information stored about a user sa = ShippingAddress() pm = PaymentMethod() # Check if a shippingAddress exists context = {} try: sa = ShippingAddress.objects.get(user_id=request.user.pk) context['sa'] = sa context['hasShippingAddress'] = True except ShippingAddress.DoesNotExist: context['hasShippingAddress'] = False # Check if a PaymentMethod exists # pm.checkIfExists(request.user.pk) try: pm = PaymentMethod.objects.get(user_id=request.user.pk) context['pm'] = pm context['hasPaymentMethod'] = True except PaymentMethod.DoesNotExist: context['hasPaymentMethod'] = False try: context['ss'] = request.session['searchHistory'] except KeyError: context['ss'] = {} try: sh = __getSearchHistory(request) so = __sortedSearchHistory(request) context['sh'] = sh context['so'] = so context['hasSearchHistory'] = True except KeyError: context['hasSearchHistory'] = False return render(request, 'user/account/account_details.html', context)
def updateBillingAddress(request): # checks if a user has inputted in the search field in the navbar if 'search_filter' in request.GET: return index(request) # Try to find a BillingAddress that exists exists try: sa = ShippingAddress.objects.get(user_id=request.user.id) except ShippingAddress.DoesNotExist: sa = ShippingAddress() if request.POST: form = ShippingAddressForm(request.POST, instance=request.user) if form.is_valid(): sa.setShippingAddressAttributes(request, form) sa.save() request.session['hasShippingMethod'] = True request.session.modified = True try: if request.session['redirect'] == None: return redirect('/user/account') else: return redirect(str(request.session['redirect'])) except: return redirect('/user/account') form = ShippingAddressForm( initial={ 'address1': sa.address1, 'address2': sa.address2, 'city': sa.city, 'country': sa.country, 'region': sa.region, 'postalCode': sa.postalCode }) context = {'updateShippingAddressForm': form} return render(request, 'user/account/shippingaddress/shippingaddress_update.html', context)
def getCart(request): # checks if a user has inputted in the search field in the navbar if 'search_filter' in request.GET: return index(request) context = __cartExists(request) return render(request, 'cart/cart.html', context)