Skip to content
This repository has been archived by the owner on Jan 28, 2020. It is now read-only.

Commit

Permalink
Merge pull request #443 from mitodl/feature/gs/export_checkboxes
Browse files Browse the repository at this point in the history
Added checkboxes to allow user to uncheck items in export panel - I've run through the functionality.
  • Loading branch information
pwilkins committed Aug 5, 2015
2 parents 6f66523 + db63e12 commit 869c145
Show file tree
Hide file tree
Showing 8 changed files with 466 additions and 54 deletions.
6 changes: 3 additions & 3 deletions exporter/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

from __future__ import unicode_literals

from tempfile import mkdtemp, mkstemp
import tarfile
import os
from shutil import rmtree
import re
import tarfile
from shutil import rmtree
from tempfile import mkdtemp, mkstemp

from django.utils.text import slugify
from django.core.files.storage import default_storage
Expand Down
6 changes: 4 additions & 2 deletions rest/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ def get_learning_resource_export_task(self, repo_slug, task_id,
if expected_status == HTTP_200_OK:
return as_json(resp)

def create_learning_resource_export_task(self, repo_slug,
def create_learning_resource_export_task(self, repo_slug, input_dict,
expected_status=HTTP_201_CREATED):
"""
Helper method to create a task for the user to export a tarball of
Expand All @@ -769,7 +769,9 @@ def create_learning_resource_export_task(self, repo_slug,
'/api/v1/repositories/{repo_slug}/'
'learning_resource_export_tasks/'.format(
repo_slug=repo_slug,
)
),
json.dumps(input_dict),
content_type='application/json',
)
self.assertEqual(expected_status, resp.status_code)
if expected_status == HTTP_201_CREATED:
Expand Down
22 changes: 20 additions & 2 deletions rest/tests/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
from tempfile import mkdtemp
from shutil import rmtree
import tarfile
import os

from django.utils.text import slugify
from django.test import override_settings
from rest_framework.status import HTTP_200_OK, HTTP_404_NOT_FOUND
from six import BytesIO
Expand All @@ -28,15 +30,19 @@ class TestExport(RESTTestCase):
)
def test_create_new_task(self):
"""Test a basic export."""
self.import_course_tarball(self.repo)
resources = LearningResource.objects.filter(
course__repository__id=self.repo.id).all()
for resource in resources:
self.create_learning_resource_export(self.repo.slug, {
"id": resource.id
})

# Skip first one to test that it's excluded from export.
task_id = self.create_learning_resource_export_task(
self.repo.slug)['id']
self.repo.slug,
{"ids": [r.id for r in resources[1:]]}
)['id']

result = self.get_learning_resource_export_tasks(
self.repo.slug)['results'][0]
Expand All @@ -61,11 +67,23 @@ def test_create_new_task(self):
self.assertEqual(HTTP_200_OK, resp.status_code)

tempdir = mkdtemp()

def make_path(resource):
"""Create a path that should exist for a resource."""
type_name = resource.learning_resource_type.name
return os.path.join(
tempdir, type_name, "{id}_{url_name}.xml".format(
id=resource.id,
url_name=slugify(resource.url_name)[:200],
)
)
try:
fakefile = BytesIO(b"".join(resp.streaming_content))
with tarfile.open(fileobj=fakefile, mode="r:gz") as tar:
tar.extractall(path=tempdir)

assert_resource_directory(self, resources, tempdir)
self.assertFalse(os.path.isfile(make_path(resources[0])))
assert_resource_directory(self, resources[1:], tempdir)

finally:
rmtree(tempdir)
13 changes: 10 additions & 3 deletions rest/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,9 +678,16 @@ def post(self, request, *args, **kwargs):

repo_slug = self.kwargs['repo_slug']
try:
exports = self.request.session[EXPORTS_KEY][repo_slug]
exports = set(self.request.session[EXPORTS_KEY][repo_slug])
except KeyError:
exports = []
exports = set()

ids = self.request.data['ids']
for resource_id in ids:
if resource_id not in exports:
raise ValidationError("id {id} is not in export list".format(
id=resource_id
))

# Cancel any old tasks.
old_tasks = self.request.session.get(
Expand All @@ -694,7 +701,7 @@ def post(self, request, *args, **kwargs):
self.request.session[EXPORT_TASK_KEY][repo_slug] = {}

learning_resources = LearningResource.objects.filter(
id__in=exports).all()
id__in=ids).all()
result = export_resources.delay(
learning_resources, self.request.user.username)

Expand Down
Loading

0 comments on commit 869c145

Please sign in to comment.