You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
2.2 KiB
Python
64 lines
2.2 KiB
Python
from io import BytesIO
|
|
from base64 import b64encode
|
|
from qrcode import make as qrcode_make
|
|
from django.shortcuts import render, HttpResponseRedirect, reverse
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.contrib import messages
|
|
from core.models import UserShippingAddress
|
|
from bids.models import ItemBid
|
|
from sales.models import ItemSale
|
|
|
|
|
|
@login_required
|
|
def get_sale(request, bid_id):
|
|
bid = ItemBid.objects.get(id=bid_id)
|
|
sale = ItemSale.objects.get(bid=bid)
|
|
|
|
# Do not proceed unless current user is a buyer or seller
|
|
if request.user != bid.bidder and request.user != sale.item.owner:
|
|
messages.error(request, "You can't view a sale you are not involved in.")
|
|
return HttpResponseRedirect(reverse('home'))
|
|
|
|
_address_qr = BytesIO()
|
|
address_qr = qrcode_make(sale.escrow_address).save(_address_qr)
|
|
|
|
context = {
|
|
'sale': sale,
|
|
'qrcode': b64encode(_address_qr.getvalue()).decode(),
|
|
'shipping_address': UserShippingAddress.objects.filter(
|
|
user=bid.bidder
|
|
).first()
|
|
}
|
|
|
|
return render(request, 'sales/get_sale.html', context)
|
|
|
|
@login_required
|
|
def confirm_shipment(request, sale_id):
|
|
sale = ItemSale.objects.get(id=sale_id)
|
|
|
|
# Only proceed if current user is the seller
|
|
if request.user == sale.item.owner:
|
|
sale.item_shipped = True
|
|
sale.save()
|
|
messages.success(request, "Package sent, buyer notified!")
|
|
return HttpResponseRedirect(reverse('get_sale', args=[sale.bid.id]))
|
|
else:
|
|
messages.error(request, "You can't confirm a package shipment for an item you don't own.")
|
|
return HttpResponseRedirect(reverse('home'))
|
|
|
|
|
|
|
|
@login_required
|
|
def confirm_receipt(request, sale_id):
|
|
sale = ItemSale.objects.get(id=sale_id)
|
|
|
|
# Do not proceed unless current user is the buyer
|
|
if request.user == sale.bid.bidder:
|
|
sale.item_received = True
|
|
sale.save()
|
|
messages.success(request, "Item received!")
|
|
return HttpResponseRedirect(reverse('get_sale', args=[sale.bid.id]))
|
|
else:
|
|
messages.error(request, "You can't confirm receipt of an item you didn't purchase.")
|
|
return HttpResponseRedirect(reverse('home'))
|