示例#1
0
文件: views.py 项目: ribozz/cratis
def update_address(request):
    order = Order.objects.get(pk=request.session['order_id'])

    if 'address_id' in request.POST:
        if order.address and order.address.temporary:
            order.address.delete()

        if request.POST['address_id'] == 0:
            order.address = None
        else:
            try:
                order.address = Address.objects.get(pk=request.POST['address_id'], profile=request.user.get_profile())

                for address in request.user.get_profile().saved_adresses():
                    address.is_main = (order.address.pk == address.pk)
                    address.save()

            except ObjectDoesNotExist:
                order.address = None
        order.save()
    else:
        if not order.address or not order.address.temporary:
            address = Address()
            address.temporary = True
            address.profile = request.user.get_profile() if not request.user.is_anonymous() else None
            address.save()
            order.address = address
            order.save()

        #if 'country' in request.POST:
        #    order.address.country = request.POST['country']
        #order.address.city = request.POST['city']
        #order.address.post_index = request.POST['post_index']
        #order.address.email = request.POST['email']
        #order.address.lastname = request.POST['lastname']
        #order.address.street_address = request.POST['street_address']
        #order.address.firstname = request.POST['firstname']
        #order.address.phone = request.POST['phone']

        address_form = AddressForm(request.POST, instance=order.address)

        if not address_form.is_valid():
            # still save data
            for key, value in request.POST.items():
                setattr(order.address, key, value)
            order.address.save()
            return HttpResponse(json.dumps({'ok': False, 'errors': address_form._errors}), mimetype='application/javascript')
        address_form.save()

    if order.address.email:
        order.email = order.address.email
        order.save()

    return HttpResponse(json.dumps({'ok':True}), mimetype='application/javascript')
示例#2
0
文件: views.py 项目: ribozz/cratis
def validate(request):
    order = Order.objects.get(pk=request.session['order_id'])

    if not order.address:
        return json_response({'ok': False, 'type': 'overall', 'msg': str(_('Address is not selected'))})

    if order.address.temporary:
        address_form = AddressForm(order.address.__dict__, instance=order.address)
        if not address_form.is_valid():
            return json_response({'ok': False, 'type': 'form', 'errors': address_form._errors,
                                  'msg': _('Address is not filled correctly')})

    if not request.user or request.user.is_anonymous():
        result = User.objects.filter(email=order.email)
        if result:
            return json_response({'ok': False, 'type': 'overall', 'msg': _('Email belongs to one of site accounts. Please login to the site to use this email.')})

    return json_response({'ok': True})