1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-09 02:42:47 +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:
zMingGit
2017-11-14 14:12:27 +08:00
committed by xiez
parent ecd6ccfe20
commit 05caf621f1
6 changed files with 256 additions and 25 deletions

View File

@@ -6,6 +6,7 @@ from rest_framework.authentication import SessionAuthentication
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView
from django.core.cache import cache
from django.core.urlresolvers import reverse
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, \
email2contact_email
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, \
is_pro_version
is_pro_version, normalize_cache_key, is_valid_email
from seahub.utils.timeutils import timestamp_to_isoformat_timestr
from seahub.utils.file_size import get_file_size_unit
from seahub.role_permissions.utils import get_available_roles
@@ -58,23 +60,21 @@ def update_user_info(request, user):
if role:
User.objects.update_role(email, role)
name = request.data.get("name")
if name:
profile = Profile.objects.get_profile_by_user(email)
if profile is None:
profile = Profile(user=email)
profile.nickname = name
profile.save()
nickname = request.data.get("name", None)
if nickname is not None:
Profile.objects.add_or_update(email, nickname)
# update account login_id
login_id = request.data.get("login_id", None)
if login_id is not None:
login_id = login_id.strip()
profile = Profile.objects.get_profile_by_user(email)
if profile is None:
profile = Profile(user=email)
profile.login_id = None if login_id == "" else login_id
profile.save()
Profile.objects.add_or_update(email, login_id=login_id)
# update account contact email
contact_email = request.data.get('contact_email', None)
if contact_email is not None:
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)
if reference_id is not None:
@@ -108,7 +108,7 @@ def get_user_info(email):
info = {}
info['email'] = 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['is_staff'] = user.is_staff
@@ -327,7 +327,17 @@ class AdminUser(APIView):
return api_error(status.HTTP_400_BAD_REQUEST,
_(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 ' ' in reference_id:
return api_error(status.HTTP_400_BAD_REQUEST, 'Reference ID can not contain spaces.')

View File

@@ -16,17 +16,31 @@ from seahub.signals import institution_deleted
logger = logging.getLogger(__name__)
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.
"""
try:
profile = self.get(user=username)
profile.nickname = nickname
profile.intro = intro
profile.lang_code = lang_code
except Profile.DoesNotExist:
profile = self.model(user=username, nickname=nickname,
intro=intro, lang_code=lang_code)
profile = self.model(user=username)
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)
return profile
@@ -51,6 +65,15 @@ class ProfileManager(models.Manager):
except Profile.DoesNotExist:
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):
"""Get a user's contact email, use username(login email) if not found.
"""

View File

@@ -65,6 +65,18 @@
<span id="set-loginid" title="{% trans "Edit"%}" class="sf2-icon-edit op-icon"></span>
</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>
<dd>
<span id="reference-id">
@@ -142,6 +154,13 @@
<input type="submit" value="{% trans "Submit" %}" class="submit" />
</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 %}
<h3>{% trans "Set user Reference ID" %}</h3>
<input type="text" name="reference_id" class="input" value="" /><br />
@@ -355,6 +374,10 @@ $('#set-loginid').click(function () {
$("#set-loginid-form").modal({appendTo:"#main"});
$('#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-form").modal({appendTo:"#main"});
$('#simplemodal-container').css({'width':'auto', 'height':'auto'});
@@ -469,6 +492,45 @@ $('#set-loginid-form').submit(function() {
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() {
var reference_id = $.trim($('[name="reference_id"]', $(this)).val());
var $referenceID = $('#reference-id');

View File

@@ -6,7 +6,8 @@ from tests.common.utils import randstring
from django.core.urlresolvers import reverse
from seahub.constants import DEFAULT_USER, GUEST_USER
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.utils.file_size import get_file_size_unit
@@ -243,6 +244,33 @@ class AdminUserTest(BaseTestCase):
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):
self.login_as(self.admin)

View File

@@ -19,3 +19,111 @@ class ProfileManagerTest(BaseTestCase):
p.contact_email = 'contact@foo.com'
p.save()
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 == ''