class ExamStatus(Resource):
    bcmp_service = BCMPService()

    @jwt.requires_auth
    def post(self, exam_id):

        csr = CSR.find_by_username(g.jwt_oidc_token_info['username'])

        try:
            exam = Exam.query.filter_by(exam_id=exam_id).first()

            if not (exam.office_id == csr.office_id
                    or csr.ita2_designate == 1):
                return {"The Exam Office ID and CSR Office ID do not match!"
                        }, 403

            bcmp_response = self.bcmp_service.send_exam_to_bcmp(exam)

            if bcmp_response:
                return {"bcmp": bcmp_response, "errors": {}}, 202
            else:
                return {
                    "message": "create_group_exam_bcmp failed",
                    "error": bcmp_response
                }, 403

        except exc.SQLAlchemyError as error:
            logging.error(error, exc_info=True)
            return {'message': 'API is down'}, 500
示例#2
0
class ExamBcmpPost(Resource):

    exam_schema = ExamSchema()
    bcmp_service = BCMPService()

    @oidc.accept_token(require_token=True)
    @api_call_with_retry
    def post(self):

        csr = CSR.find_by_username(g.oidc_token_info['username'])

        json_data = request.get_json()

        if 'bookdata' in json_data.keys():
            booking = json_data["bookdata"]
        else:
            booking = None

        exam, warning = self.exam_schema.load(json_data)

        if warning:
            logging.warning("WARNING: %s", warning)
            return {"message": warning}, 422

        if not (exam.office_id == csr.office_id or csr.ita2_designate == 1):
            return {"The Exam Office ID and CSR Office ID do not match!"}, 403

        formatted_data = ExamPost.format_data(self, json_data, exam)
        exam = formatted_data["exam"]

        invigilator = None
        if exam.invigilator_id:
            invigilator = Invigilator.query.filter_by(
                invigilator_id=exam.invigilator_id).first()

        bcmp_response = None
        if json_data["ind_or_group"] == "individual":

            exam_fees = json_data["fees"]

            logging.info("Creating individual pesticide exam")
            bcmp_response = self.bcmp_service.create_individual_exam(
                exam, exam_fees, invigilator,
                formatted_data["pesticide_office"], g.oidc_token_info)

        else:

            logging.info("Creating Group pesticide exam")
            bcmp_response = self.bcmp_service.create_group_exam_bcmp(
                exam, booking, formatted_data["candidates_list_bcmp"],
                invigilator, formatted_data["pesticide_office"],
                g.oidc_token_info)

        if bcmp_response:
            return {"bcmp_job_id": bcmp_response['jobId'], "errors": {}}, 201
        else:
            return {
                "message": "create_group_exam_bcmp failed",
                "error": bcmp_response
            }, 403
class ExamStatus(Resource):
    bcmp_service = BCMPService()

    @jwt.requires_auth
    def get(self, exam_id):

        csr = CSR.find_by_username(g.jwt_oidc_token_info['username'])

        try:
            exam = Exam.query.filter_by(exam_id=exam_id).first()

            if not (exam.office_id == csr.office_id
                    or csr.ita2_designate == 1):
                return {"The Exam Office ID and CSR Office ID do not match!"
                        }, 403

            job = self.bcmp_service.check_exam_status(exam)
            my_print(job)

            if job['jobStatus'] == 'PACKAGE_GENERATED':
                package_url = job["jobProperties"]["EXAM_PACKAGE_URL"]
                req = urllib.request.Request(package_url)
                response = urllib.request.urlopen(req).read()
                exam_file = io.BytesIO(response)
                file_wrapper = FileWrapper(exam_file)

                return Response(file_wrapper,
                                mimetype="application/pdf",
                                direct_passthrough=True,
                                headers={
                                    "Content-Disposition":
                                    'attachment; filename="%s.csv"' %
                                    exam.exam_id,
                                    "Content-Type":
                                    "application/pdf"
                                })
            else:
                return {
                    'message': 'Package not yet generated',
                    'status': job['jobStatus']
                }, 400
                # test_url = 'http://www.pdf995.com/samples/pdf.pdf'
                # req = urllib.request.Request(test_url)
                # response = urllib.request.urlopen(req).read()
                # exam_file = io.BytesIO(response)
                # file_wrapper = FileWrapper(exam_file)
                #
                # return Response(file_wrapper,
                #                 mimetype="application/pdf",
                #                 direct_passthrough=True,
                #                 headers={
                #                     "Content-Disposition": 'attachment; filename="%s.csv"' % exam.exam_id,
                #                     "Content-Type": "application/pdf"
                #                 })

        except exc.SQLAlchemyError as error:
            logging.error(error, exc_info=True)
            return {'message': 'API is down'}, 500
示例#4
0
class ExamList(Resource):
    bcmp_service = BCMPService()
    exam_schema = ExamSchema()

    @jwt.requires_auth
    def post(self):
        csr = CSR.find_by_username(g.jwt_oidc_token_info['username'])

        try:
            exams = Exam.query.filter_by(upload_received_ind=0).filter(
                Exam.bcmp_job_id.isnot(None))
            bcmp_response = self.bcmp_service.bulk_check_exam_status(exams)

            job_ids = []
            for job in bcmp_response["jobs"]:
                if job["jobStatus"] == "RESPONSE_UPLOADED":
                    job_ids.append(job["jobId"])

            my_print("job_ids to update: ")
            my_print(job_ids)

            exams_tobe_updated = None

            if len(job_ids) != 0:
                exams_tobe_updated = Exam.query.filter(
                    Exam.bcmp_job_id.in_(job_ids))

                for exam in exams_tobe_updated:
                    exam_upd = self.exam_schema.load(
                        {'upload_received_ind': 1},
                        instance=exam,
                        partial=True)
                    db.session.add(exam_upd)

                try:
                    db.session.commit()
                except:
                    db.session.rollback()
                    raise

            return {"exams_updated": exams_tobe_updated}, 200

        except exc.SQLAlchemyError as error:
            logging.error(error, exc_info=True)
            return {'message': 'API is down'}, 500
示例#5
0
class ExamEmailInvigilator(Resource):
    bcmp_service = BCMPService()

    @jwt.requires_auth
    def post(self, exam_id):

        csr = CSR.find_by_username(g.jwt_oidc_token_info['username'])

        try:
            exam = Exam.query.filter_by(exam_id=exam_id).first()

            if not (exam.office_id == csr.office_id
                    or csr.ita2_designate == 1):
                return {"The Exam Office ID and CSR Office ID do not match!"
                        }, 403

            json_data = request.get_json()
            invigilator_id = json_data["invigilator_id"]
            invigilator_name = json_data["invigilator_name"]
            invigilator_email = json_data["invigilator_email"]
            invigilator_phone = json_data["invigilator_phone"]

            if not invigilator_email or not invigilator_phone or not invigilator_name:
                return {
                    "Invigilator name, email, and phone number are required"
                }, 422

            response = self.bcmp_service.email_exam_invigilator(
                exam, invigilator_name, invigilator_email, invigilator_phone)

            if response:
                exam.invigilator_id = invigilator_id
                db.session.add(exam)
                db.session.commit()
                return {}, 200
            else:
                return {}, 500

        except exc.SQLAlchemyError as error:
            logging.error(error, exc_info=True)
            return {'message': 'API is down'}, 500
示例#6
0
class ExamPost(Resource):

    exam_schema = ExamSchema()
    bcmp_service = BCMPService()

    @jwt.has_one_of_roles([Role.internal_user.value])
    @api_call_with_retry
    def post(self):

        is_bcmp_req = True if request.args.get('bcmp_pesticide') else False

        my_print("is_bcmp_req: ")
        my_print(is_bcmp_req)

        csr = CSR.find_by_username(g.jwt_oidc_token_info['username'])

        json_data = request.get_json()

        exam = self.exam_schema.load(json_data)
        warning = self.exam_schema.validate(json_data)

        my_print("json_data: ")
        my_print(json_data)

        if warning:
            logging.warning("WARNING: %s", warning)
            return {"message": warning}, 422
        
        if not (exam.office_id == csr.office_id or csr.ita2_designate == 1):
            return {"The Exam Office ID and CSR Office ID do not match!"}, 403   
        
        if exam.is_pesticide:
            formatted_data = self.format_data(json_data, exam)
            exam = formatted_data["exam"]
            job = self.bcmp_service.check_exam_status(exam)
            my_print(job)
            if job and job['jobProperties'] and job['jobProperties']['JOB_ID']:
                exam.event_id = job['jobProperties']['JOB_ID']

        db.session.add(exam)
        db.session.commit()

        result = self.exam_schema.dump(exam)

        return {"exam": result,
                "errors": self.exam_schema.validate(exam)}, 201



    ## formating data to save on bcmp
    def format_data(self, json_data, exam):

        candidates_list_bcmp = []

        pesticide_office = None
        if json_data["sbc_managed"] == "sbc":
            pesticide_office = Office.query.filter_by(office_id=exam.office_id).first()
        else:
            pesticide_office = Office.query.filter_by(office_name="Pesticide Offsite").first()
            exam.office_id = pesticide_office.office_id

        if json_data["ind_or_group"] == "individual":
        
            exam_type = ExamType.query.filter_by(exam_type_id=exam.exam_type_id).first()

            if not exam_type:
                exam_type = ExamType.query.filter_by(pesticide_exam_ind=1, group_exam_ind=1).first()
            exam.exam_type = exam_type

        else:
            logging.info("For Group Exams")

            exam_type = ExamType.query.filter_by(exam_type_name="Group Environment Exam").first()
            if exam_type:
                exam.exam_type_id = exam_type.exam_type_id
                exam.exam_type = exam_type

            if json_data["candidates"]:
                candidates = json_data["candidates"]
                candidates_list = []
                for candidate in candidates:
                    candidate_temp = {}
                    candidate_temp["examinee_name"] = candidate["name"]
                    candidate_temp["examinee_email"] = candidate["email"]
                    candidate_temp["exam_type_id"] = candidate["exam_type_id"]
                    candidate_temp["fees"] = candidate["fees"]
                    candidate_temp["payee_ind"] = 1 if (candidate["billTo"] == "candidate") else 0
                    candidate_temp["receipt"] = candidate["receipt"]
                    candidate_temp["receipt_number"] = candidate["receipt"]
                    candidate_temp["payee_name"] = candidate["payeeName"]
                    candidate_temp["payee_email"] = candidate["payeeEmail"]
                    candidates_list.append(candidate_temp)
                    # for bcmp service
                    candidates_bcmp = copy.deepcopy(candidate_temp)
                    exam_type = ExamType.query.filter_by(exam_type_id=candidate["exam_type_id"]).first()
                    if exam_type.exam_type_name:
                        candidates_bcmp["exam_type"] = exam_type.exam_type_name
                    candidates_list_bcmp.append(candidates_bcmp)

                exam.candidates_list = candidates_list
        
        return { 
            'exam': exam, 
            'candidates_list_bcmp': candidates_list_bcmp, 
            'pesticide_office': pesticide_office,
        }