mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-14 06:11:16 +00:00
Merge branch 'add-user-from-csv-file' of git://github.com/imwhatiam/seahub into imwhatiam-add-user-from-csv-file
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
<li class="tabnav-tab"><a href="{% url 'sys_useradmin_admins' %}">{% trans "Admins" %}</a></li>
|
||||
</ul>
|
||||
<button id="add-user-btn" class="fright"><img src="{{ MEDIA_URL }}img/add.png" alt="" class="add vam" /><span class="vam">{% trans "Add user" %}</span></button>
|
||||
<button id="upload-file-btn" class="fright"><span class="icon-upload-alt"></span>{% trans "Upload csv file"%}</button>
|
||||
</div>
|
||||
|
||||
<form id="add-user-form" action="" method="post" class="hide">{% csrf_token %}
|
||||
@@ -32,6 +33,15 @@
|
||||
<input type="submit" value="{% trans "Submit" %}" class="submit" />
|
||||
</form>
|
||||
|
||||
<form id="upload-file-form" class="hide" enctype= "multipart/form-data" method = "post" action = "{{ SITE_ROOT }}useradmin/batchadduser/">{% csrf_token %}
|
||||
<h3>{% trans "Add user from local file" %}</h3><br />
|
||||
<input type = "file" name = "file" /><br />
|
||||
<p class="tip">{% trans "1. please upload a 'csv' format file;"%}</p>
|
||||
<p class="tip">{% trans "2. row 1, column 1 for username title;"%}</p>
|
||||
<p class="tip">{% trans "3. row 1, column 2 for password title;"%}</p>
|
||||
<input type = "submit" value = "submit"/>
|
||||
</form>
|
||||
|
||||
{% include "sysadmin/useradmin_table.html"%}
|
||||
{% include "snippets/admin_paginator.html" %}
|
||||
|
||||
@@ -111,6 +121,9 @@ $('#add-user-form').submit(function() {
|
||||
});
|
||||
return false;
|
||||
});
|
||||
$('#upload-file-btn').click(function () {
|
||||
$('#upload-file-form').modal();
|
||||
});
|
||||
{% include "sysadmin/useradmin_js.html" %}
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
@@ -195,7 +195,7 @@ urlpatterns = patterns('',
|
||||
url(r'^useradmin/password/reset/(?P<user_id>[^/]+)/$', user_reset, name='user_reset'),
|
||||
|
||||
url(r'^useradmin/batchmakeadmin/$', batch_user_make_admin, name='batch_user_make_admin'),
|
||||
|
||||
url(r'^useradmin/batchadduser/$', batch_add_user, name='batch_add_user'),
|
||||
)
|
||||
|
||||
if settings.SERVE_STATIC:
|
||||
|
@@ -6,6 +6,7 @@ import logging
|
||||
import simplejson as json
|
||||
import re
|
||||
import datetime
|
||||
import csv, chardet, StringIO
|
||||
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.contrib import messages
|
||||
@@ -22,7 +23,7 @@ from seahub.base.accounts import User
|
||||
from seahub.base.models import UserLastLogin
|
||||
from seahub.base.decorators import sys_staff_required
|
||||
from seahub.auth.decorators import login_required
|
||||
from seahub.utils import IS_EMAIL_CONFIGURED, string2list
|
||||
from seahub.utils import IS_EMAIL_CONFIGURED, string2list, is_valid_email
|
||||
from seahub.views import get_system_default_repo_id
|
||||
from seahub.forms import SetUserQuotaForm, AddUserForm
|
||||
from seahub.profile.models import Profile, DetailedProfile
|
||||
@@ -849,3 +850,66 @@ def batch_user_make_admin(request):
|
||||
|
||||
result['success'] = True
|
||||
return HttpResponse(json.dumps(result), content_type=content_type)
|
||||
|
||||
@login_required
|
||||
@sys_staff_required
|
||||
def batch_add_user(request):
|
||||
|
||||
if request.method != 'POST':
|
||||
raise Http404
|
||||
|
||||
next = request.META.get('HTTP_REFERER', None)
|
||||
if not next:
|
||||
next = reverse(sys_user_admin)
|
||||
|
||||
if request.FILES.has_key('file'):
|
||||
f = request.FILES['file']
|
||||
|
||||
if f.name[-3:] != "csv":
|
||||
messages.error(request, _(u'Failed! Please upload a csv format file.'))
|
||||
return HttpResponseRedirect(next)
|
||||
|
||||
content = f.read()
|
||||
encoding = chardet.detect(content)['encoding']
|
||||
if encoding != 'utf-8':
|
||||
content = content.decode(encoding, 'replace').encode('utf-8')
|
||||
|
||||
filestream = StringIO.StringIO(content)
|
||||
|
||||
lines_of_csv = filestream.read().splitlines()
|
||||
dialect = csv.Sniffer().sniff(content)
|
||||
reader = csv.DictReader(lines_of_csv, dialect=dialect)
|
||||
|
||||
username_title = lines_of_csv[0].split(',')[0]
|
||||
password_title = lines_of_csv[0].split(',')[1]
|
||||
|
||||
if username_title == password_title or username_title == "" or password_title == "":
|
||||
messages.error(request, _(u'Failed! wrong csv format.'))
|
||||
return HttpResponseRedirect(next)
|
||||
|
||||
failed = {}
|
||||
|
||||
for row in reader:
|
||||
email = row[username_title]
|
||||
password = row[password_title]
|
||||
if is_valid_email(email):
|
||||
try:
|
||||
User.objects.get(email=email)
|
||||
failed[email] = 'Already exist'
|
||||
continue
|
||||
except User.DoesNotExist:
|
||||
if password != '':
|
||||
User.objects.create_user(email, password, is_staff=False,
|
||||
is_active=True)
|
||||
else:
|
||||
failed[email] = 'Has no password'
|
||||
else:
|
||||
failed[email] = 'Unvalid email address'
|
||||
|
||||
failed = sorted(failed.items(),key=lambda e:e[1],reverse=True)
|
||||
for item in failed:
|
||||
messages.error(request, _(u"%s: add '%s' failed.") % (item[1], item[0]))
|
||||
else:
|
||||
messages.error(request, _(u'Please select a csv file first.'))
|
||||
|
||||
return HttpResponseRedirect(next)
|
||||
|
Reference in New Issue
Block a user