1 from datetime
import datetime
4 from django
.core
.management
.base
import BaseCommand
6 from mygpo
.users
.models
import User
7 from mygpo
.api
.advanced
import update_episodes
8 from mygpo
.core
.json
import json
9 from mygpo
.utils
import progress
12 class Command(BaseCommand
):
13 """Imports episode actions from a file named <userid>-<something>"""
15 def handle(self
, *args
, **options
):
19 for filename
in os
.listdir(path
):
20 user_id
, _
= filename
.split('-', 1)
21 filename
= os
.path
.join(path
, filename
)
22 self
.import_file(user_id
, filename
)
26 def import_file(self
, user_id
, filename
):
28 progress(0, 100, filename
)
30 with
open(filename
, 'r') as f
:
31 actions
= json
.load(f
)
33 progress(0, len(actions
), filename
)
35 user
= User
.get(user_id
)
41 count
= len(actions
) / batch_size
43 for low
in range(0, len(actions
), batch_size
):
45 batch
= actions
[low
:high
]
47 update_episodes(user
, batch
, now
, None)
49 progress(high
, len(actions
), filename
)