update device states in podcast states only when necessary
authorStefan Kögl <stefan@skoegl.net>
Fri, 19 Jul 2013 20:35:21 +0000 (19 22:35 +0200)
committerStefan Kögl <stefan@skoegl.net>
Fri, 19 Jul 2013 20:35:21 +0000 (19 22:35 +0200)
this optimizes saving of User objects

mygpo/db/couchdb/user.py
mygpo/users/models.py
mygpo/users/tasks.py
mygpo/web/templates/devicelist.html
mygpo/web/views/device.py

index 74b349d..05f2ca0 100644 (file)
@@ -279,3 +279,26 @@ def activate_user(user):
     user.is_active = True
     user.activation_key = None
     user.save()
+
+
+@repeat_on_conflict(['user'])
+def set_device_deleted(user, device, is_deleted):
+    device.deleted = is_deleted
+    user.set_device(device)
+    user.save()
+
+
+@repeat_on_conflict(['state'])
+def update_device_state(state, devices):
+    old_devs = set(state.disabled_devices)
+    state.set_device_state(devices)
+
+    if old_devs != set(state.disabled_devices):
+        state.save()
+
+
+@repeat_on_conflict(['user'])
+def unsync_device(user, device):
+    if user.is_synced(device):
+        user.unsync_device(device)
+        user.save()
index 428204e..6900ece 100644 (file)
@@ -785,29 +785,6 @@ class User(BaseUser, SyncedDevicesMixin, SettingsMixin):
             yield proxy_object(episode, podcast=podcast)
 
 
-
-
-    def save(self, *args, **kwargs):
-
-        from mygpo.db.couchdb.podcast_state import podcast_states_for_user
-
-        super(User, self).save(*args, **kwargs)
-
-        podcast_states = podcast_states_for_user(self)
-        for state in podcast_states:
-            @repeat_on_conflict(['state'])
-            def _update_state(state):
-                old_devs = set(state.disabled_devices)
-                state.set_device_state(self.devices)
-
-                if old_devs != set(state.disabled_devices):
-                    state.save()
-
-            _update_state(state)
-
-
-
-
     def __eq__(self, other):
         if not other:
             return False
index b985032..96f6c86 100644 (file)
@@ -5,7 +5,7 @@ from collections import Counter
 from couchdbkit import ResourceConflict
 
 from mygpo.cel import celery
-from mygpo.db.couchdb.user import suggestions_for_user
+from mygpo.db.couchdb.user import suggestions_for_user, update_device_state
 
 
 @celery.task(max_retries=5, default_retry_delay=60)
@@ -45,3 +45,12 @@ def update_suggestions(user, max_suggestions=15):
 
     except ResourceConflict as ex:
         raise update_suggestions.retry(exc=ex)
+
+
+@celery.task(max_retries=5, default_retry_delay=60)
+def set_device_task_state(user):
+    """ updates the device states of a user in all his/her podcast states """
+    from mygpo.db.couchdb.podcast_state import podcast_states_for_user
+    podcast_states = podcast_states_for_user(user)
+    for state in podcast_states:
+        update_device_state(state, user.devices)
index 2af82eb..85911c0 100644 (file)
@@ -66,6 +66,7 @@
       <td>{{ d.uid|striptags }}</td>
       <td>
        <form method="post" action="{% url "device-undelete" d.uid %}">
+        {% csrf_token %}
         <button class="btn btn-small" type="submit">
          <i class="icon-repeat"></i> {% trans "Reactivate" %}
         </button>
index c6307d8..fd50526 100644 (file)
@@ -36,8 +36,9 @@ from mygpo.api import simple
 from mygpo.decorators import allowed_methods, repeat_on_conflict
 from mygpo.users.models import Device, DeviceUIDException, \
      DeviceDoesNotExist
-from mygpo.users.tasks import sync_user
+from mygpo.users.tasks import sync_user, set_device_task_state
 from mygpo.db.couchdb.podcast_state import podcast_states_for_device
+from mygpo.db.couchdb.user import set_device_deleted, unsync_device
 
 
 @vary_on_cookie
@@ -262,19 +263,10 @@ def symbian_opml(request, device):
 @login_required
 @allowed_methods(['POST'])
 def delete(request, device):
-
-    @repeat_on_conflict(['user'])
-    def _delete(user, device):
-        if request.user.is_synced(device):
-            request.user.unsync_device(device)
-        device.deleted = True
-        user.set_device(device)
-        if user.is_synced(device):
-            user.unsync_device(device)
-        user.save()
-
-    _delete(user=request.user, device=device)
-
+    user = request.user
+    unsync_device(user, device)
+    set_device_deleted(user, device, True)
+    set_device_task_state.delay(user)
     return HttpResponseRedirect(reverse('devices'))
 
 
@@ -303,10 +295,9 @@ def delete_permanently(request, device):
 @device_decorator
 @login_required
 def undelete(request, device):
-
-    device.deleted = False
-    request.user.update_device(device)
-
+    user = request.user
+    set_device_deleted(user, device, False)
+    set_device_task_state.delay(user)
     return HttpResponseRedirect(reverse('device', args=[device.uid]))