def post(self, mine_guid, permit_guid, permit_amendment_guid=None): if permit_amendment_guid: raise BadRequest('Unexpected permit_amendement_guid.') permit = Permit.find_by_permit_guid(permit_guid) if not permit: raise NotFound('Permit does not exist.') if not str(permit.mine_guid) == mine_guid: raise BadRequest( 'Permits mine_guid and provided mine_guid mismatch.') data = self.parser.parse_args() current_app.logger.info(f'creating permit_amendment with >> {data}') party = Party.find_by_party_guid(data.get('permittee_party_guid')) if not party: raise NotFound('Party not found') party_is_active_permittee = False permittees = MinePartyAppointment.find_by_permit_guid(permit_guid) for permittee in permittees: if permittee.end_date is None: if permittee.party_guid == party.party_guid: party_is_active_permittee = True else: # inactive old permittees permittee.end_date = datetime.utcnow() permittee.save() if not party_is_active_permittee: new_permittee = MinePartyAppointment.create(permit.mine_guid, data.get( 'permittee_party_guid'), 'PMT', datetime.utcnow(), None, self.get_user_info(), permit_guid, True) new_permittee.save() new_pa = PermitAmendment.create( permit, data.get('received_date'), data.get('issue_date'), data.get('authorization_end_date'), data.get('permit_amendment_type_code', 'AMD'), description=data.get('description')) uploadedFiles = data.get('uploadedFiles', []) for newFile in uploadedFiles: new_pa_doc = PermitAmendmentDocument( document_name=newFile['fileName'], document_manager_guid=newFile['document_manager_guid'], mine_guid=permit.mine_guid, ) new_pa.related_documents.append(new_pa_doc) new_pa.save() return new_pa
def put(self, bond_guid): # Get the bond and validate that it can be transferred bond = Bond.find_by_bond_guid(bond_guid) if bond is None: raise NotFound('No bond was found with the guid provided.') if bond.bond_status_code != "ACT": raise BadRequest('Only active bonds can be transferred.') # Get the permit to transfer the bond to and validate it permit_guid = request.json.get('permit_guid', None) if not permit_guid: raise BadRequest('permit_guid is required.') permit = Permit.find_by_permit_guid(permit_guid) if not permit: raise BadRequest( 'No permit was found with the permit_guid provided.') if permit.permit_guid == bond.permit.permit_guid: raise BadRequest( 'This bond is already associated with this permit.') if bond.permit.mine_guid != permit.mine_guid: raise BadRequest( 'You can only transfer to a permit on the same mine.') # Get the note to apply to the bond and the transferred bond note = request.json.get('note', None) if note: bond.note = note # Release the bond bond.bond_status_code = "REL" # Create the new "transferred bond" new_bond_json = marshal(bond, BOND) del new_bond_json['bond_id'] del new_bond_json['bond_guid'] del new_bond_json['permit_guid'] del new_bond_json['permit_no'] del new_bond_json['payer'] new_bond_json['bond_status_code'] = 'ACT' new_bond_json['note'] = note try: new_bond = Bond._schema().load(new_bond_json) except MarshmallowError as e: raise InternalServerError(e) permit.bonds.append(new_bond) bond.save() new_bond.save() return new_bond
def put(self, permit_guid, mine_guid): permit = Permit.find_by_permit_guid(permit_guid) if not permit: raise NotFound('Permit not found.') if not str(permit.mine_guid) == mine_guid: raise BadRequest('Permit and mine_guid mismatch.') data = self.parser.parse_args() for key, value in data.items(): if key in ['permit_no', 'mine_guid', 'uploadedFiles']: continue # non-editable fields from put setattr(permit, key, value) permit.save() return permit
def post(self): try: reclamation_invoice = ReclamationInvoice._schema().load( request.json['reclamation_invoice']) except MarshmallowError as e: raise BadRequest(e) permit = Permit.find_by_permit_guid(request.json['permit_guid']) if permit is None: raise BadRequest('No permit was found with the guid provided.') reclamation_invoice.permit = permit for doc in reclamation_invoice.documents: doc.mine_guid = permit.mine_guid reclamation_invoice.save() return reclamation_invoice, 201
def post(self): try: bond = Bond._schema().load(request.json['bond']) except MarshmallowError as e: raise BadRequest(e) permit = Permit.find_by_permit_guid(request.json['permit_guid']) if permit is None: raise BadRequest('No permit was found with the guid provided.') bond.permit = permit for doc in bond.documents: doc.mine_guid = permit.mine_guid bond.save() return bond, 201
def post(self): data = self.parser.parse_args() mine = Mine.find_by_mine_guid(data['mine_guid']) permit = Permit.find_by_permit_guid(data['permit_guid']) err_str = '' if not mine: err_str += 'Mine not Found. ' if not permit: err_str += 'Permit not Found. ' if mine and not mine.major_mine_ind: err_str += 'Permit Applications can only be created on mines where major_mine_ind=True' if err_str: raise BadRequest(err_str) new_now = NOWApplicationIdentity(mine_guid=data['mine_guid'], permit=permit) new_now.now_application = NOWApplication( notice_of_work_type_code=data['notice_of_work_type_code'], now_application_status_code='REC', submitted_date=data['submitted_date'], received_date=data['received_date']) new_now.save() return new_now, 201
def test_permit_model_find_by_permit_guid(db_session): permit_guid = PermitFactory().permit_guid permit = Permit.find_by_permit_guid(str(permit_guid)) assert permit.permit_guid == permit_guid
def post(self, mine_guid, permit_guid, permit_amendment_guid=None): if permit_amendment_guid: raise BadRequest('Unexpected permit_amendement_guid.') permit = Permit.find_by_permit_guid(permit_guid) if not permit: raise NotFound('Permit does not exist.') if not str(permit.mine_guid) == mine_guid: raise BadRequest('Permits mine_guid and provided mine_guid mismatch.') data = self.parser.parse_args() current_app.logger.info(f'creating permit_amendment with >> {data}') party = Party.find_by_party_guid(data.get('permittee_party_guid')) if not party: raise NotFound('Party not found') permittees = MinePartyAppointment.find_by_permit_guid(permit_guid) if not permittees: raise NotFound('Party appointments not found') permit_issue_datetime = data.get('issue_date') # convert permit_issue_date to a date object to compare with permittee start_date, #Both dates are stored in the DB as Dates, and are being converted to SQLAlchemy dateTimes in the modals, but for some reason being returned as Python Dates. permit_issue_date = datetime.date(permit_issue_datetime) is_historical_permit = False new_end_dates = MinePartyAppointment.find_appointment_end_dates( permit_guid, permit_issue_date) for permittee in permittees: # check if the new appointment is older than the current appointment, if so create a new permittee appointment if permittee.start_date > permit_issue_date: is_historical_permit = True else: # if the amendment is the newest, change the end dates of the other appointments position = new_end_dates.index(permittee.start_date) if new_end_dates.index(permittee.start_date) == 0: permittee.save() else: permittee.end_date = new_end_dates[position - 1] permittee.save() permittee_start_date = permit_issue_date position = new_end_dates.index(permit_issue_date) permittee_end_date = new_end_dates[position - 1] if is_historical_permit else None # create a new appointment, so every amendment is associated with a permittee new_permittee = MinePartyAppointment.create( permit.mine_guid, data.get('permittee_party_guid'), 'PMT', self.get_user_info(), start_date=permittee_start_date, end_date=permittee_end_date, permit_guid=permit_guid) new_permittee.save() new_pa = PermitAmendment.create( permit, data.get('received_date'), data.get('issue_date'), data.get('authorization_end_date'), data.get('permit_amendment_type_code', 'AMD'), description=data.get('description')) uploadedFiles = data.get('uploadedFiles', []) for newFile in uploadedFiles: new_pa_doc = PermitAmendmentDocument( document_name=newFile['fileName'], document_manager_guid=newFile['document_manager_guid'], mine_guid=permit.mine_guid, ) new_pa.related_documents.append(new_pa_doc) new_pa.save() return new_pa