Exemplo n.º 1
0
def orders_partner_csv(request):
    response = make_http_response(content_type='text/html')
    response['Content-Disposition'] = 'attachment; filename="orders.csv"'

    orders = Order.objects.filter(container__owner=request.user)
    if request.method == 'GET':
        data = request.GET
        # if (sys.version_info.major, sys.version_info.minor) >= (2, 7):
        #    search_data = {
        #        key: value for key, value
        #        in data.items() if value
        #    }
        # else:  # python 2.6 sucks
        search_data = {}
        for key, value in data.items():
            if value:
                search_data.update({key: value})

        orders = orders.filter(Q(**search_data))

    t = loader.get_template('csv/orders_partner.csv')
    c = Context({
        'orders': orders
    })
    response.write(t.render(c))
    return response
Exemplo n.º 2
0
def order_payment_notification(request):
    success = ('<?xml version="1.0" encoding="UTF-8"?>'
               "<result>"
               "<code>{code}</code>"
               "</result>"
    )
    error = ('<?xml version="1.0" encoding="UTF-8"?>'
             "<result>"
             "<code>{code}</code>"
             "<comment>{comment}</comment>"
             "</result>"
    )
    # # verbose version:
    # response = '''
    # <?xml version="1.0" encoding="UTF-8"?>
    # <result>
    # <id>{id}</id>
    # <code>{code}</code>
    # <comment>{comment}</comment>
    # <course>{course}</course>
    # </result>
    # '''
    response = make_http_response(content_type='text/xml')
    if request.method != 'POST':
        response.write(
            error.format(
                code=-1,
                comment='no allowed method %s' % request.method
            )
        )
        return response
    data = request.POST
    # IMPORTANT: ensure 'code' calculation algorithm below remains idempotent
    valid = is_valid(data)
    code = valid and 'YES' or 'NO'
    order_id = data.get('orderid')  # internal order id, string (64)
    payment_id = data.get('paymentid')  # external order id, int (30)
    amount = data.get('amount', '0')
    comment_error = ''
    if not valid:
        comment_error = "key sum is invalid"

    # payment processing logic here, whatever you need
    out = success.format(
        # id='',  # internal payment id - optional, string (64)
        code=code,
        # comment='',  #  optional, string (400)
        # course='',  #  optional, float (10.2)
    ) if valid else error.format(
        code=code,
        comment=comment_error
    )
    response.write(out)

    # proceeding payment
    if valid:
        order = get_object_or_None(Order, pk=order_id)
        amount = Decimal(str(amount))
        if order:
            order.is_paid = True
            order.save()
            kw = dict(
                amount=amount, client=order.client,
                partner=order.container.owner,
                order=order,
                payment_id=payment_id

            )
            payment = PaymentTransaction.objects.filter(**kw)
            if not payment:
                PaymentTransaction.objects.create(**kw)
    return response