Exemple #1
0
def add_comments(new_nr, comments):
    # TODO: link to examiner IDs somehow - examiner_IDIR
    if comments:
        for c in comments:
            comm = Comment()
            comm.comment = c['examiner_comment']
            comm.timestamp = c['event_timestamp']

            new_nr.comments.append(comm)
Exemple #2
0
def test_add_new_comment_to_nr(client, jwt, app):
    from namex.models import Request as RequestDAO, State, Name as NameDAO, Comment as CommentDAO, User, \
    Event as EventDAO
    from sqlalchemy import desc

    #add a user for the comment
    user = User('test-user', '', '', '43e6a245-0bf7-4ccf-9bd0-e7fb85fd18cc',
                'https://sso-dev.pathfinder.gov.bc.ca/auth/realms/sbc')
    user.save_to_db()

    nr = RequestDAO()
    nr.nrNum = 'NR 0000002'
    nr.stateCd = State.INPROGRESS
    nr.requestId = 1460775
    nr._source = 'NRO'
    name1 = NameDAO()
    name1.choice = 1
    name1.name = 'TEST NAME ONE'
    nr.names = [name1]
    nr.save_to_db()

    comment1 = CommentDAO()
    comment1.comment = 'This is the first Comment'
    comment1.nr_id = nr.id
    comment1.examinerId = nr.userId
    nr.comments = [comment1]
    nr.save_to_db()

    # create JWT & setup header with a Bearer Token using the JWT
    token = jwt.create_jwt(claims, token_header)
    headers = {
        'Authorization': 'Bearer ' + token,
        'content-type': 'application/json'
    }

    # get the resource so we have a template for the request:
    rv = client.get('/api/v1/requests/NR%200000002', headers=headers)
    assert rv.status_code == 200
    # assert we're starting with just one name:
    data = json.loads(rv.data)
    assert len(data['comments']) == 1

    new_comment = {"comment": "The 13th comment entered by the user."}

    rv = client.post('/api/v1/requests/NR%200000002/comments',
                     data=json.dumps(new_comment),
                     headers=headers)

    assert b'"comment": "The 13th comment entered by the user."' in rv.data
    assert 200 == rv.status_code

    event_results = EventDAO.query.filter_by(nrId=nr.id).order_by(
        EventDAO.eventDate.desc()).first_or_404()
    assert event_results.action == 'post'
    assert event_results.eventJson[0:11] == '{"comment":'
Exemple #3
0
def add_comments(nr, comments):
    # TODO: link to examiner IDs somehow - examiner_IDIR
    # TODO: append new comments, rather than flushing out all the old ones first
    for com in nr.comments.all():
        nr.comments.remove(com)

    if comments:
        for c in comments:
            comm = Comment()
            comm.comment = c['examiner_comment']
            comm.timestamp = c['event_timestamp']

            nr.comments.append(comm)
def test_get_staff_comment_event_history(client, jwt, app):
    from namex.models import Comment, Event, State, User
    from namex.services import EventRecorder
    from namex.utils.common import convert_to_ascii

    # add a user for the comment
    user = User('test-user', '', '', '43e6a245-0bf7-4ccf-9bd0-e7fb85fd18cc',
                'https://sso-dev.pathfinder.gov.bc.ca/auth/realms/sbc')
    user.save_to_db()

    headers = create_header(jwt, [User.EDITOR])

    nr = create_base_nr()
    nr.stateCd = State.DRAFT
    nr.save_to_db()
    EventRecorder.record(user, Event.POST + ' [payment completed] CREATE', nr,
                         nr.json())

    comment_instance = Comment()
    comment_instance.examinerId = user.id
    comment_instance.nrId = nr.id
    comment_instance.comment = convert_to_ascii('test staff comment')
    comment_instance.save_to_db()

    EventRecorder.record(user, Event.POST, nr,
                         {'comment': 'test staff comment'})

    # get the resource (this is the test)
    rv = client.get('/api/v1/events/NR%200000002', headers=headers)
    assert rv.status_code == 200

    assert b'"user_action": "Staff Comment"' in rv.data
    assert b'"comment": "test staff comment"' in rv.data
def build_language_comment(english_bol, user_id, nr_id):
    lang_comment = Comment()
    lang_comment.examinerId = user_id
    lang_comment.nrId = nr_id
    if english_bol is True:
        # Add a comment for the examiner that says this is not an english name
        lang_comment.comment = 'The applicant has indicated the submitted name or names are in English.'
    else:
        lang_comment.comment = 'The applicant has indicated the submitted name or names are not English.'
    return lang_comment
Exemple #6
0
def add_language_comment(english_bol, user_id, nr_id):
    lang_comment = Comment()
    lang_comment.examinerId = user_id
    lang_comment.nrId = nr_id
    if english_bol == True:
        # add a coment for the exmainer that say this is nota ENglihs Name
        lang_comment.comment = 'The applicant has indicated the submitted name or names are in English.'
    else:
        lang_comment.comment = 'The applicant has indicated the submitted name or names are not English.'
    return lang_comment
def build_name_comment(user_id, nr_id):
    name_comment = Comment()
    name_comment.examinerId = user_id
    name_comment.nrId = nr_id
    name_comment.comment = 'The submitted name or names is a person name, coined phrase or trademark'
    return name_comment
Exemple #8
0
    def put(nr, *args, **kwargs):

        # do the cheap check first before the more expensive ones
        json_input = request.get_json()
        if not json_input:
            return jsonify(message='No input data provided'), 400
        current_app.logger.debug(json_input)

        nr_num = json_input.get('nrNum', None)
        if nr_num and nr_num != nr:
            return jsonify(
                message='Data contains a different NR# than this resource'
            ), 400

        state = json_input.get('state', None)
        if not state:
            return jsonify({"message": "state not set"}), 406

        if state not in State.VALID_STATES:
            return jsonify({"message": "not a valid state"}), 406

        #check user scopes
        if not (jwt.requires_roles(User.EDITOR)
                or jwt.requires_roles(User.APPROVER)):
            raise AuthError(
                {
                    "code": "Unauthorized",
                    "description": "You don't have access to this resource."
                }, 403)

        if (state in (State.APPROVED,
                     State.REJECTED,
                     State.CONDITIONAL))\
                and not jwt.requires_roles(User.APPROVER):
            return jsonify(message='Only Names Examiners can set state: {}'.
                           format(state)), 428

        try:
            nr_d = RequestDAO.find_by_nr(nr)
            if not nr_d:
                return jsonify(message='NR not found'), 404

            user = User.find_by_jwtToken(g.jwt_oidc_token_info)
            if not user:
                user = User.create_from_jwtToken(g.jwt_oidc_token_info)

            #NR is in a final state, but maybe the user wants to pull it back for corrections
            if nr_d.stateCd in State.COMPLETED_STATE:
                if not jwt.requires_roles(User.APPROVER):
                    return jsonify(
                        message=
                        'Only Names Examiners can alter completed Requests'
                    ), 401

                if nr_d.furnished == RequestDAO.REQUEST_FURNISHED:
                    return jsonify(
                        message=
                        'Request has already been furnished and cannot be altered'
                    ), 409

                if state != State.INPROGRESS:
                    return jsonify(
                        message=
                        'Completed unfurnished Requests can only be set to an INPROGRESS state'
                    ), 400

            elif state in State.RELEASE_STATES:
                if nr_d.userId != user.id or nr_d.stateCd != State.INPROGRESS:
                    return jsonify(
                        message=
                        'The Request must be INPROGRESS and assigned to you before you can change it.'
                    ), 401
            elif nr_d.userId != user.id or nr_d.stateCd != State.INPROGRESS:
                return jsonify(
                    message=
                    'The Request must be INPROGRESS and assigned to you before you can change it.'
                ), 401

            # update request header
            request_header_schema.load(json_input, instance=nr_d, partial=True)
            nr_d.stateCd = state
            nr_d.userId = user.id

            # update applicants
            applicants_d = nr_d.applicants.one_or_none()
            if applicants_d:
                appl = json_input.get('applicants', None)
                if appl:
                    errm = applicant_schema.validate(appl, partial=False)
                    if errm:
                        return jsonify(errm)

                    applicant_schema.load(appl,
                                          instance=applicants_d,
                                          partial=False)
                else:
                    applicants_d.delete_from_db()

            ### NAMES ###
            for nrd_name in nr_d.names.all():
                for in_name in json_input['names']:
                    if nrd_name.choice == in_name['choice']:

                        errors = names_schema.validate(in_name, partial=False)
                        if errors:
                            return jsonify(errors), 400

                        names_schema.load(in_name,
                                          instance=nrd_name,
                                          partial=False)
            ### END names ###

            ### COMMENTS ###

            # we only add new comments, we do not change existing comments
            # - we can find new comments in json as those with no ID

            for in_comment in json_input['comments']:
                is_new_comment = False
                try:
                    if in_comment['id'] is None or in_comment['id'] == 0:
                        is_new_comment = True
                except KeyError:
                    is_new_comment = True

                if is_new_comment and in_comment['comment'] is not None:
                    new_comment = Comment()
                    new_comment.comment = in_comment['comment']
                    new_comment.examiner = user
                    new_comment.nrId = nr_d.id

            ### END comments ###

            ### NWPTA ###

            for nrd_nwpta in nr_d.partnerNS.all():
                for in_nwpta in json_input['nwpta']:
                    if nrd_nwpta.partnerJurisdictionTypeCd == in_nwpta[
                            'partnerJurisdictionTypeCd']:

                        errors = nwpta_schema.validate(in_nwpta, partial=False)
                        if errors:
                            return jsonify(errors), 400

                        nwpta_schema.load(in_nwpta,
                                          instance=nrd_nwpta,
                                          partial=False)
            ### END nwpta ###

            ### Finally save the entire graph
            nr_d.save_to_db()

        except ValidationError as ve:
            return jsonify(ve.messages)

        except NoResultFound as nrf:
            # not an error we need to track in the log
            return jsonify(message='Request:{} not found'.format(nr)), 404

        except Exception as err:
            current_app.logger.error(
                "Error when replacing NR:{0} Err:{1}".format(nr, err))
            return jsonify(message='NR had an internal error'), 500

        current_app.logger.debug(nr_d.json())
        return jsonify(nr_d.json()), 200