Example #1
0
    def put(self, mine_guid, variance_guid):
        parser = CustomReqparser()
        # Arguments required by MineDocument
        parser.add_argument('document_name', type=str, required=True)
        parser.add_argument('document_manager_guid', type=str, required=True)
        parser.add_argument('variance_document_category_code', type=str, required=True)

        variance = Variance.find_by_variance_guid(variance_guid)

        if not variance:
            raise NotFound('Unable to fetch variance.')

        data = parser.parse_args()
        document_name = data.get('document_name')
        document_manager_guid = data.get('document_manager_guid')

        # Register new file upload
        mine_doc = MineDocument(mine_guid=mine_guid,
                                document_manager_guid=document_manager_guid,
                                document_name=document_name)

        if not mine_doc:
            raise BadRequest('Unable to register uploaded file as document')

        # Associate Variance & MineDocument to create Variance Document
        # Add fields specific to Variance Documents
        mine_doc.save()
        variance_doc = VarianceDocumentXref(
            mine_document_guid=mine_doc.mine_document_guid,
            variance_id=variance.variance_id,
            variance_document_category_code=data.get('variance_document_category_code'))

        variance.documents.append(variance_doc)
        variance.save()
        return variance
Example #2
0
    def put(self, mine_guid, mine_party_appt_guid):
        parser = CustomReqparser()
        # Arguments required by MineDocument
        parser.add_argument('document_name', type=str, required=True)
        parser.add_argument('document_manager_guid', type=str, required=True)

        mine_party_appt = MinePartyAppointment.find_by_mine_party_appt_guid(mine_party_appt_guid)

        if not mine_party_appt:
            raise NotFound('Mine Party Appointment.')

        data = parser.parse_args()
        document_name = data.get('document_name')
        document_manager_guid = data.get('document_manager_guid')

        # Register new file upload
        mine_doc = MineDocument(mine_guid=mine_guid,
                                document_manager_guid=document_manager_guid,
                                document_name=document_name)

        if not mine_doc:
            raise BadRequest('Unable to register uploaded file as document')

        mine_doc.save()
        mine_party_appt.documents.append(mine_doc)
        mine_party_appt.save()
        return mine_party_appt
    def post(self, mine_guid):
        mine = Mine.find_by_mine_guid(mine_guid)
        if not mine:
            raise NotFound('Mine not found')

        data = self.parser.parse_args()

        mine_report_definition = MineReportDefinition.find_by_mine_report_definition_guid(
            data['mine_report_definition_guid'])
        permit = Permit.find_by_permit_guid_or_no(data['permit_guid'])
        if mine_report_definition is None:
            raise BadRequest('A report must be selected from the list.')

        if permit and permit.mine_guid != mine.mine_guid:
            raise BadRequest(
                'The permit must be associated with the selected mine.')
        mine_report = MineReport.create(
            mine_report_definition_id=mine_report_definition.
            mine_report_definition_id,
            mine_guid=mine.mine_guid,
            due_date=data['due_date'],
            received_date=data['received_date'],
            submission_year=data['submission_year'],
            permit_id=permit.permit_id if permit else None)

        submissions = data.get('mine_report_submissions')
        if submissions:
            submission = submissions[-1]
            if len(submission.get('documents')) > 0:
                submission_status = data.get(
                    'mine_report_submission_status') if data.get(
                        'mine_report_submission_status') else 'NRQ'
                report_submission = MineReportSubmission(
                    mine_report_submission_status_code=submission_status,
                    submission_date=datetime.utcnow())
                for submission_doc in submission.get('documents'):
                    mine_doc = MineDocument(
                        mine_guid=mine.mine_guid,
                        document_name=submission_doc['document_name'],
                        document_manager_guid=submission_doc[
                            'document_manager_guid'])

                    if not mine_doc:
                        raise BadRequest(
                            'Unable to register uploaded file as document')

                    mine_doc.save()

                    report_submission.documents.append(mine_doc)

                mine_report.mine_report_submissions.append(report_submission)
        try:
            mine_report.save()
        except Exception as e:
            raise InternalServerError(f'Error when saving: {e}')

        return mine_report, 201
Example #4
0
    def put(self, mine_guid, mine_incident_guid, mine_document_guid):
        parser = CustomReqparser()
        parser.add_argument('filename', type=str, required=True)
        parser.add_argument('document_manager_guid', type=str, required=True)
        parser.add_argument('mine_incident_document_type', type=str, required=True)

        mine_incident = MineIncident.find_by_mine_incident_guid(mine_incident_guid)
        mine = Mine.find_by_mine_guid(mine_guid)

        if not mine_incident:
            raise NotFound('Mine incident not found')
        if not mine:
            raise NotFound('Mine not found.')

        data = parser.parse_args()
        document_manager_guid = data.get('document_manager_guid')
        file_name = data.get('filename')
        mine_incident_document_type = data.get('mine_incident_document_type')

        mine_doc = MineDocument(mine_guid=mine.mine_guid,
                                document_name=file_name,
                                document_manager_guid=document_manager_guid)

        if not mine_doc:
            raise BadRequest('Unable to register uploaded file as document')

        mine_doc.save()
        mine_incident_doc = MineIncidentDocumentXref(
            mine_document_guid=mine_doc.mine_document_guid,
            mine_incident_id=mine_incident.mine_incident_id,
            mine_incident_document_type_code=mine_incident_document_type
            if mine_incident_document_type else 'INI')

        mine_incident.documents.append(mine_incident_doc)
        mine_incident.save()

        return mine_incident
    def delete(self, mine_guid, variance_guid, mine_document_guid):
        variance = Variance.find_by_mine_guid_and_variance_guid(
            mine_guid, variance_guid)
        mine_document = MineDocument.find_by_mine_document_guid(
            mine_document_guid)

        if variance is None:
            raise NotFound('Variance not found.')
        if mine_document is None:
            raise NotFound('Mine document not found.')
        if mine_document not in variance.mine_documents:
            raise NotFound('Mine document not found on variance.')

        variance.mine_documents.remove(mine_document)
        variance.save()

        return ('', 204)
Example #6
0
    def delete(self, mine_guid, mine_incident_guid, mine_document_guid):
        if not mine_document_guid:
            raise BadRequest('must provide document_guid to be unlinked')

        mine_incident = MineIncident.find_by_mine_incident_guid(mine_incident_guid)
        mine_document = MineDocument.find_by_mine_document_guid(mine_document_guid)

        if mine_incident is None:
            raise NotFound('Mine Incident not found.')
        if mine_document is None:
            raise NotFound('Mine Document not found.')
        if mine_document not in mine_incident.mine_documents:
            raise NotFound('Mine document not found on incident.')

        mine_incident.mine_documents.remove(mine_document)
        mine_incident.save()

        return ('', 204)
Example #7
0
    def put(self, mine_guid, mine_incident_guid):
        incident = MineIncident.find_by_mine_incident_guid(mine_incident_guid)
        if not incident or str(incident.mine_guid) != mine_guid:
            raise NotFound("Mine Incident not found")

        data = self.parser.parse_args()

        do_sub_codes = []
        if data['determination_type_code'] == 'DO':
            do_sub_codes = data['dangerous_occurrence_subparagraph_ids']
            if not do_sub_codes:
                raise BadRequest(
                    'Dangerous occurrences require one or more cited sections of HSRC code 1.7.3'
                )

        for key, value in data.items():
            if key in [
                    'dangerous_occurrence_subparagraph_ids', 'recommendations'
            ]:
                continue
            if key in [
                    'reported_to_inspector_party_guid',
                    'responsible_inspector_party_guid',
                    'determination_inspector_party_guid'
            ]:
                tmp_party = Party.query.filter_by(party_guid=value).first()
                if tmp_party and 'INS' in tmp_party.business_roles_codes:
                    setattr(incident, key, value)
            else:
                setattr(incident, key, value)

        recommendations = data.get('recommendations')
        if recommendations is not None:
            self._handle_recommendations(incident, recommendations)

        incident.dangerous_occurrence_subparagraphs = []
        for id in do_sub_codes:
            sub = ComplianceArticle.find_by_compliance_article_id(id)
            if not _compliance_article_is_do_subparagraph(sub):
                raise BadRequest(
                    'One of the provided compliance articles is not a sub-paragraph of section 1.7.3 (dangerous occurrences)'
                )
            incident.dangerous_occurrence_subparagraphs.append(sub)

        updated_documents = data.get('updated_documents')
        if updated_documents is not None:
            for updated_document in updated_documents:
                if not any(
                        str(doc.document_manager_guid) ==
                        updated_document['document_manager_guid']
                        for doc in incident.documents):
                    mine_doc = MineDocument(
                        mine_guid=mine_guid,
                        document_name=updated_document['document_name'],
                        document_manager_guid=updated_document[
                            'document_manager_guid'])

                    if not mine_doc:
                        raise BadRequest(
                            'Unable to register uploaded file as document')

                    mine_doc.save()
                    mine_incident_doc = MineIncidentDocumentXref(
                        mine_document_guid=mine_doc.mine_document_guid,
                        mine_incident_id=incident.mine_incident_id,
                        mine_incident_document_type_code=updated_document[
                            'mine_incident_document_type_code']
                        if updated_document['mine_incident_document_type_code']
                        else 'INI')

                    incident.documents.append(mine_incident_doc)
                    mine_incident_doc.save()

            for doc in incident.documents:
                if not any(updated_document['document_manager_guid'] == str(
                        doc.document_manager_guid)
                           for updated_document in updated_documents):
                    incident.documents.remove(doc)
                    db.session.delete(doc)
                    db.session.commit()

        incident.save()
        return incident
Example #8
0
    def post(self, mine_guid):
        mine = Mine.find_by_mine_guid(mine_guid)
        if not mine:
            raise NotFound('Mine not found')

        data = self.parser.parse_args()

        do_sub_codes = []
        if data['determination_type_code'] == 'DO':
            do_sub_codes = data['dangerous_occurrence_subparagraph_ids']
            if not do_sub_codes:
                raise BadRequest(
                    'Dangerous occurrences require one or more cited sections of HSRC code 1.7.3'
                )

        incident = MineIncident.create(
            mine,
            data['incident_timestamp'],
            data['incident_description'],
            determination_type_code=data['determination_type_code'],
            mine_determination_type_code=data['mine_determination_type_code'],
            mine_determination_representative=data[
                'mine_determination_representative'],
            followup_investigation_type_code=data[
                'followup_investigation_type_code'],
            reported_timestamp=data['reported_timestamp'],
            reported_by_name=data['reported_by_name'],
        )

        incident.reported_by_email = data.get('reported_by_email')
        incident.reported_by_phone_no = data.get('reported_by_phone_no')
        incident.reported_by_phone_ext = data.get('reported_by_phone_ext')
        incident.number_of_fatalities = data.get('number_of_fatalities')
        incident.number_of_injuries = data.get('number_of_injuries')
        incident.emergency_services_called = data.get(
            'emergency_services_called')
        incident.followup_inspection = data.get('followup_inspection')
        incident.followup_inspection_date = data.get(
            'followup_inspection_date')
        incident.status_code = data.get('status_code')
        incident.proponent_incident_no = data.get('proponent_incident_no')

        # lookup and validated inspector party relationships
        tmp_party = Party.query.filter_by(
            party_guid=data.get('reported_to_inspector_party_guid')).first()
        if tmp_party and 'INS' in tmp_party.business_roles_codes:
            incident.reported_to_inspector_party_guid = tmp_party.party_guid
        tmp_party = Party.query.filter_by(
            party_guid=data.get('responsible_inspector_party_guid')).first()
        if tmp_party and 'INS' in tmp_party.business_roles_codes:
            incident.responsible_inspector_party_guid = tmp_party.party_guid
        tmp_party = Party.query.filter_by(
            party_guid=data.get('determination_inspector_party_guid')).first()
        if tmp_party and 'INS' in tmp_party.business_roles_codes:
            incident.determination_inspector_party_guid = tmp_party.party_guid

        incident.determination_type_code = data.get('determination_type_code')
        incident.followup_investigation_type_code = data.get(
            'followup_investigation_type_code')

        for id in do_sub_codes:
            sub = ComplianceArticle.find_by_compliance_article_id(id)
            if not _compliance_article_is_do_subparagraph(sub):
                raise BadRequest(
                    'One of the provided compliance articles is not a sub-paragraph of section 1.7.3 (dangerous occurrences)'
                )
            incident.dangerous_occurrence_subparagraphs.append(sub)

        updated_documents = data.get('updated_documents')
        if updated_documents is not None:
            for updated_file in updated_documents:
                mine_doc = MineDocument(
                    mine_guid=mine.mine_guid,
                    document_name=updated_file['document_name'],
                    document_manager_guid=updated_file['document_manager_guid']
                )

                if not mine_doc:
                    raise BadRequest(
                        'Unable to register uploaded file as document')

                mine_doc.save()
                mine_incident_doc = MineIncidentDocumentXref(
                    mine_document_guid=mine_doc.mine_document_guid,
                    mine_incident_id=incident.mine_incident_id,
                    mine_incident_document_type_code=updated_file[
                        'mine_incident_document_type_code']
                    if updated_file['mine_incident_document_type_code'] else
                    'INI')

                incident.documents.append(mine_incident_doc)

        try:
            incident.save()
        except Exception as e:
            raise InternalServerError(f'Error when saving: {e}')

        recommendations = data.get('recommendations')
        if recommendations is not None:
            for recommendation in recommendations:
                rec_string = recommendation.get('recommendation')
                if rec_string is None:
                    continue
                new_recommendation = MineIncidentRecommendation.create(
                    rec_string, mine_incident_id=incident.mine_incident_id)
                new_recommendation.save()

        return incident, 201
Example #9
0
    def put(self, mine_guid, mine_report_guid):
        mine = Mine.find_by_mine_guid(mine_guid)
        mine_report = MineReport.find_by_mine_report_guid(mine_report_guid)
        if not mine_report or str(mine_report.mine_guid) != mine_guid:
            raise NotFound("Mine Report not found")

        data = self.parser.parse_args()

        if 'due_date' in data:
            mine_report.due_date = data['due_date']

        if 'received_date' in data:
            mine_report.received_date = data['received_date']

        report_submissions = data.get('mine_report_submissions')
        submission_iterator = iter(report_submissions)
        new_submission = next(
            (x for x in submission_iterator
             if x.get('mine_report_submission_guid') is None), None)
        if new_submission is not None:
            new_report_submission = MineReportSubmission(
                mine_report_submission_status_code='MIA',
                submission_date=datetime.now())

            # Copy the current list of documents for the report submission
            last_submission_docs = mine_report.mine_report_submissions[
                0].documents.copy() if len(
                    mine_report.mine_report_submissions) > 0 else []

            # Gets the difference between the set of documents in the new submission and the last submission
            new_docs = [
                x for x in new_submission.get('documents') if not any(
                    str(doc.document_manager_guid) ==
                    x['document_manager_guid'] for doc in last_submission_docs)
            ]
            # Get the documents that were on the last submission but not part of the new submission
            removed_docs = [
                x for x in last_submission_docs
                if not any(doc['document_manager_guid'] == str(
                    x.document_manager_guid)
                           for doc in new_submission.get('documents'))
            ]
            # Remove the deleted documents from the existing set.
            for doc in removed_docs:
                last_submission_docs.remove(doc)

            if len(last_submission_docs) > 0:
                new_report_submission.documents.extend(last_submission_docs)

            for doc in new_docs:
                mine_doc = MineDocument(
                    mine_guid=mine.mine_guid,
                    document_name=doc['document_name'],
                    document_manager_guid=doc['document_manager_guid'])

                if not mine_doc:
                    raise BadRequest(
                        'Unable to register uploaded file as document')

                mine_doc.save()

                new_report_submission.documents.append(mine_doc)

            mine_report.mine_report_submissions.append(new_report_submission)

        try:
            mine_report.save()
        except Exception as e:
            raise InternalServerError(f'Error when saving: {e}')
        return mine_report