dispatch.py 5.5 KB

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