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
def get(self, mine_guid): mrd_category = request.args.get('mine_report_definition_category') if mrd_category: mine_reports = MineReport.find_by_mine_guid_and_category( mine_guid, mrd_category) else: mine_reports = MineReport.find_by_mine_guid(mine_guid) return mine_reports
def get(self, mine_guid, mine_report_guid): mine_report = MineReport.find_by_mine_report_guid(mine_report_guid) if not mine_report: raise NotFound('Mine report not found') mine_report_submissions = mine_report.mine_report_submissions if not mine_report_submissions: raise NotFound('No mine report submissions for this report') current_app.logger.info(f'Retrieving comments for {mine_report}') comments = [ comment.__dict__ for submission in mine_report_submissions[:-1] for comment in submission.comments ] for comment in comments: comment['from_latest_submission'] = False latest_comments = [ comment.__dict__ for submission in mine_report_submissions[-1:] for comment in submission.comments ] for comment in latest_comments: comment['from_latest_submission'] = True return comments + latest_comments, 200
def delete(self, mine_guid, mine_report_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") mine_report.deleted_ind = True mine_report.save() return None, 204
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
def test_post_mine_report_comment(test_client, db_session, auth_headers): mine_report = MineReportFactory() num_comments = len(mine_report.mine_report_submissions[-1].comments) data = {'report_comment': 'Test comment', 'comment_visibility_ind': False} post_resp = test_client.post( f'/mines/{mine_report.mine_guid}/reports/{mine_report.mine_report_guid}/comments', headers=auth_headers['full_auth_header'], json=data) updated_mine_report = MineReport.find_by_mine_report_guid( str(mine_report.mine_report_guid)) comments = updated_mine_report.mine_report_submissions[-1].comments assert post_resp.status_code == 201 assert len(comments) == num_comments + 1 assert comments[-1].report_comment == 'Test comment'
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.get('due_date') if 'received_date' in data: mine_report.received_date = data['received_date'] if 'submission_year' in data: mine_report.submission_year = data['submission_year'] if data.get('mine_report_submission_status') is not None: mine_report_submission_status = data.get( 'mine_report_submission_status') else: mine_report_submission_status = 'NRQ' 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( submission_date=datetime.now(), mine_report_submission_status_code=mine_report_submission_status ) # Copy the current list of documents for the report submission last_submission_docs = mine_report.mine_report_submissions[ -1].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) # if the status has changed, update the status of the last submission elif ( len(mine_report.mine_report_submissions) > 0 ) and mine_report_submission_status != mine_report.mine_report_submissions[ -1].mine_report_submission_status_code: mine_report.mine_report_submissions[ -1].mine_report_submission_status_code = mine_report_submission_status try: mine_report.save() except Exception as e: raise InternalServerError(f'Error when saving: {e}') return mine_report
def get(self, mine_guid, mine_report_guid): mine_report = MineReport.find_by_mine_report_guid(mine_report_guid) if not mine_report: raise NotFound("Mine Report not found") return mine_report