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