diff --git a/common/djangoapps/student/models.py b/common/djangoapps/student/models.py index 988bbeb12be5fbe5c9329bc145aea515d4bc4aad..437ddb35ed52d490a4a98008269dc768874406e3 100644 --- a/common/djangoapps/student/models.py +++ b/common/djangoapps/student/models.py @@ -915,39 +915,13 @@ class LoginFailures(models.Model): except ObjectDoesNotExist: return - def __repr__(self): - """Repr -> LoginFailures(username, count, date)""" - date_str = '-' - if self.lockout_until is not None: - date_str = self.lockout_until.isoformat() - - try: - username = unicode(self.user.username, 'utf-8') - except TypeError: - username = self.user.username - - return u'LoginFailures({username}, {count}, {date})'.format( - username=username, - count=self.failure_count, - date=date_str - ) - def __str__(self): """Str -> Username: count - date.""" - date_str = '-' - if self.lockout_until is not None: - date_str = self.lockout_until.isoformat() - - try: - username = unicode(self.user.username, 'utf-8') - except TypeError: - username = self.user.username - - return u'{username}: {count} - {date}'.format( - username=username, + return six.text_type('{username}: {count} - {date}'.format( + username=self.user.username, count=self.failure_count, - date=date_str - ) + date=self.lockout_until.isoformat() if self.lockout_until else '-' + )) class Meta: verbose_name = 'Login Failure' diff --git a/common/djangoapps/student/tests/test_admin_views.py b/common/djangoapps/student/tests/test_admin_views.py index c3b57e6d6d2237d64716f7ee2c1e1ddc640e6bdc..1ba2681ab103eec326e6da996ded43013b5bd522 100644 --- a/common/djangoapps/student/tests/test_admin_views.py +++ b/common/djangoapps/student/tests/test_admin_views.py @@ -333,6 +333,16 @@ class LoginFailuresAdminTest(TestCase): super(LoginFailuresAdminTest, self).tearDown() LoginFailures.objects.all().delete() + def test_unicode_username(self): + """ + Test if `__str__` method behaves correctly for unicode username. + It shouldn't raise `TypeError`. + """ + try: + map(str, LoginFailures.objects.all()) + except TypeError, e: + self.fail("Failed executing `__str__` with unicode: {0}".format(e)) + @ddt.data( reverse('admin:student_loginfailures_changelist'), reverse('admin:student_loginfailures_add'),