示例#1
0
文件: views.py 项目: nloncke/tex
def buy_confirmation(request):
    from book.utils import get_book
    from utils import notify_users_bought
    from models import remove_offer
    result = {}
    if request.method == "POST":
        is_auction = request.POST.get("is_auction", "")
        if is_auction:
            from models import remove_auction
            result = {}
            auction_id = request.POST.get("auction_id", "0")
            buyer_id = request.user.username
            sold_auction = remove_auction(auction_id, True, buyer_id=buyer_id)
            if sold_auction:
                isbn = sold_auction["isbn"]
                seller_id = sold_auction["seller_id"]
                result = get_book(isbn=isbn)
                result["sold_offer"] = sold_auction
                result["seller_id"] = seller_id
                notify_users_bought(buyer=buyer_id, offer=sold_auction)
            if sold_auction == None:
                return render(request, 'nocheating.html', {
                    'buyer_id': buyer_id,
                    'error': "cheating"
                })
        else:
            offer_id = request.POST.get("offer_id", "0")
            buyer_id = request.user.username
            sold_offer = remove_offer(offer_id, buyer_id=buyer_id)
            if sold_offer == None:
                return render(request, 'nocheating.html', {
                    'buyer_id': buyer_id,
                    'error': "cheating"
                })
            if sold_offer:
                isbn = sold_offer["isbn"]
                seller_id = sold_offer["seller_id"]
                result = get_book(isbn=isbn)
                result["sold_offer"] = sold_offer
                result["seller_id"] = seller_id
                notify_users_bought(buyer=buyer_id, offer=sold_offer)
        return render(request, "buy_confirmation.html", result)

    return render(request, 'error_page.html')
示例#2
0
文件: views.py 项目: nloncke/tex
def buy_confirmation(request):
    from book.utils import get_book
    from utils import notify_users_bought
    from models import remove_offer
    result = {}
    if request.method == "POST":
        is_auction = request.POST.get("is_auction","")
        if is_auction:
            from models import remove_auction
            result = {}
            auction_id = request.POST.get("auction_id","0")
            buyer_id = request.user.username
            sold_auction = remove_auction(auction_id, True, buyer_id=buyer_id)
            if sold_auction:
                isbn = sold_auction["isbn"]
                seller_id = sold_auction["seller_id"]
                result = get_book(isbn=isbn)
                result["sold_offer"] = sold_auction
                result["seller_id"] = seller_id
                notify_users_bought(buyer=buyer_id, offer=sold_auction)      
            if sold_auction == None:
                return render(request, 'nocheating.html', {'buyer_id':buyer_id, 'error':"cheating"})         
        else:
            offer_id = request.POST.get("offer_id", "0")
            buyer_id = request.user.username
            sold_offer = remove_offer(offer_id, buyer_id=buyer_id)
            if sold_offer == None:
                return render(request, 'nocheating.html', {'buyer_id':buyer_id, 'error':"cheating"})
            if sold_offer:
                isbn = sold_offer["isbn"]
                seller_id = sold_offer["seller_id"]
                result = get_book(isbn=isbn)
                result["sold_offer"] = sold_offer
                result["seller_id"] = seller_id
                notify_users_bought(buyer=buyer_id, offer=sold_offer)
        return render(request, "buy_confirmation.html", result)
    
    return render(request, 'error_page.html')
示例#3
0
def account_index(request):
    from account.models import get_seller_offers, get_seller_auctions, get_follow_list, unfollow, save_user
    from buy.models import remove_offer, remove_auction
    from sell.utils import get_book_info
    from book.utils import get_book
    from search.utils import validate_isbn
    
    # only post if removing offer
    if request.method == "POST": 
        action = request.POST.get("action", "")
        if action == "remove_offer":
            offer_id = request.POST.get("offer_id", "0")
            remove_offer(offer_id=offer_id) 
        elif action == "unfollow":     
            isbn = request.POST.get("target_isbn", "0")
            user = request.user
            if validate_isbn(isbn=isbn):
                unfollow(user=user, isbn=isbn) 
        elif action == "update":
            info = {"class_year": request.POST.get("class_year")}
            info["default_search"] = request.POST.get("default_search")
            save_user(request.user, **info)
        else:
            return render(request, 'error_page.html')
   
    result_offers = []
    result_auctions = []
    result_follow = []
    seller_id = request.user.username
    user = request.user
 
    seller_offers = get_seller_offers(seller_id=seller_id)
    offers = {}
    for seller_offer in seller_offers:
        book_info = get_book_info(isbn=seller_offer.isbn)["book"]
        result_offers.append({"title":book_info["title"], "price":seller_offer.price, "offer_id":seller_offer.id,
                       "isbn":seller_offer.isbn})
    
    seller_auctions = get_seller_auctions(seller_id=seller_id)
    auctions = {}
    for seller_auction in seller_auctions:
        book_info = get_book_info(isbn=seller_auction.isbn)["book"]
        result_auctions.append({"title":book_info["title"], "current_price":seller_auction.current_price, "auction_id":seller_auction.id,
                       "isbn":seller_auction.isbn, "end_time":seller_auction.end_time})
    
    follow_isbns = get_follow_list(user=user)      
    for isbn in follow_isbns:
        min_offer = {}
        min_auction = {}
        if isbn:
            book_info = get_book(isbn=isbn)
            offers = book_info["offers"]
            auctions = book_info["auctions"]
            if offers:
                min_offer = offers[0]
            if auctions:
                min_auction = auctions[0]
            result_follow.append({"isbn":isbn, "book":book_info["book"], "min_offer":min_offer, "min_auction":min_auction})
        else:
            pass
             
    return render(request,'account_index.html', {"offers":result_offers, "auctions":result_auctions, "follows":result_follow})