def update(cls, channel_dict: dict): """ Update the current channel. Import: You must use save() to storage it in the database. :param channel_dict: A dictionary representation of the channel. :return: The updated channel or None if user if not valid. """ channel: ChannelModel = cls.from_dict(channel_dict) valid_channel: ChannelModel = ChannelModel.find_by_id(channel.id) if not valid_channel: return None # validate creation to check if there is time overlap. channel.creation_validation() # Copy all attributes from channel to valid_channel. valid_channel << channel # update equipments. equipments = [ EquipmentModel.from_dict(eq_dict) for eq_dict in channel_dict.get("equipments") ] valid_channel._delete_equipments() valid_channel.add_equipments(equipments) return valid_channel
def create_channel(cls, channel_dict: dict): channel: ChannelModel = cls.from_dict(channel_dict) channel.id = app_utils.generate_id(16) # validate creation channel.creation_validation() # Add equipments relational field. equipments = [ EquipmentModel.from_dict(eq_dict) for eq_dict in channel_dict.get("equipments") ] channel.add_equipments(equipments=equipments) return channel.save()