From 69fa0f6dfaf4ad0c282e362d95d30de99361e8cb Mon Sep 17 00:00:00 2001 From: Jamie Alexandre Date: Tue, 10 Mar 2015 19:19:05 -0700 Subject: [PATCH 1/5] Fixes #3232 (hopefully): handle metadata check when Device is imported. --- python-packages/securesync/devices/models.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/python-packages/securesync/devices/models.py b/python-packages/securesync/devices/models.py index 1d0cd002d6..4db881fccc 100755 --- a/python-packages/securesync/devices/models.py +++ b/python-packages/securesync/devices/models.py @@ -207,10 +207,15 @@ def set_key(self, key): self.key = key def get_key(self): + self.key = None if not self.key: - if self.get_metadata().is_own_device: - self.key = crypto.get_own_key() - elif self.public_key: + try: + if self.get_metadata().is_own_device: + self.key = crypto.get_own_key() + except: + # If device has not yet been saved, but ID is set, then getting metadata may fail (on MySQL) + pass + if not self.key and self.public_key: self.key = crypto.Key(public_key_string=self.public_key) return self.key From e5be88de761ff5560a73139e103dd4f6b3f92a2a Mon Sep 17 00:00:00 2001 From: Jamie Alexandre Date: Thu, 12 Mar 2015 13:23:30 -0700 Subject: [PATCH 2/5] Don't overwrite self.key (it already gets set to a default of None). --- python-packages/securesync/devices/models.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python-packages/securesync/devices/models.py b/python-packages/securesync/devices/models.py index 4db881fccc..155b753854 100755 --- a/python-packages/securesync/devices/models.py +++ b/python-packages/securesync/devices/models.py @@ -207,7 +207,6 @@ def set_key(self, key): self.key = key def get_key(self): - self.key = None if not self.key: try: if self.get_metadata().is_own_device: From 1b22ea1b01ed420cf07b41b9513f41a16ffef5a6 Mon Sep 17 00:00:00 2001 From: Jamie Alexandre Date: Thu, 12 Mar 2015 13:25:11 -0700 Subject: [PATCH 3/5] Be more specific in exception catching for MySQL error. --- python-packages/securesync/devices/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python-packages/securesync/devices/models.py b/python-packages/securesync/devices/models.py index 155b753854..3e436050d0 100755 --- a/python-packages/securesync/devices/models.py +++ b/python-packages/securesync/devices/models.py @@ -8,7 +8,7 @@ from django.conf import settings from django.contrib.auth.models import check_password from django.core.exceptions import ValidationError, ObjectDoesNotExist -from django.db import models, transaction +from django.db import models, transaction, IntegrityError from django.db.models import Q from django.db.models.expressions import F from django.utils.text import compress_string @@ -211,7 +211,7 @@ def get_key(self): try: if self.get_metadata().is_own_device: self.key = crypto.get_own_key() - except: + except IntegrityError: # If device has not yet been saved, but ID is set, then getting metadata may fail (on MySQL) pass if not self.key and self.public_key: From 628b2ba6a2bf1e20a52d1f7032623ad18bd296a7 Mon Sep 17 00:00:00 2001 From: Michael Gallaspy Date: Thu, 12 Mar 2015 19:21:24 -0700 Subject: [PATCH 4/5] Attempt to improve readability --- python-packages/securesync/devices/models.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/python-packages/securesync/devices/models.py b/python-packages/securesync/devices/models.py index 3e436050d0..1754d7e7c0 100755 --- a/python-packages/securesync/devices/models.py +++ b/python-packages/securesync/devices/models.py @@ -211,8 +211,8 @@ def get_key(self): try: if self.get_metadata().is_own_device: self.key = crypto.get_own_key() - except IntegrityError: - # If device has not yet been saved, but ID is set, then getting metadata may fail (on MySQL) + except Device.DoesNotExist: + # get_metadata can fail if the Device instance hasn't been persisted to the db pass if not self.key and self.public_key: self.key = crypto.Key(public_key_string=self.public_key) @@ -223,7 +223,12 @@ def _hashable_representation(self): return super(Device, self)._hashable_representation(fields=fields) def get_metadata(self): - return DeviceMetadata.objects.get_or_create(device=self)[0] + try: + return DeviceMetadata.objects.get_or_create(device=self)[0] + except IntegrityError as e: + # Possible from get_or_create if the DeviceMetadata object doesn't exist + # and the Device hasn't been persisted to the database yet. + raise Device.DoesNotExist(e.message) def get_counter_position(self): """ From dfc5e8248b946033e5b0f3ab9a8121dba5719726 Mon Sep 17 00:00:00 2001 From: Michael Gallaspy Date: Thu, 12 Mar 2015 19:34:11 -0700 Subject: [PATCH 5/5] BaseException.message is deprecated --- python-packages/securesync/devices/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python-packages/securesync/devices/models.py b/python-packages/securesync/devices/models.py index 1754d7e7c0..b84efe9bfe 100755 --- a/python-packages/securesync/devices/models.py +++ b/python-packages/securesync/devices/models.py @@ -228,7 +228,7 @@ def get_metadata(self): except IntegrityError as e: # Possible from get_or_create if the DeviceMetadata object doesn't exist # and the Device hasn't been persisted to the database yet. - raise Device.DoesNotExist(e.message) + raise Device.DoesNotExist() def get_counter_position(self): """