-
Notifications
You must be signed in to change notification settings - Fork 44
/
requests.py
1559 lines (1289 loc) · 69.6 KB
/
requests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Requests used to support the namex API
TODO: Fill in a larger description once the API is defined for V1
"""
from http import HTTPStatus
from flask import request, jsonify, g, current_app, get_flashed_messages
from flask_restx import Namespace, Resource, fields, cors
from flask_jwt_oidc import AuthError
from namex.constants import DATE_TIME_FORMAT_SQL
from namex.models.request import RequestsAuthSearchSchema
from namex.utils.logging import setup_logging
from sqlalchemy.orm import load_only, lazyload, eagerload
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy import and_, func, or_, text
from sqlalchemy.inspection import inspect
from namex import jwt, nro, services
from namex.exceptions import BusinessException
from namex.models import db, ValidationError
from namex.models import Request as RequestDAO, RequestsSchema, RequestsHeaderSchema, RequestsSearchSchema
from namex.models import Applicant, Name, NameSchema, PartnerNameSystemSchema
from namex.models import User, State, Comment, NameCommentSchema, Event
from namex.models import ApplicantSchema
from namex.models import DecisionReason
from namex.services.lookup import nr_filing_actions
from namex.services import ServicesError, MessageServices, EventRecorder
from namex.services.name_request.utils import check_ownership, get_or_create_user_by_jwt, valid_state_transition
from namex.utils.common import (convert_to_ascii,
convert_to_utc_min_date_time,
convert_to_utc_max_date_time)
from namex.utils.auth import cors_preflight
from namex.analytics import SolrQueries, RestrictedWords, VALID_ANALYSIS as ANALYTICS_VALID_ANALYSIS
from namex.services.nro import NROServicesError
from namex.resources.name_requests import ReportResource
from namex.resources.flask_threads import FlaskThread
import ldclient
import datetime
setup_logging() # Important to do this first
# Register a local namespace for the requests
api = Namespace('namexRequests', description='Namex - Requests API')
# Marshmallow schemas
request_schema = RequestsSchema(many=False)
request_schemas = RequestsSchema(many=True)
request_header_schema = RequestsHeaderSchema(many=False)
request_search_schemas = RequestsSearchSchema(many=True)
request_auth_search_schemas = RequestsAuthSearchSchema(many=True)
names_schema = NameSchema(many=False)
names_schemas = NameSchema(many=True)
nwpta_schema = PartnerNameSystemSchema(many=False)
name_comment_schema = NameCommentSchema(many=False)
applicant_schema = ApplicantSchema(many=False)
@api.errorhandler(AuthError)
def handle_auth_error(ex):
response = jsonify(ex.error)
response.status_code = ex.status_code
return response
# noinspection PyUnresolvedReferences
@cors_preflight("GET")
@api.route('/echo', methods=['GET', 'OPTIONS'])
class Echo(Resource):
"""Helper method to echo back all your JWT token info
"""
@staticmethod
@cors.crossdomain(origin='*')
@jwt.requires_auth
def get(*args, **kwargs):
try:
return jsonify(g.jwt_oidc_token_info), 200
except Exception as err:
return {"error": "{}".format(err)}, 500
#################### QUEUES #######################
@cors_preflight("GET")
@api.route('/queues/@me/oldest', methods=['GET', 'OPTIONS'])
class RequestsQueue(Resource):
"""Acting like a QUEUE this gets the next NR (just the NR number)
and assigns it to your auth id, and marks it as INPROGRESS
"""
@staticmethod
@cors.crossdomain(origin='*')
@jwt.requires_roles([User.APPROVER])
def get():
""" Gets the oldest nr num, that is in DRAFT status
It then marks the NR as INPROGRESS, and assigns it to the User as found in the JWT
It also moves control of the Request from NRO so that NameX fully owns it
:Authorization: (JWT): valid JWT with the User.APPROVER role
:return: (str) (dict) (https status): 500, 404, or NR NUM, or NR NUM and a system alert in the dict
"""
# GET existing or CREATE new user based on the JWT info
try:
user = get_or_create_user_by_jwt(g.jwt_oidc_token_info)
except ServicesError as se:
return jsonify(message='unable to get ot create user, aborting operation'), 500
except Exception as unmanaged_error:
current_app.logger.error(unmanaged_error.with_traceback(None))
return jsonify(message='internal server error'), 500
# get the next NR assigned to the User
try:
nr, new_assignment = RequestDAO.get_queued_oldest(user)
except BusinessException as be:
return jsonify(message='There are no more requests in the {} Queue'.format(State.DRAFT)), 404
except Exception as unmanaged_error:
current_app.logger.error(unmanaged_error.with_traceback(None))
return jsonify(message='internal server error'), 500
current_app.logger.debug('got the nr:{} and its a new assignment?{}'.format(nr.nrNum, new_assignment))
# if no NR returned
if 'nr' not in locals() or not nr:
return jsonify(message='No more NRs in Queue to process'), 200
# if it's an NR already INPROGRESS and assigned to the user
if nr and not new_assignment:
return jsonify(nameRequest='{}'.format(nr.nrNum)), 200
# if it's a new assignment, then LOGICALLY lock the record in NRO
# if we fail to do that, send back the NR and the errors for user-intervention
if new_assignment:
warnings = nro.move_control_of_request_from_nro(nr, user)
if 'warnings' in locals() and warnings:
return jsonify(nameRequest='{}'.format(nr.nrNum), warnings=warnings), 206
EventRecorder.record(user, Event.GET, nr, {})
return jsonify(nameRequest='{}'.format(nr.nrNum)), 200
@cors_preflight('GET, POST')
@api.route('', methods=['GET', 'POST', 'OPTIONS'])
class Requests(Resource):
a_request = api.model('Request', {'submitter': fields.String('The submitter name'),
'corpType': fields.String('The corporation type'),
'reqType': fields.String('The name request type')
})
START = 0
ROWS = 10
@staticmethod
@cors.crossdomain(origin='*')
@jwt.requires_auth
def get(*args, **kwargs):
# validate row & start params
start = request.args.get('start', Requests.START)
rows = request.args.get('rows', Requests.ROWS)
try:
start = int(start)
rows = int(rows)
except Exception as err:
current_app.logger.info('start or rows not an int, err: {}'.format(err))
return jsonify({'message': 'paging parameters were not integers'}), 406
# queue must be a list of states
queue = request.args.get('queue', None)
if queue:
if queue == 'COMPLETED':
queue = 'COMPLETED'
queue = queue.upper().split(',')
for q in queue:
if q not in State.VALID_STATES:
return jsonify({'message': '\'{}\' is not a valid queue'.format(queue)}), 406
# order must be a string of 'column:asc,column:desc'
order = request.args.get('order', 'submittedDate:desc,stateCd:desc')
# order=dict((x.split(":")) for x in order.split(',')) // con't pass as a dict as the order is lost
# create the order by txt, looping through Request Attributes and mapping to column names
# TODO: this is fragile across joins, fix it up if queries are going to sort across joins
cols = inspect(RequestDAO).columns
col_keys = cols.keys()
sort_by = ''
order_list = ''
for k, v in ((x.split(":")) for x in order.split(',')):
vl = v.lower()
if (k in col_keys) and (vl == 'asc' or vl == 'desc'):
if len(sort_by) > 0:
sort_by = sort_by + ', '
order_list = order_list + ', '
sort_by = sort_by + '{columns} {direction} NULLS LAST'.format(columns=cols[k], direction=vl)
order_list = order_list + '{attribute} {direction} NULLS LAST'.format(attribute=k, direction=vl)
# Assemble the query
nrNum = request.args.get('nrNum', None)
activeUser = request.args.get('activeUser', None)
compName = request.args.get('compName', None)
firstName = request.args.get('firstName', None)
lastName = request.args.get('lastName', None)
consentOption = request.args.get('consentOption', None)
priority = request.args.get('ranking', None)
notification = request.args.get('notification', None)
submittedInterval = request.args.get('submittedInterval', None)
lastUpdateInterval = request.args.get('lastUpdateInterval', None)
current_hour = int(request.args.get('hour', 0))
submittedStartDate = request.args.get('submittedStartDate', None)
submittedEndDate = request.args.get('submittedEndDate', None)
q = RequestDAO.query.filter()
if queue:
q = q.filter(RequestDAO.stateCd.in_(queue))
q = q.filter(RequestDAO.nrNum.notlike('NR L%'))
if nrNum:
# set any variation of mixed case 'nr' to 'NR'
nrNum = nrNum.upper().strip()
# remove spaces within string
nrNum = nrNum.replace(' ', '')
# add single space between NR and number
nrNum = nrNum.replace('NR', 'NR ')
nrNum = '%' + nrNum + '%'
q = q.filter(RequestDAO.nrNum.like(nrNum))
if activeUser:
q = q.join(RequestDAO.activeUser).filter(User.username.ilike('%' + activeUser + '%'))
if compName:
compName = compName.strip().replace(' ', '%')
## nameSearch column is populated like: '|1<name 1>|2<name 2>|3<name 3>
# to ensure we don't get a match that spans over a single name
compName1 = '%|1%' + compName + '%1|%'
compName2 = '%|2%' + compName + '%2|%'
compName3 = '%|3%' + compName + '%3|%'
q = q.filter(or_(
RequestDAO.nameSearch.ilike(compName1),
RequestDAO.nameSearch.ilike(compName2),
RequestDAO.nameSearch.ilike(compName3)
))
if firstName:
firstName = firstName.strip().replace(' ', '%')
q = q.join(RequestDAO.applicants).filter(Applicant.firstName.ilike('%' + firstName + '%'))
if lastName:
lastName = lastName.strip().replace(' ', '%')
q = q.join(RequestDAO.applicants).filter(Applicant.lastName.ilike('%' + lastName + '%'))
if consentOption == 'Received':
q = q.filter(or_(RequestDAO.consentFlag == 'R', RequestDAO.consent_dt.isnot(None)))
if consentOption == 'Yes':
q = q.filter(RequestDAO.consentFlag == 'Y')
elif consentOption == 'No':
q = q.filter(and_(RequestDAO.consentFlag == 'N', RequestDAO.consent_dt == None))
if priority == 'Standard':
q = q.filter(RequestDAO.priorityCd != 'Y')
elif priority == 'Priority':
q = q.filter(RequestDAO.priorityCd != 'N')
if notification == 'Notified':
q = q.filter(RequestDAO.furnished != 'N')
elif notification == 'Not Notified':
q = q.filter(RequestDAO.furnished != 'Y')
if submittedInterval == 'Today':
q = q.filter(RequestDAO.submittedDate > text(
'NOW() - INTERVAL \'{hour_offset} HOURS\''.format(hour_offset=current_hour)))
elif submittedInterval == '7 days':
q = q.filter(RequestDAO.submittedDate > text(
'NOW() - INTERVAL \'{hour_offset} HOURS\''.format(hour_offset=current_hour + 24 * 6)))
elif submittedInterval == '30 days':
q = q.filter(RequestDAO.submittedDate > text(
'NOW() - INTERVAL \'{hour_offset} HOURS\''.format(hour_offset=current_hour + 24 * 29)))
elif submittedInterval == '90 days':
q = q.filter(RequestDAO.submittedDate > text(
'NOW() - INTERVAL \'{hour_offset} HOURS\''.format(hour_offset=current_hour + 24 * 89)))
elif submittedInterval == '1 year':
q = q.filter(RequestDAO.submittedDate > text('NOW() - INTERVAL \'1 YEARS\''))
elif submittedInterval == '3 years':
q = q.filter(RequestDAO.submittedDate > text('NOW() - INTERVAL \'3 YEARS\''))
elif submittedInterval == '5 years':
q = q.filter(RequestDAO.submittedDate > text('NOW() - INTERVAL \'5 YEARS\''))
if lastUpdateInterval == 'Today':
q = q.filter(RequestDAO.lastUpdate > text(
'(now() at time zone \'utc\') - INTERVAL \'{hour_offset} HOURS\''.format(hour_offset=current_hour)))
if lastUpdateInterval == 'Yesterday':
today_offset = current_hour
yesterday_offset = today_offset + 24
q = q.filter(RequestDAO.lastUpdate < text(
'(now() at time zone \'utc\') - INTERVAL \'{today_offset} HOURS\''.format(today_offset=today_offset)))
q = q.filter(RequestDAO.lastUpdate > text(
'(now() at time zone \'utc\') - INTERVAL \'{yesterday_offset} HOURS\''.format(yesterday_offset=yesterday_offset)))
elif lastUpdateInterval == '2 days':
q = q.filter(RequestDAO.lastUpdate > text(
'(now() at time zone \'utc\') - INTERVAL \'{hour_offset} HOURS\''.format(hour_offset=current_hour + 24)))
elif lastUpdateInterval == '7 days':
q = q.filter(RequestDAO.lastUpdate > text(
'(now() at time zone \'utc\') - INTERVAL \'{hour_offset} HOURS\''.format(hour_offset=current_hour + 24 * 6)))
elif lastUpdateInterval == '30 days':
q = q.filter(RequestDAO.lastUpdate > text(
'(now() at time zone \'utc\') - INTERVAL \'{hour_offset} HOURS\''.format(hour_offset=current_hour + 24 * 29)))
if submittedInterval and (submittedStartDate or submittedEndDate):
return jsonify({"message": "submittedInterval cannot be used in conjuction with submittedStartDate and submittedEndDate"}), 400
submittedStartDateTimeUtcObj = None
submittedEndDateTimeUtcObj = None
if submittedStartDate:
try:
submittedStartDateTimeUtcObj = convert_to_utc_min_date_time(submittedStartDate)
# convert date to format db expects
submittedStartDateTimeUtc = submittedStartDateTimeUtcObj.strftime(DATE_TIME_FORMAT_SQL)
q = q.filter(RequestDAO.submittedDate >=
text('\'{submittedStartDateTimeUtc}\''
.format(submittedStartDateTimeUtc=submittedStartDateTimeUtc)))
except ValueError as ve:
return jsonify({"message": "Invalid submittedStartDate: {}. Must be of date format %Y-%m-%d"
.format(submittedStartDate)}), 400
if submittedEndDate:
try:
submittedEndDateTimeUtcObj = convert_to_utc_max_date_time(submittedEndDate)
# convert date to format db expects
submittedEndDateTimeUtc = submittedEndDateTimeUtcObj.strftime(DATE_TIME_FORMAT_SQL)
q = q.filter(RequestDAO.submittedDate <=
text('\'{submittedEndDateTimeUtc}\''
.format(submittedEndDateTimeUtc=submittedEndDateTimeUtc)))
except ValueError as ve:
return jsonify({"message": "Invalid submittedEndDate: {}. Must be of date format %Y-%m-%d"
.format(submittedEndDate)}), 400
if (submittedStartDateTimeUtcObj and submittedEndDateTimeUtcObj)\
and submittedEndDateTimeUtcObj < submittedStartDateTimeUtcObj:
return jsonify({"message": "submittedEndDate must be after submittedStartDate"}), 400
q = q.order_by(text(sort_by))
# get a count of the full set size, this ignore the offset & limit settings
count_q = q.statement.with_only_columns([func.count()]).order_by(None)
count = db.session.execute(count_q).scalar()
# Add the paging
q = q.offset(start)
q = q.limit(rows)
# create the response
rep = {'response': {'start': start,
'rows': rows,
'numFound': count,
'numPriorities': 0,
'numUpdatedToday': 0,
'queue': queue,
'order': order_list
},
'nameRequests': [request_search_schemas.dump(q.all()), {}]
}
return jsonify(rep), 200
# @api.errorhandler(AuthError)
# def handle_auth_error(ex):
# response = jsonify(ex.error)
# response.status_code = ex.status_code
# return response, 401
# return {}, 401
# noinspection PyUnusedLocal,PyUnusedLocal
@api.expect(a_request)
@cors.crossdomain(origin='*')
@jwt.requires_auth
def post(self, *args, **kwargs):
current_app.logger.info('Someone is trying to post a new request')
return jsonify({'message': 'Not Implemented'}), 501
# For sbc-auth - My Business Registry page.
@cors_preflight("POST")
@api.route('/search', methods=['POST'])
class RequestSearch(Resource):
@staticmethod
@cors.crossdomain(origin='*')
@jwt.has_one_of_roles([User.SYSTEM])
def post():
search = request.get_json()
identifiers = search.get('identifiers', [])
# Only names and applicants are needed for this query, we want this query to be lighting fast
# to prevent putting a load on namex-api.
q = RequestDAO.query.filter(RequestDAO.nrNum.in_(identifiers)) \
.options(
lazyload('*'),
eagerload(RequestDAO.names).load_only(Name.state, Name.name),
eagerload(RequestDAO.applicants).load_only(
Applicant.emailAddress, Applicant.phoneNumber
),
load_only(
RequestDAO.id,
RequestDAO.nrNum,
RequestDAO.stateCd,
RequestDAO.requestTypeCd,
RequestDAO.natureBusinessInfo,
RequestDAO._entity_type_cd,
RequestDAO.expirationDate,
RequestDAO.consentFlag
))
requests = request_auth_search_schemas.dump(q.all())
actions_array = [nr_filing_actions.get_actions(r['requestTypeCd'], r['entity_type_cd']) for r in requests]
for r, additional_fields in zip(requests, actions_array):
if additional_fields:
r.update(additional_fields)
return jsonify(requests)
# noinspection PyUnresolvedReferences
@cors_preflight("GET, PATCH, PUT, DELETE")
@api.route('/<string:nr>', methods=['GET', 'PATCH', 'PUT', 'DELETE', 'OPTIONS'])
class Request(Resource):
@staticmethod
@cors.crossdomain(origin='*')
@jwt.has_one_of_roles([User.APPROVER, User.EDITOR, User.VIEWONLY])
def get(nr):
# return jsonify(request_schema.dump(RequestDAO.query.filter_by(nr=nr.upper()).first_or_404()))
return jsonify(RequestDAO.query.filter_by(nrNum=nr.upper()).first_or_404().json())
@staticmethod
# @cors.crossdomain(origin='*')
@jwt.requires_roles([User.APPROVER, User.EDITOR])
def delete(nr):
return '', 501 # not implemented
# nrd = RequestDAO.find_by_nr(nr)
# even if not found we still return a 204, which is expected spec behaviour
# if nrd:
# nrd.stateCd = State.CANCELLED
# nrd.save_to_db()
#
# return '', 204
@staticmethod
@cors.crossdomain(origin='*')
@jwt.has_one_of_roles([User.APPROVER, User.EDITOR, User.SYSTEM])
def patch(nr, *args, **kwargs):
""" Patches the NR. Currently only handles STATE (with optional comment) and Previous State.
:param nr (str): NameRequest Number in the format of 'NR 000000000'
:param args: __futures__
:param kwargs: __futures__
:return: 200 - success; 40X for errors
:HEADER: Valid JWT Bearer Token for a valid REALM
:JWT Scopes: - USER.APPROVER, USER.EDITOR
APPROVERS: Can change from almost any state, other than CANCELLED, EXPIRED and ( COMPLETED not yet furnished )
EDITOR: Can't change to a COMPLETED state (COMPLETED)
"""
# do the cheap check first before the more expensive ones
# check states
json_input = request.get_json()
if not json_input:
return jsonify({'message': 'No input data provided'}), 400
# find NR
try:
user = get_or_create_user_by_jwt(g.jwt_oidc_token_info)
nrd = RequestDAO.find_by_nr(nr)
if not nrd:
return jsonify({"message": "Request:{} not found".format(nr)}), 404
start_state = nrd.stateCd
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 patching NR:{0} Err:{1}".format(nr, err))
return jsonify({"message": "NR had an internal error"}), 404
try:
### STATE ###
# all these checks to get removed to marshmallow
state = json_input.get('state', None)
if state:
if state not in State.VALID_STATES:
return jsonify({"message": "not a valid state"}), 406
if not nrd:
return jsonify({"message": "Request:{} not found".format(nr)}), 404
if not valid_state_transition(user, nrd, state):
return jsonify(message='you are not authorized to make these changes'), 401
# if the user has an existing (different) INPROGRESS NR, revert to previous state (default to HOLD)
existing_nr = RequestDAO.get_inprogress(user)
if existing_nr:
if existing_nr.previousStateCd:
existing_nr.stateCd = existing_nr.previousStateCd
existing_nr.previousStateCd = None
else:
existing_nr.stateCd = State.HOLD
existing_nr.save_to_db()
# if the NR is in DRAFT then LOGICALLY lock the record in NRO
# if we fail to do that, send back the NR and the errors for user-intervention
if nrd.stateCd == State.DRAFT:
warnings = nro.move_control_of_request_from_nro(nrd, user)
# if we're changing to DRAFT, update NRO status to "D" in NRO
if state == State.DRAFT:
change_flags = {
'is_changed__request': False,
'is_changed__previous_request': False,
'is_changed__applicant': False,
'is_changed__address': False,
'is_changed__name1': False,
'is_changed__name2': False,
'is_changed__name3': False,
'is_changed__nwpta_ab': False,
'is_changed__nwpta_sk': False,
'is_changed__request_state': True,
'is_changed_consent': False
}
warnings = nro.change_nr(nrd, change_flags)
if warnings:
MessageServices.add_message(MessageServices.ERROR,
'change_request_in_NRO', warnings)
nrd.stateCd = state
nrd.userId = user.id
if state == State.CANCELLED:
nro.cancel_nr(nrd, user.username)
# if our state wasn't INPROGRESS and it is now, ensure the furnished flag is N
if (start_state in locals()
and start_state != State.INPROGRESS
and nrd.stateCd == State.INPROGRESS):
# set / reset the furnished flag to N
nrd.furnished = 'N'
# if we're changing to a completed or cancelled state, clear reset flag on NR record
if state in State.COMPLETED_STATE + [State.CANCELLED]:
nrd.hasBeenReset = False
if nrd.stateCd == State.CONDITIONAL and nrd.consentFlag is None:
nrd.consentFlag = 'Y'
### COMMENTS ###
# we only add new comments, we do not change existing comments
# - we can find new comments in json as those with no ID
if json_input.get('comments', None):
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 = convert_to_ascii(in_comment['comment'])
new_comment.examiner = user
new_comment.nrId = nrd.id
### END comments ###
elif consume := json_input.get('consume', None):
corp_num = consume.get('corpNum', None)
nro.consume_nr(nrd, user, corp_num)
### PREVIOUS STATE ###
# - None (null) is a valid value for Previous State
if 'previousStateCd' in json_input.keys():
nrd.previousStateCd = json_input.get('previousStateCd', None)
# save record
nrd.save_to_db()
EventRecorder.record(user, Event.PATCH, nrd, json_input)
except Exception as err:
current_app.logger.debug(err.with_traceback(None))
return jsonify(message='Internal server error'), 500
if 'warnings' in locals() and warnings:
return jsonify(message='Request:{} - patched'.format(nr), warnings=warnings), 206
if state in [State.APPROVED, State.CONDITIONAL, State.REJECTED]:
ldc = ldclient.get()
emailer_enable = ldc.variation('emailer-enable', {'key': 'anonymous'}, False)
if emailer_enable:
thread = FlaskThread(target=Request._email_report, args=(nrd.id, ))
thread.daemon = True
thread.start()
return jsonify(message='Request:{} - patched'.format(nr)), 200
def _email_report(nr_id):
report = ReportResource()
report.email_report(nr_id)
def _email_consent(nr_id):
report = ReportResource()
report.email_consent_letter(nr_id)
@staticmethod
@cors.crossdomain(origin='*')
@jwt.has_one_of_roles([User.APPROVER, User.EDITOR])
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
try:
user = get_or_create_user_by_jwt(g.jwt_oidc_token_info)
nrd = RequestDAO.find_by_nr(nr)
if not nrd:
return jsonify({"message": "Request:{} not found".format(nr)}), 404
orig_nrd = nrd.json()
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 patching NR:{0} Err:{1}".format(nr, err))
return jsonify({"message": "NR had an internal error"}), 404
if not valid_state_transition(user, nrd, state):
return jsonify(message='you are not authorized to make these changes'), 401
name_choice_exists = {1: False, 2: False, 3: False}
for name in json_input.get('names', None):
if name['name'] and name['name'] != '':
name_choice_exists[name['choice']] = True
if not name_choice_exists[1]:
return jsonify(message='Data does not include a name choice 1'), 400
if not name_choice_exists[2] and name_choice_exists[3]:
return jsonify(message='Data contains a name choice 3 without a name choice 2'), 400
try:
existing_nr = RequestDAO.get_inprogress(user)
if existing_nr:
existing_nr.stateCd = State.HOLD
existing_nr.save_to_db()
# convert Expiration Date to correct format
if json_input.get('expirationDate', None):
json_input['expirationDate'] = str(datetime.datetime.strptime(
str(json_input['expirationDate'][5:]), '%d %b %Y %H:%M:%S %Z'))
# convert Submitted Date to correct format
if json_input.get('submittedDate', None):
json_input['submittedDate'] = str(datetime.datetime.strptime(
str(json_input['submittedDate'][5:]), '%d %b %Y %H:%M:%S %Z'))
if json_input.get('consent_dt', None):
json_input['consent_dt'] = str(datetime.datetime.strptime(
str(json_input['consent_dt'][5:]), '%d %b %Y %H:%M:%S %Z'))
# convert NWPTA dates to correct format
if json_input.get('nwpta', None):
for region in json_input['nwpta']:
try:
if region['partnerNameDate'] == '':
region['partnerNameDate'] = None
if region['partnerNameDate']:
region['partnerNameDate'] = str(datetime.datetime.strptime(
str(region['partnerNameDate']), '%d-%m-%Y'))
except ValueError:
pass
# pass on this error and catch it when trying to add to record, to be returned
# ## If the current state is DRAFT, the transfer control from NRO to NAMEX
# if the NR is in DRAFT then LOGICALLY lock the record in NRO
# if we fail to do that, send back the NR and the errors for user-intervention
if nrd.stateCd == State.DRAFT:
warnings = nro.move_control_of_request_from_nro(nrd, user)
if warnings:
MessageServices.add_message(MessageServices.WARN, 'nro_lock', warnings)
### REQUEST HEADER ###
# update request header
# if reset is set to true then this nr will be set to H + name_examination proc will be called in oracle
reset = False
if nrd.furnished == RequestDAO.REQUEST_FURNISHED and json_input.get('furnished', None) == 'N':
reset = True
nrd.additionalInfo = convert_to_ascii(json_input.get('additionalInfo', None))
nrd.consentFlag = json_input.get('consentFlag', None)
nrd.consent_dt = json_input.get('consent_dt', None)
nrd.corpNum = json_input.get('corpNum', None)
nrd.checkedOutBy = json_input.get('checkedOutBy', None)
nrd.checkedOutDt = json_input.get('checkedOutDt', None)
nrd.entity_type_cd = json_input.get('entity_type_cd', None)
nrd.expirationDate = json_input.get('expirationDate', None)
nrd.furnished = json_input.get('furnished', 'N')
nrd.hasBeenReset = json_input.get('hasBeenReset', None)
nrd.homeJurisNum = json_input.get('homeJurisNum', None)
nrd.natureBusinessInfo = convert_to_ascii(json_input.get('natureBusinessInfo', None))
nrd.previousNr = json_input.get('previousNr', None)
nrd.previousRequestId = json_input.get('previousRequestId', None)
nrd.priorityCd = json_input.get('priorityCd', None)
nrd.priorityDate = json_input.get('priorityDate', None)
nrd.requestTypeCd = json_input.get('requestTypeCd', None)
nrd.request_action_cd = json_input.get('request_action_cd', None)
nrd.stateCd = state
nrd.tradeMark = json_input.get('tradeMark', None)
nrd.userId = user.id
nrd.xproJurisdiction = json_input.get('xproJurisdiction', None)
if reset:
# set the flag indicating that the NR has been reset
nrd.hasBeenReset = True
# add a generated comment re. this NR being reset
json_input['comments'].append({'comment': 'This NR was RESET.'})
try:
previousNr = json_input['previousNr']
nrd.previousRequestId = RequestDAO.find_by_nr(previousNr).requestId
except AttributeError:
nrd.previousRequestId = None
except KeyError:
nrd.previousRequestId = None
# if we're changing to a completed or cancelled state, clear reset flag on NR record
if state in State.COMPLETED_STATE + [State.CANCELLED]:
nrd.hasBeenReset = False
# check if any of the Oracle db fields have changed, so we can send them back
is_changed__request = False
is_changed__previous_request = False
is_changed__request_state = False
is_changed_consent = False
if nrd.requestTypeCd != orig_nrd['requestTypeCd']:
is_changed__request = True
if nrd.expirationDate != orig_nrd['expirationDate']:
is_changed__request = True
if nrd.xproJurisdiction != orig_nrd['xproJurisdiction']:
is_changed__request = True
if nrd.additionalInfo != orig_nrd['additionalInfo']:
is_changed__request = True
if nrd.natureBusinessInfo != orig_nrd['natureBusinessInfo']:
is_changed__request = True
if nrd.previousRequestId != orig_nrd['previousRequestId']:
is_changed__previous_request = True
if nrd.stateCd != orig_nrd['state']:
is_changed__request_state = True
if nrd.consentFlag != orig_nrd['consentFlag']:
is_changed_consent = True
emailer_enable = ldclient.get().variation('emailer-enable', {'key': 'anonymous'}, False)
if emailer_enable:
thread = FlaskThread(target=Request._email_consent, args=(nrd.id, ))
thread.daemon = True
thread.start()
# Need this for a re-open
if nrd.stateCd != State.CONDITIONAL and is_changed__request_state:
nrd.consentFlag = None
nrd.consent_dt = None
### END request header ###
### APPLICANTS ###
is_changed__applicant = False
is_changed__address = False
if nrd.applicants:
applicants_d = nrd.applicants[0]
orig_applicant = applicants_d.as_dict()
appl = json_input.get('applicants', None)
if appl:
errm = applicant_schema.validate(appl, partial=True)
if errm:
# return jsonify(errm), 400
MessageServices.add_message(MessageServices.ERROR, 'applicants_validation', errm)
# convert data to ascii, removing data that won't save to Oracle
applicants_d.lastName = convert_to_ascii(appl.get('lastName', None))
applicants_d.firstName = convert_to_ascii(appl.get('firstName', None))
applicants_d.middleName = convert_to_ascii(appl.get('middleName', None))
applicants_d.phoneNumber = convert_to_ascii(appl.get('phoneNumber', None))
applicants_d.faxNumber = convert_to_ascii(appl.get('faxNumber', None))
applicants_d.emailAddress = convert_to_ascii(appl.get('emailAddress', None))
applicants_d.contact = convert_to_ascii(appl.get('contact', None))
applicants_d.clientFirstName = convert_to_ascii(appl.get('clientFirstName', None))
applicants_d.clientLastName = convert_to_ascii(appl.get('clientLastName', None))
applicants_d.addrLine1 = convert_to_ascii(appl.get('addrLine1', None))
applicants_d.addrLine2 = convert_to_ascii(appl.get('addrLine2', None))
applicants_d.addrLine3 = convert_to_ascii(appl.get('addrLine3', None))
applicants_d.city = convert_to_ascii(appl.get('city', None))
applicants_d.postalCd = convert_to_ascii(appl.get('postalCd', None))
applicants_d.stateProvinceCd = convert_to_ascii(appl.get('stateProvinceCd', None))
applicants_d.countryTypeCd = convert_to_ascii(appl.get('countryTypeCd', None))
# check if any of the Oracle db fields have changed, so we can send them back
if applicants_d.lastName != orig_applicant['lastName']:
is_changed__applicant = True
if applicants_d.firstName != orig_applicant['firstName']:
is_changed__applicant = True
if applicants_d.middleName != orig_applicant['middleName']:
is_changed__applicant = True
if applicants_d.phoneNumber != orig_applicant['phoneNumber']:
is_changed__applicant = True
if applicants_d.faxNumber != orig_applicant['faxNumber']:
is_changed__applicant = True
if applicants_d.emailAddress != orig_applicant['emailAddress']:
is_changed__applicant = True
if applicants_d.contact != orig_applicant['contact']:
is_changed__applicant = True
if applicants_d.clientFirstName != orig_applicant['clientFirstName']:
is_changed__applicant = True
if applicants_d.clientLastName != orig_applicant['clientLastName']:
is_changed__applicant = True
if applicants_d.declineNotificationInd != orig_applicant['declineNotificationInd']:
is_changed__applicant = True
if applicants_d.addrLine1 != orig_applicant['addrLine1']:
is_changed__address = True
if applicants_d.addrLine2 != orig_applicant['addrLine2']:
is_changed__address = True
if applicants_d.addrLine3 != orig_applicant['addrLine3']:
is_changed__address = True
if applicants_d.city != orig_applicant['city']:
is_changed__address = True
if applicants_d.postalCd != orig_applicant['postalCd']:
is_changed__address = True
if applicants_d.stateProvinceCd != orig_applicant['stateProvinceCd']:
is_changed__address = True
if applicants_d.countryTypeCd != orig_applicant['countryTypeCd']:
is_changed__address = True
else:
applicants_d.delete_from_db()
is_changed__applicant = True
is_changed__address = True
### END applicants ###
### NAMES ###
# TODO: set consumptionDate not working -- breaks changing name values
is_changed__name1 = False
is_changed__name2 = False
is_changed__name3 = False
deleted_names = [False] * 3
if len(nrd.names) == 0:
new_name_choice = Name()
new_name_choice.nrId = nrd.id
# convert data to ascii, removing data that won't save to Oracle
new_name_choice.name = convert_to_ascii(new_name_choice.name)
nrd.names.append(new_name_choice)
for nrd_name in nrd.names:
orig_name = nrd_name.as_dict()
for in_name in json_input.get('names', []):
if len(nrd.names) < in_name['choice']:
errors = names_schema.validate(in_name, partial=False)
if errors:
MessageServices.add_message(MessageServices.ERROR, 'names_validation', errors)
# return jsonify(errors), 400
# don't save if the name is blank
if in_name.get('name') and in_name.get('name') != '':
new_name_choice = Name()
new_name_choice.nrId = nrd.id
new_name_choice.choice = in_name.get('choice')
new_name_choice.conflict1 = in_name.get('conflict1')
new_name_choice.conflict2 = in_name.get('conflict2')
new_name_choice.conflict3 = in_name.get('conflict3')
new_name_choice.conflict1_num = in_name.get('conflict1_num')
new_name_choice.conflict2_num = in_name.get('conflict2_num')
new_name_choice.conflict3_num = in_name.get('conflict3_num')
new_name_choice.consumptionDate = in_name.get('consumptionDate')
new_name_choice.corpNum = in_name.get('corpNum')
new_name_choice.decision_text = in_name.get('decision_text')
new_name_choice.designation = in_name.get('designation')
new_name_choice.name_type_cd = in_name.get('name_type_cd')
new_name_choice.name = in_name.get('name')
new_name_choice.state = in_name.get('state')
new_name_choice.name = convert_to_ascii(new_name_choice.name.upper())
nrd.names.append(new_name_choice)
if new_name_choice.choice == 2:
is_changed__name2 = True
if new_name_choice.choice == 3:
is_changed__name3 = True
elif nrd_name.choice == in_name['choice']:
errors = names_schema.validate(in_name, partial=False)
if errors:
MessageServices.add_message(MessageServices.ERROR, 'names_validation', errors)
# return jsonify(errors), 400
nrd_name.choice = in_name.get('choice')
nrd_name.conflict1 = in_name.get('conflict1')
nrd_name.conflict2 = in_name.get('conflict2')
nrd_name.conflict3 = in_name.get('conflict3')
nrd_name.conflict1_num = in_name.get('conflict1_num')
nrd_name.conflict2_num = in_name.get('conflict2_num')
nrd_name.conflict3_num = in_name.get('conflict3_num')
nrd_name.consumptionDate = in_name.get('consumptionDate')
nrd_name.corpNum = in_name.get('corpNum')
nrd_name.decision_text = in_name.get('decision_text')
nrd_name.designation = in_name.get('designation')
nrd_name.name_type_cd = in_name.get('name_type_cd')
nrd_name.name = in_name.get('name')
nrd_name.state = in_name.get('state')
nrd_name.name = convert_to_ascii(nrd_name.name.upper())
# set comments (existing or cleared)
if in_name.get('comment', None) is not None:
# if there is a comment ID in data, just set it
if in_name['comment'].get('id', None) is not None:
nrd_name.commentId = in_name['comment'].get('id')
# if no comment id, it's a new comment, so add it
else:
# no business case for this at this point - this code will never run
pass
else:
nrd_name.comment = None
# convert data to ascii, removing data that won't save to Oracle
# - also force uppercase
nrd_name.name = convert_to_ascii(nrd_name.name)
if (nrd_name.name is not None):
nrd_name.name = nrd_name.name.upper()
# check if any of the Oracle db fields have changed, so we can send them back
# - this is only for editing a name from the Edit NR section, NOT making a decision
if nrd_name.name != orig_name['name']:
if nrd_name.choice == 1:
is_changed__name1 = True
json_input['comments'].append({'comment': 'Name choice 1 changed from {0} to {1}'
.format(orig_name['name'], nrd_name.name)})
if nrd_name.choice == 2:
is_changed__name2 = True
if not nrd_name.name:
deleted_names[nrd_name.choice - 1] = True
json_input['comments'].append({'comment': 'Name choice 2 changed from {0} to {1}'
.format(orig_name['name'], nrd_name.name)})
if nrd_name.choice == 3:
is_changed__name3 = True
if not nrd_name.name:
deleted_names[nrd_name.choice - 1] = True
json_input['comments'].append({'comment': 'Name choice 3 changed from {0} to {1}'
.format(orig_name['name'], nrd_name.name)})
### 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
# - This must come after names section above, to handle comments re. changed names.
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: