def payment_view(request, pk):
    order = Order.objects.filter(id=pk)
    if not order or order.get().customer.user.id != request.user.id:
        return redirect("home")
    order = order.get()
    price_usd = Decimal(sum([cart_option.menu_item.price * cart_option.quantity
                             for cart_option in order.cartoption_set.all()]))
    try:
        rate = ExchangeRate.get_exchange_rate('usd', 'btc')
    except BaseException as e:
        rate = 1
    crypto_prices = {'btc': Decimal(price_usd * rate).quantize(Decimal(10) ** -5, rounding=ROUND_HALF_UP)}
    if request.POST:
        form = OrderPaymentForm(request.POST)
        if form.is_valid():
            crypto_order = CryptoOrder(
                currency=form.cleaned_data['currency'],
                amount=crypto_prices[form.cleaned_data['currency']],
                date=timezone.now(),
                redirect_to='home'
            )
            crypto_order.save()
            form.instance.crypto_order = crypto_order
            form.instance.customer = order.customer
            form.instance.id = order.id
            form.instance.status = "Submitted"
            form.save()
            return redirect(cryptocoin.process, id=crypto_order.id)
    else:
        form = OrderPaymentForm(initial={"restaurant": order.restaurant})
    context = {'form': form, 'crypto_prices': crypto_prices, 'order': order}
    return render_to_response('payment.html', context=context,
                              context_instance=RequestContext(request))
from django_cryptocoin.settings import CRYPTO_COINS


def home(request):
    price_usd = Decimal(0.1)
    crypto_prices = {}
    for currency in CRYPTO_COINS.keys():
        try:
            rate = ExchangeRate.get_exchange_rate('usd', currency)
        except Exception, e:
            rate = 1
        crypto_prices[currency] = Decimal(price_usd * rate).quantize(Decimal(10) ** -5, rounding=ROUND_HALF_UP)

    form = OrderForm(request.POST or None)
    if form.is_valid():
        crypto_order = CryptoOrder(
            currency=form.cleaned_data['currency'],
            amount=crypto_prices[form.cleaned_data['currency']],
            date=timezone.now(),
            redirect_to=reverse('home')
        )
        crypto_order.save()
        form.instance.crypto_order = crypto_order
        form.save()
        return redirect('cryptocoin-order-process', addr=crypto_order.addr)

    return render(request, 'home.html', {
        'form': form,
        'crypto_prices': crypto_prices,
        'messages': Message.objects.all()
    })
def home(request):
    price_usd = Decimal(0.1)
    crypto_prices = {}
    for currency in CRYPTO_COINS.keys():
        try:
            rate = ExchangeRate.get_exchange_rate('usd', currency)
        except Exception, e:
            rate = 1
        crypto_prices[currency] = Decimal(price_usd * rate).quantize(
            Decimal(10)**-5, rounding=ROUND_HALF_UP)

    form = OrderForm(request.POST or None)
    if form.is_valid():
        crypto_order = CryptoOrder(
            currency=form.cleaned_data['currency'],
            amount=crypto_prices[form.cleaned_data['currency']],
            date=timezone.now(),
            redirect_to=reverse('home'))
        crypto_order.save()
        form.instance.crypto_order = crypto_order
        form.save()
        return redirect('cryptocoin-order-process', addr=crypto_order.addr)

    return render(
        request, 'home.html', {
            'form': form,
            'crypto_prices': crypto_prices,
            'messages': Message.objects.all()
        })