-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Feature/cdodge/soft delete assets #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3fa6e77
add new collection configuration to support a 'trashcan' for assets. …
9b749c4
add ajax callback entry point to remove the asset
85b904f
fix sizing of the delete column
443d6a3
add CHANGELOG entry for work
274d899
update warning message
70ee4b8
add unit tests for asset delete/restore
cd0087c
use the new notification tools for the confirmation dialog
b443629
remove invalid comments
ab94b86
forgot to internationalize one string
7f1768f
put back the verbose switch in xml_importer.py
7e46447
there is no default for empty_asset_trashcan, caller must provide a list
dc94ea1
add unit test for emptying the trashcan
7cebb87
allow for an optional throw_on_not_found on contentstore().find() so …
16e476e
refactored asset page related JS into it's own page
0d6f213
I think we need to add the new assets.js into the PIPELIE
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
cms/djangoapps/contentstore/management/commands/empty_asset_trashcan.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| from django.core.management.base import BaseCommand, CommandError | ||
| from xmodule.course_module import CourseDescriptor | ||
| from xmodule.contentstore.utils import empty_asset_trashcan | ||
| from xmodule.modulestore.django import modulestore | ||
| from .prompt import query_yes_no | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = '''Empty the trashcan. Can pass an optional course_id to limit the damage.''' | ||
|
|
||
| def handle(self, *args, **options): | ||
| if len(args) != 1 and len(args) != 0: | ||
| raise CommandError("empty_asset_trashcan requires one or no arguments: |<location>|") | ||
|
|
||
| locs = [] | ||
|
|
||
| if len(args) == 1: | ||
| locs.append(CourseDescriptor.id_to_location(args[0])) | ||
| else: | ||
| courses = modulestore('direct').get_courses() | ||
| for course in courses: | ||
| locs.append(course.location) | ||
|
|
||
| if query_yes_no("Emptying trashcan. Confirm?", default="no"): | ||
| empty_asset_trashcan(locs) |
13 changes: 13 additions & 0 deletions
13
cms/djangoapps/contentstore/management/commands/restore_asset_from_trashcan.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| from django.core.management.base import BaseCommand, CommandError | ||
| from xmodule.contentstore.utils import restore_asset_from_trashcan | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = '''Restore a deleted asset from the trashcan back to it's original course''' | ||
|
|
||
| def handle(self, *args, **options): | ||
| if len(args) != 1 and len(args) != 0: | ||
| raise CommandError("restore_asset_from_trashcan requires one argument: <location>") | ||
|
|
||
| restore_asset_from_trashcan(args[0]) | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,8 @@ | |
| from xmodule.modulestore import Location | ||
| from xmodule.contentstore.content import StaticContent | ||
| from xmodule.util.date_utils import get_default_time_display | ||
| from xmodule.modulestore import InvalidLocationError | ||
| from xmodule.exceptions import NotFoundError | ||
|
|
||
| from ..utils import get_url_reverse | ||
| from .access import get_location_and_verify_access | ||
|
|
@@ -78,10 +80,17 @@ def asset_index(request, org, course, name): | |
| 'active_tab': 'assets', | ||
| 'context_course': course_module, | ||
| 'assets': asset_display, | ||
| 'upload_asset_callback_url': upload_asset_callback_url | ||
| 'upload_asset_callback_url': upload_asset_callback_url, | ||
| 'remove_asset_callback_url': reverse('remove_asset', kwargs={ | ||
| 'org': org, | ||
| 'course': course, | ||
| 'name': name | ||
| }) | ||
| }) | ||
|
|
||
|
|
||
| @login_required | ||
| @ensure_csrf_cookie | ||
| def upload_asset(request, org, course, coursename): | ||
| ''' | ||
| cdodge: this method allows for POST uploading of files into the course asset library, which will | ||
|
|
@@ -145,6 +154,57 @@ def upload_asset(request, org, course, coursename): | |
| return response | ||
|
|
||
|
|
||
| @ensure_csrf_cookie | ||
| @login_required | ||
| def remove_asset(request, org, course, name): | ||
| ''' | ||
| This method will perform a 'soft-delete' of an asset, which is basically to copy the asset from | ||
| the main GridFS collection and into a Trashcan | ||
| ''' | ||
| get_location_and_verify_access(request, org, course, name) | ||
|
|
||
| location = request.POST['location'] | ||
|
|
||
| # make sure the location is valid | ||
| try: | ||
| loc = StaticContent.get_location_from_path(location) | ||
| except InvalidLocationError: | ||
| # return a 'Bad Request' to browser as we have a malformed Location | ||
| response = HttpResponse() | ||
| response.status_code = 400 | ||
| return response | ||
|
|
||
| # also make sure the item to delete actually exists | ||
| try: | ||
| content = contentstore().find(loc) | ||
| except NotFoundError: | ||
| response = HttpResponse() | ||
| response.status_code = 404 | ||
| return response | ||
|
|
||
| # ok, save the content into the trashcan | ||
| contentstore('trashcan').save(content) | ||
|
|
||
| # see if there is a thumbnail as well, if so move that as well | ||
| if content.thumbnail_location is not None: | ||
| try: | ||
| thumbnail_content = contentstore().find(content.thumbnail_location) | ||
| contentstore('trashcan').save(thumbnail_content) | ||
| # hard delete thumbnail from origin | ||
| contentstore().delete(thumbnail_content.get_id()) | ||
| # remove from any caching | ||
| del_cached_content(thumbnail_content.location) | ||
| except: | ||
| pass # OK if this is left dangling | ||
|
|
||
| # delete the original | ||
| contentstore().delete(content.get_id()) | ||
| # remove from cache | ||
| del_cached_content(content.location) | ||
|
|
||
| return HttpResponse() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may want to return a "204 No Content" HTTP status code. |
||
|
|
||
|
|
||
| @ensure_csrf_cookie | ||
| @login_required | ||
| def import_course(request, org, course, name): | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's remove_asset's behavior if the asset doesn't exist (or was already deleted)? Should you test using a garbage asset url? (Use case: 2 users try to delete same asset w/o reloading screen)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove_asset will return a HTTP 404 to the caller
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a test? Not crucial. I'm thinking of situations where such queries end up deleting everything or the wrong thing, but testing for that would be a pain and that outcome is very unlikely. (It's not like the client is sending a cursor back.)