dispatch.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. # Dispatches data to hasher and adds to database tables
  2. import sqlalchemy
  3. import os
  4. from datetime import datetime
  5. from flask import flash
  6. from flask_login import current_user
  7. from apscheduler.schedulers.background import BackgroundScheduler as BS
  8. from .models import Hashchain, Art, List, TX , User, Bids, List
  9. from . import db
  10. from app.lib import clean_file as cf
  11. from app.lib import hasher as hsh
  12. def mint(designated_fn, art_name, art_desc, min_price, buyout_price, close_date):
  13. # gen filehash
  14. filehash = hsh.hashfile(f'{cf.UPLOAD_FOLDER}/{designated_fn}')
  15. # gen txhash (if exchanged or minted)
  16. ddt = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
  17. dt = sqlalchemy.func.now()
  18. txstring = f'[{filehash}, {ddt}, {current_user.id}, {current_user.id}, 0.0]' # minting
  19. txhash = hsh.gen_txhash(txstring)
  20. # fetch prev block hash OR GEN HASH
  21. try:
  22. prev_bhash = db.session.query(Hashchain).order_by(Hashchain.id.desc()).first().blockhash
  23. if prev_bhash is None:
  24. prev_bhash = hsh.GENESIS_HASH
  25. except:
  26. prev_bhash = hsh.GENESIS_HASH
  27. # gen block hash add to hashchain
  28. new_bhash = hsh.append_tx(txhash, prev_bhash)
  29. new_block = Hashchain(blockhash = new_bhash)
  30. db.session.add(new_block)
  31. # move the file into external secure storage (NOT DONE)
  32. os.rename(f'{cf.UPLOAD_FOLDER}/{designated_fn}', f'{cf.REPO_FOLDER}/{designated_fn}')
  33. # add to TX table (mint)
  34. new_tx = TX(
  35. filehash = filehash,
  36. datetime = dt,
  37. source_id = current_user.id,
  38. destination_id = current_user.id,
  39. price = 0.0,
  40. txhash = txhash
  41. )
  42. db.session.add(new_tx)
  43. # add to Art table
  44. new_art = Art(
  45. name = art_name,
  46. description = art_desc,
  47. filehash = filehash,
  48. owner = current_user.id,
  49. creator = current_user.id,
  50. dname = designated_fn
  51. )
  52. db.session.add(new_art)
  53. # add to List table
  54. new_listing = List(
  55. filehash = filehash,
  56. seller = current_user.id,
  57. min_price = min_price,
  58. out_price = buyout_price,
  59. timeout = close_date,
  60. )
  61. db.session.add(new_listing)
  62. try:
  63. db.session.commit()
  64. flash('Minted & Listed!', category='success')
  65. except Exception:
  66. flash('Error Minting! Cannot Add an Already Existing File!', category='error')
  67. def list_item(designated_fn, art_name, art_desc, min_price, buyout_price, close_date, filehash):
  68. # gen txhash
  69. ddt = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
  70. dt = sqlalchemy.func.now()
  71. txstring = f'[{filehash}, {ddt}, {current_user.id}, {current_user.id}, 0.0]' # minting
  72. txhash = hsh.gen_txhash(txstring)
  73. # fetch prev block hash OR GEN HASH
  74. try:
  75. prev_bhash = db.session.query(Hashchain).order_by(Hashchain.id.desc()).first().blockhash
  76. if prev_bhash is None:
  77. prev_bhash = hsh.GENESIS_HASH
  78. except:
  79. prev_bhash = hsh.GENESIS_HASH
  80. # gen block hash add to hashchain
  81. new_bhash = hsh.append_tx(txhash, prev_bhash)
  82. new_block = Hashchain(blockhash = new_bhash)
  83. db.session.add(new_block)
  84. # add to TX table (list)
  85. new_tx = TX(
  86. filehash = filehash,
  87. datetime = dt,
  88. source_id = current_user.id,
  89. destination_id = current_user.id,
  90. price = 0.0,
  91. txhash = txhash
  92. )
  93. db.session.add(new_tx)
  94. # add to List table
  95. new_listing = List(
  96. filehash = filehash,
  97. seller = current_user.id,
  98. min_price = min_price,
  99. out_price = buyout_price,
  100. timeout = close_date,
  101. )
  102. db.session.add(new_listing)
  103. db.session.commit()
  104. def tx_exchange(filehash, source_id, price):
  105. # create tx hash
  106. dt = sqlalchemy.func.now()
  107. ddt = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
  108. txstring = f'[{filehash}, {ddt}, {source_id}, {current_user.id}, {price}]' # exchange
  109. txhash = hsh.gen_txhash(txstring)
  110. # fetch prev block hash OR GEN HASH
  111. try:
  112. prev_bhash = db.session.query(Hashchain).order_by(Hashchain.id.desc()).first().blockhash
  113. if prev_bhash is None:
  114. prev_bhash = hsh.GENESIS_HASH
  115. except:
  116. prev_bhash = hsh.GENESIS_HASH
  117. # gen block hash add to hashchain
  118. new_bhash = hsh.append_tx(txhash, prev_bhash)
  119. new_block = Hashchain(blockhash = new_bhash)
  120. db.session.add(new_block)
  121. new_tx = TX(
  122. filehash = filehash,
  123. datetime = dt,
  124. source_id = source_id,
  125. destination_id = current_user.id,
  126. price = price,
  127. txhash = txhash
  128. )
  129. # add to TX table (exchange)
  130. db.session.add(new_tx)
  131. # remove from List table
  132. List.query.filter_by(filehash = filehash).delete()
  133. db.session.commit()
  134. def clean_bid_table(filehash):
  135. # cleans Bid table when Artwork is bought
  136. old_bids = Bids.query.filter_by(filehash = filehash).all()
  137. for bid in old_bids:
  138. db.session.delete(bid)
  139. db.session.commit()
  140. def enter_bid(user_bid, focus):
  141. # add to Bids table
  142. new_bid = Bids(
  143. filehash = current_user.focus,
  144. buyer = current_user.id,
  145. bid_price = user_bid
  146. )
  147. db.session.add(new_bid)
  148. # update List table
  149. dbcall = List.query.filter_by(filehash = current_user.focus).first()
  150. dbcall.min_price = user_bid
  151. dbcall.cur_bid = user_bid
  152. db.session.commit()
  153. def save_pp(pp_path):
  154. new_pppath = User.query.filter_by(id=current_user.id).first()
  155. new_pppath.profile_image = pp_path
  156. db.session.commit()
  157. def check_time():
  158. # background scheduler runs this daily
  159. # to check for Auction end
  160. # go through List table and check for timeout
  161. # if close_date and min bid reached
  162. # if close_date and min bid not reached