summaryrefslogtreecommitdiffstats
path: root/virtualenv
diff options
context:
space:
mode:
authorThomas NOEL <tnoel@entrouvert.com>2014-01-16 17:21:34 (GMT)
committerThomas NOEL <tnoel@entrouvert.com>2014-01-16 17:21:34 (GMT)
commit4488d6a11985a666c24f626319c2ebc7ac1f342e (patch)
treea67adc482d5a2c521659dbef161f822b248e704e /virtualenv
parenteb1506988a3aaa1fa7ec2c36c57fa7ffbc0030e6 (diff)
downloadunivnautes-old-4488d6a11985a666c24f626319c2ebc7ac1f342e.zip
univnautes-old-4488d6a11985a666c24f626319c2ebc7ac1f342e.tar.gz
univnautes-old-4488d6a11985a666c24f626319c2ebc7ac1f342e.tar.bz2
pfidp/users_admin: csv import, first draft (tbd)
Diffstat (limited to 'virtualenv')
-rw-r--r--virtualenv/pfidp/templates/users_admin/import.html46
-rw-r--r--virtualenv/pfidp/templates/users_admin/index.html6
-rw-r--r--virtualenv/pfidp/users_admin/forms.py3
-rw-r--r--virtualenv/pfidp/users_admin/urls.py1
-rw-r--r--virtualenv/pfidp/users_admin/views.py27
5 files changed, 80 insertions, 3 deletions
diff --git a/virtualenv/pfidp/templates/users_admin/import.html b/virtualenv/pfidp/templates/users_admin/import.html
new file mode 100644
index 0000000..de82398
--- /dev/null
+++ b/virtualenv/pfidp/templates/users_admin/import.html
@@ -0,0 +1,46 @@
+{% extends "users_admin/base.html" %}
+{% load i18n %}
+{% load staticfiles %}
+
+{% block title %}Importer des utilisateus{% endblock %}
+
+{% block content %}
+
+<h1>Importer des utilisateurs</h1>
+
+<br />
+
+<form action="" method="post" enctype="multipart/form-data">
+ {% csrf_token %}
+ <table>
+ {{ form.as_table }}
+ </table>
+
+ <div class="row">
+ <div class="span12">
+ <h3>Format du fichier attendu : CSV</h3>
+ <ul>
+ <li>4 colonnes : login, nom complet, date d'expiration, mot de passe ;</li>
+ <li>date au format AAAA-MM-DD ;</li>
+ <li>si le mot de passe n'est pas fourni, un mot de passe sera généré ;</li>
+ <li>si l'utilisateur existe déjà, il sera mis à jour ;</li>
+ <li>séparateur : la virgule ;</li>
+ <li>codage UTF-8 ;</li>
+ <li>la première ligne ne sera pas prise en compte.</li>
+ </ul>
+ <h3>Exemple</h3>
+ <pre class="span6">
+"login","nom complet","expiration","mot de passe"
+"joe","Joseph","2016-01-01","GxLGFP2"
+"bar","Anne Honyme","2010-01-01",""
+"team","","2014-07-22",""
+</pre>
+ </div></div>
+
+ <div class="form-actions">
+ <input type="submit" value="Importer" class="btn btn-primary" />
+ <a href="./" class="btn"><i class="icon-remove"></i> Annuler</a>
+ </div>
+</form>
+
+{% endblock %}
diff --git a/virtualenv/pfidp/templates/users_admin/index.html b/virtualenv/pfidp/templates/users_admin/index.html
index f93adb8..2771414 100644
--- a/virtualenv/pfidp/templates/users_admin/index.html
+++ b/virtualenv/pfidp/templates/users_admin/index.html
@@ -5,7 +5,8 @@
{% block title %}Accueil{% endblock %}
{% block nav %}
- <li><a href="create">Créer des utilisateurs</a></li>
+ <li><a href="create"><i class="icon-plus"></i> Créer des utilisateurs</a></li>
+ <li><a href="import"><i class="icon-download"></i> Importer des utilisateurs</a></li>
{% endblock %}
{% block title_content %}
@@ -80,7 +81,8 @@
<div class="form-actions">
-<a href="create" class="btn">Créer des utilisateurs</a>
+<a href="create" class="btn"><i class="icon-plus"></i> Créer des utilisateurs</a>
+<a href="import" class="btn"><i class="icon-download"></i> Importer des utilisateurs</a>
<div class=" pull-right">
<input type="submit" value="Ok" class="btn pull-right" />
<select name="action" class="pull-right">
diff --git a/virtualenv/pfidp/users_admin/forms.py b/virtualenv/pfidp/users_admin/forms.py
index 1d67379..d5655be 100644
--- a/virtualenv/pfidp/users_admin/forms.py
+++ b/virtualenv/pfidp/users_admin/forms.py
@@ -8,6 +8,9 @@ import pfusers
class ConfirmForm(forms.Form):
pass
+class UploadFileForm(forms.Form):
+ file = forms.FileField(label=u"Fichier", required=True)
+
class UserForm(forms.Form):
name = forms.RegexField(label=u"Nom d'utilisateur (login)", regex='^[a-z0-9\.\-_]+$', min_length=3, max_length=16, required=True,
widget=forms.TextInput(attrs={'readonly': True, 'size':'16', 'autocomplete':'off', 'class':'span2'}))
diff --git a/virtualenv/pfidp/users_admin/urls.py b/virtualenv/pfidp/users_admin/urls.py
index bd026a2..e6cc4a4 100644
--- a/virtualenv/pfidp/users_admin/urls.py
+++ b/virtualenv/pfidp/users_admin/urls.py
@@ -12,5 +12,6 @@ urlpatterns = patterns('',
(r'^desactivate/(?P<name>[a-z0-9\.\-_]+)$', login_required(views.desactivate)),
(r'^activate/(?P<name>[a-z0-9\.\-_]+)$', login_required(views.activate)),
(r'^multiple$', login_required(views.multiple)),
+ (r'^import$', login_required(views.csv_import)),
)
diff --git a/virtualenv/pfidp/users_admin/views.py b/virtualenv/pfidp/users_admin/views.py
index b490038..da3dd80 100644
--- a/virtualenv/pfidp/users_admin/views.py
+++ b/virtualenv/pfidp/users_admin/views.py
@@ -9,7 +9,7 @@ from django.contrib import messages
from django.http import HttpResponse
import pfusers
-from forms import UserForm, NewUserForm, ConfirmForm
+from .forms import UserForm, NewUserForm, ConfirmForm, UploadFileForm
import datetime
@user_passes_test(lambda user: user.is_staff, login_url='/logout')
@@ -294,3 +294,28 @@ def csv(users):
response.write(t.render(c))
return response
+@user_passes_test(lambda user: user.is_staff, login_url='/logout')
+def csv_import(request):
+ if request.method == 'POST':
+ form = UploadFileForm(request.POST, request.FILES)
+ if form.is_valid():
+ try:
+ new_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)
+ return redirect('.')
+ else:
+ form = UploadFileForm()
+ return render_to_response('users_admin/import.html',
+ {'form': form},
+ context_instance=RequestContext(request))
+
+def import_csv_file(f):
+ filename = '/var/tmp/users-import.csv'
+ with open(filename, 'wb+') as destination:
+ for chunk in f.chunks():
+ destination.write(chunk)
+ raise Exception("TBD")
+ return 0