Skip to content

Commit

Permalink
Merge pull request #3253 from jamalex/device_metadata_fix
Browse files Browse the repository at this point in the history
Fixes #3232 (hopefully): handle metadata check when Device is imported.
  • Loading branch information
aronasorman committed Mar 13, 2015
2 parents 31b3c7e + f90d1d6 commit be90e00
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions python-packages/securesync/devices/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -208,9 +208,13 @@ def set_key(self, key):

def get_key(self):
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 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)
return self.key

Expand All @@ -219,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()

def get_counter_position(self):
"""
Expand Down

0 comments on commit be90e00

Please sign in to comment.