dispatch.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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
  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 bought... or sold, 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()