add has_anonymous_username and has_anonymous_display_name to user account api (#6426)

This commit is contained in:
Andrew Williamson 2017-09-15 16:37:01 +01:00 коммит произвёл GitHub
Родитель 998620bce2
Коммит 628e0afd96
8 изменённых файлов: 42 добавлений и 18 удалений

Просмотреть файл

@ -37,6 +37,8 @@ A developer is defined as a user who is listed as a developer or owner of one or
:>json string|null picture_type: the image type (only 'image/png' is supported) if a user defined photo has been provided, or none if no photo has been provided.
:>json boolean is_addon_developer: The user has developed and listed add-ons on this website.
:>json boolean is_artist: The user has developed and listed themes on this website.
:>json boolean has_anonymous_display_name: The user has chosen neither a name nor a username.
:>json boolean has_anonymous_username: The user hasn't chosen a username.
If you authenticate and access your own account by specifing your own ``user_id`` the following additional fields are returned.

Просмотреть файл

@ -22,7 +22,8 @@ class PublicUserProfileSerializer(BaseUserSerializer):
class Meta(BaseUserSerializer.Meta):
fields = BaseUserSerializer.Meta.fields + (
'average_addon_rating', 'created', 'biography', 'homepage',
'average_addon_rating', 'created', 'biography',
'has_anonymous_display_name', 'has_anonymous_username', 'homepage',
'is_addon_developer', 'is_artist', 'location', 'occupation',
'num_addons_listed', 'picture_type', 'picture_url',
)

Просмотреть файл

@ -74,6 +74,25 @@ class TestPublicUserProfileSerializer(TestCase):
result = self.serialize()
assert result['url'] == absolutify(self.user.get_url_path())
def test_anonymous_username_display_name(self):
self.user = user_factory(
username='anonymous-bb4f3cbd422e504080e32f2d9bbfcee0')
data = self.serialize()
assert self.user.has_anonymous_username is True
assert data['has_anonymous_username'] is True
assert self.user.has_anonymous_display_name is True
assert data['has_anonymous_display_name'] is True
self.user.update(display_name=u'Bób dé bob')
data = self.serialize()
assert data['has_anonymous_username'] is True
assert data['has_anonymous_display_name'] is False
self.user.update(username='bob')
data = self.serialize()
assert data['has_anonymous_username'] is False
assert data['has_anonymous_display_name'] is False
class TestUserProfileSerializer(TestPublicUserProfileSerializer):
serializer = UserProfileSerializer

Просмотреть файл

@ -82,7 +82,7 @@ class UserEditForm(happyforms.ModelForm):
self.request = kwargs.pop('request', None)
instance = kwargs.get('instance')
if instance and instance.has_anonymous_username():
if instance and instance.has_anonymous_username:
kwargs.setdefault('initial', {})
kwargs['initial']['username'] = ''
@ -130,7 +130,7 @@ class UserEditForm(happyforms.ModelForm):
name = self.cleaned_data['username']
if not name:
if self.instance.has_anonymous_username():
if self.instance.has_anonymous_username:
name = self.instance.username
else:
name = self.instance.anonymize_username()

Просмотреть файл

@ -306,7 +306,7 @@ class UserProfile(OnChangeMixin, ModelBase, AbstractBaseUser):
def name(self):
if self.display_name:
return force_text(self.display_name)
elif self.has_anonymous_username():
elif self.has_anonymous_username:
# L10n: {id} will be something like "13ad6a", just a random number
# to differentiate this user from other anonymous users.
return ugettext('Anonymous user {id}').format(
@ -323,7 +323,7 @@ class UserProfile(OnChangeMixin, ModelBase, AbstractBaseUser):
return self.username
def _anonymous_username_id(self):
if self.has_anonymous_username():
if self.has_anonymous_username:
return self.username.split('-')[1][:6]
def anonymize_username(self):
@ -335,11 +335,13 @@ class UserProfile(OnChangeMixin, ModelBase, AbstractBaseUser):
self.username = 'anonymous-{}'.format(os.urandom(16).encode('hex'))
return self.username
@property
def has_anonymous_username(self):
return re.match('^anonymous-[0-9a-f]{32}$', self.username)
return re.match('^anonymous-[0-9a-f]{32}$', self.username) is not None
@property
def has_anonymous_display_name(self):
return not self.display_name and self.has_anonymous_username()
return not self.display_name and self.has_anonymous_username
@cached_property
def reviews(self):

Просмотреть файл

@ -15,7 +15,7 @@ data-default-locale="{{ request.LANG|lower }}"
{% block content %}
{% include 'users/includes/navigation.html' %}
<div id="user_edit" class="primary prettyform grid" role="main">
{% if user.has_anonymous_display_name() %}
{% if user.has_anonymous_display_name %}
<div class="notification-box error anonymous">
<h2>{{ _('Please set your display name') }}</h2>
<p>{{ _('Please set your display name or username to complete the registration process.') }}</p>

Просмотреть файл

@ -62,12 +62,12 @@ class TestUserEditForm(UserFormBase):
self.url = reverse('users.edit')
def test_no_username_or_display_name(self):
assert not self.user.has_anonymous_username()
assert not self.user.has_anonymous_username
data = {'username': '',
'email': 'jbalogh@mozilla.com'}
response = self.client.post(self.url, data)
self.assertNoFormErrors(response)
assert self.user.reload().has_anonymous_username()
assert self.user.reload().has_anonymous_username
def test_change_username(self):
assert self.user.username != 'new-username'

Просмотреть файл

@ -85,38 +85,38 @@ class TestUserProfile(TestCase):
def test_has_anonymous_username_no_names(self):
user = UserProfile(display_name=None)
user.anonymize_username()
assert user.has_anonymous_username()
assert user.has_anonymous_username
def test_has_anonymous_username_username_set(self):
user = UserProfile(username='bob', display_name=None)
assert not user.has_anonymous_username()
assert not user.has_anonymous_username
def test_has_anonymous_username_display_name_set(self):
user = UserProfile(display_name='Bob Bobbertson')
user.anonymize_username()
assert user.has_anonymous_username()
assert user.has_anonymous_username
def test_has_anonymous_username_both_names_set(self):
user = UserProfile(username='bob', display_name='Bob Bobbertson')
assert not user.has_anonymous_username()
assert not user.has_anonymous_username
def test_has_anonymous_display_name_no_names(self):
user = UserProfile(display_name=None)
user.anonymize_username()
assert user.has_anonymous_display_name()
assert user.has_anonymous_display_name
def test_has_anonymous_display_name_username_set(self):
user = UserProfile(username='bob', display_name=None)
assert not user.has_anonymous_display_name()
assert not user.has_anonymous_display_name
def test_has_anonymous_display_name_display_name_set(self):
user = UserProfile(display_name='Bob Bobbertson')
user.anonymize_username()
assert not user.has_anonymous_display_name()
assert not user.has_anonymous_display_name
def test_has_anonymous_display_name_both_names_set(self):
user = UserProfile(username='bob', display_name='Bob Bobbertson')
assert not user.has_anonymous_display_name()
assert not user.has_anonymous_display_name
def test_add_admin_powers(self):
user = UserProfile.objects.get(username='jbalogh')