Files
python-teos/teos/extended_appointment.py
Sergi Delgado Segura 66dce42526 Adds get_summary to extended appointment
The Watcher used to create the summary, but that was not modular enough and introduced issues when testing
2020-04-20 17:58:41 +02:00

47 lines
1.6 KiB
Python

from common.appointment import Appointment
class ExtendedAppointment(Appointment):
def __init__(self, locator, to_self_delay, encrypted_blob, user_id):
super().__init__(locator, to_self_delay, encrypted_blob)
self.user_id = user_id
def get_summary(self):
"""
Returns the summary of an appointment, consisting on the locator, the user_id and the appointment size.
Returns:
:obj:`dict`: the appointment summary.
"""
return {"locator": self.locator, "user_id": self.user_id, "size": len(self.encrypted_blob)}
@classmethod
def from_dict(cls, appointment_data):
"""
Builds an appointment from a dictionary.
This method is useful to load data from a database.
Args:
appointment_data (:obj:`dict`): a dictionary containing the following keys:
``{locator, to_self_delay, encrypted_blob, user_id}``
Returns:
:obj:`ExtendedAppointment <teos.extended_appointment.ExtendedAppointment>`: An appointment initialized
using the provided data.
Raises:
ValueError: If one of the mandatory keys is missing in ``appointment_data``.
"""
appointment = Appointment.from_dict(appointment_data)
user_id = appointment_data.get("user_id")
if not user_id:
raise ValueError("Wrong appointment data, user_id is missing")
else:
appointment = cls(appointment.locator, appointment.to_self_delay, appointment.encrypted_blob, user_id)
return appointment