dispatch.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # Dispatches data to hasher and adds to database tables
  2. import sqlalchemy
  3. from datetime import datetime
  4. from flask_login import current_user
  5. from .models import Hashchain, Art, List, TX
  6. from . import db
  7. from . import clean_file as cf
  8. from . import hasher as hsh
  9. def mint(designated_fn, art_name, art_desc, min_price, buyout_price, close_date):
  10. # gen filehash
  11. filehash = hsh.hashfile(f'{cf.UPLOAD_FOLDER}/{designated_fn}')
  12. # gen txhash (if bought... or sold, or minted)
  13. ddt = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
  14. dt = sqlalchemy.func.now()
  15. txstring = f'[{filehash}, {ddt}, {current_user.id}, {current_user.id}, 0.0]' # minting
  16. txhash = hsh.gen_txhash(txstring)
  17. # fetch prev block hash OR GEN HASH
  18. try:
  19. prev_bhash = db.session.query(Hashchain).order_by(Hashchain.id.desc()).first().blockhash
  20. if prev_bhash is None:
  21. prev_bhash = hsh.GENESIS_HASH
  22. except:
  23. prev_bhash = hsh.GENESIS_HASH
  24. # gen block hash add to hashchain
  25. new_bhash = hsh.append_tx(txhash, prev_bhash)
  26. new_block = Hashchain(blockhash = new_bhash)
  27. db.session.add(new_block)
  28. # move the file into external secure storage + another local dir (NOT DONE)
  29. # add to TX table (mint)
  30. new_tx = TX(
  31. filehash = filehash,
  32. datetime = dt,
  33. source_id = current_user.id,
  34. destination_id = current_user.id,
  35. price = 0.0,
  36. txhash = txhash
  37. )
  38. db.session.add(new_tx)
  39. # add to Art table
  40. new_art = Art(name = art_name, description = art_desc, filehash = filehash)
  41. db.session.add(new_art)
  42. # add to List table
  43. new_listing = List(
  44. filehash = filehash,
  45. seller = current_user.id,
  46. min_price = min_price,
  47. out_price = buyout_price,
  48. timeout = close_date,
  49. )
  50. db.session.add(new_listing)
  51. db.session.commit()