ajax_request render_to popups buy1click html def buy1click request pro

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
@ajax_request
@render_to('popups/buy1click.html')
def buy1click(request, product_id):
site_settings = get_site_settings()
product = get_object_or_404(Product, pk=product_id)
form = Form1Click(request.POST or None)
if request.method == 'POST':
if form.is_valid():
message = product.name + '<br>'
message += form.cleaned_data['name'] + '<br>'
message += form.cleaned_data['phone'] + '<br>'
send_html_mail(u'Заказ в один клик на сайте Babyprestige.ru', 'text', message, settings.EMAIL_FROM,
[site_settings.email])
return HttpResponse(simplejson.dumps({'response': u'Заявка отправлена', 'result': 'success'}))
else:
t = loader.get_template('popups/buy1click.html')
ctx = RequestContext(request, {'form': form, 'product': product})
response = t.render(ctx)
return HttpResponse(simplejson.dumps({'response': unicode(response), 'result': 'error'}))
return locals()
@render_to('shopping/step1.html')
def step1(request):
cart = Cart(request)
if cart.count() == 0:
return {'error': u'Ошибка! В корзине нет товаров.'}
init = {'city': 1}
if request.session.get('step1', False):
init = request.session['step1']
form = StepFirst(request.POST or None, initial=init)
if request.method == 'POST' and form.is_valid():
request.session['step2'] = {}
request.session['step1'] = form.cleaned_data
request.session.modified = True
return redirect('step2')
return locals()
@render_to('shopping/step2.html')
def step2(request):
is_send = False
cart = Cart(request)
if cart.count() == 0:
return {'error': u'Ошибка! В корзине нет товаров.'}
if not request.session.get('step1'):
return {'error': u'Ошибка! Не заполнены данные предыдущего шага.'}
city = get_object_or_404(City, pk=int(request.session['step1']['city']))
deliveries = get_deliveries_by_city(city)
payments = get_payments_by_city(city)
delivery = deliveries[0]
# forms
form = StepSecond(request.POST or None)
params = {}
if delivery.type == 2:
params['city_id'] = city.id
form_delivery_item = form_delivery_classes.get(delivery.type, AddressForm)(request.POST or None, **params)
# обрабатываем форму
if request.method == 'POST':
is_send = True
if form.is_valid():
delivery = get_object_or_404(Delivery, pk=int(form.cleaned_data['delivery_id']))
payment = get_object_or_404(Payment, pk=int(form.cleaned_data['payment_id']))
comment = form.cleaned_data['comment']
params = {}
if delivery.type == 2:
params['city_id'] = city.id
form_delivery_item = form_delivery_classes.get(delivery.type, AddressForm)(request.POST, **params)
if form_delivery_item.is_valid():
if not cart.cart.checked_out:
if delivery.type == 2:
issue_id = Issue.objects.get(pk=int(form_delivery_item.cleaned_data['issue'])).id
else:
issue_id = None
if delivery.type in [4, 5]:
street = form_delivery_item.cleaned_data['street']
house = form_delivery_item.cleaned_data['house']
corp = form_delivery_item.cleaned_data['corp']
build = form_delivery_item.cleaned_data['build']
flat = form_delivery_item.cleaned_data['flat']
address = u'Улица %s, дом %s, корпус %s, строение %s, офис(квартира) %s' \
% (street, house, corp, build, flat)
else:
try:
address = form_delivery_item.cleaned_data['address']
except KeyError:
address = ''
request.session['step2'] = {
'city': city.name,
'delivery_id': delivery.id,
'payment_id': payment.id,
'comment': comment,
'address': address,
'issue_id': issue_id,
}
request.session.modified = True
return redirect(reverse('shopping.views.checkout'))
else:
return {'error': u'Данный заказ уже был зарегистрирован.'}
return locals()
@render_to('shopping/form_delivery_item.html')
def form_delivery_item(request, delivery_id, city_id=None):
params = {}
delivery = get_object_or_404(Delivery, pk=delivery_id)
if delivery.type == 2 and city_id:
city = get_object_or_404(City, pk=city_id)
params['city_id'] = city.id
form_delivery_item = form_delivery_classes.get(delivery.type, AddressForm)(request.POST or None, **params)
return locals()
@render_to('shopping/checkout.html')
def checkout(request):
cart = Cart(request)
if cart.count() == 0:
return {'error': u'Ошибка! В корзине нет товаров.'}
if not request.session.get('step1') or not request.session.get('step2'):
return {'error': u'Ошибка! Не заполнены данные предыдущего шага.'}
if cart.cart.checked_out:
return {'error': u'Ошибка! Данный заказ уже завершен.'}
products = u"\n".join([u'%s %s, кол-во: %s, на сумму: %s' %
(item.product.product.name, item.product.name, item.quantity, item.total_price)
for item in cart])
city = get_object_or_404(City, pk=int(request.session['step1']['city']))
delivery = get_object_or_404(Delivery, pk=int(request.session['step2']['delivery_id']))
payment = get_object_or_404(Payment, pk=int(request.session['step2']['payment_id']))
if delivery.type == 2:
issue = get_object_or_404(Issue, pk=int(request.session['step2']['issue_id']))
else:
issue = None
order = Order()
order.first_name = request.session['step1']['first_name']
order.last_name = request.session['step1']['last_name']
order.phone = request.session['step1']['phone']
order.phone2 = request.session['step1']['phone2']
order.email = request.session['step1']['email']
order.city = city.name
order.delivery = delivery
order.payment = payment
order.comment = request.session['step2']['comment']
order.products = products
order.address = request.session['step2']['address']
order.issue = issue
order.cart = cart.cart
order.save()
sending_mail(order, cart)
cart.cart.checked_out = True
cart.cart.save()
del request.session['step1']
del request.session['step2']
request.session.modified = True
return locals()
def sending_mail(order, cart):
email_params = {
'order': order,
'cart': cart,
'SITE_DOMAIN': site_settings.site.domain,
'EMAIL': site_settings.email
}
send_html_mail(u'Заказ №%d на сайте Babyprestige.ru' % order.id, 'text',
render_to_string('email/order.html', email_params), settings.EMAIL_FROM,
[order.email, site_settings.email])
@ajax_request
def post_index(request, idx):
data = {}
try:
post_idx = PostIdx.objects.get(INDEX=idx)
city = []
if post_idx.REGION:
city.append(capfirst(post_idx.REGION.lower()))
if post_idx.AUTONOM:
city.append(capfirst(post_idx.AUTONOM.lower()))
if post_idx.AREA:
city.append(capfirst(post_idx.AREA.lower()))
if post_idx.CITY:
city.append(capfirst(post_idx.CITY.lower()))
if post_idx.CITY_1:
city.append(capfirst(post_idx.CITY_1.lower()))
data['city'] = ', '.join(city)
data['res'] = 1
except PostIdx.DoesNotExist:
data['res'] = 0
return data
def get_city_by_ip(request):
'''
try:
client_address = request.META['HTTP_X_FORWARDED_FOR']
except KeyError:
client_address = request.META['REMOTE_ADDR']
ipgeobases = IPGeoBase.objects.by_ip(client_address)
if ipgeobases.exists():
city_name = ipgeobases[0].city
try:
city = City.objects.get(name__exact=city_name)
except:
city = City.objects.get(pk=1)
else:
city = City.objects.get(pk=1)
'''
city = City.objects.get(pk=1)
return city
def get_deliveries_by_city(city):
if city.delivery_set.count() == 0:
delivery = Delivery.objects.filter(active=True, main=True)
else:
delivery = city.delivery_set.all()
return delivery
def get_payments_by_city(city):
if city.payment_set.count() == 0:
payments = Payment.objects.filter(active=True, main=True)
else:
payments = city.payment_set.filter(active=True)
return payments
form_delivery_classes = {
1: AddressForm,
2: IssueForm,
3: EmptyForm,
4: CourierDeliveryForm,
5: CourierDeliveryForm,
}