class InvigilatorListOffsiteGet(Resource): invigilator_schema = InvigilatorSchema(many=True) @jwt.requires_auth def get(self): csr = CSR.find_by_username(g.jwt_oidc_token_info['username']) pesticide_office = Office.query.filter_by( office_name="Pesticide Offsite").first() try: invigilators = Invigilator.query.filter_by(office_id=pesticide_office.office_id)\ .filter(Invigilator.deleted.is_(None)) result = self.invigilator_schema.dump(invigilators) return { 'invigilators': result, 'errors': self.invigilator_schema.validate(invigilators) }, 200 except exc.SQLAlchemyError as error: logging.error(error, exc_info=True) return {"message": "api is down"}, 500
class BookingSchema(ma.ModelSchema): class Meta: model = Booking jit = toastedmarshmallow.Jit booking_id = fields.Int(dump_only=True) booking_name = fields.Str() end_time = fields.DateTime() fees = fields.Str() room_id = fields.Int() start_time = fields.DateTime() invigilator_id = fields.Int(allow_none=True) office_id = fields.Int() sbc_staff_invigilated = fields.Int() booking_contact_information = fields.Str() invigilator = fields.Nested( InvigilatorSchema(only=('invigilator_id', 'invigilator_name', 'invigilator_notes'))) room = fields.Nested(RoomSchema(exclude=( "booking", "office", ))) office = fields.Nested( OfficeSchema(only=('appointments_enabled_ind', 'exams_enabled_ind', 'office_id', 'office_name', 'office_number', 'timezone')))
class BookingSchema(ma.SQLAlchemySchema): class Meta: model = Booking include_relationships = True load_instance = True jit = toastedmarshmallow.Jit booking_id = fields.Int(dump_only=True) booking_name = fields.Str() end_time = fields.DateTime() fees = fields.Str() room_id = fields.Int() start_time = fields.DateTime() shadow_invigilator_id = fields.Int(allow_none=True) office_id = fields.Int() sbc_staff_invigilated = fields.Int() booking_contact_information = fields.Str() blackout_flag = fields.Str(allow_none=True) blackout_notes = fields.Str(allow_none=True) recurring_uuid = fields.Str(allow_none=True) room = fields.Nested(RoomSchema(exclude=( "booking", "office", ))) office = fields.Nested( OfficeSchema(only=('appointments_enabled_ind', 'exams_enabled_ind', 'office_id', 'office_name', 'office_number', 'timezone'))) # NOTE: The reason for the exclude, rather than just a single include, is because # an include with a single field didn't seem to work. When I added a second field, it worked. # I only want a single field, so had to use an exclude instead. ????? invigilators = fields.Nested(InvigilatorSchema( exclude=('contact_name', 'contact_email', 'contract_number', 'contract_expiry_date', 'invigilator_name', 'invigilator_notes', 'shadow_count', 'shadow_flag', 'contact_phone', 'deleted', 'office')), many=True) def update_invigilators(self, data): invigilator_data = data.get('invigilators') invigilator_list = [] for invigilator in invigilator_data: id = invigilator.get('invigilator_id') invigilator_list.append(id) data['invigilators'] = invigilator_list return data @post_dump(pass_many=True) def fix_invigilators(self, data, many, **kwargs): if not many: data = self.update_invigilators(data) else: for booking in data: booking = self.update_invigilators(booking) return data
class InvigilatorPut(Resource): invigilator_schema = InvigilatorSchema() @oidc.accept_token(require_token=True) def put(self, id): try: add_param = request.args.get("add") subtract_param = request.args.get("subtract") validate_params(add_param, subtract_param) invigilator = Invigilator.query.filter_by(invigilator_id=id).first_or_404() invigilator_shadow_count = invigilator.shadow_count invigilator_shadow_flag = invigilator.shadow_flag if add_param == 'True' and subtract_param == 'False': if invigilator_shadow_count == 0 and invigilator_shadow_flag == 'N': invigilator_shadow_count += 1 elif invigilator_shadow_count == 1 and invigilator_shadow_flag == 'N': invigilator_shadow_count += 1 invigilator_shadow_flag = 'Y' else: return {"message": "Invigilator data is not correct in order to Add."}, 422 elif subtract_param == 'True' and add_param == 'False': if invigilator_shadow_count > 0 and invigilator_shadow_count <= 2: if invigilator_shadow_flag == 'Y': invigilator_shadow_flag = 'N' invigilator_shadow_count -= 1 else: invigilator_shadow_count -= 1 elif invigilator_shadow_count == 0: invigilator_shadow_count = 0 invigilator_shadow_flag = 'N' else: return {"message": "Invigilator data is outside of range to subtract."}, 422 data = {} data['shadow_count'] = invigilator_shadow_count data['shadow_flag'] = invigilator_shadow_flag invigilator, warning = self.invigilator_schema.load(data, instance=invigilator, partial=True) if warning: logging.warning("WARNING: %s", warning) return {"message": warning}, 422 db.session.add(invigilator) db.session.commit() result = self.invigilator_schema.dump(invigilator) return {"invigilator": result.data, "errors": result.errors}, 200 except exc.SQLAlchemyError as error: logging.error(error, exc_info=True) return {"message": "API is down."}, 500
class ExamSchema(ma.SQLAlchemySchema): class Meta: model = Exam include_relationships = True load_instance = True jit = toastedmarshmallow.Jit booking_id = fields.Int(allow_none=True) deleted_date = fields.Str(allow_none=True) event_id = fields.Str(allow_none=True) exam_destroyed_date = fields.Str(allow_none=True) exam_id = fields.Int(dump_only=True) exam_method = fields.Str() exam_name = fields.Str() exam_received = fields.Int() exam_received_date = fields.DateTime(allow_none=True) exam_type_id = fields.Int() examinee_name = fields.Str(allow_none=True) examinee_phone = fields.Str(allow_none=True) examinee_email = fields.Str(allow_none=True) expiry_date = fields.DateTime(allow_none=True) notes = fields.Str(allow_none=True) number_of_students = fields.Int(allow_none=True) office_id = fields.Int() invigilator_id = fields.Int(allow_none=True) session_number = fields.Int(allow_none=True) exam_returned_ind = fields.Int() exam_returned_date = fields.Str(allow_none=True) exam_returned_tracking_number = fields.Str(allow_none=True) exam_written_ind = fields.Int() upload_received_ind = fields.Int(allow_none=True) offsite_location = fields.Str(allow_none=True) sbc_managed_ind = fields.Int(allow_none=True) receipt = fields.Str(allow_none=True) receipt_number = fields.Str() fees = fields.Str() payee_ind = fields.Int(allow_none=True) receipt_sent_ind = fields.Int(allow_none=True) payee_name = fields.Str(allow_none=True) payee_email = fields.Str(allow_none=True) payee_phone = fields.Str(allow_none=True) bcmp_job_id = fields.Str(allow_none=True) is_pesticide = fields.Int(allow_none=True) candidates_list = fields.Nested(CandidateSchema) booking = fields.Nested(BookingSchema()) exam_type = fields.Nested(ExamTypeSchema()) invigilator = fields.Nested(InvigilatorSchema()) office = fields.Nested(OfficeSchema(only=('appointments_enabled_ind', 'exams_enabled_ind', 'office_id', 'office_name', 'office_number', 'timezone')))
class InvigilatorList(Resource): invigilator_schema = InvigilatorSchema(many=True) @oidc.accept_token(require_token=True) def get(self): csr = CSR.find_by_username(g.oidc_token_info['username']) try: invigilators = Invigilator.query.filter_by(office_id=csr.office_id) result = self.invigilator_schema.dump(invigilators) return {'invigilators': result.data, 'errors': result.errors}, 200 except exc.SQLAlchemyError as error: logging.error(error, exc_info=True) return {"message": "api is down"}, 500
class BookingSchema(ma.ModelSchema): class Meta: model = Booking jit = toastedmarshmallow.Jit booking_id = fields.Int(dump_only=True) booking_name = fields.Str() end_time = fields.DateTime() fees = fields.Str() room_id = fields.Int() start_time = fields.DateTime() invigilator_id = fields.Int() invigilator = fields.Nested(InvigilatorSchema()) room = fields.Nested(RoomSchema(exclude=( "booking", "office", )))
class BookingSchema(BaseSchema): class Meta: model = Booking include_relationships = True load_instance = True unknown = EXCLUDE booking_id = fields.Int(dump_only=True) booking_name = fields.Str() end_time = fields.DateTime() fees = fields.Str() room_id = fields.Int() start_time = fields.DateTime() shadow_invigilator_id = fields.Int(allow_none=True) office_id = fields.Int() sbc_staff_invigilated = fields.Int() booking_contact_information = fields.Str() blackout_flag = fields.Str(allow_none=True) blackout_notes = fields.Str(allow_none=True) recurring_uuid = fields.Str(allow_none=True) stat_flag = fields.Boolean(allow_none=True) room = fields.Nested(RoomSchema(exclude=("office",))) office = fields.Nested(OfficeSchema(only=('appointments_enabled_ind', 'exams_enabled_ind', 'office_id', 'office_name', 'office_number', 'timezone'))) # NOTE: The reason for the exclude, rather than just a single include, is because # an include with a single field didn't seem to work. When I added a second field, it worked. # I only want a single field, so had to use an exclude instead. ????? invigilators = fields.Nested(InvigilatorSchema(exclude=( 'contact_email', 'contract_number', 'contract_expiry_date', 'invigilator_name', 'invigilator_notes', 'shadow_count', 'shadow_flag', 'contact_phone', 'deleted', 'office' )), many=True) def update_invigilators(self, data): invigilator_data = data.get('invigilators') invigilator_list = [] # NOTE: The not none test put in by Chris to fix an error # PUT /bookings/recurring/uuid call if invigilator_data is not None: for invigilator in invigilator_data: identifier = invigilator.get('invigilator_id') invigilator_list.append(identifier) data['invigilators'] = invigilator_list return data @post_dump(pass_many=True) def fix_invigilators(self, data, many, **kwargs): if not many: data = self.update_invigilators(data) else: for booking in data: booking = self.update_invigilators(booking) return data @pre_load def convert_bool_to_int(self, in_data, **kwargs): if type(in_data) == dict and 'sbc_staff_invigilated' in in_data and type( in_data['sbc_staff_invigilated']) == bool: in_data['sbc_staff_invigilated'] = 1 if in_data['sbc_staff_invigilated'] else 0 return in_data