77from sqlalchemy .sql import text
88
99
10- from data .datastore import (session_scope , Order , Cart , Resource , System ,
11- Library , User , Lang , Audn , MatType , Vendor ,
12- OrderLocation , Fund , Branch , ShelfCode , Wlos )
10+ from data .datastore import (
11+ session_scope ,
12+ Order ,
13+ Cart ,
14+ Resource ,
15+ System ,
16+ Library ,
17+ User ,
18+ Lang ,
19+ Audn ,
20+ MatType ,
21+ Vendor ,
22+ OrderLocation ,
23+ Fund ,
24+ Branch ,
25+ ShelfCode ,
26+ Wlos ,
27+ )
1328from data .datastore_worker import retrieve_record
1429from errors import BabelError
1530from logging_settings import format_traceback
1631
1732
18- mlogger = logging .getLogger (' babel' )
33+ mlogger = logging .getLogger (" babel" )
1934
2035
2136@lru_cache (maxsize = 2 )
2237def get_shelfcode (session , shelfcode_id , audn_code ):
23- shelfcode = ''
38+ shelfcode = ""
2439 if shelfcode_id is not None :
25- stmn = text ("""
40+ stmn = text (
41+ """
2642 SELECT code, includes_audn FROM shelfcode
2743 WHERE did=:shelfcode_id
28- """ )
44+ """
45+ )
2946 stmn = stmn .bindparams (shelfcode_id = shelfcode_id )
3047 instances = session .execute (stmn )
31- shelfcode = ''
48+ shelfcode = ""
3249 for i in instances :
3350 if i .includes_audn :
34- shelfcode = f' { audn_code } { i .code } '
51+ shelfcode = f" { audn_code } { i .code } "
3552 else :
36- shelfcode = f' { i .code } '
53+ shelfcode = f" { i .code } "
3754 break
3855 return shelfcode
3956
@@ -42,10 +59,12 @@ def get_shelfcode(session, shelfcode_id, audn_code):
4259def get_fund_code (session , fund_id ):
4360 code = None
4461 if fund_id is not None :
45- stmn = text ("""
62+ stmn = text (
63+ """
4664 SELECT code FROM fund
4765 WHERE did=:fund_id
48- """ )
66+ """
67+ )
4968 stmn = stmn .bindparams (fund_id = fund_id )
5069 instances = session .execute (stmn )
5170 for i in instances :
@@ -58,10 +77,12 @@ def get_fund_code(session, fund_id):
5877def get_branch_code (session , branch_id ):
5978 code = None
6079 if branch_id is not None :
61- stmn = text ("""
80+ stmn = text (
81+ """
6282 SELECT code FROM branch
6383 WHERE did=:branch_id
64- """ )
84+ """
85+ )
6586 stmn = stmn .bindparams (branch_id = branch_id )
6687 instances = session .execute (stmn )
6788 for i in instances :
@@ -74,22 +95,26 @@ def get_branch_code(session, branch_id):
7495def get_vendor_code (session , vendor_id , system ):
7596 code = None
7697 if vendor_id is not None :
77- if system == 'BPL' :
78- stmn = text ("""
98+ if system == "BPL" :
99+ stmn = text (
100+ """
79101 SELECT bpl_code FROM vendor
80102 WHERE did=:vendor_id
81- """ )
82- elif system == 'NYP' :
83- stmn = text ("""
103+ """
104+ )
105+ elif system == "NYP" :
106+ stmn = text (
107+ """
84108 SELECT nyp_code FROM vendor
85109 WHERE did=:vendor_id
86- """ )
110+ """
111+ )
87112 stmn = stmn .bindparams (vendor_id = vendor_id )
88113 instances = session .execute (stmn )
89114 for i in instances :
90- if system == ' BPL' :
115+ if system == " BPL" :
91116 code = i .bpl_code
92- elif system == ' NYP' :
117+ elif system == " NYP" :
93118 code = i .nyp_code
94119 break
95120 return code
@@ -99,10 +124,12 @@ def get_vendor_code(session, vendor_id, system):
99124def get_lang_name (session , lang_id ):
100125 name = None
101126 if lang_id is not None :
102- stmn = text ("""
127+ stmn = text (
128+ """
103129 SELECT name FROM lang
104130 WHERE did=:lang_id
105- """ )
131+ """
132+ )
106133 stmn = stmn .bindparams (lang_id = lang_id )
107134 instances = session .execute (stmn )
108135 for i in instances :
@@ -116,10 +143,12 @@ def get_audn_name_and_code(session, audn_id):
116143 name = None
117144 code = None
118145 if audn_id is not None :
119- stmn = text ("""
146+ stmn = text (
147+ """
120148 SELECT name, code FROM audn
121149 WHERE did=:audn_id
122- """ )
150+ """
151+ )
123152 stmn = stmn .bindparams (audn_id = audn_id )
124153 instances = session .execute (stmn )
125154 for i in instances :
@@ -133,10 +162,12 @@ def get_audn_name_and_code(session, audn_id):
133162def get_mattype_name (session , mattype_id ):
134163 name = None
135164 if mattype_id is not None :
136- stmn = text ("""
165+ stmn = text (
166+ """
137167 SELECT name FROM mattype
138168 WHERE did=:mattype_id
139- """ )
169+ """
170+ )
140171 stmn = stmn .bindparams (mattype_id = mattype_id )
141172 instances = session .execute (stmn )
142173 for i in instances :
@@ -147,10 +178,12 @@ def get_mattype_name(session, mattype_id):
147178
148179@lru_cache (maxsize = 4 )
149180def get_status_name (session , status_id ):
150- stmn = text ("""
181+ stmn = text (
182+ """
151183 SELECT name FROM status
152184 WHERE did=:status_id
153- """ )
185+ """
186+ )
154187 stmn = stmn .bindparams (status_id = status_id )
155188 instances = session .execute (stmn )
156189 for i in instances :
@@ -161,10 +194,12 @@ def get_status_name(session, status_id):
161194
162195@lru_cache (maxsize = 2 )
163196def get_library_name (session , library_id ):
164- stmn = text ("""
197+ stmn = text (
198+ """
165199 SELECT name FROM library
166200 WHERE did=:library_id
167- """ )
201+ """
202+ )
168203 stmn = stmn .bindparams (library_id = library_id )
169204 instances = session .execute (stmn )
170205
@@ -179,10 +214,12 @@ def get_library_name(session, library_id):
179214@lru_cache (maxsize = 2 )
180215def get_system_name (session , system_id ):
181216 name = None
182- stmn = text ("""
183- SELECT name FROM system
217+ stmn = text (
218+ """
219+ SELECT name FROM `system`
184220 WHERE did=:system_id
185- """ )
221+ """
222+ )
186223 stmn = stmn .bindparams (system_id = system_id )
187224 instances = session .execute (stmn )
188225 for i in instances :
@@ -194,10 +231,12 @@ def get_system_name(session, system_id):
194231@lru_cache (maxsize = 4 )
195232def get_owner (session , user_id ):
196233 owner = None
197- stmn = text ("""
198- SELECT name FROM user
234+ stmn = text (
235+ """
236+ SELECT name FROM `user`
199237 WHERE did=:user_id
200- """ )
238+ """
239+ )
201240 stmn = stmn .bindparams (user_id = user_id )
202241 instances = session .execute (stmn )
203242 for i in instances :
@@ -207,35 +246,33 @@ def get_owner(session, user_id):
207246
208247
209248def get_data_by_identifier (keyword , keyword_type ):
210- if keyword_type == ' bib #' :
249+ if keyword_type == " bib #" :
211250 param = Order .bid
212- elif keyword_type == ' order #' :
251+ elif keyword_type == " order #" :
213252 param = Order .oid
214- elif keyword_type == ' wlo #' :
253+ elif keyword_type == " wlo #" :
215254 param = Order .wlo
216- elif keyword_type == ' ISBN' :
255+ elif keyword_type == " ISBN" :
217256 param = Resource .isbn
218- elif keyword_type == ' UPC' :
257+ elif keyword_type == " UPC" :
219258 param = Resource .upc
220- elif keyword_type == ' other #' :
259+ elif keyword_type == " other #" :
221260 param = Resource .other_no
222- elif keyword_type == ' blanketPO' :
261+ elif keyword_type == " blanketPO" :
223262 param = Cart .blanketPO
224263 else :
225- raise AttributeError (' Invalid keyword_type passed' )
226- mlogger .debug (f' Basic search params: { keyword } , type { param } ' )
264+ raise AttributeError (" Invalid keyword_type passed" )
265+ mlogger .debug (f" Basic search params: { keyword } , type { param } " )
227266
228267 try :
229268 with session_scope () as session :
230269 recs = (
231- session .query (
232- Cart ,
233- Order ,
234- Resource )
270+ session .query (Cart , Order , Resource )
235271 .join (Order , Cart .did == Order .cart_id )
236272 .join (Resource , Order .did == Resource .order_id )
237273 .filter (param == keyword )
238- .all ())
274+ .all ()
275+ )
239276
240277 results = []
241278 for cart_rec , ord_rec , res_rec in recs :
@@ -255,20 +292,17 @@ def get_data_by_identifier(keyword, keyword_type):
255292 po = ord_rec .poPerLine
256293
257294 lang = get_lang_name (session , ord_rec .lang_id )
258- audn_name , audn_code = get_audn_name_and_code (
259- session , ord_rec .audn_id )
295+ audn_name , audn_code = get_audn_name_and_code (session , ord_rec .audn_id )
260296 vendor = get_vendor_code (session , ord_rec .vendor_id , system )
261297 mattype = get_mattype_name (session , ord_rec .matType_id )
262298
263299 locs = []
264300 for loc in ord_rec .locations :
265301 branch = get_branch_code (session , loc .branch_id )
266- shelfcode = get_shelfcode (
267- session , loc .shelfcode_id , audn_code )
302+ shelfcode = get_shelfcode (session , loc .shelfcode_id , audn_code )
268303 qty = loc .qty
269304 fund = get_fund_code (session , loc .fund_id )
270- locs .append (
271- f'{ branch } { shelfcode } ({ qty } )/{ fund } ' )
305+ locs .append (f"{ branch } { shelfcode } ({ qty } )/{ fund } " )
272306
273307 # resouce
274308 title = res_rec .title
@@ -297,7 +331,8 @@ def get_data_by_identifier(keyword, keyword_type):
297331 audn_name = audn_name ,
298332 mattype = mattype ,
299333 po = po ,
300- locs = ', ' .join (locs ))
334+ locs = ", " .join (locs ),
335+ )
301336 results .append (unit )
302337 session .expunge_all ()
303338
@@ -306,16 +341,14 @@ def get_data_by_identifier(keyword, keyword_type):
306341 except Exception as exc :
307342 _ , _ , exc_traceback = sys .exc_info ()
308343 tb = format_traceback (exc , exc_traceback )
309- mlogger .error (
310- 'Unhandled error during Basic search.'
311- f'Traceback: { tb } ' )
312- raise BabelError ('Unable to retrieve records.' )
344+ mlogger .error ("Unhandled error during Basic search." f"Traceback: { tb } " )
345+ raise BabelError ("Unable to retrieve records." )
313346
314347
315348def complex_search (conditions ):
316349 """
317350 args:
318- condictions : dict, key=element name, value=tuple(con, value)
351+ conditions : dict, key=element name, value=tuple(con, value)
319352 """
320353
321354 # will tackle in next iteration
0 commit comments