Esempio n. 1
0
def product_request(request, slug):
    product = get_object_or_404(Product, slug=slug, is_active=True)
    if request.method == 'POST':
        form = RequestForm(request.POST)
        if form.is_valid():
            product_request = form.cleaned_data

            # Email customers
            html_content = render_to_string(
                'email-product_request_customer.html', dict(product_request=product_request))
            msg = EmailMultiAlternatives(
                u'Archive | %s' % product.name,
                strip_tags(html_content),
                settings.EMAIL_HOST_USER,
                [product_request['customer_email']]
            )
            msg.attach_alternative(html_content, 'text/html')
            msg.send()
            
            # Email managers           
            mail_managers(
                u'Запрос по товару %s от %s' % (product.name,
                    product_request['customer_email']),
                render_to_string('email-product_request_managers.html',
                    dict(product_request=product_request))
            )

            # Update session
            request.session['customer_info'] = {}
            for k, v in product_request.items():
                if k != 'customer_message':
                    request.session['customer_info'][k] = v

            return HttpResponse(
                json_encode({'response': u'Ваш запрос отправлен. Спасибо!', 
                    'result': 'success'}),
                mimetype='application/json'
            )
        else:
            response = {}
            for k, v in form.errors.items():
                response[k] = v[0]
            return HttpResponse(
                json_encode({'response': response, 'result': 'error'}),
                mimetype='application/json'
            )
    else:
        return render(
            request,
            'product_request.html',
            {'form': RequestForm(request.session.get('customer_info', {})), 'product': product}
        )
Esempio n. 2
0
 def wrapper(request, *args, **kwargs):
     response = func(request, *args, **kwargs)
     return HttpResponse(json_encode(response), mimetype='application/json')