def creditcardview(request, user_id, creditcard_id=None): # instantiate variables creditcard = None creditcards_list = CreditCard.objects.filter(user=user_id) if creditcard_id: try: creditcard = CreditCard.objects.get(pk=creditcard_id) creditcards_list = CreditCard.objects.filter(user=user_id).exclude( pk=creditcard_id) except: return rendermessage( request, 'Credit Card | Error', 'Credit card does not exist', '', reverse('users:creditcards', args=[str(user_id)]), 'creditcards page') if request.method == "POST": if creditcard: creditcardform = CreditCardForm(request.POST, instance=creditcard) else: creditcardform = CreditCardForm(request.POST, initial=request.POST) if creditcardform.is_valid(): newcreditcard = creditcardform.save(commit=False) newcreditcard.user_id = user_id newcreditcard.save() return rendermessage( request, 'Credit card | Confirmation', 'Credit card added/updated succefully', '', reverse('users:creditcards', args=[str(user_id)]), 'creditcards page') return rendermessage( request, 'Credit card | Error', 'Credit card ', 'There was an error processing the creditcard. ' + creditcardform.error_message, reverse('users:creditcards', args=[str(user_id)]), 'creditcards page') else: # GET if creditcard: creditcardform = CreditCardForm(instance=creditcard) button_text = 'Modify credit card' page_title = creditcard.name else: creditcardform = CreditCardForm() button_text = 'Add credit card' page_title = 'New' return render( request, 'creditcards/creditcards.html', { 'user_id': user_id, 'creditcard': creditcard, 'form': creditcardform, 'page_title': page_title, 'creditcards_list': creditcards_list, 'button_text': button_text, })
def addressview(request, user_id, address_id=None): address = None addresses_list = Address.objects.all().filter(user=user_id) if address_id: try: address = Address.objects.get(pk=address_id) addresses_list = Address.objects.filter(user=user_id).exclude( pk=address_id) except: return rendermessage( request, 'Error', 'Shipping does not exist', '', reverse('users:addresses', args=[str(user_id)]), 'shipping addresses page') if request.method == "POST": if address: addressform = AddressForm(request.POST, instance=address) else: addressform = AddressForm(request.POST, initial=request.POST) if addressform.is_valid(): newaddress = addressform.save(commit=False) newaddress.user_id = user_id newaddress.save() return rendermessage( request, 'New address confirmation', 'Shipping address added succefully', '', reverse('users:addresses', args=[str(user_id)]), 'addresses page') return rendermessage( request, 'Shipping address | Error', 'Shipping address ', 'There was an error adding the shipping address. ' + addressform.error_message, reverse('users:addresses', args=[str(user_id)]), 'shipping addresses page') else: # GET if address: addressform = AddressForm(instance=address) button_text = 'Modify shiping address' page_title = address.name else: addressform = AddressForm() button_text = 'Add new shipping address' page_title = 'New' return render( request, 'addresses/addresses.html', { 'user_id': user_id, 'address': address, 'page_title': page_title, 'form': addressform, 'addresses_list': addresses_list, 'button_text': button_text, })
def creditcarddeleteview(request, user_id, creditcard_id): try: user = User.objects.get(pk=user_id) creditcard = CreditCard.objects.get(pk=creditcard_id) except: return render_access_denied_message(request) form = DeleteCreditCardConfirmation(request) if request.method == 'POST': form = DeleteCreditCardConfirmation(request.POST) if request.POST.get('Confirm'): #Confirmed deletion creditcard.delete() return rendermessage( request, 'Credit Card Delete | Confirmation', 'Credit card removed succefully', '', reverse('users:creditcards', args=[str(user_id)]), 'creditcards page') else: # Cancelled deletion returned to credit cards page return HttpResponseRedirect( reverse('users:creditcards', args=[str(user_id)])) return renderconfirmation( request, form, 'Delete credit card', 'Please confirm', 'Are you sure you want to delete the credit card?')
def addressdeleteview(request, user_id, address_id): try: user = User.objects.get(pk=user_id) address = Address.objects.get(pk=address_id) except: return render_access_denied_message(request) form = DeleteAddressConfirmation(request) if request.method == 'POST': form = DeleteAddressConfirmation(request.POST) if request.POST.get('Confirm'): #Confirmed deletion address.delete() return rendermessage( request, 'Delete confirmation', 'Shipping address removed succefully', '', reverse('users:addresses', args=[str(user_id)]), 'shipping addresses page') else: # Cancelled deletion returned to the shippping addresses page return HttpResponseRedirect( reverse('users:addresses', args=[str(user_id)])) return renderconfirmation( request, form, 'Delete shipping address', 'Please confirm', 'Are you sure you want to delete the shipping address?')
def changepassword(request, user_id): form = PasswordChangeForm(user=request.user) if request.method == 'POST': if "Cancel" in request.POST: return reverse('users:profile', args=[str(user_id)]), form = PasswordChangeForm(user=request.user, data=request.POST) if form.is_valid(): form.save() update_session_auth_hash(request, form.user) return rendermessage(request, 'Password Confirmation', 'Password changed succefully', '', reverse('users:profile', args=[str(user_id)]), 'profile') return render(request, 'users/changepassword.html', { 'form': form, 'user_name': User.objects.get(pk=user_id) })
def confirmation_page(request, user_id): return rendermessage(request, 'Profile', 'Profile updated succefully', '', reverse('users:profile', args=[str(user_id)]), 'profile page')