| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- import json
- import uuid
- from lib.address import Address
- class Contract(object):
- class Term:
- def __init__(self):
- self.id = None
- self.title = None
- self.description = None
- self.deadline = None
- def create(self, title: str, desc: str, deadline: int):
- self.id = str(uuid.uuid1())
- self.title = title
- self.description = desc
- self.deadline = deadline
- self.log(f'Created: {self.title}')
- def update(self, key: str, value, by: Address, comment: str):
- if hasattr(self, key):
- setattr(self, key, value)
- self.modified_by = by
- self.last_comment = comment
- return True
- return False
- def serialize(self, out_json=False):
- obj = {
- '@type': 'term',
- 'id': self.id,
- 'title': self.title,
- 'description': self.description,
- 'deadline': self.deadline
- }
- self.log('Serialized')
- if out_json:
- json.dumps(obj, sort_keys=True)
- return obj
- def log(self, text):
- print(f'[ TERM ] {text}')
- def __init__(self):
- self.id = None
- self.client = None
- self.contractor = None
- self.deadline = None
- self.description = None
- self.title = None
- self.price = None
- self.people = None
- self.initiator = None
- self.terms = []
- self.initial_block = None
- self.modified_by = None
- self.last_comment = None
- self.state = None
- def add_term(self, term: Term):
- self.log(f'Term added: {term.id} | {term.title}')
- self.terms.append(term)
- def update(self, key: str, value, by: Address, comment: str):
- if self.has_attribute(key):
- self.log(f'Updating {key}. {getattr(self, key)} => {value}')
- setattr(self, key, value)
- self.modified_by = by
- self.comment = comment
- return True
- return False
- def create(self, title: str, desc: str, deadline: int, price: float, state:bool, client, contractor, **kwargs):
- if self.id is None:
- self.id = str(uuid.uuid1())
- self.title = title
- self.description = desc
- self.deadline = deadline
- self.price = price
- self.state = state
- self.client = client
- self.contractor = contractor
- self.log(f'Contract created: {self.id}')
- return True
- return False
- def serialize(self, out_json=False):
- # TODO: Add all properties
- term_serialize = []
- for term in self.terms:
- term_serialize.append(term.serialize())
- obj = {
- 'id': self.id,
- 'terms': term_serialize
- }
- self.log(f'Serialized: {len(term_serialize)} terms')
- if out_json:
- return json.dumps(obj, sort_keys=True)
- return obj
- def log(self, text):
- print(f'[ CONTR ] {text}')
- def has_attribute(self, key):
- return hasattr(self, key)
|