|
|
@@ -7,8 +7,6 @@ from datetime import datetime
|
|
|
from flask import flash
|
|
|
from flask_login import current_user
|
|
|
|
|
|
-from apscheduler.schedulers.background import BackgroundScheduler as BS
|
|
|
-
|
|
|
from .models import Hashchain, Art, List, TX , User, Bids, List
|
|
|
from . import db
|
|
|
|
|
|
@@ -112,11 +110,15 @@ def list_item(designated_fn, art_name, art_desc, min_price, buyout_price, close_
|
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
-def tx_exchange(filehash, source_id, price):
|
|
|
+def tx_exchange(filehash, source_id, price, dest_id=None):
|
|
|
+ if not dest_id:
|
|
|
+ if current_user.is_authenticated:
|
|
|
+ dest_id = current_user.id
|
|
|
+
|
|
|
# create tx hash
|
|
|
dt = sqlalchemy.func.now()
|
|
|
ddt = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
|
|
|
- txstring = f'[{filehash}, {ddt}, {source_id}, {current_user.id}, {price}]' # exchange
|
|
|
+ txstring = f'[{filehash}, {ddt}, {source_id}, {dest_id}, {price}]' # exchange
|
|
|
txhash = hsh.gen_txhash(txstring)
|
|
|
# fetch prev block hash OR GEN HASH
|
|
|
try:
|
|
|
@@ -134,7 +136,7 @@ def tx_exchange(filehash, source_id, price):
|
|
|
filehash = filehash,
|
|
|
datetime = dt,
|
|
|
source_id = source_id,
|
|
|
- destination_id = current_user.id,
|
|
|
+ destination_id = dest_id,
|
|
|
price = price,
|
|
|
txhash = txhash
|
|
|
)
|
|
|
@@ -173,12 +175,25 @@ def save_pp(pp_path):
|
|
|
new_pppath.profile_image = pp_path
|
|
|
db.session.commit()
|
|
|
|
|
|
-def check_time():
|
|
|
+def check_auction_end():
|
|
|
# background scheduler runs this daily
|
|
|
# to check for Auction end
|
|
|
+ all_listings = List.query.all()
|
|
|
|
|
|
# go through List table and check for timeout
|
|
|
-
|
|
|
- # if close_date and min bid reached
|
|
|
-
|
|
|
- # if close_date and min bid not reached
|
|
|
+ for listing in all_listings:
|
|
|
+ # if close_date and min bid reached
|
|
|
+ ddt = datetime.utcnow().strftime("%Y-%m-%d")
|
|
|
+ if listing.timeout == ddt and listing.cur_bid > listing.min_price:
|
|
|
+ print(f'DEBUG: scheduler >>> EXCHANGING!!') ##
|
|
|
+ # transaction for the last bid made
|
|
|
+ # get buyer id from Bids table using filehash
|
|
|
+ buyer_bid = Bids.query.filter_by(filehash = listing.filehash).order_by(desc(Bids.id)).first()
|
|
|
+ # initiate exchange transaction and clean bid table
|
|
|
+ tx_exchange(listing.filehash, listing.seller, buyer_bid.bid_price, buyer_bid.buyer)
|
|
|
+ clean_bid_table(listing.filehash)
|
|
|
+ # if close_date and min bid not reached (no one made a qualifying bid)
|
|
|
+ elif listing.timeout == ddt and not listing.cur_bid > listing.min_price:
|
|
|
+ print(f'DEBUG: scheduler >>> DE-LISTING!!') ##
|
|
|
+ # remove from List table
|
|
|
+ List.query.filter_by(filehash = filehash).delete()
|