mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-20 10:58:33 +00:00
[sysadmin] Refactor user import
This commit is contained in:
@@ -197,3 +197,8 @@ class SharedLinkPasswordForm(forms.Form):
|
|||||||
|
|
||||||
return self.cleaned_data
|
return self.cleaned_data
|
||||||
|
|
||||||
|
class BatchAddUserForm(forms.Form):
|
||||||
|
"""
|
||||||
|
Form for importing users from CSV file.
|
||||||
|
"""
|
||||||
|
file = forms.FileField()
|
||||||
|
@@ -33,13 +33,11 @@
|
|||||||
<input type="submit" value="{% trans "Submit" %}" class="submit" />
|
<input type="submit" value="{% trans "Submit" %}" class="submit" />
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<form id="upload-file-form" class="hide" enctype= "multipart/form-data" method = "post" action = "{{ SITE_ROOT }}useradmin/batchadduser/">{% csrf_token %}
|
<form id="upload-csv-form" class="hide" enctype= "multipart/form-data" method = "post" action = "{% url 'batch_add_user' %}">{% csrf_token %}
|
||||||
<h3>{% trans "Add user from local file" %}</h3><br />
|
<h3>{% trans "Import user from CSV file" %}</h3><br />
|
||||||
<input type = "file" name = "file" /><br />
|
<input type = "file" name = "file" /><br />
|
||||||
<p class="tip">{% trans "1. please upload a 'csv' format file;"%}</p>
|
<p class="tip">{% trans "CSV file format: user@mail.com, password"%}</p>
|
||||||
<p class="tip">{% trans "2. row 1, column 1 for username title;"%}</p>
|
<input type="submit" value="{% trans "Submit" %}" class="submit" />
|
||||||
<p class="tip">{% trans "3. row 1, column 2 for password title;"%}</p>
|
|
||||||
<input type = "submit" value = "submit"/>
|
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
{% include "sysadmin/useradmin_table.html"%}
|
{% include "sysadmin/useradmin_table.html"%}
|
||||||
@@ -122,7 +120,7 @@ $('#add-user-form').submit(function() {
|
|||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
$('#upload-file-btn').click(function () {
|
$('#upload-file-btn').click(function () {
|
||||||
$('#upload-file-form').modal();
|
$('#upload-csv-form').modal();
|
||||||
});
|
});
|
||||||
{% include "sysadmin/useradmin_js.html" %}
|
{% include "sysadmin/useradmin_js.html" %}
|
||||||
</script>
|
</script>
|
||||||
|
@@ -23,9 +23,9 @@ from seahub.base.accounts import User
|
|||||||
from seahub.base.models import UserLastLogin
|
from seahub.base.models import UserLastLogin
|
||||||
from seahub.base.decorators import sys_staff_required
|
from seahub.base.decorators import sys_staff_required
|
||||||
from seahub.auth.decorators import login_required
|
from seahub.auth.decorators import login_required
|
||||||
from seahub.utils import IS_EMAIL_CONFIGURED, string2list, is_valid_email
|
from seahub.utils import IS_EMAIL_CONFIGURED, string2list, is_valid_username
|
||||||
from seahub.views import get_system_default_repo_id
|
from seahub.views import get_system_default_repo_id
|
||||||
from seahub.forms import SetUserQuotaForm, AddUserForm
|
from seahub.forms import SetUserQuotaForm, AddUserForm, BatchAddUserForm
|
||||||
from seahub.profile.models import Profile, DetailedProfile
|
from seahub.profile.models import Profile, DetailedProfile
|
||||||
from seahub.share.models import FileShare
|
from seahub.share.models import FileShare
|
||||||
|
|
||||||
@@ -854,62 +854,43 @@ def batch_user_make_admin(request):
|
|||||||
@login_required
|
@login_required
|
||||||
@sys_staff_required
|
@sys_staff_required
|
||||||
def batch_add_user(request):
|
def batch_add_user(request):
|
||||||
|
"""Batch add users. Import users from CSV file.
|
||||||
|
"""
|
||||||
if request.method != 'POST':
|
if request.method != 'POST':
|
||||||
raise Http404
|
raise Http404
|
||||||
|
|
||||||
next = request.META.get('HTTP_REFERER', None)
|
form = BatchAddUserForm(request.POST, request.FILES)
|
||||||
if not next:
|
if form.is_valid():
|
||||||
next = reverse(sys_user_admin)
|
content = request.FILES['file'].read()
|
||||||
|
|
||||||
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']
|
encoding = chardet.detect(content)['encoding']
|
||||||
if encoding != 'utf-8':
|
if encoding != 'utf-8':
|
||||||
content = content.decode(encoding, 'replace').encode('utf-8')
|
content = content.decode(encoding, 'replace').encode('utf-8')
|
||||||
|
|
||||||
filestream = StringIO.StringIO(content)
|
filestream = StringIO.StringIO(content)
|
||||||
|
reader = csv.reader(filestream)
|
||||||
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:
|
for row in reader:
|
||||||
email = row[username_title]
|
if not row:
|
||||||
password = row[password_title]
|
continue
|
||||||
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)
|
username = row[0].strip()
|
||||||
for item in failed:
|
password = row[1].strip()
|
||||||
messages.error(request, _(u"%s: add '%s' failed.") % (item[1], item[0]))
|
|
||||||
|
if not is_valid_username(username):
|
||||||
|
continue
|
||||||
|
|
||||||
|
if password == '':
|
||||||
|
continue
|
||||||
|
|
||||||
|
try:
|
||||||
|
User.objects.get(email=username)
|
||||||
|
continue
|
||||||
|
except User.DoesNotExist:
|
||||||
|
User.objects.create_user(username, password, is_staff=False,
|
||||||
|
is_active=True)
|
||||||
|
messages.success(request, _('Success'))
|
||||||
else:
|
else:
|
||||||
messages.error(request, _(u'Please select a csv file first.'))
|
messages.error(request, _(u'Please select a csv file first.'))
|
||||||
|
|
||||||
|
next = request.META.get('HTTP_REFERER', reverse(sys_user_admin))
|
||||||
return HttpResponseRedirect(next)
|
return HttpResponseRedirect(next)
|
||||||
|
Reference in New Issue
Block a user