def test_mine_report_definition_model_due_date_function():
    rand_due_date_report = MineReportDefinition(
        due_date_period_months=12, mine_report_due_date_type='FIS')
    #it calls this computation function
    assert rand_due_date_report.default_due_date == _calculate_due_date(
        datetime.now(), rand_due_date_report.mine_report_due_date_type,
        rand_due_date_report.due_date_period_months)
Пример #2
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
Пример #3
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
Пример #4
0
def test_post_first_mine_tailings_storage_facility_by_mine_guid_creates_tsf_required_reports(
    test_client, db_session, auth_headers):
    mine = MineFactory(minimal=True)
    assert len(mine.mine_tailings_storage_facilities) == 0

    post_resp = test_client.post(
        f'/mines/{mine.mine_guid}/tailings',
        data={'mine_tailings_storage_facility_name': 'a name'},
        headers=auth_headers['full_auth_header'])
    assert post_resp.status_code == 201
    tsf_required_reports = MineReportDefinition.find_required_reports_by_category('TSF')
    assert len(mine.mine_reports) == len(tsf_required_reports)
Пример #5
0
def test_post_mine_report_with_permit(test_client, db_session, auth_headers):
    mine = MineFactory(mine_reports=ONE_REPORT)
    mine_report = mine.mine_reports[0]

    mine_report_definition = MineReportDefinition.active()
    new_report_definition = [
        x for x in mine_report_definition if x.mine_report_definition_guid !=
        mine_report.mine_report_definition_guid
    ][0]

    data = {
        'mine_report_definition_guid':
        new_report_definition.mine_report_definition_guid,
        'submission_year': '2019',
        'due_date': '2019-07-05',
        'permit_guid': mine.mine_permit[0].permit_guid,
        'received_date': None,
    }
    post_resp = test_client.post(f'/mines/{mine.mine_guid}/reports',
                                 headers=auth_headers['full_auth_header'],
                                 json=data)
    post_data = json.loads(post_resp.data.decode())
    assert post_resp.status_code == 201
    assert post_data['permit_guid'] == str(mine.mine_permit[0].permit_guid)
Пример #6
0
def test_post_mine_report(test_client, db_session, auth_headers):
    mine = MineFactory(mine_reports=ONE_REPORT)
    mine_report = mine.mine_reports[0]

    mine_report_definition = MineReportDefinition.active()

    num_reports = len(mine.mine_reports)
    new_report_definition = [
        x for x in mine_report_definition if x.mine_report_definition_guid !=
        mine_report.mine_report_definition_guid
    ][0]
    data = {
        'mine_report_definition_guid':
        str(new_report_definition.mine_report_definition_guid),
        'submission_year':
        '2019',
        'due_date':
        '2019-07-05',
        'permit_guid':
        None,
        'received_date':
        None,
    }
    post_resp = test_client.post(f'/mines/{mine.mine_guid}/reports',
                                 headers=auth_headers['full_auth_header'],
                                 json=data)
    post_data = json.loads(post_resp.data.decode())

    updated_mine = Mine.find_by_mine_guid(str(mine.mine_guid))
    reports = updated_mine.mine_reports

    assert post_resp.status_code == 201
    assert len(reports) == num_reports + 1
    assert new_report_definition.mine_report_definition_id in [
        x.mine_report_definition_id for x in reports
    ]
Пример #7
0
 def get(self):
     return MineReportDefinition.active()
Пример #8
0
def RandomMineReportDefinitionWithDueDate():
    return random.choice([
        x.mine_report_definition_id for x in MineReportDefinition.active()
        if x.due_date_period_months and x.due_date_period_months > 0
    ])
Пример #9
0
def RandomMineReportDefinition():
    return random.choice(
        [x.mine_report_definition_id for x in MineReportDefinition.active()])
Пример #10
0
 def get(self):
     return MineReportDefinition.get_all()