Nextcloud poll csv import
This commit is contained in:
4
code/check_csv.sh
Executable file
4
code/check_csv.sh
Executable file
@@ -0,0 +1,4 @@
|
||||
#!/bin/bash
|
||||
|
||||
python3 convert_nextcloud_poll_csv.py ../samples/poll1.csv 'Très Bien' 'Bien' 'Assez Bien' 'Passable' 'Insuffisant' 'A Rejeter'
|
||||
|
||||
67
code/convert_nextcloud_poll_csv.py
Normal file
67
code/convert_nextcloud_poll_csv.py
Normal file
@@ -0,0 +1,67 @@
|
||||
import json
|
||||
import sys
|
||||
import csv
|
||||
|
||||
# using non modified nextcloud standard text poll
|
||||
nom_mentions=['yes','maybe','','no']
|
||||
|
||||
if len(sys.argv) > 1:
|
||||
csv_file=sys.argv[1]
|
||||
if len(sys.argv) > 2:
|
||||
nom_mentions=[]
|
||||
for i in range(2,len(sys.argv)):
|
||||
nom_mentions.append(sys.argv[i])
|
||||
else:
|
||||
print('missing file argument')
|
||||
exit(1)
|
||||
|
||||
def mention_index(mention):
|
||||
return nom_mentions.index(mention)
|
||||
|
||||
verbose=False
|
||||
|
||||
mentions=len(nom_mentions)
|
||||
default_mention=mentions-1
|
||||
|
||||
warnings=[]
|
||||
candidats={}
|
||||
candidats_alias=[]
|
||||
a_vote={}
|
||||
votes=[]
|
||||
with open(csv_file,'r') as csv_fd:
|
||||
vote_reader = csv.reader(csv_fd, delimiter=',')
|
||||
for row in vote_reader:
|
||||
if len(candidats_alias) == 0:
|
||||
for i in range(2,len(row)):
|
||||
candidat_alias=row[i]
|
||||
candidats_alias.append(candidat_alias)
|
||||
# no other name given
|
||||
if not candidat_alias in candidats:
|
||||
candidats[candidat_alias]=candidat_alias
|
||||
else:
|
||||
# check if not duplicated
|
||||
participant=row[0]
|
||||
if participant in a_vote:
|
||||
warnings.append("Mutliple vote " + participant + " no record")
|
||||
continue
|
||||
a_vote[participant]=row[1]
|
||||
vote={}
|
||||
for i in range(2,len(row)):
|
||||
candidat_index=i-2
|
||||
candidat_alias=candidats_alias[candidat_index]
|
||||
mention=row[i]
|
||||
vote[candidat_alias]=mention_index(mention)
|
||||
votes.append(vote)
|
||||
|
||||
|
||||
default_vote={}
|
||||
|
||||
if verbose:
|
||||
print(','.join(candidats))
|
||||
print(','.join(nom_mentions))
|
||||
|
||||
result = {'candidats':candidats,'votants':{'decompte':len(a_vote)},'mentions':nom_mentions,'votation':{'votes':votes}}
|
||||
print(json.dumps(result))
|
||||
|
||||
sys.exit(0)
|
||||
|
||||
@@ -38,7 +38,7 @@ for candidat in candidats:
|
||||
collect={}
|
||||
for vote in votes:
|
||||
|
||||
# missing candidat in vote is the worts one
|
||||
# missing candidat in vote picks the worse one
|
||||
for candidat in candidats:
|
||||
if not candidat in vote:
|
||||
vote[candidat]=default_mention
|
||||
|
||||
Reference in New Issue
Block a user