Skip to content

Commit

Permalink
Annotate content db after unpacking assessment zip - fixes learningeq…
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaoming committed Feb 5, 2016
1 parent 3d62e24 commit b42d920
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
19 changes: 9 additions & 10 deletions kalite/contentload/management/commands/unpack_assessment_zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from django.conf import settings as django_settings
from django.core.management.base import BaseCommand, CommandError
from django.core.management import call_command

logging = django_settings.LOG

Expand Down Expand Up @@ -39,17 +40,17 @@ def handle(self, *args, **kwargs):
return

if is_valid_url(ziplocation): # url; download the zip
print "Downloading assessment item data from a remote server. Please be patient; this file is big, so this may take some time..."
logging.info("Downloading assessment item data from a remote server. Please be patient; this file is big, so this may take some time...")
# this way we can download stuff larger than the device's RAM
r = requests.get(ziplocation, prefetch=False)
content_length = r.headers.get("Content-Length")
print "Downloaded size: ", str(int(content_length) // 1024 // 1024) + " MB" if content_length else "Unknown"
logging.info("Downloaded size: ", str(int(content_length) // 1024 // 1024) + " MB" if content_length else "Unknown")
sys.stdout.write("Downloading file...")
sys.stdout.flush()
f = tempfile.TemporaryFile("r+")
r.raise_for_status()
for cnt, chunk in enumerate(r.iter_content(chunk_size=1024)):
if chunk: # filter out keep-alive new chunks
if chunk: # filter out keep-alive new chunks
f.write(chunk)
if cnt % 1000 == 0:
sys.stdout.write(".")
Expand All @@ -60,10 +61,14 @@ def handle(self, *args, **kwargs):
else: # file; just open it normally
f = open(ziplocation, "rb")

print "Unpacking..."
logging.info("Unpacking...")
zf = zipfile.ZipFile(f, "r")
unpack_zipfile_to_content_folder(zf)

logging.info("Scanning items and updating content db...")
call_command("annotate_content_items")
logging.info("Done, assessment items installed and everything updated. Refresh your browser!")


def should_upgrade_assessment_items():
# if assessmentitems.version doesn't exist, then we assume
Expand Down Expand Up @@ -101,12 +106,6 @@ def unpack_zipfile_to_content_folder(zf):
os.path.join(folder, 'assessmentitems.version'),
settings.KHAN_ASSESSMENT_ITEM_VERSION_PATH
)
# JSON file is apparrently not required (not in the test at least)
if os.path.isfile(os.path.join(folder, 'assessmentitems.json')):
shutil.move(
os.path.join(folder, 'assessmentitems.json'),
settings.KHAN_ASSESSMENT_ITEM_JSON_PATH
)


def is_valid_url(url):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import os

from django.core.management import call_command
from optparse import make_option

from django.conf import settings as django_settings
Expand All @@ -12,8 +9,6 @@

from django.core.management.base import BaseCommand

from django.utils.translation import gettext as _


class Command(BaseCommand):

Expand Down

0 comments on commit b42d920

Please sign in to comment.