From 97a76f32386d0b8a46fa86371e95d7482e0fdae1 Mon Sep 17 00:00:00 2001 From: Samuel Bichsel Date: Mon, 27 Apr 2015 17:00:25 +0200 Subject: [PATCH] updating devices if they already exist and one is trying to register the, --- CHANGES.txt | 3 ++- scarface/models.py | 13 ++++++++++++- setup.py | 2 +- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 40e80d5..7a3045e 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,3 +1,4 @@ v1.0, 2015/01/05 -- Initial release. v1.0.1, 2015/01/06 -- Updated Documentation, added logging settings, Python 3 support -v1.0.2, 2015/01/06 -- The PushMessage model allows now more null values \ No newline at end of file +v1.0.2, 2015/01/06 -- The PushMessage model allows now more null values +v1.0.3, 2015/04/27 -- Improving the create_or_update SNSDevice method: If the device is already registered with other attributes, the given error gets parsed an updated with the new attributes. \ No newline at end of file diff --git a/scarface/models.py b/scarface/models.py index 14283a0..5806e0a 100644 --- a/scarface/models.py +++ b/scarface/models.py @@ -1,6 +1,8 @@ from abc import ABCMeta, abstractproperty, abstractmethod import json +from boto.exception import BotoServerError from django.conf import settings +import re from .utils import DefaultConnection, PushLogger, APP_PREFIX from django.db import models @@ -99,7 +101,16 @@ def register_or_update(self, new_token=None, custom_user_data=u"", connection=No if self.is_registered: result = self.update(new_token, custom_user_data, connection) else: - result = self.register(custom_user_data, connection) + try: + result = self.register(custom_user_data, connection) + # Heavily inspired by http://stackoverflow.com/a/28316993/270265 + except BotoServerError as err: + result_re = re.compile(r'Endpoint(.*)already', re.IGNORECASE) + result = result_re.search(err.message) + if result: + arn = result.group(0).replace('Endpoint ','').replace(' already','') + self.arn = arn + self.update(new_token, custom_user_data, connection) return result @DefaultConnection diff --git a/setup.py b/setup.py index 933044b..5401bb2 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ setup( name='django-scarface', - version='1.0.2', + version='1.0.3', packages=['scarface'], include_package_data=True, license='MIT License',