Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.50 KB | None | 0 0
  1. from django.contrib.localflavor.us.us_states import STATE_CHOICES
  2. from django import forms
  3. from django.contrib.localflavor.us.forms import USPhoneNumberField, USStateField
  4. from satchmo.contact.models import Contact
  5. from atsi.models import JoinRequest
  6. from django.forms.util import ErrorList
  7. from django.utils.safestring import mark_safe
  8. from atsi.models import *
  9. from django.contrib.formtools.wizard import FormWizard
  10. from django.forms.widgets import RadioSelect, CheckboxSelectMultiple, PasswordInput, Select
  11. from django.http import HttpResponseRedirect
  12. from django.core.urlresolvers import reverse
  13. from atsi.atsi_settings import COUNTRIES
  14. from django.contrib.auth.models import User
  15. import datetime
  16. from satchmo.shop.models import Cart
  17. from django.contrib.sites.models import Site
  18. from django.contrib.auth.views import login
  19. from ImageFile import raise_ioerror
  20.  
  21. class ATSIMembershipWizard(FormWizard):
  22.  
  23.  
  24. def get_template(self, step):
  25. return ['signup/membership/wizard_%s.html' % step]
  26.  
  27.  
  28. def done(self, request, form_list):
  29. form_copy = request.POST.copy()#contact info
  30.  
  31. membershiptypes = form_copy['0-membershiptypes']
  32.  
  33. name = form_copy['1-name']
  34. website = form_copy['1-website']
  35. city = form_copy['1-city']
  36. zip = form_copy['1-zip']
  37. addressln1 = form_copy['1-addressln1']
  38. addressln2 = form_copy['1-addressln2']
  39. addressln3 = form_copy['1-addressln3']
  40. tollfree = form_copy['1-tollfree']
  41. description = form_copy['1-description']
  42. fax = form_copy['1-fax']
  43. opt_outlisting = form_copy['1-opt_outlisting']
  44. state = form_copy['1-state']
  45. phone = form_copy['1-phone']
  46. country = form_copy['1-country']
  47.  
  48. platforms = form_copy['2-platforms']
  49. equipment_types = form_copy['2-equipment_types']
  50. services = form_copy['2-services']
  51. affilitates = form_copy['2-affilitates']
  52.  
  53. username = form_copy['3-username']
  54. password = form_copy['3-password']
  55. password2 = form_copy['3-password2']
  56. first_name = form_copy['3-first_name']
  57. last_name = form_copy['3-last_name']
  58. dob = form_copy['3-dob']
  59. email = form_copy['3-email']
  60.  
  61. date_time_created = datetime.datetime.now()
  62.  
  63. # create a user
  64. try:
  65. u = User(username=username, first_name=first_name, last_name=last_name, email=email, password=password)
  66. u.save()
  67. login(request, u)
  68. except ValueError:
  69. print "error"
  70.  
  71. #create contact
  72. try:
  73. c = Contact(first_name=first_name, last_name=last_name, user=u, email=email, create_date=date_time_created)
  74. c.save()
  75. except ValueError:
  76. print "error"
  77.  
  78. #create ATSIOrganization
  79. try:
  80. membership_type = MembershipType.objects.get(id=membershiptypes)
  81. except ValueError:
  82. print "error"
  83.  
  84.  
  85. try:
  86. org = ATSIOrganization(membership_type=membership_type, name=name, role='Atsi Member')
  87. org.save()
  88. except ValueError:
  89. print "error"
  90.  
  91. try:
  92. site = Site.objects.get_current()
  93. newcart = Cart(site=site)
  94. newcart.save()
  95. request.session['cart'] = newcart.id
  96. except ValueError:
  97. print "error"
  98.  
  99.  
  100. try:
  101. newcart.add_item(membership_type.product.product, number_added=1)
  102. newcart.save()
  103. except ValueError:
  104. print "error"
  105.  
  106. return HttpResponseRedirect(reverse('DUMMY_satchmo_checkout-step2'))
  107.  
  108. class AtsiMembershipType(forms.Form):
  109. membershiptypes = forms.ModelChoiceField(MembershipType.objects.filter(published=True), widget=RadioSelect(), label='Choose The Size of Your Company', empty_label=None)
  110.  
  111. def clean(self):
  112. cleaned_data = self.cleaned_data
  113. membershiptypes = self.cleaned_data.get('membershiptypes')
  114.  
  115. if not membershiptypes :
  116. msg = "Please Select One"
  117. self._errors["membershiptypes"] = ErrorList([msg])
  118.  
  119. class AtsiMembershipCompanyInfo1(forms.Form):
  120. platforms = forms.ModelMultipleChoiceField(CommunicationPlatform.objects.filter(published=True), widget=CheckboxSelectMultiple(), label='Which communication platforms do you use?')
  121. affilitates = forms.ModelMultipleChoiceField(RegionalAffiliate.objects.filter(published=True), widget=CheckboxSelectMultiple(), label='What Affiliations does your company have?')
  122. equipment_types = forms.ModelMultipleChoiceField(EquipmentType.objects.filter(published=True), widget=CheckboxSelectMultiple(), label='Which Type of Equipment does your company use?')
  123. services = forms.ModelMultipleChoiceField(ServiceOffered.objects.filter(published=True), widget=CheckboxSelectMultiple(), label='Which services does your company provide?')
  124.  
  125. class AtsiMembershipCompanyInfo(forms.ModelForm):
  126.  
  127. ATSI_STATE_CHOICES = list(STATE_CHOICES)
  128. ATSI_STATE_CHOICES.insert(0, ('', '---------'))
  129.  
  130. COUNTRY_CHOICES = list(COUNTRIES)
  131. COUNTRY_CHOICES.insert(0, ('', '---------'))
  132.  
  133. phone = USPhoneNumberField(label="Phone Number")
  134. state = USStateField(widget=Select(choices=ATSI_STATE_CHOICES))
  135. city = forms.CharField(required=True)
  136. country = forms.CharField(widget=Select(choices=COUNTRY_CHOICES))
  137.  
  138. def __init__(self, *args, **kw):
  139. super(forms.ModelForm, self).__init__(*args, **kw)
  140. self.fields.keyOrder = [
  141. 'opt_outlisting',
  142. 'name',
  143. 'country',
  144. 'addressln1',
  145. 'addressln2',
  146. 'addressln3',
  147. 'city',
  148. 'state',
  149. 'zip',
  150. 'phone',
  151. 'fax',
  152. 'tollfree',
  153. 'website',
  154. 'description', ]
  155.  
  156. class Meta:
  157. model=ATSIOrganization
  158.  
  159. class AtsiMembershipAdminInfo(forms.ModelForm):
  160. title = forms.CharField(max_length=100)
  161. username = forms.CharField(max_length=100, label="Create Username")
  162. password = forms.CharField(max_length=100, widget=PasswordInput(), label="Create Password" )
  163. password2 = forms.CharField(max_length=100, widget=PasswordInput(), label="Re-Type Password" )
  164.  
  165. def __init__(self, *args, **kw):
  166. super(forms.ModelForm, self).__init__(*args, **kw)
  167. self.fields.keyOrder = [
  168. 'username',
  169. 'password',
  170. 'password2',
  171. 'first_name',
  172. 'last_name',
  173. 'dob',
  174. 'email',
  175. ]
  176.  
  177.  
  178. def clean(self):
  179. cleaned_data = self.cleaned_data
  180. title = self.cleaned_data.get('title')
  181. username = self.cleaned_data.get('username')
  182. password = self.cleaned_data.get('password')
  183. email = self.cleaned_data.get('email')
  184.  
  185. #check to make sure the username has not been used
  186. if User.objects.filter(username=username).count() >0:
  187. msg = "This username is already in use"
  188. self._errors["username"] = ErrorList([msg])
  189.  
  190. if User.objects.filter(email=email).count() > 0:
  191. msg = "This Email is already in use"
  192. self._errors["email"] = ErrorList([msg])
  193.  
  194.  
  195.  
  196. class Meta:
  197. model=Contact
  198.  
  199. class EmployeeSignUpForm(forms.Form):
  200. first_name = forms.CharField(max_length=50)
  201. last_name = forms.CharField(max_length=50)
  202. phone_number = USPhoneNumberField()
  203. email = forms.EmailField()
  204. selected_organization = forms.CharField(max_length=100, label="Enter a Company")
  205.  
  206.  
  207. def clean(self):
  208. cleaned_data = self.cleaned_data
  209. first_name = cleaned_data.get('first_name')
  210. last_name = cleaned_data.get('last_name')
  211. phone_number = cleaned_data.get('phone_number')
  212. email = cleaned_data.get('email')
  213. selected_organization = cleaned_data.get('selected_organization')
  214.  
  215. form_msg = None;
  216. #If a user has already submitted a company request raise General Validation Error
  217. # To tell if they have already submitted a request use the email they have entered
  218.  
  219. existing_join_requests = JoinRequest.objects.filter(email=email).count()
  220. if existing_join_requests > 0:
  221. user_exists = True
  222. else:
  223. user_exists = False
  224.  
  225. companies = ATSIOrganization.objects.filter(name=selected_organization).count()
  226.  
  227. if companies > 0:
  228. company_exists = True
  229. else:
  230. company_exists = False
  231.  
  232.  
  233. if user_exists :
  234. msg = "This Email Is already associated with an Join Request"
  235. self._errors["email"] = ErrorList([msg])
  236. existing_joinrequest = JoinRequest.objects.get(email=email)
  237. hash = existing_joinrequest.hash
  238. vmsg = mark_safe("To cancel your current Join Request for %s please %s:" % (selected_organization, '<a href="/signup/joinrequest/cancel/?hs='+hash+'&type=1" %}">click here</a>'))
  239. #form_msg = forms.ValidationError(vmsg)
  240.  
  241. if not company_exists:
  242. msg = "Please Select A Valid Company"
  243. self._errors["selected_organization"] = ErrorList([msg])
  244.  
  245.  
  246. # if form_msg and company_exists: raise form_msg
  247.  
  248. return cleaned_data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement