def load(self, data, many=None, partial=None): de_data = super().load(data, many=many, partial=partial) de_data = [de_data] if not many else de_data for de_data_item in de_data: option_class, option_name = de_data_item['repeat_option'].split( '.') de_data_item['repeat_option'] = eval(option_class)[option_name] base_time_slot_dicts = de_data_item.get('base_time_slots') if base_time_slot_dicts is not None: base_slots = [] for base_slot_dict in base_time_slot_dicts: base_slot = TimeSlot.create(commit=False, **base_slot_dict) base_slots.append(base_slot) de_data_item['base_time_slots'] = base_slots repeat_time_slot_dicts = de_data_item.get('repeat_time_slots', []) if repeat_time_slot_dicts is not None: repeat_slots = [] for repeat_slot_dict in repeat_time_slot_dicts: repeat_slot = TimeSlot.create(commit=False, **repeat_slot_dict) repeat_slots.append(repeat_slot) de_data_item['repeat_time_slots'] = repeat_slots return de_data if many else de_data[0]
def post(self): json_data = request.get_json() if not json_data: raise RequestException("No input data", 400) try: data = timeslot_schema.load(json_data) except ValidationError as err: raise RequestException("Invalid input data", 400, err.messages) timeslot = TimeSlot.create(**data) result = timeslot_schema.dump(timeslot) response = jsonify({ APIConst.MESSAGE: 'created new timeslot', APIConst.DATA: result }) return response