dispatch.py 5.0 KB

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