empty_asset_trashcan.py 1.11 KB
Newer Older
1
from django.core.management.base import BaseCommand, CommandError
2 3 4 5
from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey
from opaque_keys.edx.locations import SlashSeparatedCourseKey

6 7
from xmodule.contentstore.utils import empty_asset_trashcan
from xmodule.modulestore.django import modulestore
8

9 10 11 12 13 14 15 16
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:
17
            raise CommandError("empty_asset_trashcan requires one or no arguments: |<course_id>|")
18 19

        if len(args) == 1:
20 21 22 23 24 25
            try:
                course_key = CourseKey.from_string(args[0])
            except InvalidKeyError:
                course_key = SlashSeparatedCourseKey.from_deprecated_string(args[0])

            course_ids = [course_key]
26
        else:
27
            course_ids = [course.id for course in modulestore().get_courses()]
28 29

        if query_yes_no("Emptying trashcan. Confirm?", default="no"):
30
            empty_asset_trashcan(course_ids)