summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas NOEL <tnoel@entrouvert.com>2014-01-17 14:58:46 (GMT)
committerThomas NOEL <tnoel@entrouvert.com>2014-01-17 14:58:46 (GMT)
commit55ef6d1ffec252e5e33ae4d038a3a4f13e5332cc (patch)
treed75e4c23c3bc8aac6c1234c4a06d6bbed468d0b3
parent6eeb61de5cf1f9710f4e26068a2d9fbe59663899 (diff)
downloadunivnautes-old-55ef6d1ffec252e5e33ae4d038a3a4f13e5332cc.zip
univnautes-old-55ef6d1ffec252e5e33ae4d038a3a4f13e5332cc.tar.gz
univnautes-old-55ef6d1ffec252e5e33ae4d038a3a4f13e5332cc.tar.bz2
pfidp/users_admin: csv import
-rw-r--r--virtualenv/pfidp/templates/users_admin/users.csv2
-rw-r--r--virtualenv/pfidp/users_admin/pfusers.py2
-rw-r--r--virtualenv/pfidp/users_admin/views.py46
3 files changed, 41 insertions, 9 deletions
diff --git a/virtualenv/pfidp/templates/users_admin/users.csv b/virtualenv/pfidp/templates/users_admin/users.csv
index ecaaa46..23c23e5 100644
--- a/virtualenv/pfidp/templates/users_admin/users.csv
+++ b/virtualenv/pfidp/templates/users_admin/users.csv
@@ -1,2 +1,2 @@
"login","nom complet","expiration","mot de passe"{% for user in users %}
-"{{ user.name|addslashes }}","{{ user.descr|safe|addslashes }} ","{{ user.expires|date:"c" }}","{{ user.password|default_if_none:"-"|safe|addslashes }} "{% endfor %}
+"{{ user.name|addslashes }}","{{ user.descr|safe|addslashes }} ","{{ user.expires|date:"c" }}","{{ user.password|default_if_none:"-"|safe|addslashes }}"{% endfor %}
diff --git a/virtualenv/pfidp/users_admin/pfusers.py b/virtualenv/pfidp/users_admin/pfusers.py
index 4148036..abc355b 100644
--- a/virtualenv/pfidp/users_admin/pfusers.py
+++ b/virtualenv/pfidp/users_admin/pfusers.py
@@ -186,7 +186,7 @@ def activate(username):
def delete(username):
return call('delete', name=username)
-def update(username, password, expires, disabled, descr, multiple):
+def update(username, password, expires, disabled=False, descr='', multiple=False):
if isinstance(expires, datetime.date):
expires_str = expires.strftime('%m/%d/%Y')
else:
diff --git a/virtualenv/pfidp/users_admin/views.py b/virtualenv/pfidp/users_admin/views.py
index a3879e0..7b4cab5 100644
--- a/virtualenv/pfidp/users_admin/views.py
+++ b/virtualenv/pfidp/users_admin/views.py
@@ -1,6 +1,9 @@
# views for user admin
# -*- encoding: utf-8 -*-
+import datetime
+import csv
+
from django.conf import settings
from django.shortcuts import render_to_response, redirect
from django.contrib.auth.decorators import user_passes_test
@@ -10,7 +13,6 @@ from django.http import HttpResponse
import pfusers
from .forms import UserForm, NewUserForm, ConfirmForm, UploadFileForm
-import datetime
@user_passes_test(lambda user: user.is_staff, login_url='/logout')
def index(request):
@@ -249,7 +251,7 @@ def multiple(request):
return render_to_response('users_admin/read-list.html',
{ 'users': users, }, context_instance=RequestContext(request))
if action == "csv":
- return csv(users)
+ return csv_export(users)
request.session['univnautes_idpua_action'] = action
request.session['univnautes_idpua_names'] = names
form = ConfirmForm()
@@ -290,7 +292,7 @@ def multiple(request):
return redirect('.')
-def csv(users):
+def csv_export(users):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="users.csv"'
t = loader.get_template('users_admin/users.csv')
@@ -304,11 +306,12 @@ def csv_import(request):
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
try:
- new_users = import_csv_file(request.FILES['file'])
+ created_users, updated_users = import_csv_file(request.FILES['file'])
except Exception as e:
messages.error(request, u'Import du fichier impossible, erreur : %s' % e)
else:
- messages.success(request, u'%d utilisateurs importés' % new_users)
+ messages.success(request, u'%d utilisateurs créés, %d mis à jour' % \
+ (created_users, updated_users))
return redirect('.')
else:
form = UploadFileForm()
@@ -321,5 +324,34 @@ def import_csv_file(f):
with open(filename, 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
- raise Exception("TBD")
- return 0
+ csvfile = open(filename, 'rb')
+ dialect = csv.Sniffer().sniff(csvfile.read(1024))
+ csvfile.seek(0)
+ reader = csv.reader(csvfile, dialect)
+ created_users = []
+ updated_users = []
+ all_pfusers = pfusers.get_all_pfusers()
+
+ # analyse all the file, then create (if no exception)
+
+ headers = reader.next()
+ for row in reader:
+ user = {
+ 'username': row[0].strip(),
+ 'descr': row[1].strip(),
+ 'expires': datetime.datetime.strptime(row[2],'%Y-%m-%d').date(),
+ 'password': row[3].strip() or None
+ }
+ if user['username'] in all_pfusers:
+ updated_users.append(user)
+ else:
+ created_users.append(user)
+ csvfile.close()
+
+ for user in created_users:
+ pfusers.create(**user)
+ for user in updated_users:
+ pfusers.update(**user)
+
+ return len(created_users), len(updated_users)
+