1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-12 13:24:52 +00:00

[API2] add login_id field, and test case

[api] update account and test

[template] increase the function of modifying logind

[review] trans

[review] trans again
This commit is contained in:
zming
2017-05-19 23:18:23 +08:00
parent dda3ac0c48
commit 1ed03f96e1
3 changed files with 146 additions and 3 deletions

View File

@@ -30,6 +30,7 @@ json_content_type = 'application/json; charset=utf-8'
def get_account_info(user):
email = user.username
d_profile = DetailedProfile.objects.get_detailed_profile_by_user(email)
profile = Profile.objects.get_profile_by_user(email)
info = {}
info['email'] = email
@@ -39,6 +40,7 @@ def get_account_info(user):
info['is_staff'] = user.is_staff
info['is_active'] = user.is_active
info['create_time'] = user.ctime
info['login_id'] = profile.login_id if profile else ''
info['total'] = seafile_api.get_user_quota(email)
info['usage'] = seafile_api.get_user_self_usage(email)
@@ -108,10 +110,18 @@ class Account(APIView):
profile = Profile.objects.get_profile_by_user(email)
if profile is None:
profile = Profile(user=email)
profile.nickname = name
profile.save()
# update account loginid
loginid = request.data.get("login_id", '').strip()
if loginid != '':
profile = Profile.objects.get_profile_by_user(email)
if profile is None:
profile = Profile(user=email)
profile.login_id = loginid
profile.save()
# update account detailed profile
department = request.data.get("department", None)
if department is not None:
@@ -165,6 +175,18 @@ class Account(APIView):
return api_error(status.HTTP_400_BAD_REQUEST,
_(u"Name should not include '/'."))
#argument check for loginid
loginid = request.data.get("login_id", None)
if loginid is not None:
loginid = loginid.strip()
if loginid == "":
return api_error(status.HTTP_400_BAD_REQUEST,
_(u"Login id can't be empty"))
usernamebyloginid = Profile.objects.get_username_by_login_id(loginid)
if usernamebyloginid is not None:
return api_error(status.HTTP_400_BAD_REQUEST,
_(u"Login id %s already exists." % loginid))
# argument check for department
department = request.data.get("department", None)
if department is not None:

View File

@@ -53,6 +53,18 @@
<span id="set-name" title="{% trans "Edit"%}" class="sf2-icon-edit op-icon"></span>
</dd>
<dt>{% trans "Login ID" %}</dt>
<dd>
<span id="loginid">
{% if profile and profile.login_id %}
{{ profile.login_id }}
{% else %}
--
{% endif %}
</span>
<span id="set-loginid" title="{% trans "Edit"%}" class="sf2-icon-edit op-icon"></span>
</dd>
<dt>{% trans "Department" %}</dt>
<dd>
<span id="department">
@@ -98,6 +110,13 @@
<input type="submit" value="{% trans "Submit" %}" class="submit" />
</form>
<form id="set-loginid-form" method="post" action="" class="hide">{% csrf_token %}
<h3>{% trans "Set user Login ID" %}</h3>
<input type="text" name="loginid" class="input" value="" /><br />
<p class="error hide"></p>
<input type="submit" value="{% trans "Submit" %}" class="submit" />
</form>
<form id="set-quota-form" method="post" class="hide">{% csrf_token %}
<h3>{% trans "Set user storage limit" %}</h3>
<input type="text" name="space_quota" class="input" /> MB
@@ -300,6 +319,10 @@ $('#set-name').click(function() {
$("#set-name-form").modal({appendTo: "#main"});
$('#simplemodal-container').css({'width':'auto', 'height':'auto'});
});
$('#set-loginid').click(function () {
$("#set-loginid-form").modal({appendTo:"#main"});
$('#simplemodal-container').css({'width':'auto', 'height':'auto'});
})
$('#set-dept').click(function() {
$("#set-dept-form").modal({appendTo: "#main"});
$('#simplemodal-container').css({'width':'auto', 'height':'auto'});
@@ -351,6 +374,43 @@ $('#set-name-form').submit(function() {
return false;
});
$('#set-loginid-form').submit(function() {
var loginid = $.trim($('[name="loginid"]', $(this)).val());
var $loginid = $('#loginid');
var $error = $('.error', $(this));
var $submitBtn = $('[type="submit"]', $(this));
if (!loginid){
$error.html("{% trans "Login id can't be empty" %}").show();
return false;
}
disable($submitBtn);
$.ajax({
url: '{% url 'api2-account' email %}',
type: 'PUT',
dataType: 'json',
cache: false,
beforeSend: prepareCSRFToken,
data: {'login_id': loginid},
success: function(data) {
$loginid.html(HTMLescape(data['login_id']));
$.modal.close();
},
error: function(xhr, textStatus, errorThrown) {
var err_msg;
if (xhr.responseText) {
err_msg = $.parseJSON(xhr.responseText).error_msg;
} else {
err_msg = "{% trans "Failed. Please check the network." %}";
}
$error.html(err_msg).show();
enable($submitBtn);
}
});
return false;
});
$('#set-dept-form').submit(function() {
var department = $.trim($('[name="department"]', $(this)).val());
var $department = $('#department');

View File

@@ -45,7 +45,40 @@ class AccountTest(BaseTestCase):
def _do_update(self):
return self.client.put(
reverse('api2-account', args=[self.user1.username]),
'password=654321&is_staff=1&is_active=0&name=user1&storage=102400',
'password=654321&is_staff=1&is_active=0&name=user1&storage=102400&login_id=hello',
'application/x-www-form-urlencoded',
)
def _do_update_name(self):
return self.client.put(
reverse('api2-account', args=[self.user1.username]),
'name=user1',
'application/x-www-form-urlencoded',
)
def _do_update_loginid(self):
return self.client.put(
reverse('api2-account', args=[self.user1.username]),
'login_id=hello',
'application/x-www-form-urlencoded',
)
def _do_update_loginid_useemptystring(self):
return self.client.put(
reverse('api2-account', args=[self.user1.username]),
'login_id=',
'application/x-www-form-urlencoded',
)
def _do_update_loginid_sendagain(self):
self.client.put(
reverse('api2-account', args=[self.user1.username]),
'login_id=test',
'application/x-www-form-urlencoded',
)
return self.client.put(
reverse('api2-account', args=[self.user1.username]),
'login_id=test',
'application/x-www-form-urlencoded',
)
@@ -77,7 +110,7 @@ class AccountTest(BaseTestCase):
resp = self._do_get_info()
json_resp = json.loads(resp.content)
assert len(json_resp) == 9
assert len(json_resp) == 10
assert json_resp['email'] == self.user1.username
assert json_resp['is_staff'] is False
assert json_resp['is_active'] is True
@@ -99,11 +132,39 @@ class AccountTest(BaseTestCase):
'654321'))
self.assertTrue(User.objects.get(self.user1.username).is_staff)
self.assertFalse(User.objects.get(self.user1.username).is_active)
self.assertEqual(Profile.objects.get_profile_by_user(
self.user1.username).login_id, 'hello')
self.assertEqual(Profile.objects.get_profile_by_user(
self.user1.username).nickname, 'user1')
self.assertEqual(seafile_api.get_user_quota(
self.user1.username), 102400000000)
def test_update_name(self):
"""only test name"""
self.login_as(self.admin)
resp = self._do_update_name()
self.assertEqual(Profile.objects.get_profile_by_user(
self.user1.username).nickname, 'user1')
def test_update_loginid(self):
"""only test loginid"""
self.login_as(self.admin)
resp = self._do_update_loginid()
self.assertEqual(Profile.objects.get_profile_by_user(
self.user1.username).login_id, 'hello')
def test_update_loginid_useemptystring(self):
"""test loginid, longid send the empty"""
self.login_as(self.admin)
resp = self._do_update_loginid_useemptystring()
self.assertEqual(400, resp.status_code)
def test_update_loginid_sendagain(self):
"""test loginid,sent twice"""
self.login_as(self.admin)
resp = self._do_update_loginid_sendagain()
self.assertEqual(400, resp.status_code)
def test_refresh_profile_cache_after_update(self):
self.login_as(self.admin)
self.assertEqual(email2nickname(self.user1.username),