mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-08 18:30:53 +00:00
Userinfo contactemail (#1894)
* Show contact_email on user profile I think it would be good to have the edit functionality for that attribute, too, but I did not implement that right now. It's marked with TODO though. * update api and tests * review
This commit is contained in:
@@ -6,6 +6,7 @@ from rest_framework.authentication import SessionAuthentication
|
|||||||
from rest_framework.permissions import IsAdminUser
|
from rest_framework.permissions import IsAdminUser
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
|
from django.core.cache import cache
|
||||||
from django.core.urlresolvers import reverse
|
from django.core.urlresolvers import reverse
|
||||||
from django.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _
|
||||||
|
|
||||||
@@ -20,8 +21,9 @@ from seahub.base.accounts import User
|
|||||||
from seahub.base.templatetags.seahub_tags import email2nickname, \
|
from seahub.base.templatetags.seahub_tags import email2nickname, \
|
||||||
email2contact_email
|
email2contact_email
|
||||||
from seahub.profile.models import Profile, DetailedProfile
|
from seahub.profile.models import Profile, DetailedProfile
|
||||||
|
from seahub.profile.settings import CONTACT_CACHE_TIMEOUT, CONTACT_CACHE_PREFIX
|
||||||
from seahub.utils import is_valid_username, is_org_context, \
|
from seahub.utils import is_valid_username, is_org_context, \
|
||||||
is_pro_version
|
is_pro_version, normalize_cache_key, is_valid_email
|
||||||
from seahub.utils.timeutils import timestamp_to_isoformat_timestr
|
from seahub.utils.timeutils import timestamp_to_isoformat_timestr
|
||||||
from seahub.utils.file_size import get_file_size_unit
|
from seahub.utils.file_size import get_file_size_unit
|
||||||
from seahub.role_permissions.utils import get_available_roles
|
from seahub.role_permissions.utils import get_available_roles
|
||||||
@@ -58,23 +60,21 @@ def update_user_info(request, user):
|
|||||||
if role:
|
if role:
|
||||||
User.objects.update_role(email, role)
|
User.objects.update_role(email, role)
|
||||||
|
|
||||||
name = request.data.get("name")
|
nickname = request.data.get("name", None)
|
||||||
if name:
|
if nickname is not None:
|
||||||
profile = Profile.objects.get_profile_by_user(email)
|
Profile.objects.add_or_update(email, nickname)
|
||||||
if profile is None:
|
|
||||||
profile = Profile(user=email)
|
|
||||||
profile.nickname = name
|
|
||||||
profile.save()
|
|
||||||
|
|
||||||
# update account login_id
|
# update account login_id
|
||||||
login_id = request.data.get("login_id", None)
|
login_id = request.data.get("login_id", None)
|
||||||
if login_id is not None:
|
if login_id is not None:
|
||||||
login_id = login_id.strip()
|
Profile.objects.add_or_update(email, login_id=login_id)
|
||||||
profile = Profile.objects.get_profile_by_user(email)
|
|
||||||
if profile is None:
|
# update account contact email
|
||||||
profile = Profile(user=email)
|
contact_email = request.data.get('contact_email', None)
|
||||||
profile.login_id = None if login_id == "" else login_id
|
if contact_email is not None:
|
||||||
profile.save()
|
Profile.objects.add_or_update(email, contact_email=contact_email)
|
||||||
|
key = normalize_cache_key(email, CONTACT_CACHE_PREFIX)
|
||||||
|
cache.set(key, contact_email, CONTACT_CACHE_TIMEOUT)
|
||||||
|
|
||||||
reference_id = request.data.get("reference_id", None)
|
reference_id = request.data.get("reference_id", None)
|
||||||
if reference_id is not None:
|
if reference_id is not None:
|
||||||
@@ -108,7 +108,7 @@ def get_user_info(email):
|
|||||||
info = {}
|
info = {}
|
||||||
info['email'] = email
|
info['email'] = email
|
||||||
info['name'] = email2nickname(email)
|
info['name'] = email2nickname(email)
|
||||||
info['contact_email'] = email2contact_email(email)
|
info['contact_email'] = profile.contact_email if profile and profile.contact_email else ''
|
||||||
info['login_id'] = profile.login_id if profile and profile.login_id else ''
|
info['login_id'] = profile.login_id if profile and profile.login_id else ''
|
||||||
|
|
||||||
info['is_staff'] = user.is_staff
|
info['is_staff'] = user.is_staff
|
||||||
@@ -327,7 +327,17 @@ class AdminUser(APIView):
|
|||||||
return api_error(status.HTTP_400_BAD_REQUEST,
|
return api_error(status.HTTP_400_BAD_REQUEST,
|
||||||
_(u"Login id %s already exists." % login_id))
|
_(u"Login id %s already exists." % login_id))
|
||||||
|
|
||||||
reference_id = request.data.get("reference_id", "").strip()
|
contact_email = request.data.get("contact_email", None)
|
||||||
|
if contact_email is not None and contact_email.strip() != '':
|
||||||
|
if not is_valid_email(contact_email):
|
||||||
|
error_msg = 'Contact email invalid.'
|
||||||
|
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
|
||||||
|
profile = Profile.objects.get_profile_by_contact_email(contact_email)
|
||||||
|
if profile:
|
||||||
|
error_msg = 'Contact email %s already exists.' % contact_email
|
||||||
|
return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
|
||||||
|
|
||||||
|
reference_id = request.data.get("reference_id", "")
|
||||||
if reference_id:
|
if reference_id:
|
||||||
if ' ' in reference_id:
|
if ' ' in reference_id:
|
||||||
return api_error(status.HTTP_400_BAD_REQUEST, 'Reference ID can not contain spaces.')
|
return api_error(status.HTTP_400_BAD_REQUEST, 'Reference ID can not contain spaces.')
|
||||||
|
@@ -16,17 +16,31 @@ from seahub.signals import institution_deleted
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class ProfileManager(models.Manager):
|
class ProfileManager(models.Manager):
|
||||||
def add_or_update(self, username, nickname, intro='', lang_code=None):
|
def add_or_update(self, username, nickname=None, intro=None, lang_code=None,
|
||||||
|
login_id=None, contact_email=None, institution=None):
|
||||||
"""Add or update user profile.
|
"""Add or update user profile.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
profile = self.get(user=username)
|
profile = self.get(user=username)
|
||||||
profile.nickname = nickname
|
|
||||||
profile.intro = intro
|
|
||||||
profile.lang_code = lang_code
|
|
||||||
except Profile.DoesNotExist:
|
except Profile.DoesNotExist:
|
||||||
profile = self.model(user=username, nickname=nickname,
|
profile = self.model(user=username)
|
||||||
intro=intro, lang_code=lang_code)
|
|
||||||
|
if nickname is not None:
|
||||||
|
nickname = nickname.strip()
|
||||||
|
profile.nickname = nickname
|
||||||
|
if intro is not None:
|
||||||
|
profile.intro = intro
|
||||||
|
if lang_code is not None:
|
||||||
|
profile.lang_code = lang_code
|
||||||
|
if login_id is not None:
|
||||||
|
login_id = login_id.strip()
|
||||||
|
profile.login_id = login_id
|
||||||
|
if contact_email is not None:
|
||||||
|
contact_email = contact_email.strip()
|
||||||
|
profile.contact_email = contact_email
|
||||||
|
if institution is not None:
|
||||||
|
institution = institution.strip()
|
||||||
|
profile.institution = institution
|
||||||
profile.save(using=self._db)
|
profile.save(using=self._db)
|
||||||
return profile
|
return profile
|
||||||
|
|
||||||
@@ -51,6 +65,15 @@ class ProfileManager(models.Manager):
|
|||||||
except Profile.DoesNotExist:
|
except Profile.DoesNotExist:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def get_profile_by_contact_email(self, contact_email):
|
||||||
|
res = super(ProfileManager, self).filter(contact_email=contact_email)
|
||||||
|
if len(res) > 0:
|
||||||
|
if len(res) > 1:
|
||||||
|
logger.warning('Repeated contact email %s' % contact_email)
|
||||||
|
return res[0]
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
def get_contact_email_by_user(self, username):
|
def get_contact_email_by_user(self, username):
|
||||||
"""Get a user's contact email, use username(login email) if not found.
|
"""Get a user's contact email, use username(login email) if not found.
|
||||||
"""
|
"""
|
||||||
|
@@ -65,6 +65,18 @@
|
|||||||
<span id="set-loginid" title="{% trans "Edit"%}" class="sf2-icon-edit op-icon"></span>
|
<span id="set-loginid" title="{% trans "Edit"%}" class="sf2-icon-edit op-icon"></span>
|
||||||
</dd>
|
</dd>
|
||||||
|
|
||||||
|
<dt>{% trans "Contact Email" %}</dt>
|
||||||
|
<dd>
|
||||||
|
<span id="contact-email">
|
||||||
|
{% if profile and profile.contact_email %}
|
||||||
|
{{ profile.contact_email }}
|
||||||
|
{% else %}
|
||||||
|
--
|
||||||
|
{% endif %}
|
||||||
|
</span>
|
||||||
|
<span id="set-contact-email" title="{% trans "Edit"%}" class="sf2-icon-edit op-icon"></span>
|
||||||
|
</dd>
|
||||||
|
|
||||||
<dt>{% trans "Reference ID" %}</dt>
|
<dt>{% trans "Reference ID" %}</dt>
|
||||||
<dd>
|
<dd>
|
||||||
<span id="reference-id">
|
<span id="reference-id">
|
||||||
@@ -142,6 +154,13 @@
|
|||||||
<input type="submit" value="{% trans "Submit" %}" class="submit" />
|
<input type="submit" value="{% trans "Submit" %}" class="submit" />
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
<form id="set-contact-email-form" method="post" action="" class="hide">{% csrf_token %}
|
||||||
|
<h3>{% trans "Set user contact email" %}</h3>
|
||||||
|
<input type="text" name="contact_email" class="input" value="" /><br />
|
||||||
|
<p class="error hide"></p>
|
||||||
|
<input type="submit" value="{% trans "Submit" %}" class="submit" />
|
||||||
|
</form>
|
||||||
|
|
||||||
<form id="set-reference-id-form" method="post" action="" class="hide">{% csrf_token %}
|
<form id="set-reference-id-form" method="post" action="" class="hide">{% csrf_token %}
|
||||||
<h3>{% trans "Set user Reference ID" %}</h3>
|
<h3>{% trans "Set user Reference ID" %}</h3>
|
||||||
<input type="text" name="reference_id" class="input" value="" /><br />
|
<input type="text" name="reference_id" class="input" value="" /><br />
|
||||||
@@ -355,6 +374,10 @@ $('#set-loginid').click(function () {
|
|||||||
$("#set-loginid-form").modal({appendTo:"#main"});
|
$("#set-loginid-form").modal({appendTo:"#main"});
|
||||||
$('#simplemodal-container').css({'width':'auto', 'height':'auto'});
|
$('#simplemodal-container').css({'width':'auto', 'height':'auto'});
|
||||||
})
|
})
|
||||||
|
$('#set-contact-email').click(function() {
|
||||||
|
$("#set-contact-email-form").modal({appendTo: "#main"});
|
||||||
|
$('#simplemodal-container').css({'width':'auto', 'height':'auto'});
|
||||||
|
});
|
||||||
$('#set-reference-id').click(function () {
|
$('#set-reference-id').click(function () {
|
||||||
$("#set-reference-id-form").modal({appendTo:"#main"});
|
$("#set-reference-id-form").modal({appendTo:"#main"});
|
||||||
$('#simplemodal-container').css({'width':'auto', 'height':'auto'});
|
$('#simplemodal-container').css({'width':'auto', 'height':'auto'});
|
||||||
@@ -469,6 +492,45 @@ $('#set-loginid-form').submit(function() {
|
|||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$('#set-contact-email-form').submit(function() {
|
||||||
|
var $form = $(this);
|
||||||
|
var contact_email = $.trim($('[name="contact_email"]', $form).val());
|
||||||
|
var $contactEmail = $('#contact-email');
|
||||||
|
var $error = $('.error', $form);
|
||||||
|
|
||||||
|
var $submitBtn = $('[type="submit"]', $form);
|
||||||
|
disable($submitBtn);
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: '{% url 'api-v2.1-admin-user' email %}',
|
||||||
|
type: 'PUT',
|
||||||
|
dataType: 'json',
|
||||||
|
cache: false,
|
||||||
|
beforeSend: prepareCSRFToken,
|
||||||
|
data: {'contact_email': contact_email},
|
||||||
|
success: function(data) {
|
||||||
|
if (data['contact_email'] == ''){
|
||||||
|
$contactEmail.html('--');
|
||||||
|
} else {
|
||||||
|
$contactEmail.html(HTMLescape(data['contact_email']));
|
||||||
|
}
|
||||||
|
$.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-reference-id-form').submit(function() {
|
$('#set-reference-id-form').submit(function() {
|
||||||
var reference_id = $.trim($('[name="reference_id"]', $(this)).val());
|
var reference_id = $.trim($('[name="reference_id"]', $(this)).val());
|
||||||
var $referenceID = $('#reference-id');
|
var $referenceID = $('#reference-id');
|
||||||
|
@@ -6,7 +6,8 @@ from tests.common.utils import randstring
|
|||||||
from django.core.urlresolvers import reverse
|
from django.core.urlresolvers import reverse
|
||||||
from seahub.constants import DEFAULT_USER, GUEST_USER
|
from seahub.constants import DEFAULT_USER, GUEST_USER
|
||||||
from seahub.test_utils import BaseTestCase
|
from seahub.test_utils import BaseTestCase
|
||||||
from seahub.base.templatetags.seahub_tags import email2nickname
|
from seahub.base.templatetags.seahub_tags import email2nickname, \
|
||||||
|
email2contact_email
|
||||||
from seahub.profile.models import DetailedProfile
|
from seahub.profile.models import DetailedProfile
|
||||||
from seahub.utils.file_size import get_file_size_unit
|
from seahub.utils.file_size import get_file_size_unit
|
||||||
|
|
||||||
@@ -243,6 +244,33 @@ class AdminUserTest(BaseTestCase):
|
|||||||
|
|
||||||
assert email2nickname(self.tmp_email) == tmp_name
|
assert email2nickname(self.tmp_email) == tmp_name
|
||||||
|
|
||||||
|
def test_update_contact_email(self):
|
||||||
|
|
||||||
|
self.login_as(self.admin)
|
||||||
|
|
||||||
|
# change user name
|
||||||
|
tmp_email = randstring(10) + '@seafile.test'
|
||||||
|
data = {'contact_email': tmp_email}
|
||||||
|
resp = self.client.put(self.url, json.dumps(data),
|
||||||
|
'application/json')
|
||||||
|
json_resp = json.loads(resp.content)
|
||||||
|
self.assertEqual(200, resp.status_code)
|
||||||
|
assert json_resp['contact_email'] == tmp_email
|
||||||
|
|
||||||
|
assert email2contact_email(self.tmp_email) == tmp_email
|
||||||
|
|
||||||
|
def test_update_contact_email_with_invalid(self):
|
||||||
|
self.login_as(self.admin)
|
||||||
|
|
||||||
|
# change user name
|
||||||
|
tmp_email = randstring(10)
|
||||||
|
data = {'contact_email': tmp_email}
|
||||||
|
resp = self.client.put(self.url, json.dumps(data),
|
||||||
|
'application/json')
|
||||||
|
json_resp = json.loads(resp.content)
|
||||||
|
self.assertEqual(400, resp.status_code)
|
||||||
|
|
||||||
|
|
||||||
def test_update_department(self):
|
def test_update_department(self):
|
||||||
|
|
||||||
self.login_as(self.admin)
|
self.login_as(self.admin)
|
||||||
|
@@ -21,14 +21,14 @@ class Email2nicknameTest(BaseTestCase):
|
|||||||
def test_nickname_is_space(self):
|
def test_nickname_is_space(self):
|
||||||
Profile.objects.add_or_update(self.user.username, ' ')
|
Profile.objects.add_or_update(self.user.username, ' ')
|
||||||
assert len(Profile.objects.all()) == 1
|
assert len(Profile.objects.all()) == 1
|
||||||
assert Profile.objects.all()[0].nickname == ' '
|
assert Profile.objects.all()[0].nickname == ''
|
||||||
|
|
||||||
assert email2nickname(self.user.username) == self.user.username.split('@')[0]
|
assert email2nickname(self.user.username) == self.user.username.split('@')[0]
|
||||||
|
|
||||||
def test_nickname_contains_space(self):
|
def test_nickname_contains_space(self):
|
||||||
Profile.objects.add_or_update(self.user.username, ' foo bar ')
|
Profile.objects.add_or_update(self.user.username, ' foo bar ')
|
||||||
assert len(Profile.objects.all()) == 1
|
assert len(Profile.objects.all()) == 1
|
||||||
assert Profile.objects.all()[0].nickname == ' foo bar '
|
assert Profile.objects.all()[0].nickname == 'foo bar'
|
||||||
|
|
||||||
assert email2nickname(self.user.username) == 'foo bar'
|
assert email2nickname(self.user.username) == 'foo bar'
|
||||||
|
|
||||||
|
@@ -19,3 +19,111 @@ class ProfileManagerTest(BaseTestCase):
|
|||||||
p.contact_email = 'contact@foo.com'
|
p.contact_email = 'contact@foo.com'
|
||||||
p.save()
|
p.save()
|
||||||
assert 'contact@foo.com' == Profile.objects.get_contact_email_by_user(username)
|
assert 'contact@foo.com' == Profile.objects.get_contact_email_by_user(username)
|
||||||
|
|
||||||
|
def test_add_or_update(self):
|
||||||
|
username = self.user.username
|
||||||
|
profiles = Profile.objects.filter(user=username)
|
||||||
|
for profile in profiles:
|
||||||
|
profile.delete()
|
||||||
|
|
||||||
|
profile = Profile.objects.add_or_update(username, 'nickname',
|
||||||
|
intro='hello', lang_code='ch',
|
||||||
|
login_id=username,
|
||||||
|
contact_email=username,
|
||||||
|
institution='test')
|
||||||
|
assert profile.nickname == 'nickname'
|
||||||
|
assert profile.user == username
|
||||||
|
assert profile.intro == 'hello'
|
||||||
|
assert profile.lang_code == 'ch'
|
||||||
|
assert profile.login_id == username
|
||||||
|
assert profile.contact_email == username
|
||||||
|
assert profile.institution == 'test'
|
||||||
|
|
||||||
|
# test whether other will be changed when some one updated
|
||||||
|
profile = Profile.objects.add_or_update(username, 'nick')
|
||||||
|
assert profile.nickname == 'nick'
|
||||||
|
assert profile.user == username
|
||||||
|
assert profile.intro == 'hello'
|
||||||
|
assert profile.lang_code == 'ch'
|
||||||
|
assert profile.login_id == username
|
||||||
|
assert profile.contact_email == username
|
||||||
|
assert profile.institution == 'test'
|
||||||
|
|
||||||
|
profile = Profile.objects.add_or_update(username, intro='intro')
|
||||||
|
assert profile.nickname == 'nick'
|
||||||
|
assert profile.user == username
|
||||||
|
assert profile.intro == 'intro'
|
||||||
|
assert profile.lang_code == 'ch'
|
||||||
|
assert profile.login_id == username
|
||||||
|
assert profile.contact_email == username
|
||||||
|
assert profile.institution == 'test'
|
||||||
|
|
||||||
|
profile = Profile.objects.add_or_update(username, lang_code='en')
|
||||||
|
assert profile.nickname == 'nick'
|
||||||
|
assert profile.user == username
|
||||||
|
assert profile.intro == 'intro'
|
||||||
|
assert profile.lang_code == 'en'
|
||||||
|
assert profile.login_id == username
|
||||||
|
assert profile.contact_email == username
|
||||||
|
assert profile.institution == 'test'
|
||||||
|
|
||||||
|
profile = Profile.objects.add_or_update(username, login_id='test@test.com')
|
||||||
|
assert profile.nickname == 'nick'
|
||||||
|
assert profile.user == username
|
||||||
|
assert profile.intro == 'intro'
|
||||||
|
assert profile.lang_code == 'en'
|
||||||
|
assert profile.login_id == 'test@test.com'
|
||||||
|
assert profile.contact_email == username
|
||||||
|
assert profile.institution == 'test'
|
||||||
|
|
||||||
|
profile = Profile.objects.add_or_update(username, contact_email='test@contact.com')
|
||||||
|
assert profile.nickname == 'nick'
|
||||||
|
assert profile.user == username
|
||||||
|
assert profile.intro == 'intro'
|
||||||
|
assert profile.lang_code == 'en'
|
||||||
|
assert profile.login_id == 'test@test.com'
|
||||||
|
assert profile.contact_email == 'test@contact.com'
|
||||||
|
assert profile.institution == 'test'
|
||||||
|
|
||||||
|
profile = Profile.objects.add_or_update(username, institution='insti')
|
||||||
|
assert profile.nickname == 'nick'
|
||||||
|
assert profile.user == username
|
||||||
|
assert profile.intro == 'intro'
|
||||||
|
assert profile.lang_code == 'en'
|
||||||
|
assert profile.login_id == 'test@test.com'
|
||||||
|
assert profile.contact_email == 'test@contact.com'
|
||||||
|
assert profile.institution == 'insti'
|
||||||
|
|
||||||
|
def test_add_or_update_with_empty(self):
|
||||||
|
username = self.user.username
|
||||||
|
profiles = Profile.objects.filter(user=username)
|
||||||
|
for profile in profiles:
|
||||||
|
profile.delete()
|
||||||
|
|
||||||
|
profile = Profile.objects.add_or_update(username, 'nickname',
|
||||||
|
intro='hello', lang_code='ch',
|
||||||
|
login_id=username,
|
||||||
|
contact_email=username,
|
||||||
|
institution='test')
|
||||||
|
assert profile.nickname == 'nickname'
|
||||||
|
assert profile.user == username
|
||||||
|
assert profile.intro == 'hello'
|
||||||
|
assert profile.lang_code == 'ch'
|
||||||
|
assert profile.login_id == username
|
||||||
|
assert profile.contact_email == username
|
||||||
|
assert profile.institution == 'test'
|
||||||
|
|
||||||
|
profile = Profile.objects.add_or_update(username, '')
|
||||||
|
assert profile.nickname == ''
|
||||||
|
|
||||||
|
profile = Profile.objects.add_or_update(username, intro='')
|
||||||
|
assert profile.intro == ''
|
||||||
|
|
||||||
|
profile = Profile.objects.add_or_update(username, lang_code='')
|
||||||
|
assert profile.lang_code == ''
|
||||||
|
|
||||||
|
profile = Profile.objects.add_or_update(username, login_id='')
|
||||||
|
assert profile.login_id == ''
|
||||||
|
|
||||||
|
profile = Profile.objects.add_or_update(username, contact_email='')
|
||||||
|
assert profile.contact_email == ''
|
||||||
|
Reference in New Issue
Block a user