Exemple #1
0
def create_bid(item, bidder, bid_amount):
    bid = Bids()
    bid.item = item
    bid.bidder = bidder
    bid.bid_amount = bid_amount
    bid.save()
    return bid
Exemple #2
0
def Highestbid(N, item):
    topN={}
    i=1
    s = Bids.search().sort('-bid_amount')
    topN_result = s.filter("term", item = item.item_name).execute()[:N]
    for bid in topN_result:
        topN[i]=({'Bidder':bid.bidder, 'Item':bid.item, 'Bid Amount': int(bid.bid_amount)}) #int to remove L character from output
        i=i+1
    return topN
Exemple #3
0
def create_bid(item, bidder, bid_amount):
    bid = Bids()
    bid.item = item
    bid.bidder = bidder
    bid.bid_amount = bid_amount
    bid.save()
    return bid
Exemple #4
0
def Highestbid(N, item):
    topN = {}
    i = 1
    s = Bids.search().sort('-bid_amount')
    topN_result = s.filter("term", item=item.item_name).execute()[:N]
    for bid in topN_result:
        topN[i] = ({
            'Bidder': bid.bidder,
            'Item': bid.item,
            'Bid Amount': int(bid.bid_amount)
        })  #int to remove L character from output
        i = i + 1
    return topN
Exemple #5
0
def sell_items(request, item):
    s = Bids.search()
    s.aggs.bucket('SP', 'terms', field=ITEM_INDEX).metric('max_bid', 'max', field='bid_amount')
    response = s.execute()

    for term in response.aggregations.SP.buckets:
        if term.key == item.item_name:
            selling_price = term.max_bid.value

    buyer = get_bid(item_name=item.item_name, amount=selling_price)[0].bidder
    doc = dict(status="Sold", sold_to=buyer)
    update_item(item= item, key_value=doc)

    return HttpResponse("Item Sold: {0} at {1} to {2}".format(item.item_name, selling_price, buyer))
Exemple #6
0
def sell_items(request, item):
    s = Bids.search()
    s.aggs.bucket('SP', 'terms', field=ITEM_INDEX).metric('max_bid',
                                                          'max',
                                                          field='bid_amount')
    response = s.execute()

    for term in response.aggregations.SP.buckets:
        if term.key == item.item_name:
            selling_price = term.max_bid.value

    buyer = get_bid(item_name=item.item_name, amount=selling_price)[0].bidder
    doc = dict(status="Sold", sold_to=buyer)
    update_item(item=item, key_value=doc)

    return HttpResponse("Item Sold: {0} at {1} to {2}".format(
        item.item_name, selling_price, buyer))
Exemple #7
0
def get_bid(bidder=None, item_name = None, amount=None):
    try:
        s= Bids.search()
        if item_name:
            if bidder:
                bid = s.filter("term", item = item_name).filter("term", bidder = bidder).execute()
            elif amount:
                bid = s.filter("term", item = item_name).filter("term", bid_amount = amount).execute()
            else:
                bid = s.filter("term", item = item_name).execute()
        elif bidder:
            bid = s.filter("term", bidder = bidder).execute()
        else:
            print "ERROR: Item_name and Bidder not present"
            bid = False
    except NotFoundError:
        return False
    else:
        return bid
Exemple #8
0
def get_bid(bidder=None, item_name=None, amount=None):
    try:
        s = Bids.search()
        if item_name:
            if bidder:
                bid = s.filter("term",
                               item=item_name).filter("term",
                                                      bidder=bidder).execute()
            elif amount:
                bid = s.filter("term", item=item_name).filter(
                    "term", bid_amount=amount).execute()
            else:
                bid = s.filter("term", item=item_name).execute()
        elif bidder:
            bid = s.filter("term", bidder=bidder).execute()
        else:
            print "ERROR: Item_name and Bidder not present"
            bid = False
    except NotFoundError:
        return False
    else:
        return bid
Exemple #9
0
from datetime import datetime, timedelta
from django.contrib.auth.decorators import login_required
from django.db.models import Max
from view_helper import read_request_item, scheduler, is_sold, Highestbid, values, is_owner, update_item
from items.ES.constants import messages
from elastic import add_item, get_item, get_bid
from es_models import Items,Bids
from elasticsearch_dsl.connections import connections
from django.http import HttpResponse


# initializing
connections.create_connection(hosts=['localhost'])
s = scheduler()
Items.init()
Bids.init()

ITEM_INDEX= 'item'


@login_required(login_url='http://localhost:8000/login_message/')
def add_items(request):
    seller, item_name, min_bid =read_request_item(request)
    print seller, item_name, min_bid
    #item = add_item(seller, item_name, min_bid)
    item = get_item(item_name=item_name)
    exec_time= datetime.now() + timedelta(hours = 1)
    s.addjob(function=sell_items, time= exec_time, arguments=[request, item])
    return ("Added Item: {0} {1}".format(item.item_name, item.__dict__))

Exemple #10
0
from datetime import datetime, timedelta
from django.contrib.auth.decorators import login_required
from django.db.models import Max
from view_helper import read_request_item, scheduler, is_sold, Highestbid, values, is_owner, update_item
from items.ES.constants import messages
from elastic import add_item, get_item, get_bid
from es_models import Items, Bids
from elasticsearch_dsl.connections import connections
from django.http import HttpResponse

# initializing
connections.create_connection(hosts=['localhost'])
s = scheduler()
Items.init()
Bids.init()

ITEM_INDEX = 'item'


@login_required(login_url='http://localhost:8000/login_message/')
def add_items(request):
    seller, item_name, min_bid = read_request_item(request)
    print seller, item_name, min_bid
    #item = add_item(seller, item_name, min_bid)
    item = get_item(item_name=item_name)
    exec_time = datetime.now() + timedelta(hours=1)
    s.addjob(function=sell_items, time=exec_time, arguments=[request, item])
    return ("Added Item: {0} {1}".format(item.item_name, item.__dict__))


#Is payement logic required?