From cde2eeb4f15de06def22d3bf07120a0a416e807f Mon Sep 17 00:00:00 2001 From: prathamesh Date: Mon, 6 Nov 2017 14:50:21 +0530 Subject: Upload user to the course via csv. The csv takes firstname, lastname and email. User and Profile are created with username and password been same as email. Following cases are handled for csv upload: - wrong csv headders - missing csv values - already existing users - invalid csv --- yaksh/file_utils.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'yaksh/file_utils.py') diff --git a/yaksh/file_utils.py b/yaksh/file_utils.py index f41c531..361b29c 100644 --- a/yaksh/file_utils.py +++ b/yaksh/file_utils.py @@ -2,7 +2,7 @@ import shutil import os import zipfile import tempfile - +import csv def copy_files(file_paths): """ Copy Files to current directory, takes @@ -50,3 +50,30 @@ def extract_files(zip_file, path=None): zip_file.close() return zfiles, extract_path + +def is_csv(document): + try: + try: + content = document.read(1024).decode('utf-8') + except AttributeError: + document.seek(0) + content = document.read(1024) + sniffer = csv.Sniffer() + dialect = sniffer.sniff(content) + document.seek(0) + except (csv.Error, UnicodeDecodeError): + return False + return True + + +def headers_present(dict_reader, headers): + fields = dict_reader.fieldnames + clist = set() + for field in fields: + if field.strip() in headers.keys(): + headers[field.strip()] = field + clist.add(field.strip()) + if clist != headers.keys(): + return False + return True + -- cgit From bce87033267afdbb4aff958c26d1e6d0cac6d220 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Mon, 6 Nov 2017 15:51:05 +0530 Subject: Added info for upload converted list to set as dict.keys() returns list in python 2.7 --- yaksh/file_utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'yaksh/file_utils.py') diff --git a/yaksh/file_utils.py b/yaksh/file_utils.py index 361b29c..0d80b8f 100644 --- a/yaksh/file_utils.py +++ b/yaksh/file_utils.py @@ -68,12 +68,12 @@ def is_csv(document): def headers_present(dict_reader, headers): fields = dict_reader.fieldnames - clist = set() + header_fields = set() for field in fields: if field.strip() in headers.keys(): headers[field.strip()] = field - clist.add(field.strip()) - if clist != headers.keys(): + header_fields.add(field.strip()) + if header_fields != set(headers.keys()): return False return True -- cgit From 589c5ecbd4fcafffa2e237012c4c2124b53dca0a Mon Sep 17 00:00:00 2001 From: prathamesh Date: Tue, 7 Nov 2017 13:47:50 +0530 Subject: Removed nested try..except --- yaksh/file_utils.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'yaksh/file_utils.py') diff --git a/yaksh/file_utils.py b/yaksh/file_utils.py index 0d80b8f..4b8640e 100644 --- a/yaksh/file_utils.py +++ b/yaksh/file_utils.py @@ -53,11 +53,10 @@ def extract_files(zip_file, path=None): def is_csv(document): try: - try: - content = document.read(1024).decode('utf-8') - except AttributeError: - document.seek(0) - content = document.read(1024) + content = document.read(1024).decode('utf-8') + except AttributeError: + document.seek(0) + content = document.read(1024) sniffer = csv.Sniffer() dialect = sniffer.sniff(content) document.seek(0) -- cgit From 2eeddaf3bc2eaf9af3fe3e08caec12ff8bc9071a Mon Sep 17 00:00:00 2001 From: prathamesh Date: Tue, 7 Nov 2017 14:16:44 +0530 Subject: Need nested try..except in is_csv --- yaksh/file_utils.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'yaksh/file_utils.py') diff --git a/yaksh/file_utils.py b/yaksh/file_utils.py index 4b8640e..0d80b8f 100644 --- a/yaksh/file_utils.py +++ b/yaksh/file_utils.py @@ -53,10 +53,11 @@ def extract_files(zip_file, path=None): def is_csv(document): try: - content = document.read(1024).decode('utf-8') - except AttributeError: - document.seek(0) - content = document.read(1024) + try: + content = document.read(1024).decode('utf-8') + except AttributeError: + document.seek(0) + content = document.read(1024) sniffer = csv.Sniffer() dialect = sniffer.sniff(content) document.seek(0) -- cgit From 7c755d00d9c22a156c203f3f2b3312bdb583b5ce Mon Sep 17 00:00:00 2001 From: prathamesh Date: Fri, 10 Nov 2017 16:04:32 +0530 Subject: Optimised the code and added dialect while reading to handle different delimiters --- yaksh/file_utils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'yaksh/file_utils.py') diff --git a/yaksh/file_utils.py b/yaksh/file_utils.py index 0d80b8f..7aef249 100644 --- a/yaksh/file_utils.py +++ b/yaksh/file_utils.py @@ -52,6 +52,7 @@ def extract_files(zip_file, path=None): def is_csv(document): + ''' Check if document is csv with ',' as the delimiter''' try: try: content = document.read(1024).decode('utf-8') @@ -62,8 +63,8 @@ def is_csv(document): dialect = sniffer.sniff(content) document.seek(0) except (csv.Error, UnicodeDecodeError): - return False - return True + return False, None + return True, dialect def headers_present(dict_reader, headers): -- cgit From dadf1d1148982c841b5002fe0cc26a82d9d34c95 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Mon, 13 Nov 2017 16:47:56 +0530 Subject: csv can now take extra fields related to profile. Updates if the username is same. Additional csv header "remove" added, to remove user from the course. --- yaksh/file_utils.py | 12 ------------ 1 file changed, 12 deletions(-) (limited to 'yaksh/file_utils.py') diff --git a/yaksh/file_utils.py b/yaksh/file_utils.py index 7aef249..c53c2dc 100644 --- a/yaksh/file_utils.py +++ b/yaksh/file_utils.py @@ -66,15 +66,3 @@ def is_csv(document): return False, None return True, dialect - -def headers_present(dict_reader, headers): - fields = dict_reader.fieldnames - header_fields = set() - for field in fields: - if field.strip() in headers.keys(): - headers[field.strip()] = field - header_fields.add(field.strip()) - if header_fields != set(headers.keys()): - return False - return True - -- cgit From 29e358a6fcd438fa7ddf3d3bccb2af1839158ba6 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Tue, 14 Nov 2017 17:49:35 +0530 Subject: Added ',' as the specific delimiter for the csv upload --- yaksh/file_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'yaksh/file_utils.py') diff --git a/yaksh/file_utils.py b/yaksh/file_utils.py index c53c2dc..b178eeb 100644 --- a/yaksh/file_utils.py +++ b/yaksh/file_utils.py @@ -60,7 +60,7 @@ def is_csv(document): document.seek(0) content = document.read(1024) sniffer = csv.Sniffer() - dialect = sniffer.sniff(content) + dialect = sniffer.sniff(content, delimiters=',') document.seek(0) except (csv.Error, UnicodeDecodeError): return False, None -- cgit