22import json
33import asyncio
44
5- import bson
6- import paramiko
5+ from paramiko import SSHClient , AutoAddPolicy
76from logging import Logger
87from datetime import datetime
9- import re
8+ from re import match
109
11- import motor .motor_asyncio
10+ from motor .motor_asyncio import AsyncIOMotorClient
1211from dotenv import load_dotenv
1312from pymongo import MongoClient
1413import logging
15- import base64
16- import requests
14+ from base64 import b64decode
15+ from requests import get , post , exceptions
1716from bson import ObjectId
1817
1918# Configure logging
2221# Load environment variables from .env file
2322load_dotenv ()
2423
25- ssh = paramiko . SSHClient ()
26- ssh .set_missing_host_key_policy (paramiko . AutoAddPolicy ())
24+ ssh = SSHClient ()
25+ ssh .set_missing_host_key_policy (AutoAddPolicy ())
2726
2827ssh .connect (
2928 os .getenv ("IMPORT_SSH_HOSTNAME" ),
3635mongo_url = os .getenv ("MONGODB_URL" )
3736token_path = os .path .join (os .path .dirname (__file__ ), '.dev-token.json' )
3837import_mongo_url = os .getenv ("IMPORT_MONGO_DB" )
39- client = motor . motor_asyncio . AsyncIOMotorClient (import_mongo_url )
38+ client = AsyncIOMotorClient (import_mongo_url )
4039
4140# sequence = LT000001
4241# sequence = IMP00001
@@ -74,10 +73,10 @@ async def populate_collection_sla(collection, headers):
7473 }
7574 }
7675 try :
77- response = requests . post (f"{ api_endpoint } /{ collection .get ('endpoint' )} " , headers = headers , json = data )
76+ response = post (f"{ api_endpoint } /{ collection .get ('endpoint' )} " , headers = headers , json = data )
7877 response .raise_for_status ()
7978 logger .info (f"{ collection .get ('name' )} inserted" )
80- except requests . exceptions .HTTPError as e :
79+ except exceptions .HTTPError as e :
8180 logger .warning (f"Failed to insert { collection .get ('name' )} : { e .response .json ().get ('message' )} " )
8281
8382
@@ -108,10 +107,10 @@ async def populate_collection_entities(collection, headers):
108107 }
109108 }
110109 try :
111- response = requests . post (f"{ api_endpoint } /{ collection .get ('endpoint' )} " , headers = headers , json = data )
110+ response = post (f"{ api_endpoint } /{ collection .get ('endpoint' )} " , headers = headers , json = data )
112111 response .raise_for_status ()
113112 logger .info (f"{ collection .get ('name' )} inserted" )
114- except requests . exceptions .HTTPError as e :
113+ except exceptions .HTTPError as e :
115114 logger .warning (f"Failed to insert { collection .get ('name' )} : { e .response .json ().get ('message' )} " )
116115
117116
@@ -135,10 +134,10 @@ async def populate_collection_categories(collection, headers):
135134 }
136135 }
137136 try :
138- response = requests . post (f"{ api_endpoint } /{ collection .get ('endpoint' )} " , headers = headers , json = data )
137+ response = post (f"{ api_endpoint } /{ collection .get ('endpoint' )} " , headers = headers , json = data )
139138 response .raise_for_status ()
140139 logger .info (f"{ collection .get ('name' )} inserted" )
141- except requests . exceptions .HTTPError as e :
140+ except exceptions .HTTPError as e :
142141 logger .warning (f"Failed to insert { collection .get ('name' )} : { e .response .json ().get ('message' )} " )
143142 for subcat in document .get ('subCategory' ):
144143 dataSubCat = {
@@ -157,10 +156,10 @@ async def populate_collection_categories(collection, headers):
157156 }
158157 }
159158 try :
160- response = requests . post (f"{ api_endpoint } /{ collection .get ('endpoint' )} " , headers = headers , json = dataSubCat )
159+ response = post (f"{ api_endpoint } /{ collection .get ('endpoint' )} " , headers = headers , json = dataSubCat )
161160 response .raise_for_status ()
162161 logger .info (f"{ collection .get ('name' )} inserted" )
163- except requests . exceptions .HTTPError as e :
162+ except exceptions .HTTPError as e :
164163 logger .warning (f"Failed to insert { collection .get ('name' )} : { e .response .json ()} " )
165164
166165
@@ -183,10 +182,10 @@ async def populate_collection_source_requests(collection, headers):
183182 }
184183 }
185184 try :
186- response = requests . post (f"{ api_endpoint } /{ collection .get ('endpoint' )} " , headers = headers , json = data )
185+ response = post (f"{ api_endpoint } /{ collection .get ('endpoint' )} " , headers = headers , json = data )
187186 response .raise_for_status ()
188187 logger .info (f"{ collection .get ('name' )} inserted" )
189- except requests . exceptions .HTTPError as e :
188+ except exceptions .HTTPError as e :
190189 logger .warning (f"Failed to insert { collection .get ('name' )} : { e .response .json ().get ('message' )} " )
191190
192191
@@ -279,10 +278,10 @@ async def populate_collection_source_ticket(collection, headers):
279278 }
280279 }
281280 try :
282- response = requests . post (f"{ api_endpoint } /{ collection .get ('endpoint' )} " , headers = headers , json = data )
281+ response = post (f"{ api_endpoint } /{ collection .get ('endpoint' )} " , headers = headers , json = data )
283282 response .raise_for_status ()
284283 logger .info (f"{ collection .get ('name' )} inserted" )
285- except requests . exceptions .HTTPError as e :
284+ except exceptions .HTTPError as e :
286285 logger .warning (f"Failed to insert { collection .get ('name' )} : { e .response .json ().get ('message' )} " )
287286
288287 pattern = r'\((.*?)\) <(.*?)>'
@@ -326,7 +325,7 @@ async def populate_collection_source_ticket(collection, headers):
326325 if thread .get ('from' ) is not None :
327326 if isinstance (thread ['from' ], list ) and len (thread ['from' ]) == 0 :
328327 thread ['from' ] = ''
329- matchFrom = re . match (pattern , thread ['from' ][0 ] if isinstance (thread ['from' ], list ) and len (thread ['from' ]) > 0 else thread ['from' ])
328+ matchFrom = match (pattern , thread ['from' ][0 ] if isinstance (thread ['from' ], list ) and len (thread ['from' ]) > 0 else thread ['from' ])
330329 if matchFrom is not None :
331330 fromMailInfo = {
332331 "name" : matchFrom .group (1 ),
@@ -336,7 +335,7 @@ async def populate_collection_source_ticket(collection, headers):
336335 if thread .get ('to' ) is not None :
337336 if isinstance (thread ['to' ], list ) and len (thread ['to' ]) == 0 :
338337 thread ['to' ] = ''
339- matchTo = re . match (pattern , thread ['to' ][0 ] if isinstance (thread ['to' ], list ) and len (thread ['to' ]) > 0 else thread ['to' ])
338+ matchTo = match (pattern , thread ['to' ][0 ] if isinstance (thread ['to' ], list ) and len (thread ['to' ]) > 0 else thread ['to' ])
340339 if matchTo is not None :
341340 toMailInfo = {
342341 "name" : matchTo .group (1 ),
@@ -346,7 +345,7 @@ async def populate_collection_source_ticket(collection, headers):
346345 if thread .get ('cc' ) is not None :
347346 if isinstance (thread ['cc' ], list ) and len (thread ['cc' ]) == 0 :
348347 thread ['cc' ] = ''
349- matchCc = re . match (pattern , thread ['cc' ][0 ] if isinstance (thread ['cc' ], list ) and len (thread ['cc' ]) > 0 else thread ['cc' ])
348+ matchCc = match (pattern , thread ['cc' ][0 ] if isinstance (thread ['cc' ], list ) and len (thread ['cc' ]) > 0 else thread ['cc' ])
350349 if matchCc is not None :
351350 ccMailInfo = {
352351 "name" : matchCc .group (1 ),
@@ -372,10 +371,10 @@ async def populate_collection_source_ticket(collection, headers):
372371 }
373372 else :
374373 try :
375- response = requests . get (f"{ api_endpoint } /{ collection .get ('endpointThread' )} ?filters%5BticketId%5D={ str (document ['_id' ])} &filters%5B%3DcustomFields.importData.date%5D={ thread ['date' ]} &filters%5B%3DcustomFields.importData.text%5D={ thread ['text' ]} " , headers = headers )
374+ response = get (f"{ api_endpoint } /{ collection .get ('endpointThread' )} ?filters%5BticketId%5D={ str (document ['_id' ])} &filters%5B%3DcustomFields.importData.date%5D={ thread ['date' ]} &filters%5B%3DcustomFields.importData.text%5D={ thread ['text' ]} " , headers = headers )
376375 response .raise_for_status ()
377376 verifyThread = False if len (response .json ()['data' ]) > 0 else True
378- except requests . exceptions .HTTPError as e :
377+ except exceptions .HTTPError as e :
379378 logger .warning (f"Failed to search thread duplicate with text : <{ thread ['text' ]} >: { e .response .json ()} " )
380379 verifyThread = False
381380 if not verifyThread :
@@ -404,11 +403,11 @@ async def populate_collection_source_ticket(collection, headers):
404403 }
405404 }
406405 try :
407- response = requests . post (f"{ api_endpoint } /{ collection .get ('endpointThread' )} " ,
406+ response = post (f"{ api_endpoint } /{ collection .get ('endpointThread' )} " ,
408407 headers = headers , json = dataThread )
409408 response .raise_for_status ()
410409 logger .info (f"{ collection .get ('name' )} inserted" )
411- except requests . exceptions .HTTPError as e :
410+ except exceptions .HTTPError as e :
412411 logger .warning (f"Failed to insert { collection .get ('nameThread' )} : { e .response .json ()} " )
413412
414413
@@ -439,7 +438,7 @@ async def populate_collection_filestorage(collection, headers):
439438 print (f"Ignore mail : { document ['filename' ]} " )
440439 continue
441440 remote_file = sftp .open (os .getenv ("IMPORT_SSH_BASEPATH" ) + document ['path' ])
442- decoded_file = base64 . b64decode (remote_file .read ())
441+ decoded_file = b64decode (remote_file .read ())
443442 files = {'file' : decoded_file }
444443 # noinspection PyUnresolvedReferences
445444 data = {
@@ -457,52 +456,52 @@ async def populate_collection_filestorage(collection, headers):
457456 # }
458457 }
459458 try :
460- response = requests . post (f"{ api_endpoint } /{ collection .get ('endpoint' )} " , data = data , files = files )
459+ response = post (f"{ api_endpoint } /{ collection .get ('endpoint' )} " , data = data , files = files )
461460 response .raise_for_status ()
462461 logger .info (f"{ collection .get ('name' )} inserted" )
463- except requests . exceptions .HTTPError as e :
462+ except exceptions .HTTPError as e :
464463 logger .warning (f"Failed to insert { collection .get ('name' )} : { e } \n { e .response .text } " )
465464
466465collections = [
467- # {
468- # "name": "filestorage",
469- # "import_db": "libertech-sys",
470- # "import_db_ticket": "libertech-data",
471- # "import_db_mail": "mails",
472- # "import_collection": "userstorage",
473- # "import_collection_ticket": "tickets",
474- # "import_collection_mail": "mailsStore",
475- # "endpoint": "core/filestorage",
476- # "method": populate_collection_filestorage,
477- # },
478- # {
479- # "name": "sla",
480- # "import_db": "libertech-data",
481- # "import_collection": "tickets_sla",
482- # "endpoint": "tickets/sla",
483- # "method": populate_collection_sla
484- # },
485- # {
486- # "name": "entities",
487- # "import_db": "libertech-data",
488- # "import_collection": "peoples",
489- # "endpoint": "core/entities",
490- # "method": populate_collection_entities
491- # },
492- # {
493- # "name": "categories",
494- # "import_db": "libertech-data",
495- # "import_collection": "tickets_category",
496- # "endpoint": "core/categories",
497- # "method": populate_collection_categories
498- # },
499- # {
500- # "name": "source-requests",
501- # "import_db": "libertech-data",
502- # "import_collection": "tickets_sourcetype",
503- # "endpoint": "tickets/source-request",
504- # "method": populate_collection_source_requests
505- # },
466+ {
467+ "name" : "filestorage" ,
468+ "import_db" : "libertech-sys" ,
469+ "import_db_ticket" : "libertech-data" ,
470+ "import_db_mail" : "mails" ,
471+ "import_collection" : "userstorage" ,
472+ "import_collection_ticket" : "tickets" ,
473+ "import_collection_mail" : "mailsStore" ,
474+ "endpoint" : "core/filestorage" ,
475+ "method" : populate_collection_filestorage ,
476+ },
477+ {
478+ "name" : "sla" ,
479+ "import_db" : "libertech-data" ,
480+ "import_collection" : "tickets_sla" ,
481+ "endpoint" : "tickets/sla" ,
482+ "method" : populate_collection_sla
483+ },
484+ {
485+ "name" : "entities" ,
486+ "import_db" : "libertech-data" ,
487+ "import_collection" : "peoples" ,
488+ "endpoint" : "core/entities" ,
489+ "method" : populate_collection_entities
490+ },
491+ {
492+ "name" : "categories" ,
493+ "import_db" : "libertech-data" ,
494+ "import_collection" : "tickets_category" ,
495+ "endpoint" : "core/categories" ,
496+ "method" : populate_collection_categories
497+ },
498+ {
499+ "name" : "source-requests" ,
500+ "import_db" : "libertech-data" ,
501+ "import_collection" : "tickets_sourcetype" ,
502+ "endpoint" : "tickets/source-request" ,
503+ "method" : populate_collection_source_requests
504+ },
506505 {
507506 "name" : "ticket" ,
508507 "nameThread" : "thread" ,
0 commit comments