mirror of
https://github.com/haiwen/seahub.git
synced 2025-04-28 03:10:45 +00:00
[profile] Raise exception for duplicated contact email
This commit is contained in:
parent
2353540060
commit
2eb725b7e1
@ -2,7 +2,7 @@
|
||||
import logging
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import models
|
||||
from django.db import models, IntegrityError
|
||||
from django.core.cache import cache
|
||||
from django.dispatch import receiver
|
||||
from django.core.exceptions import MultipleObjectsReturned
|
||||
@ -16,6 +16,10 @@ from seahub.signals import institution_deleted
|
||||
# Get an instance of a logger
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class DuplicatedContactEmailError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class ProfileManager(models.Manager):
|
||||
def add_or_update(self, username, nickname=None, intro=None, lang_code=None,
|
||||
login_id=None, contact_email=None, institution=None):
|
||||
@ -42,8 +46,12 @@ class ProfileManager(models.Manager):
|
||||
if institution is not None:
|
||||
institution = institution.strip()
|
||||
profile.institution = institution
|
||||
profile.save(using=self._db)
|
||||
return profile
|
||||
|
||||
try:
|
||||
profile.save(using=self._db)
|
||||
return profile
|
||||
except IntegrityError:
|
||||
raise DuplicatedContactEmailError
|
||||
|
||||
def update_contact_email(self, username, contact_email):
|
||||
"""
|
||||
@ -55,8 +63,12 @@ class ProfileManager(models.Manager):
|
||||
except Profile.DoesNotExist:
|
||||
logger.warn('%s profile does not exists' % username)
|
||||
return None
|
||||
profile.save(using=self._db)
|
||||
return profile
|
||||
|
||||
try:
|
||||
profile.save(using=self._db)
|
||||
return profile
|
||||
except IntegrityError:
|
||||
raise DuplicatedContactEmailError
|
||||
|
||||
def get_profile_by_user(self, username):
|
||||
"""Get a user's profile.
|
||||
@ -105,10 +117,6 @@ class ProfileManager(models.Manager):
|
||||
return super(ProfileManager, self).get(contact_email=contact_email).user
|
||||
except Profile.DoesNotExist:
|
||||
return None
|
||||
except MultipleObjectsReturned as e:
|
||||
logger.warn('Failed to get username by contact email: %s' % contact_email)
|
||||
logger.warn(e)
|
||||
return None
|
||||
|
||||
def convert_login_str_to_username(self, login_str):
|
||||
"""
|
||||
|
@ -1,4 +1,4 @@
|
||||
from seahub.profile.models import Profile
|
||||
from seahub.profile.models import Profile, DuplicatedContactEmailError
|
||||
from seahub.test_utils import BaseTestCase
|
||||
|
||||
|
||||
@ -13,9 +13,9 @@ class ProfileManagerTest(BaseTestCase):
|
||||
Profile.objects.add_or_update(user1, contact_email='a@a.com')
|
||||
assert Profile.objects.get_username_by_contact_email('a@a.com') == user1
|
||||
|
||||
user2 = self.admin.username
|
||||
Profile.objects.add_or_update(user2, contact_email='a@a.com')
|
||||
assert Profile.objects.get_username_by_contact_email('a@a.com') is None
|
||||
# user2 = self.admin.username
|
||||
# Profile.objects.add_or_update(user2, contact_email='a@a.com')
|
||||
# assert Profile.objects.get_username_by_contact_email('a@a.com') is None
|
||||
|
||||
def test_convert_login_str_to_username(self):
|
||||
s = Profile.objects
|
||||
@ -151,3 +151,30 @@ class ProfileManagerTest(BaseTestCase):
|
||||
|
||||
profile = Profile.objects.add_or_update(username, contact_email='')
|
||||
assert profile.contact_email == ''
|
||||
|
||||
def test_duplicated_contact_email(self, ):
|
||||
profile = Profile.objects.add_or_update('test@test.com', '',
|
||||
contact_email='a@a.com')
|
||||
|
||||
try:
|
||||
_ = Profile.objects.add_or_update('1@1.com', '',
|
||||
contact_email='a@a.com')
|
||||
except DuplicatedContactEmailError:
|
||||
assert True
|
||||
else:
|
||||
assert False
|
||||
|
||||
def test_updated_contact_email(self, ):
|
||||
_ = Profile.objects.add_or_update('1@1.com', '',
|
||||
contact_email='a@a.com')
|
||||
|
||||
username = self.user.username
|
||||
profile = Profile.objects.add_or_update(username, '',
|
||||
contact_email='b@b.com')
|
||||
|
||||
try:
|
||||
Profile.objects.update_contact_email(username, contact_email='a@a.com')
|
||||
except DuplicatedContactEmailError:
|
||||
assert True
|
||||
else:
|
||||
assert False
|
||||
|
Loading…
Reference in New Issue
Block a user