-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
837 lines (647 loc) · 30.5 KB
/
common.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
import json
import uuid
from urllib.parse import urljoin
import numpy
import pandas as pd
import re
import requests
from config import config
def get_employee_types(tenantid, auth_token):
headers = {'Content-Type': 'application/json'}
post_data = {"RequestInfo": {"authToken": auth_token}}
post_response = requests.post(url=config.HOST + '/hr-masters-v2/employeetypes/_search?tenantId=' + tenantid,
headers=headers,
json=post_data)
return post_response.json()['EmployeeType']
def get_employee_status(tenantid, auth_token):
headers = {'Content-Type': 'application/json'}
post_data = {"RequestInfo": {"authToken": auth_token}}
post_response = requests.post(url=config.HOST + '/hr-masters-v2/hrstatuses/_search?tenantId=' + tenantid,
headers=headers,
json=post_data)
return post_response.json()['HRStatus']
def open_excel_file(path):
xl_file = pd.ExcelFile(path)
dfs = {sheet_name: xl_file.parse(sheet_name)
for sheet_name in xl_file.sheet_names}
return dfs
def clean_name(name):
return re.sub(r"[^a-z0-9]+", "", name.lower())
def get_sheet(dfs, sheet_name):
if sheet_name in dfs:
return dfs[sheet_name]
else:
new_sheet_name = clean_name(sheet_name)
for key in dfs.keys():
key_name = clean_name(key)
if key_name == new_sheet_name:
return dfs[key]
# we have not found any sheet matching full name, lets check starts with
for key in dfs.keys():
key_name = clean_name(key)
if key_name.startswith(new_sheet_name):
return dfs[key]
def get_column_index(df, column_name):
new_column_name = clean_name(column_name)
for i, name in enumerate(df.columns.values.tolist()):
new_name = clean_name(name)
if new_name.startswith(new_column_name):
return i
def superuser_login():
return login_egov(config.SUPERUSER.username, config.SUPERUSER.password, config.SUPERUSER.tenant_id, "EMPLOYEE")
def login_egov(username, password, tenant_id, user_type="EMPLOYEE"):
resp = requests.post(config.URL_LOGIN, data={
"username": username,
"password": password,
"grant_type": "password",
"scope": "read",
"tenantId": tenant_id,
"userType": user_type
}, headers={
"Authorization": "Basic ZWdvdi11c2VyLWNsaWVudDplZ292LXVzZXItc2VjcmV0"
})
assert resp.status_code == 200, "Login should respond with 200: " + json.dumps(resp.json(), indent=2)
return resp.json()
def open_google_spreadsheet(link_or_id_or_path: str, sheet_name: str = None):
import os
if os.path.isfile(link_or_id_or_path):
return open_excel_file(link_or_id_or_path)
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name(config.GOOGLE_AUTH_CONFIG, scope)
gc = gspread.authorize(credentials)
if link_or_id_or_path.startswith("http"):
wks = gc.open_by_url(link_or_id_or_path)
else:
wks = gc.open_by_key(link_or_id_or_path)
dfs = {}
for wk in wks.worksheets():
if sheet_name is None:
dfs[wk.title] = pd.DataFrame(wk.get_all_records())
elif clean_name(sheet_name) == clean_name(wk.title):
dfs[wk.title] = pd.DataFrame(wk.get_all_records())
return dfs, wks
def get_slab_template():
return {
"RequestInfo": {
"authToken": ""
},
"BillingSlab": [
{
"tenantId": "",
"propertyType": "",
"propertySubType": "",
"usageCategoryMajor": "",
"usageCategoryMinor": "",
"usageCategorySubMinor": "",
"usageCategoryDetail": "",
"ownerShipCategory": "",
"subOwnerShipCategory": "",
"areaType": "",
"fromPlotSize": -100,
"toPlotSize": -100,
"occupancyType": "",
"fromFloor": -100,
"toFloor": -100,
"unitRate": -100,
"isPropertyMultiFloored": False,
"unBuiltUnitRate": -100,
"arvPercent": -100
}
]
}
def validate_boundary_data(auth_token, boundary_data, boundary_type, duplicate_check=True,
used_boundary_codes_check=True):
tenant_boundary = {}
errors = []
for tenantboundary in boundary_data["TenantBoundary"]:
if tenantboundary["hierarchyType"]["code"] == boundary_type:
tenant_boundary = tenantboundary["boundary"]
break
if not tenant_boundary:
return errors
locality_code_map = {}
locality_name_map = {}
for zone in tenant_boundary["children"]:
for wardOrBlock in zone["children"]:
for locality in wardOrBlock["children"]:
locality_code_map[locality["code"]] = locality_code_map.get(locality["code"], 0) + 1
locality_name_map[locality["name"]] = locality_name_map.get(locality["name"], []) + [
locality["code"]] + [zone["name"]] + [wardOrBlock["name"]]
if used_boundary_codes_check:
localities_in_use = get_used_localities(auth_token, boundary_data, boundary_type)
missing_boundary_codes = list(set(localities_in_use) - set(locality_code_map.keys()))
for locality in missing_boundary_codes:
errors.append(
"\nBoundary code \"{}\" is used by existing properties and not present in current boundary".format(
locality))
if duplicate_check:
for locality_code, count in locality_code_map.items():
if count > 1:
errors.append("\nDuplicate Locality Code \"{}\" Repeated for \"{}\" times".format(locality_code, count))
for locality_name, zone_n_block in locality_name_map.items():
if len(zone_n_block) > 3:
# As we are adding locality code, zone name, ward name locality name against locality name
# So, each locality name contains atleast 3 data in the list.
# If size of list is more that 3 then locality name is repeated for multiple times
errors.append(
"\nDuplicate Locality Name: \"{}\" Zone : \"{}\" Ward/Block : \"{}\"".format(locality_name,
zone_n_block[1],
zone_n_block[2]))
# errors.append("Name : {}".format(locality_name))
# errors.append("Zone : {}".format(zone_n_block[1]))
# errors.append("Ward/Block : {}".format(zone_n_block[2]))
# Getting all the locality code from "zone_n_block" list
same_name_locality_code = [zone_n_block[i] for i in range(0, len(zone_n_block)) if i % 3 == 0]
errors.append("\nRepetition : \"{}\" locality code: \"{}\" ".format(len(same_name_locality_code),
same_name_locality_code))
for loc_code in same_name_locality_code:
if loc_code in localities_in_use:
errors.append("\t\t{} : {}".format(loc_code, "USED"))
else:
errors.append("\t\t{} : {}".format(loc_code, "NOT USED"))
return errors
def get_used_localities(auth_token, boundary_data, boundary_type):
if boundary_type == "REVENUE":
URL = config.URL_SEARCH_LOCALITIES_USED_IN_REVENUE
elif boundary_type == "ADMIN":
URL = config.URL_SEARCH_LOCALITIES_USED_IN_ADMIN
tenant_id = boundary_data["tenantId"]
resp = requests.post(URL, data=json.dumps({
"RequestInfo": {
"authToken": auth_token
},
"searchCriteria": {
"tenantId": tenant_id
}
}), headers={'Content-Type': 'application/json'})
localities_used = resp.json()["services"]
localities_in_use = []
if boundary_type == "REVENUE":
for locality in localities_used:
localities_in_use.append(locality["locality"])
elif boundary_type == "ADMIN":
for locality in localities_used:
if locality["mohalla"] != None:
localities_in_use.append(locality["mohalla"])
return localities_in_use
def create_boundary(config_function, boundary_type):
# load_admin_boundary_config()
current_boundary_type = boundary_type
config_function()
dfs = open_excel_file(config.SHEET)
wards = get_sheet(dfs, config.SHEET_WARDS)
zones = get_sheet(dfs, config.SHEET_ZONES)
locality = get_sheet(dfs, config.SHEET_LOCALITY)
offset = 1
index_code = get_column_index(wards, config.COLUMN_WARD_CODE) or 1
index_name = get_column_index(wards, config.COLUMN_WARD_NAME) or 2
index_zone_name = get_column_index(wards, config.COLUMN_WARD_ADMIN_ZONE_NAME) or 3
ward_to_code_map = {}
for _, row in wards.iterrows():
ward_to_code_map[row[index_code].strip()] = row[index_name].strip()
ward_to_code_map[row[index_name].strip()] = row[index_code].strip()
wards_data = wards.apply(lambda row: {"id": str(uuid.uuid4()),
"boundaryNum": 1,
"name": row[index_name].strip(),
"localname": row[index_name].strip(),
"longitude": None,
"latitude": None,
"label": "Block",
"code": row[index_code].strip(),
"zone": row[index_zone_name].strip(),
"children": []}
, axis=1)
index_code = get_column_index(zones, config.COLUMN_ZONE_CODE) or 1
index_name = get_column_index(zones, config.COLUMN_ZONE_NAME) or 2
zone_to_code_map = {}
for _, row in zones.iterrows():
zone_to_code_map[row[index_code].strip()] = row[index_name].strip()
zone_to_code_map[row[index_name].strip()] = row[index_code].strip()
zones_data = zones.apply(lambda row: {"id": str(uuid.uuid4()),
"boundaryNum": 1,
"name": row[index_name].strip(),
"localname": row[index_name].strip(),
"longitude": None,
"latitude": None,
"label": "Zone",
"code": row[index_code].strip(),
"children": []}
, axis=1)
index_code = get_column_index(locality, config.COLUMN_LOCALITY_CODE) or 1
index_name = get_column_index(locality, config.COLUMN_LOCALITY_NAME) or 2
index_admin_block = get_column_index(locality, config.COLUMN_LOCALITY_ADMIN_BLOCK) or 3
if current_boundary_type == "REVENUE":
index_area = get_column_index(locality, config.COLUMN_LOCALITY_AREA) or 4
def process_locality(row):
area = "" if current_boundary_type == "ADMIN" else row[index_area].strip().upper().replace(" ", "").replace(
"-", "")
area_code = area.replace("AREA", "A")
if current_boundary_type == "REVENUE" and area not in ("AREA1", "AREA2", "AREA3"):
raise InvalidArgumentException("Area type is not valid - " + area)
if area_code:
area_code = " - " + area_code
return {
"id": str(uuid.uuid4()),
"boundaryNum": 1,
"name": row[index_name].strip() + " - " + ward_to_code_map[row[index_admin_block].strip()] + area_code,
"localname": row[index_name].strip() + " - " + ward_to_code_map[row[index_admin_block].strip()] + area_code,
"longitude": None,
"latitude": None,
"label": "Locality",
"code": row[index_code].strip(),
"ward": row[index_admin_block].strip(),
"area": area,
"children": []
}
locality_data = locality.apply(process_locality, axis=1)
wards_list = wards_data.tolist()
locality_list = locality_data.tolist()
zones_list = zones_data.tolist()
wards_map = {}
for ward in wards_list:
zone = ward.pop('zone')
wards_map[zone] = wards_map.get(zone, [])
wards_map[zone].append(ward)
wards_map[zone_to_code_map[zone]] = wards_map[zone]
locality_map = {}
for loca in locality_list:
ward = loca.pop('ward')
locality_map[ward] = locality_map.get(ward, [])
locality_map[ward].append(loca)
locality_map[ward_to_code_map[ward]] = locality_map[ward]
for ward in wards_list:
code = ward['code']
ward['children'] = locality_map.get(code, [])
for zone in zones_list:
name = zone['name']
zone['children'] = wards_map[name]
import json
# current_boundary_type = "ADMIN"
new_boundary_data = {
"hierarchyType": {
"code": current_boundary_type,
"name": current_boundary_type
},
"boundary": {
"id": 1,
"boundaryNum": 1,
"name": config.CITY_NAME,
"localname": config.CITY_NAME,
"longitude": None,
"latitude": None,
"label": "City",
"code": config.TENANT_ID,
"children": zones_list
}
}
final_data = {
"tenantId": config.TENANT_ID,
"moduleName": "egov-location",
"TenantBoundary": [
new_boundary_data
]
}
data = json.dumps(final_data, indent=2)
print(data)
auth_token = superuser_login()["access_token"]
errors = validate_boundary_data(auth_token, final_data, boundary_type, config.BOUNDARY_DUPLICATE_CHECK,
config.BOUNDARY_USED_CHECK)
if len(errors) > 0:
for error in errors:
print(error)
return
import os
response = os.getenv("ASSUME_YES", None) or input("Do you want to append the data in repo (y/[n])?")
if response.lower() == "y":
boundary_path = config.MDMS_LOCATION / config.CITY_NAME.lower() / "egov-location"
os.makedirs(boundary_path, exist_ok=True)
if os.path.isfile(boundary_path / "boundary-data.json"):
with open(boundary_path / "boundary-data.json") as f:
existing_boundary_data = json.load(f)
if len(existing_boundary_data["TenantBoundary"]) == 0:
# should never happen but just in case
existing_boundary_data["TenantBoundary"].append(new_boundary_data)
print("Boundary added")
elif len(existing_boundary_data["TenantBoundary"]) == 1:
if existing_boundary_data["TenantBoundary"][0]["hierarchyType"]["code"] == current_boundary_type:
existing_boundary_data["TenantBoundary"][0] = new_boundary_data
print("Boundary already exists. Overwriting")
else:
existing_boundary_data["TenantBoundary"].append(new_boundary_data)
print("Boundary file exists. Adding new data")
elif len(existing_boundary_data["TenantBoundary"]) == 2:
if existing_boundary_data["TenantBoundary"][0]["hierarchyType"]["code"] == current_boundary_type:
existing_boundary_data["TenantBoundary"][0] = new_boundary_data
else:
existing_boundary_data["TenantBoundary"][1] = new_boundary_data
print("Boundary already exists. Overwriting")
print("File Path : ", boundary_path, "boundary-data.json")
else:
# the file doesn't exists already, so we can safely generate current boundary
print("Boundary didn't exist. Creating one")
print("File Path : ", boundary_path + "boundary-data.json")
existing_boundary_data = final_data
with open(boundary_path / "boundary-data.json", "w") as f:
json.dump(existing_boundary_data, f, indent=2)
def fix_value(val, default_str="", default_nan=None):
if val is None:
return default_str
if type(val) is str and "°" in val:
return val.split("°")[0]
if type(val) is str:
return val.strip()
elif numpy.isnan(val):
return default_nan
else:
return str(val)
def get_employees(auth_token, **kwargs):
data = requests.post(url=config.HOST + '/user/_search',
json={
**{
"RequestInfo": {
"authToken": auth_token
}
}, **kwargs,
})
# print(data.json())
return data.json()["user"]
def get_employees_by_id(auth_token, username, tenantid, active=True):
# data = requests.post(url=config.HOST + '/user/_search',
# json={
# "RequestInfo": {
# "authToken": auth_token
# },
# "userName": username,
# "tenantId": tenantid
# })
#
# return data.json()["user"]
return get_employees(auth_token, userName=username, tenantId=tenantid, active=active)
def get_employees_by_phone(auth_token, phone, tenantid):
# data = requests.post(url=config.HOST + '/user/_search',
# json={
# "RequestInfo": {
# "authToken": auth_token
# },
# "mobileNumber": phone,
# "tenantId": tenantid
# })
#
# return data.json()["user"]
return get_employees(auth_token, mobileNumber=phone, tenantId=tenantid)
def add_role_to_user(auth_token, username, tenant_id, add_roles, change_roles={}, remove_previous_roles=False):
user = get_employees_by_id(auth_token, username, tenant_id)
if remove_previous_roles:
user[0]["roles"] = []
if change_roles:
changed = False
for role in user[0]["roles"]:
if role["code"] in change_roles:
role["code"] = change_roles[role["code"]]
changed = True
if not changed:
return
for role in add_roles:
user[0]["roles"].append({
"code": role,
"name": config.ROLE_CODE_MAP[role]
})
user[0]['dob'] = None
data = requests.post(url=config.HOST + '/user/users/_updatenovalidate',
json={
"RequestInfo": {
"authToken": auth_token
},
"user": user[0],
})
return data.json()["user"]
def update_user_password(auth_token, tenant_id, username, password):
user = get_employees_by_id(auth_token, username, tenant_id)
user[0]['dob'] = None
user[0]['password'] = password
data = requests.post(url=config.HOST + '/user/users/_updatenovalidate',
json={
"RequestInfo": {
"authToken": auth_token
},
"user": user[0],
})
return data.json()["user"]
def remove_user_photo(auth_token, tenant_id, username):
user = get_employees_by_id(auth_token, username, tenant_id)
user[0]['dob'] = None
user[0]["photo"] = None
data = requests.post(url=config.HOST + '/user/users/_updatenovalidate',
json={
"RequestInfo": {
"authToken": auth_token
},
"user": user[0],
})
return data.json()["user"]
def unlock_user(auth_token, tenant_id, username):
user = get_employees_by_id(auth_token, username, tenant_id)
user[0]['dob'] = None
user[0]["accountLocked"] = False
data = requests.post(url=config.HOST + '/user/users/_updatenovalidate',
json={
"RequestInfo": {
"authToken": auth_token
},
"user": user[0],
})
return data.json()["user"]
def update_user_activation(auth_token, tenant_id, username, activate=False):
user = get_employees_by_id(auth_token, username, tenant_id, active=not activate)
user[0]['dob'] = None
user[0]['active'] = activate
data = requests.post(url=config.HOST + '/user/users/_updatenovalidate',
json={
"RequestInfo": {
"authToken": auth_token
},
"user": user[0],
})
return data.json()["user"]
def search_property(auth_token, tenant_id, property_id):
data = requests.post(url=config.HOST + '/pt-services-v2/property/_search',
json={
"RequestInfo": {
"authToken": auth_token
}
}, params={"ids": property_id, "tenantId": tenant_id})
return data.json()
def update_property(auth_token, tenant_id, property):
data = requests.post(url=config.HOST + '/pt-services-v2/property/_update',
json={
"RequestInfo": {
"authToken": auth_token
},
"Properties": property
}, params={"tenantId": tenant_id})
return data.json()
def search_demand(auth_token, tenantId=None, consumerCode=None, businessService=None):
args = {}
if tenantId:
args["tenantId"] = tenantId
if consumerCode:
args["consumerCode"] = consumerCode
if businessService:
args["businessCode"] = businessService
data = requests.post(url=config.HOST + '/billing-service/demand/_search',
json={
"RequestInfo": {
"authToken": auth_token
}
}, params=args)
return data.json()
def update_demand(auth_token, demands):
data = requests.post(url=config.HOST + '/billing-service/demand/_update',
json={
"RequestInfo": {
"authToken": auth_token
},
"Demands": demands
})
return data.json()
def generate_bill(auth_token, tenant_id, demand_id, consumer_code, business_service):
data = requests.post(url=config.HOST + '/billing-service/bill/_generate',
json={
"RequestInfo": {
"authToken": auth_token
},
"GenerateBillCriteria": [
]
}, params={
"tenantId": tenant_id,
"demandId": demand_id,
"consumerCode": consumer_code,
"businessService": business_service,
})
return data.json()
def create_receipt(auth_token, tenant_id, receipt):
data = requests.post(url=config.HOST + '/collection-services/receipts/_create',
json={
"RequestInfo": {
"authToken": auth_token
},
"Receipt": receipt
}, params={"tenantId": tenant_id})
return data.json()
def search_receipt(auth_token, receiptNumbers=None, tenantId=None, consumerCode=None, businessCode=None, status=None):
args = {}
if status:
args["status"] = status
if receiptNumbers:
args["receiptNumbers"] = receiptNumbers
if tenantId:
args["tenantId"] = tenantId
if consumerCode:
args["consumerCode"] = consumerCode
if businessCode:
args["businessCode"] = businessCode
data = requests.post(url=config.HOST + '/collection-services/receipts/_search',
json={
"RequestInfo": {
"authToken": auth_token
}
}, params=args)
return data.json()
def cancel_receipt(auth_token, receipt_number, consumer_code,tenant_id, message):
data = requests.post(url=config.HOST + '/collection-services/receipts/_workflow',
json={
"RequestInfo": {
"authToken": auth_token
},
"ReceiptWorkflow": [
{
"consumerCode": consumer_code,
"receiptNumber": receipt_number,
"action": "CANCEL",
"tenantId": tenant_id,
"reason": message
}
]
})
return data.json()
def upsert_localization(auth_token, body):
body["RequestInfo"]["authToken"] = auth_token
data = requests.post(url=config.HOST + '/localization/messages/v1/_upsert', json=body)
return data.json()
def mdms_call(auth_token, module_name, master_details):
url = urljoin(config.HOST, '/egov-mdms-service/v1/_search')
request_body = {}
request_body["RequestInfo"] = {"authToken": auth_token}
request_body["MdmsCriteria"] = {"tenantId": "pb", "moduleDetails": [
{"moduleName": module_name, "masterDetails": [{"name": master_details}]}]}
params = {"tenantId": "pb"}
return requests.post(url, params=params, json=request_body).json()
def search_localization(auth_token, module_name, locale, tenant_id=config.TENANT_ID):
url = urljoin(config.HOST, '/localization/messages/v1/_search')
request_body = {}
request_body["RequestInfo"] = {"authToken": auth_token}
params = {"tenantId": tenant_id, "module": module_name, "locale": locale}
return requests.post(url, params=params, json=request_body).json()
def search_tl_billing_slab(auth_token, tenant_id=config.TENANT_ID):
url = urljoin(config.HOST, '/tl-calculator/billingslab/_search')
request_body = {}
request_body["RequestInfo"] = {"authToken": auth_token}
params = {"tenantId": tenant_id}
return requests.post(url, params=params, json=request_body).json()
def cancel_property(auth_token, tenant_id, property_id, assessment_numbers=None, action="CANCEL_PROPERTY",
status="INACTIVE"):
url = urljoin(config.HOST, '/pt-services-v2/property/_cancel')
request_body = {}
request_body["RequestInfo"] = {"authToken": auth_token}
params = {
"tenantId": tenant_id,
"propertyId": property_id,
"assessmentNumbers": assessment_numbers,
"action": action,
"status": status
}
return requests.post(url, params=params, json=request_body).json()
def update_property_status(auth_token, properties, status="INACTIVE"):
for property_id, tenant_id in properties:
if status == "INACTIVE":
search_properties = search_property(auth_token, tenant_id, property_id)["Properties"]
if len(search_properties) > 0:
consumer_codes = list(map(lambda pd: property_id + ':' + pd["assessmentNumber"], search_properties[0]["propertyDetails"]))
receipts = search_receipt(auth_token, None, tenant_id, ','.join(consumer_codes))["Receipt"]
if len(receipts) > 0:
print(
"there is a active receipt {} property cannot be de-activated. Please cancel all receipts first".format(
property_id))
continue
else:
print("Property {} not found".format(property_id))
cancel_property(auth_token, tenant_id, property_id, status=status)
def cleanup_property(auth_token, properties):
for property_id, tenant_id in properties:
search_properties = search_property(auth_token, tenant_id, property_id)["Properties"]
if len(search_properties) > 0:
consumer_codes = list(map(lambda pd: property_id + ':' + pd["assessmentNumber"],
sorted(search_properties[0]["propertyDetails"], key=lambda p: p["assessmentDate"])))
receipts = search_receipt(auth_token, None, tenant_id, ','.join(consumer_codes))["Receipt"]
consumer_codes_with_receipts = list(map(lambda r: r['consumerCode'], receipts))
if len(consumer_codes_with_receipts) == 0:
# We have no receipts, so only assessment we want to keep is the first one
consumer_codes_to_inactivate = consumer_codes[1:]
else:
consumer_codes_to_inactivate = list(filter(lambda code: code not in consumer_codes_with_receipts, consumer_codes))
if len(consumer_codes_to_inactivate) > 0:
assessment_numbers = list(map(lambda code: code.split(":")[1], consumer_codes_to_inactivate))
cancel_property(auth_token, tenant_id, property_id, ','.join(assessment_numbers),
action="CANCEL_ASSESSMENT")
else:
print("No unpaid assessments found for {}".format(property_id))
else:
print("Property {} not found".format(property_id))
#
# # get all consumer codes for which receipts are not there
# # cancel_property, cancel the all the assessments where no receipt was there
# assessment_numbers = "????"
# cancel_property(auth_token, tenant_id, property_id, assessment_numbers, action="CANCEL_ASSESSMENT")