1 | import json
|
---|
2 | from django.views.generic import TemplateView
|
---|
3 | from django.views.generic.edit import FormView
|
---|
4 | from django.views.generic.list import MultipleObjectMixin
|
---|
5 | from django.views.generic.list import ListView
|
---|
6 | from forms import PromoModelForm
|
---|
7 | from django.http import HttpResponse
|
---|
8 | from django.template import RequestContext
|
---|
9 | from models import ContestEntries, PromoCodes,EmailRecord,loguservisits, Customers
|
---|
10 | from django.core.mail import EmailMessage
|
---|
11 | from django.conf import settings
|
---|
12 | from django.template.loader import render_to_string
|
---|
13 | from django.contrib.localflavor.us.us_states import US_STATES
|
---|
14 | from django.shortcuts import render_to_response
|
---|
15 | from django.db import IntegrityError
|
---|
16 | from django.http import Http404
|
---|
17 | from django.utils.translation import ugettext as _
|
---|
18 | import datetime
|
---|
19 | import hashlib
|
---|
20 | from hashlib import md5
|
---|
21 | #from reportlab.pdfgen import canvas
|
---|
22 | from django.http import HttpResponse
|
---|
23 | from django.db import connection, transaction
|
---|
24 | #import reportlab
|
---|
25 | import cx_Oracle
|
---|
26 | from django.db import transaction
|
---|
27 | from django.core import serializers
|
---|
28 |
|
---|
29 |
|
---|
30 | class IndexView(FormView, MultipleObjectMixin): #A view that displays a form. On error, redisplays the form with validation errors; on success, redirects to a new URL.#
|
---|
31 | template_name = "index.html" #(A base view for displaying a list of objects. It is not intended to be used directly, but
|
---|
32 | form_class = PromoModelForm
|
---|
33 | success_url = ''
|
---|
34 | model = ContestEntries
|
---|
35 |
|
---|
36 |
|
---|
37 | def get(self, request, *args, **kwargs):
|
---|
38 |
|
---|
39 | self.object_list = self.get_queryset()
|
---|
40 | allow_empty = self.get_allow_empty()
|
---|
41 | if not allow_empty and len(self.object_list) == 0:
|
---|
42 | raise Http404(_(u"Empty list and '%(class_name)s.allow_empty' is False.")
|
---|
43 | % {'class_name': self.__class__.__name__})
|
---|
44 |
|
---|
45 | form_class = self.get_form_class()
|
---|
46 | form = self.get_form(form_class)
|
---|
47 | context = self.get_context_data(object_list=self.object_list)
|
---|
48 | context['form'] = form
|
---|
49 | return self.render_to_response(context)
|
---|
50 |
|
---|
51 | def get_context_data(self, **kwargs):
|
---|
52 | context = super(MultipleObjectMixin, self).get_context_data(**kwargs)
|
---|
53 | context['states'] = self.request.GET.get('shop_state', '')
|
---|
54 | context['email'] = self.request.GET.get('email', '')
|
---|
55 | context['back_url'] = self.request.META.get('HTTP_REFERER', '/')
|
---|
56 | context['website_url'] = 'http://' + self.request.META['HTTP_HOST']
|
---|
57 | return context
|
---|
58 |
|
---|
59 | def post(self, request, *args, **kwargs):
|
---|
60 | if request.is_ajax():
|
---|
61 | entries = self.request.POST['model']
|
---|
62 | entry = json.loads(entries)
|
---|
63 | PROMOCODE = entry['PROMOCODE']
|
---|
64 | attempt = entry['attempt']
|
---|
65 | CUST_PERSONAL_EMAIL = entry['CUST_PERSONAL_EMAIL']
|
---|
66 | CUST_FIRST_NAME = entry['CUST_FIRST_NAME']
|
---|
67 | CUST_LAST_NAME = entry['CUST_LAST_NAME']
|
---|
68 | CUST_TYPE_OF_BUSINESS = entry['CUST_TYPE_OF_BUSINESS']
|
---|
69 | CUST_ADDRESS = entry['CUST_ADDRESS']
|
---|
70 | CUST_CITY = entry['CUST_CITY']
|
---|
71 | CUST_STATE = entry['CUST_STATE']
|
---|
72 | CUST_POSTAL_CODE = entry['CUST_POSTAL_CODE']
|
---|
73 | CUST_HOME_PHONE = entry['CUST_HOME_PHONE']
|
---|
74 | value = entry['radio1']
|
---|
75 | form_type = entry['form_type']
|
---|
76 | attempt = attempt + 1
|
---|
77 | error = {'attempt': attempt, 'status': 'error', 'form_type': 'simple'}
|
---|
78 | moreinfo = {'attempt': attempt, 'status': 'moreinfo', 'form_type': 'extended', }
|
---|
79 | success = {'attempt': attempt, 'status': 'success', 'form_type': 'simple', }
|
---|
80 | fail = {'attempt': attempt, 'status': 'fail', 'form_type': 'simple'}
|
---|
81 | form_success = {'attempt': attempt, 'status': 'form-success',
|
---|
82 | 'form_type': 'extended', 'PROMOCODE': self.default_promocode}
|
---|
83 |
|
---|
84 | try:
|
---|
85 | ContestEntries.objects.get(promocode=PROMOCODE)
|
---|
86 | ContestEntries.objects.filter(promocode=PROMOCODE).update(first_name=CUST_FIRST_NAME,last_name=CUST_LAST_NAME,shop_name=CUST_TYPE_OF_BUSINESS,
|
---|
87 | shop_address=CUST_ADDRESS,shop_city=CUST_CITY,shop_state=CUST_STATE,shop_zip=CUST_POSTAL_CODE,phone=CUST_HOME_PHONE,email=CUST_PERSONAL_EMAIL,address=value,)
|
---|
88 | return HttpResponse(json.dumps(form_success),
|
---|
89 | mimetype='application/json')
|
---|
90 |
|
---|
91 | except:
|
---|
92 | entry = ContestEntries(
|
---|
93 | first_name=CUST_FIRST_NAME,
|
---|
94 | last_name=CUST_LAST_NAME,
|
---|
95 | shop_name=CUST_TYPE_OF_BUSINESS,
|
---|
96 | shop_address=CUST_ADDRESS,
|
---|
97 | email=CUST_PERSONAL_EMAIL,
|
---|
98 | shop_city=CUST_CITY,
|
---|
99 | shop_state=CUST_STATE,
|
---|
100 | shop_zip=CUST_POSTAL_CODE,
|
---|
101 | phone=CUST_HOME_PHONE,
|
---|
102 | promocode=PROMOCODE,
|
---|
103 | address=value,)
|
---|
104 | self.send_confirmation_email(CUST_PERSONAL_EMAIL,
|
---|
105 | entry.confirmation_code)
|
---|
106 | entry.save()
|
---|
107 | return HttpResponse(json.dumps(form_success),
|
---|
108 | mimetype='application/json')
|
---|
109 |
|
---|
110 | else:
|
---|
111 | return render_to_response('error.html',
|
---|
112 | context_instance=RequestContext(request))
|
---|
113 |
|
---|
114 |
|
---|
115 |
|
---|
116 |
|
---|
117 | """
|
---|
118 | Displays the email confirmation page
|
---|
119 |
|
---|
120 | """
|
---|
121 |
|
---|
122 |
|
---|
123 | class ConfirmationView(ListView):
|
---|
124 | template_name = "confirmation.html"
|
---|
125 | model = ContestEntries
|
---|
126 |
|
---|
127 |
|
---|
128 | def get(self, request, *args, **kwargs):
|
---|
129 | self.object_list = self.get_queryset()
|
---|
130 | allow_empty = self.get_allow_empty()
|
---|
131 | if not allow_empty and len(self.object_list) == 0:
|
---|
132 | raise Http404(_(u"Empty list and '%(class_name)s.allow_empty' is False.")
|
---|
133 | % {'class_name': self.__class__.__name__})
|
---|
134 |
|
---|
135 | context = self.get_context_data(object_list=self.object_list)
|
---|
136 | context.update()
|
---|
137 | return render_to_response('confirmation.html', context, context_instance=RequestContext(request))
|
---|
138 |
|
---|
139 |
|
---|
140 | def get_context_data(self, **kwargs):
|
---|
141 | context = super(ConfirmationView, self).get_context_data(**kwargs)
|
---|
142 | email = self.request.GET.get('email', '')
|
---|
143 | decodeemail = email.decode('base64')
|
---|
144 | code = self.request.GET.get('code', '')
|
---|
145 | changecode = code.decode('base64')
|
---|
146 | now = datetime.datetime.now()
|
---|
147 |
|
---|
148 | try:
|
---|
149 | email_record = EmailRecord.objects.get(email=email)
|
---|
150 | except:
|
---|
151 | pass
|
---|
152 | else:
|
---|
153 | email_record.now_confirmed = True
|
---|
154 | date_confirmed = str(now).split()
|
---|
155 | email_record.date_confirmed = date_confirmed[0]
|
---|
156 | email_record.save()
|
---|
157 |
|
---|
158 | try:
|
---|
159 | entry = ContestEntries.objects.get(email=decodeemail,
|
---|
160 | confirmation_code=changecode)
|
---|
161 | except:
|
---|
162 | context['result'] = 'error'
|
---|
163 | else:
|
---|
164 | entry.confirmed = True
|
---|
165 | entry.save()
|
---|
166 | context['result'] = 'confirmed'
|
---|
167 | return context
|
---|
168 |
|
---|
169 |
|
---|
170 |
|
---|
171 | class Registerview(FormView,ListView):
|
---|
172 | template_name = "valid.html"
|
---|
173 | model = Customers
|
---|
174 | form_class = PromoModelForm
|
---|
175 |
|
---|
176 | def get(self, request, *args, **kwargs):
|
---|
177 | week = Customers.objects.all()
|
---|
178 | try:
|
---|
179 | p=Customers.objects.get(PROMOCODE=request.GET['PROMOCODE'])
|
---|
180 | wer=Customers.objects.filter(PROMOCODE=request.GET['PROMOCODE'])
|
---|
181 | if request.method == 'POST': # If the form has been submitted...
|
---|
182 | form = PromoModelForm(request.POST, request.FILES, instance=wer) # A form bound to the POST data
|
---|
183 | if form.is_valid(): # All val
|
---|
184 | x = {'form': form, }
|
---|
185 | raise Http404
|
---|
186 | except Customers.DoesNotExist:
|
---|
187 | raise Http404
|
---|
188 | else:
|
---|
189 | form = PromoModelForm(instance=p) # An unbound form
|
---|
190 | x = RequestContext(request,{
|
---|
191 | 'week':week,
|
---|
192 | 'form':form
|
---|
193 | })
|
---|
194 | return render_to_response('valid.html',x, context_instance = RequestContext(request))
|
---|
195 |
|
---|
196 |
|
---|
197 |
|
---|
198 |
|
---|
199 |
|
---|
200 |
|
---|
201 | class EmailView(TemplateView):
|
---|
202 | template_name = "email_confirmation.html"
|
---|
203 |
|
---|