From 9449b50da6a68e5e91b5156599cc7cce455d427d Mon Sep 17 00:00:00 2001
From: Agrendalath <piotr@surowiec.it>
Date: Wed, 18 Sep 2019 05:32:50 +0200
Subject: [PATCH] Refactor after the review, add test

---
 common/djangoapps/student/models.py           | 34 +++----------------
 .../student/tests/test_admin_views.py         | 10 ++++++
 2 files changed, 14 insertions(+), 30 deletions(-)

diff --git a/common/djangoapps/student/models.py b/common/djangoapps/student/models.py
index 988bbeb12be..437ddb35ed5 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 c3b57e6d6d2..1ba2681ab10 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'),
-- 
GitLab