Guest User

Untitled

a guest
Feb 11th, 2019
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.25 KB | None | 0 0
  1. from django.db import models
  2. from django.contrib.auth.models import User
  3.  
  4. class Customer(models.Model):
  5. user =models.OneToOneField(User)
  6. birthday =models.DateField()
  7. website =models.CharField(max_length=50)
  8. store =models.CharField(max_length=50)
  9. welcomemail =models.CharField(max_length=50)
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16. def __unicode__(self):
  17. return self.user
  18.  
  19. class Customer_check_attributes(models.Model):
  20. user =models.ManyToManyField(User)
  21. billing_add =models.CharField(max_length=50)
  22. shipping_add =models.CharField(max_length=50)
  23. payment_method =models.CharField(max_length=50)
  24. shipping_method =models.CharField(max_length=50)
  25. reward_points =models.CharField(max_length=50)
  26.  
  27. class Registration_Form(ModelForm):
  28. first_name = forms.CharField(label=(u'First Name'))
  29. last_name = forms.CharField(label=(u'Last Name'))
  30. username = forms.CharField(label=(u'User Name'))
  31. email = forms.EmailField(label=(u'Email Address'))
  32. password = forms.CharField(label=(u'Password'), widget=forms.PasswordInput(render_value=False))
  33.  
  34. :
  35. class Meta:
  36. model=Customer
  37.  
  38. exclude=('user',)
  39.  
  40. <div class="register_div">
  41. {% if form.first_name.errors %}<p class="error">{{ form.first_name.errors }}</p>{% endif %}
  42. <p><label for="first_name" {% if form.first_name.errors %} class="error"{% endif %}>Firstname:</label></p>
  43. <p>{{ form.first_name }}</p>
  44. </div>
  45.  
  46. <div class="register_div">
  47. {% if check.billing_add.errors %}<p class="error">{{ check.billing_add.errors }}</p>{% endif %}
  48. <p><label for="billing_add" {% if check.billing_add.errors %} class="error"{% endif %}>Billing Address:</label></p>
  49. <p>{{ check.billing_add }}</p>
  50. </div>
  51.  
  52. from django.contrib.auth.models import User
  53. from customer_reg.models import Customer,Customer_check_attributes
  54. from django.http import HttpResponseRedirect
  55. from django.shortcuts import render_to_response
  56. from django.template import RequestContext
  57. from customer_reg.forms import Registration_Form, Check_Attribute_Form, LoginForm
  58. from django.contrib.auth import authenticate, login, logout
  59. from django.contrib.auth.decorators import login_required
  60.  
  61.  
  62. def CustomerRegistration(request):
  63. if request.user.is_authenticated():
  64. return HttpResponseRedirect('/profile/')
  65. if request.method == 'POST':
  66. form = Registration_Form(request.POST)
  67. if form.is_valid():
  68. user=User.objects.create_user(username=form.cleaned_data['username'], email=form.cleaned_data['email'], password = form.cleaned_data['password'])
  69. user.first_name = form.cleaned_data['first_name']
  70. user.last_name = form.cleaned_data['last_name']
  71. user.save()
  72.  
  73. #customer=user.get_profile()
  74. #customer.birthday=form.cleaned_data['birthday']
  75. #customer.website=form.cleaned_data['website']
  76. #customer.store=form.cleaned_data['store']
  77. #customer.welcomemail=form.cleaned_data['welcomemail']
  78. #customer.save()
  79.  
  80. customer=Customer(user=user, website=form.cleaned_data['website'], birthday=form.cleaned_data['birthday'], store=form.cleaned_data['store'], welcomemail=form.cleaned_data['welcomemail'])
  81. customer.save()
  82.  
  83. return HttpResponseRedirect('/profile/')
  84. else:
  85. check_form=Check_Attribute_Form()
  86. context={'form':form, 'check':check_form}
  87. return render_to_response('customer_register.html',context , context_instance=RequestContext(request))
  88. else:
  89. ''' user is not submitting the form, show them a blank registration form '''
  90.  
  91. form = Registration_Form()
  92. check_form = Check_Attribute_Form()
  93. context={'form':form,'check':check_form}
  94. return render_to_response('customer_register.html',context , context_instance=RequestContext(request))
  95.  
  96. #############################################PROFILE##########################################################
  97.  
  98. @login_required
  99. def Profile(request):
  100. if not request.user.is_authenticated():
  101. return HttpResponseRedirect('/login/')
  102. #select = select * from auth_users
  103. #reg_users = User.objects.all()
  104. customer = request.user.get_profile
  105. context = {'customer':customer}
  106. return render_to_response('sucess.html',context, context_instance=RequestContext(request))
  107.  
  108.  
  109. ##########################################LOGIN########################################
  110.  
  111.  
  112. def LoginRequest(request):
  113. if request.user.is_authenticated():
  114. return HttpResponseRedirect('/profile/')
  115. if request.method == 'POST':
  116. form = LoginForm(request.POST)
  117. if form.is_valid():
  118. username = form.cleaned_data['username']
  119. password = form.cleaned_data['password']
  120. customer = authenticate(username=username, password=password)
  121. if customer is not None:
  122. login(request, customer)
  123. return HttpResponseRedirect('/profile/')
  124. else:
  125. return render_to_response('login1.html',{'form':form} , context_instance=RequestContext(request))
  126. else:
  127. return render_to_response('login1.html',{'form':form} , context_instance=RequestContext(request))
  128.  
  129. else:
  130. ''' user is not submitting the form, show the login form '''
  131. form = LoginForm()
  132. context={'form':form}
  133. return render_to_response('login1.html',{'form':form} , context_instance=RequestContext(request))
  134.  
  135.  
  136. #######################################checkout attributes##################################################
  137.  
  138.  
  139. def Checkout_Attributes(request):
  140.  
  141.  
  142. #try:
  143. #user_profile = Customer.objects.get(user=request.user)
  144. #except Customer.DoesNotExist:
  145. # this user has no profile
  146. # return render_to_response('error')
  147. #user_profile_form = Check_Attribute_Form(instance=user_profile)
  148. #return render_to_response('checkout.html',{'form':user_profile_form},context_instance=RequestContext(request))
  149.  
  150. check_form = Check_Attribute_Form()
  151. context={'form':check_form}
  152. return render_to_response('checkout.html',context,context_instance=RequestContext(request))
  153.  
  154. def Set_Checkout_Attributes(request):
  155.  
  156. #if request.user.is_authenticated():
  157. #return HttpResponseRedirect('/checkout/')
  158.  
  159. if request.method == 'POST':
  160. check_form = Check_Attribute_Form(request.POST)
  161. if check_form.is_valid():
  162. customer_check=Customer_check_attributes(billing_add=check_form.cleaned_data['billing_add'],shipping_add=check_form['shipping_add'],payment_method=check_form['payment_method'],shipping_method=check_form['shipping_method'],reward_points=check_form['reward_points'])
  163. customer_check.save()
  164. return HttpResponseRedirect('/profile/')
  165. else:
  166. check_form=Check_Attribute_Form()
  167. return render_to_response('a.html',{'check_form':check_form} , context_instance=RequestContext(request))
  168. else:
  169. return render_to_response('f')
  170.  
  171.  
  172.  
  173. ###############################################Logout#############################################################
  174.  
  175. def LogoutRequest(request):
  176. logout(request)
  177. return HttpResponseRedirect('/login/')
Add Comment
Please, Sign In to add comment