Skip to content

Commit

Permalink
Merge pull request #50 from nethesis/phonebook_csv_upload
Browse files Browse the repository at this point in the history
Fix phonebook csv upload nethesis/ns8-nethvoice#203
  • Loading branch information
Stell0 authored May 21, 2024
2 parents a630737 + 4901082 commit b5bccdb
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 222 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

92 changes: 0 additions & 92 deletions root/usr/share/nethesis/NethServer/Module/Phonebook.php

This file was deleted.

32 changes: 0 additions & 32 deletions root/usr/share/nethesis/NethServer/Template/Phonebook.php

This file was deleted.

30 changes: 19 additions & 11 deletions root/usr/share/phonebooks/phonebook-import
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ def syncSourceCsv(path, output, deleteonly):
sid = next(iter(sConfig))
crl = pycurl.Curl()

crl.setopt(crl.URL, str(sConfig[sid]['url']))
crl.setopt(crl.URL, str(sConfig[sid]['url']))

b_obj = io.StringIO()
b_obj = io.BytesIO()
crl.setopt(crl.WRITEFUNCTION, b_obj.write)

crl.setopt(crl.FAILONERROR, 1)
Expand All @@ -182,6 +182,7 @@ def syncSourceCsv(path, output, deleteonly):

try:
crl.perform()
s_obj = io.StringIO(b_obj.getvalue().decode('utf-8'))
logger.debug('source "' + sid + '" connection (' + sConfig[sid]['url'] + '): ok')
except Exception as err:
logger.error('source "' + sid + '" connection (' + sConfig[sid]['url'] + '): failed')
Expand All @@ -190,15 +191,15 @@ def syncSourceCsv(path, output, deleteonly):
finally:
crl.close()

b_obj.seek(0)
s_obj.seek(0)

df = []
sample = b_obj.readline()
b_obj.seek(0)
sample = s_obj.readline()
s_obj.seek(0)
dialect = csv.Sniffer().sniff(sample)
if (not csv.Sniffer().has_header(sample)):
logger.warn('CSV doesn\'t have a valid header!')
csvr = csv.reader(b_obj, dialect=dialect)
csvr = csv.reader(s_obj, dialect=dialect)
header = next(csvr, None)
for row in csvr:
r = {}
Expand All @@ -212,12 +213,19 @@ def syncSourceCsv(path, output, deleteonly):
return

try:
with open(DEST_PATH, 'r') as configFile:
dConfig = json.load(configFile)
dConfig = {
"dbtype": "mysql",
"host": os.environ['PHONEBOOK_DB_HOST'],
"port": os.environ['PHONEBOOK_DB_PORT'],
"user": os.environ['PHONEBOOK_DB_USER'],
"password": os.environ['PHONEBOOK_DB_PASS'],
"dbname": os.environ['PHONEBOOK_DB_NAME'],
"dbtable": 'phonebook'
}
except Exception as err:
logger.error('reading ' + DEST_PATH)
logger.error(str(err))
sys.exit(1)

dbDest = getDbConn(dConfig)
if dbDest != None:
logger.debug('destination db connection ("phonebook"): ok')
Expand All @@ -243,7 +251,7 @@ def syncSourceCsv(path, output, deleteonly):
start = datetime.datetime.now().replace(microsecond=0)

sourceCols = sConfig[sid]['mapping'].keys()
destCols = sConfig[sid]['mapping'].values()
destCols = list(sConfig[sid]['mapping'].values())
destCols.append('sid_imported')

if sConfig[sid]['type'] != None:
Expand All @@ -256,7 +264,7 @@ def syncSourceCsv(path, output, deleteonly):
for row in df:
values = []
for el in sourceCols:
values.append(row[el.encode('utf-8')].decode('utf-8'))
values.append(row[el])
values.append(str(sid))
if sConfig[sid]['type'] != None:
values.append(str(sConfig[sid]['type']))
Expand Down

0 comments on commit b5bccdb

Please sign in to comment.