From cb418475cfd8bd47ef94ca041f8e7e25b87952ab Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Thu, 12 Mar 2020 07:45:11 +0100 Subject: [PATCH 1/2] Handle when user has never logged into the system. --- musician/__init__.py | 2 +- musician/models.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/musician/__init__.py b/musician/__init__.py index 5b64a68..cc9f205 100644 --- a/musician/__init__.py +++ b/musician/__init__.py @@ -2,7 +2,7 @@ Package metadata definition. """ -VERSION = (0, 2, 0, 'alpha', 0) +VERSION = (0, 2, 0, 'alpha', 1) def get_version(): diff --git a/musician/models.py b/musician/models.py index 712834b..bccd595 100644 --- a/musician/models.py +++ b/musician/models.py @@ -120,8 +120,9 @@ class UserAccount(OrchestraModel): if 'language' in data: language = data['language'].lower() - if 'last_login' in data: - last_login = parse_datetime(data['last_login']) + last_login = data.get('last_login') + if last_login is not None: + last_login = parse_datetime(last_login) return super().new_from_json(data=data, billing=billing, language=language, last_login=last_login) From 2c44098ee7c7a030e9543778884916940ab13d63 Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Wed, 18 Mar 2020 08:03:19 +0100 Subject: [PATCH 2/2] Add test to reproduce the issue. --- musician/tests.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/musician/tests.py b/musician/tests.py index fe0215c..44118a2 100644 --- a/musician/tests.py +++ b/musician/tests.py @@ -39,6 +39,29 @@ class UserAccountTest(TestCase): account = UserAccount.new_from_json(data) self.assertIsNone(account.last_login) + def test_user_never_logged2(self): + # issue #6 Error on login when user never has logged into the system + data = { + 'billcontact': {'address': 'bar', + 'city': 'Barcelona', + 'country': 'ES', + 'name': '', + 'vat': '12345678Z', + 'zipcode': '34561'}, + 'date_joined': '2020-01-14T12:38:31Z', + 'full_name': 'Pep', + 'id': 2, + 'is_active': True, + 'language': 'CA', + 'last_login': None, + 'short_name': '', + 'type': 'INDIVIDUAL', + 'url': 'http://127.0.0.1:9090/api/accounts/2/', + 'username': 'pepe' + } + account = UserAccount.new_from_json(data) + self.assertIsNone(account.last_login) + class GetBootstrapedPercentTest(TestCase): BS_WIDTH = [0, 25, 50, 100]