contract.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import json
  2. import uuid
  3. from lib.address import Address
  4. class Contract(object):
  5. class Term:
  6. def __init__(self):
  7. self.id = None
  8. self.title = None
  9. self.description = None
  10. self.deadline = None
  11. self.modified_by = None
  12. self.last_comment = None
  13. self.state = None
  14. def create(self, title: str, desc: str, deadline: int):
  15. self.id = str(uuid.uuid1())
  16. self.title = title
  17. self.description = desc
  18. self.deadline = deadline
  19. self.log(f'Created: {self.title}')
  20. def update(self, key: str, value, by: Address, comment: str):
  21. if hasattr(self, key):
  22. setattr(self, key, value)
  23. self.modified_by = by
  24. self.last_comment = comment
  25. return True
  26. return False
  27. def serialize(self, out_json=False):
  28. obj = {
  29. '@type': 'term',
  30. 'id': self.id,
  31. 'title': self.title,
  32. 'description': self.description,
  33. 'deadline': self.deadline
  34. }
  35. self.log('Serialized')
  36. if out_json:
  37. json.dumps(obj, sort_keys=True)
  38. return obj
  39. def log(self, text):
  40. print(f'[ TERM ] {text}')
  41. def __init__(self):
  42. self.id = None
  43. self.client = None
  44. self.contractor = None
  45. self.deadline = None
  46. self.description = None
  47. self.title = None
  48. self.price = None
  49. self.people = None
  50. self.initiator = None
  51. self.terms = []
  52. self.initial_block = None
  53. self.modified_by = None
  54. self.comment = None
  55. def add_term(self, term: Term):
  56. self.log(f'Term added: {term.id} | {term.title}')
  57. self.terms.append(term)
  58. def update(self, key: str, value, by: Address, comment: str):
  59. if self.has_attribute(key):
  60. self.log(f'Updating {key}. {getattr(self, key)} => {value}')
  61. setattr(self, key, value)
  62. self.modified_by = by
  63. self.comment = comment
  64. return True
  65. return False
  66. def create(self, title: str, desc: str, deadline: int, price: float, **kwargs):
  67. if self.id is None:
  68. self.id = str(uuid.uuid1())
  69. self.title = title
  70. self.description = desc
  71. self.deadline = deadline
  72. self.price = price
  73. self.log(f'Contract created: {self.id}')
  74. return True
  75. return False
  76. def serialize(self, out_json=False):
  77. # TODO: Add all properties
  78. term_serialize = []
  79. for term in self.terms:
  80. term_serialize.append(term.serialize())
  81. obj = {
  82. 'id': self.id,
  83. 'terms': term_serialize
  84. }
  85. self.log(f'Serialized: {len(term_serialize)} terms')
  86. if out_json:
  87. return json.dumps(obj, sort_keys=True)
  88. return obj
  89. def log(self, text):
  90. print(f'[ CONTR ] {text}')
  91. def has_attribute(self, key):
  92. return hasattr(self, key)