def test_post_permit_amendment_with_date_params(test_client, db_session,
                                                auth_headers):
    permit_guid = PermitFactory().permit_guid
    party_guid = PartyFactory(company=True).party_guid

    data = {
        'permittee_party_guid':
        party_guid,
        'received_date':
        datetime.today().strftime('%Y-%m-%d'),
        'issue_date':
        datetime.today().strftime('%Y-%m-%d'),
        'authorization_end_date':
        (datetime.today() + timedelta(days=1)).strftime('%Y-%m-%d')
    }

    post_resp = test_client.post(f'/permits/{permit_guid}/amendments',
                                 json=data,
                                 headers=auth_headers['full_auth_header'])
    post_data = json.loads(post_resp.data.decode())

    permittees = MinePartyAppointment.find_by_permit_guid(permit_guid)

    assert post_resp.status_code == 200, post_resp.response
    assert post_data['permit_guid'] == str(permit_guid), str(post_data)
    assert post_data['received_date'] == data['received_date']
    assert post_data['issue_date'] == data['issue_date']
    assert post_data['authorization_end_date'] == data[
        'authorization_end_date']
    assert permittees[0].party_guid == party_guid

    #permit_amdendment is actually in db
    assert PermitAmendment.find_by_permit_amendment_guid(
        post_data['permit_amendment_guid'])
예제 #2
0
def test_post_permit(test_client, db_session, auth_headers):
    mine = MineFactory()
    party_guid = PartyFactory(company=True).party_guid

    no_of_permits = len(mine.mine_permit)

    PERMIT_NO = 'mx-test-999'
    data = {
        'permittee_party_guid': str(party_guid),
        'permit_no': PERMIT_NO,
        'permit_status_code': 'O',
        'received_date': '1999-12-12',
        'issue_date': '1999-12-21',
        'authorization_end_date': '2012-12-02'
    }
    post_resp = test_client.post(f'/mines/{mine.mine_guid}/permits',
                                 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))
    permittees = MinePartyAppointment.find_by_permit_guid(
        updated_mine.mine_permit[0].permit_guid)

    assert post_resp.status_code == 200
    assert updated_mine.mine_permit[0].permit_no == PERMIT_NO
    assert permittees[0].party_guid == party_guid
    assert len(updated_mine.mine_permit) == no_of_permits + 1
예제 #3
0
    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
예제 #4
0
    def post(self, permit_guid=None, permit_amendment_guid=None):
        if not permit_guid:
            return self.create_error_payload(
                400, 'Permit_guid must be provided'), 400
        if permit_amendment_guid:
            return self.create_error_payload(
                400, 'unexpected permit_amendement_id'), 400

        permit = Permit.find_by_permit_guid(permit_guid)
        if not permit:
            return self.create_error_payload(404, 'permit does not exist'), 404

        data = self.parser.parse_args()
        current_app.logger.info(f'creating permit_amendment with >> {data}')

        received_date = data.get('received_date')
        issue_date = data.get('issue_date')
        authorization_end_date = data.get('authorization_end_date')
        permit_amendment_type_code = data.get('permit_amendment_type_code',
                                              'AMD')
        description = data.get('description')
        uploadedFiles = data.get('uploadedFiles', [])

        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()

        try:
            new_pa = PermitAmendment.create(permit,
                                            received_date,
                                            issue_date,
                                            authorization_end_date,
                                            permit_amendment_type_code,
                                            description=description)

            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.documents.append(new_pa_doc)
            new_pa.save()
        except Exception as e:
            return self.create_error_payload(500, 'Error: {}'.format(e)), 500
        return new_pa.json()
예제 #5
0
    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