From c9f9179685d07f06a26ef7a6565fb3fe0ce2304b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Stefan=20K=C3=B6gl?= Date: Sat, 5 Jan 2013 15:14:11 +0100 Subject: [PATCH] simplify some @repeat_on_conflict decorated methods --- mygpo/api/backend.py | 44 ++++++++++++++++++++------------------------ mygpo/users/models.py | 13 +++++-------- mygpo/web/views/users.py | 4 ++-- 3 files changed, 27 insertions(+), 34 deletions(-) diff --git a/mygpo/api/backend.py b/mygpo/api/backend.py index 7ac45ee3..89de7451 100644 --- a/mygpo/api/backend.py +++ b/mygpo/api/backend.py @@ -51,6 +51,7 @@ def get_random_picks(languages=None): +@repeat_on_conflict(['user']) def get_device(user, uid, user_agent, undelete=True): """ Loads or creates the device indicated by user, uid. @@ -60,36 +61,31 @@ def get_device(user, uid, user_agent, undelete=True): store_ua = user.settings.get('store_user_agent', True) - @repeat_on_conflict(['user']) - def _get(user, uid, undelete): + save = False - save = False + try: + device = user.get_device_by_uid(uid, only_active=False) - try: - device = user.get_device_by_uid(uid, only_active=False) - - except DeviceDoesNotExist: - device = Device(uid=uid) - user.devices.append(device) - save = True - - if device.deleted and undelete: - device.deleted = False - user.set_device(device) - save = True + except DeviceDoesNotExist: + device = Device(uid=uid) + user.devices.append(device) + save = True - if store_ua and user_agent and \ - getattr(device, 'user_agent', None) != user_agent: - device.user_agent = user_agent - user.set_device(device) - save = True + if device.deleted and undelete: + device.deleted = False + user.set_device(device) + save = True - if save: - user.save() + if store_ua and user_agent and \ + getattr(device, 'user_agent', None) != user_agent: + device.user_agent = user_agent + user.set_device(device) + save = True - return device + if save: + user.save() - return _get(user=user, uid=uid, undelete=undelete) + return device class BulkSubscribe(object): diff --git a/mygpo/users/models.py b/mygpo/users/models.py index f7ce3c9c..c8ba3203 100644 --- a/mygpo/users/models.py +++ b/mygpo/users/models.py @@ -501,6 +501,7 @@ class User(BaseUser, SyncedDevicesMixin): + @repeat_on_conflict(['self']) def get_token(self, token_name): """ returns a token, and generate those that are still missing """ @@ -563,15 +564,11 @@ class User(BaseUser, SyncedDevicesMixin): raise DeviceDoesNotExist('There is no device with UID %s' % uid) + @repeat_on_conflict(['self']) def update_device(self, device): """ Sets the device and saves the user """ - - @repeat_on_conflict(['user']) - def _update(user, device): - user.set_device(device) - user.save() - - _update(user=self, device=device) + self.set_device(device) + self.save() def set_device(self, device): @@ -799,7 +796,7 @@ class User(BaseUser, SyncedDevicesMixin): if old_devs != set(state.disabled_devices): state.save() - _update_state(state=state) + _update_state(state) diff --git a/mygpo/web/views/users.py b/mygpo/web/views/users.py index d8ef379e..562cec4a 100644 --- a/mygpo/web/views/users.py +++ b/mygpo/web/views/users.py @@ -82,7 +82,7 @@ def login_user(request): 'activation_needed': True, }) - login(request=request, user=user) + login(request, user) if 'next' in request.POST and request.POST['next'] and request.POST['next'] != '/login/': return HttpResponseRedirect(request.POST['next']) @@ -126,7 +126,7 @@ def restore_password(request): subject = _('Reset password for your account on %s') % site message = _('Here is your new password for your account %(username)s on %(site)s: %(password)s') % {'username': user.username, 'site': site, 'password': pwd} user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL) - _set_password(user=user, password=pwd) + _set_password(user, pwd) return render(request, 'password_reset.html') -- 2.11.4.GIT