Adds class method to create from dict and on_sync method

This commit is contained in:
Sergi Delgado Segura
2019-10-25 20:27:37 +01:00
parent db87fab9ee
commit 162de92026
2 changed files with 49 additions and 3 deletions

View File

@@ -15,14 +15,32 @@ class Appointment:
self.cipher = cipher
self.hash_function = hash_function
@classmethod
def from_dict(cls, appointment_data):
locator = appointment_data.get("locator")
start_time = appointment_data.get("start_time") # ToDo: #4-standardize-appointment-fields
end_time = appointment_data.get("end_time") # ToDo: #4-standardize-appointment-fields
dispute_delta = appointment_data.get("dispute_delta")
encrypted_blob_data = appointment_data.get("encrypted_blob")
cipher = appointment_data.get("cipher")
hash_function = appointment_data.get("hash_function")
if all([locator, start_time, end_time, dispute_delta, encrypted_blob_data, cipher, hash_function]) is not None:
appointment = cls(locator, start_time, end_time, dispute_delta, EncryptedBlob(encrypted_blob_data), cipher,
hash_function)
else:
raise ValueError("Wrong appointment data, some fields are missing")
return appointment
def to_dict(self):
# ToDO: #3-improve-appointment-structure
appointment = {"locator": self.locator, "start_time": self.start_time, "end_time": self.end_time,
"dispute_delta": self.dispute_delta, "encrypted_blob": self.encrypted_blob.data,
"cipher": self.cipher, "hash_function": self.hash_function}
return appointment
# ToDO: #3-improve-appointment-structure
def to_json(self):
return json.dumps(self.to_dict(), sort_keys=True, separators=(',', ':'))