Skip to content

Commit

Permalink
Phase 3 (bcgov#368)
Browse files Browse the repository at this point in the history
* This commit contains working and generally finished (all questions are asked in the right order for the correct process with correct required/minLength/numeric enforecements E&OE).
Both the capture Individual and the capture Group workflows are working.  For working on wiring to the API.

* Added create individual and group exams API calls, and status call to stream files

* Clean up directory

* Added download and transfer document to BCMP

* All changes for SBC Managed, individual pesticide exams

* Fix migration file

* Test

* Fixed bug with exam inventory

* Fixed bugs with examinee email and phone

* Finished everything for individual exams

* Cleanup for everything with group exams

Co-authored-by: Scott Rumsby <[email protected]>
  • Loading branch information
sjrumsby and scottrumsby authored Feb 28, 2020
1 parent b068b17 commit dc9e54f
Show file tree
Hide file tree
Showing 52 changed files with 3,582 additions and 1,071 deletions.
16 changes: 15 additions & 1 deletion api/app/models/bookings/exam.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ class Exam(Base):

exam_id = db.Column(db.Integer, primary_key=True, autoincrement=True, nullable=False)
booking_id = db.Column(db.Integer, db.ForeignKey("booking.booking_id", ondelete="set null"), nullable=True)
exam_type_id = db.Column(db.Integer, db.ForeignKey("examtype.exam_type_id"), nullable=False)
exam_type_id = db.Column(db.Integer, db.ForeignKey("examtype.exam_type_id"), nullable=True)
office_id = db.Column(db.Integer, db.ForeignKey("office.office_id"), nullable=False)
invigilator_id = db.Column(db.Integer, db.ForeignKey("invigilator.invigilator_id"), nullable=True)
event_id = db.Column(db.String(25), nullable=True)
exam_name = db.Column(db.String(50), nullable=False)
examinee_name = db.Column(db.String(50), nullable=True)
examinee_email = db.Column(db.String(400), nullable=True)
examinee_phone = db.Column(db.String(400), nullable=True)
expiry_date = db.Column(db.DateTime, nullable=True)
notes = db.Column(db.String(400), nullable=True)
exam_received_date = db.Column(db.DateTime, nullable=True)
Expand All @@ -36,10 +39,21 @@ class Exam(Base):
exam_returned_tracking_number = db.Column(db.String(255), nullable=True)
exam_written_ind = db.Column(db.Integer, nullable=False, default=0)
offsite_location = db.Column(db.String(50), nullable=True)
bcmp_job_id = db.Column(db.String(100), nullable=True)
exam_destroyed_date = db.Column(db.String(50), nullable=True)
upload_received_ind = db.Column(db.Integer, nullable=True, default=0)
sbc_managed_ind = db.Column(db.Integer, nullable=True, default=0)
receipt = db.Column(db.String(50), nullable=True)
payee_ind = db.Column(db.Integer, nullable=True, default=0)
receipt_sent_ind = db.Column(db.Integer, nullable=True, default=0)
payee_email = db.Column(db.String(50), nullable=True)
payee_name = db.Column(db.String(50), nullable=True)
payee_phone = db.Column(db.String(50), nullable=True)

booking = db.relationship("Booking")
exam_type = db.relationship("ExamType")
office = db.relationship("Office")
invigilator = db.relationship("Invigilator")

def __repr__(self):
return '<Exam Name: (name={self.exam_name!r})>'.format(self=self)
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
41 changes: 41 additions & 0 deletions api/app/resources/bookings/exam/exam_bulk_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'''Copyright 2018 Province of British Columbia
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.'''

import logging
from flask import g
from flask_restplus import Resource
from sqlalchemy import exc
from app.models.bookings import Exam
from app.models.theq import CSR
from app.utilities.bcmp_service import BCMPService
from qsystem import api, oidc


@api.route("/exams/bcmp_status/", methods=["POST"])
class ExamList(Resource):
bcmp_service = BCMPService()

@oidc.accept_token(require_token=True)
def post(self):
csr = CSR.find_by_username(g.oidc_token_info['username'])

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

return {}, 200

except exc.SQLAlchemyError as error:
logging.error(error, exc_info=True)
return {'message': 'API is down'}, 500
78 changes: 78 additions & 0 deletions api/app/resources/bookings/exam/exam_download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'''Copyright 2018 Province of British Columbia
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.'''

from flask import g, Response
from flask_restplus import Resource
import io
import logging
import urllib
from werkzeug.wsgi import FileWrapper
from sqlalchemy import exc
from app.models.theq import CSR
from app.models.bookings import Exam
from app.utilities.bcmp_service import BCMPService
from qsystem import api, oidc


@api.route("/exams/<int:exam_id>/download/", methods=["GET"])
class ExamStatus(Resource):
bcmp_service = BCMPService()

@oidc.accept_token(require_token=True)
def get(self, exam_id):

csr = CSR.find_by_username(g.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.liaison_designate == 1):
return {"The Exam Office ID and CSR Office ID do not match!"}, 403

status = self.bcmp_service.check_exam_status(exam)
print(status)

if status == 'PACKAGE_GENERATED':
package_url = status["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': 'API is down'}, 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
66 changes: 66 additions & 0 deletions api/app/resources/bookings/exam/exam_email_invigilator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'''Copyright 2018 Province of British Columbia
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.'''

from flask import g, request
from flask_restplus import Resource
import logging
from sqlalchemy import exc
from app.models.theq import CSR
from app.models.bookings import Exam
from app.utilities.bcmp_service import BCMPService
from qsystem import api, db, oidc


@api.route("/exams/<int:exam_id>/email_invigilator/", methods=["POST"])
class ExamEmailInvigilator(Resource):
bcmp_service = BCMPService()

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

csr = CSR.find_by_username(g.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.liaison_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
51 changes: 41 additions & 10 deletions api/app/resources/bookings/exam/exam_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@
import logging
from flask import request, g
from flask_restx import Resource
from app.models.theq import CSR
from app.models.theq import CSR, Office
from flask_restplus import Resource
from app.models.bookings import ExamType
from app.schemas.bookings import ExamSchema
from qsystem import api, api_call_with_retry, db, oidc
from app.utilities.bcmp_service import BCMPService


@api.route("/exams/", methods=["POST"])
class ExamPost(Resource):

exam_schema = ExamSchema()
bcmp_service = BCMPService()

@oidc.accept_token(require_token=True)
@api_call_with_retry
Expand All @@ -38,15 +42,42 @@ def post(self):
if warning:
logging.warning("WARNING: %s", warning)
return {"message": warning}, 422
print("+=+=+=+= NAME: %s +=+=+=+=" % exam.examinee_name)

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

if exam_type.pesticide_exam_ind:
if not exam_type.group_exam_ind:
logging.info("Create BCMP exam since this is a pesticide exam")

if json_data["sbc_managed"] != "sbc":
print("Setting non-SBC shit")
pesticide_office = Office.query.filter_by(office_name="Pesticide Offsite").first()
exam.office_id = pesticide_office.office_id

if exam_type.group_exam_ind:
logging.info("Creating group pesticide exam")
bcmp_response = self.bcmp_service.create_group_exam(exam)
else:
logging.info("Creating individual pesticide exam")
bcmp_response = self.bcmp_service.create_individual_exam(exam, exam_type)

if bcmp_response:
exam.bcmp_job_id = bcmp_response['jobId']
else:
print("Do the group exam shit here")
else:
if not (exam.office_id == csr.office_id or csr.liaison_designate == 1):
return {"The Exam Office ID and CSR Office ID do not match!"}, 403

if exam.office_id == csr.office_id or csr.liaison_designate == 1:

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

result = self.exam_schema.dump(exam)
result = self.exam_schema.dump(exam)

return {"exam": result.data,
"errors": result.errors}, 201
else:
return {"The Exam Office ID and CSR Office ID do not match!"}, 403
return {"exam": result.data,
"errors": result.errors}, 201
45 changes: 45 additions & 0 deletions api/app/resources/bookings/exam/exam_transfer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'''Copyright 2018 Province of British Columbia
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.'''

from flask import g
from flask_restplus import Resource
import logging
from sqlalchemy import exc
from app.models.theq import CSR
from app.models.bookings import Exam
from app.utilities.bcmp_service import BCMPService
from qsystem import api, oidc


@api.route("/exams/<int:exam_id>/transfer/", methods=["POST"])
class ExamStatus(Resource):
bcmp_service = BCMPService()

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

csr = CSR.find_by_username(g.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.liaison_designate == 1):
return {"The Exam Office ID and CSR Office ID do not match!"}, 403

self.bcmp_service.send_exam_to_bcmp(exam)
return {}

except exc.SQLAlchemyError as error:
logging.error(error, exc_info=True)
return {'message': 'API is down'}, 500
52 changes: 52 additions & 0 deletions api/app/resources/bookings/exam/exam_upload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'''Copyright 2018 Province of British Columbia
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.'''

from flask import g, request
from flask_restplus import Resource
import logging
from sqlalchemy import exc
from app.models.theq import CSR
from app.models.bookings import Exam
from app.utilities.document_service import DocumentService
from qsystem import api, application, oidc


@api.route("/exams/<int:exam_id>/upload/", methods=["GET"])
class ExamStatus(Resource):

@oidc.accept_token(require_token=True)
def get(self, exam_id):
csr = CSR.find_by_username(g.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.liaison_designate == 1):
return {"The Exam Office ID and CSR Office ID do not match!"}, 403
client = DocumentService(
application.config["MINIO_HOST"],
application.config["MINIO_BUCKET"],
application.config["MINIO_ACCESS_KEY"],
application.config["MINIO_SECRET_KEY"],
application.config["MINIO_USE_SECURE"]
)

object_name = "%s.pdf" % exam_id
url = client.get_presigned_put_url(object_name)

return {"url": url}

except exc.SQLAlchemyError as error:
logging.error(error, exc_info=True)
return {'message': 'API is down'}, 500
Loading

0 comments on commit dc9e54f

Please sign in to comment.