示例#1
0
    def post(self, mine_guid):
        mine = Mine.find_by_mine_guid(mine_guid)
        if not mine:
            raise NotFound('Mine not found.')

        # see if this would be the first TSF
        mine_tsf_list = mine.mine_tailings_storage_facilities
        is_mine_first_tsf = len(mine_tsf_list) == 0

        data = self.parser.parse_args()
        mine_tsf = MineTailingsStorageFacility.create(
            mine, mine_tailings_storage_facility_name=data['mine_tailings_storage_facility_name'])
        mine.mine_tailings_storage_facilities.append(mine_tsf)

        if is_mine_first_tsf:
            try:
                tsf_required_reports = MineReportDefinition.find_required_reports_by_category('TSF')

                for tsf_req_doc in tsf_required_reports:
                    calculated_due_date = tsf_req_doc.default_due_date or datetime.utcnow()
                    MineReport.create(
                        mine_report_definition_id=tsf_req_doc.mine_report_definition_id,
                        mine_guid=mine.mine_guid,
                        due_date=calculated_due_date,
                        received_date=None,
                        submission_year=calculated_due_date.year,
                        permit_id=None)

            except Exception as e:
                db.session.rollback()
                current_app.logger.error(str(e))
                raise InternalServerError(str(e) + ", tsf not created")
        mine.save()
        return mine_tsf
示例#2
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()

        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.get('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