mgmt command for importing episode actions from file
authorStefan Kögl <stefan@skoegl.net>
Sat, 27 Apr 2013 12:00:07 +0000 (27 14:00 +0200)
committerStefan Kögl <stefan@skoegl.net>
Sat, 27 Apr 2013 12:00:07 +0000 (27 14:00 +0200)
mygpo/maintenance/management/commands/import-episode-actions.py [new file with mode: 0755]

diff --git a/mygpo/maintenance/management/commands/import-episode-actions.py b/mygpo/maintenance/management/commands/import-episode-actions.py
new file mode 100755 (executable)
index 0000000..8eb4a6a
--- /dev/null
@@ -0,0 +1,49 @@
+from datetime import datetime
+import os.path
+
+from django.core.management.base import BaseCommand
+
+from mygpo.users.models import User
+from mygpo.api.advanced import update_episodes
+from mygpo.core.json import json
+from mygpo.utils import progress
+
+
+class Command(BaseCommand):
+    """Imports episode actions from a file named <userid>-<something>"""
+
+    def handle(self, *args, **options):
+
+        path = args[0]
+
+        for filename in os.listdir(path):
+            user_id, _ = filename.split('-', 1)
+            filename = os.path.join(path, filename)
+            self.import_file(user_id, filename)
+            print
+
+
+    def import_file(self, user_id, filename):
+
+        progress(0, 100, filename)
+
+        with open(filename, 'r') as f:
+            actions = json.load(f)
+
+        progress(0, len(actions), filename)
+
+        user = User.get(user_id)
+
+        now = datetime.now()
+
+        batch_size = 100
+
+        count = len(actions) / batch_size
+
+        for low in range(0, len(actions), batch_size):
+            high = low+batch_size
+            batch = actions[low:high]
+
+            update_episodes(user, batch, now, None)
+
+            progress(high, len(actions), filename)