<commit_before>"""
Command to load course overviews.
"""
import logging
from optparse import make_option

from django.core.management.base import BaseCommand, CommandError
from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey
from xmodule.modulestore.django import modulestore

from openedx.core.djangoapps.content.course_overviews.models import CourseOverview


log = logging.getLogger(__name__)


class Command(BaseCommand):
    """
    Example usage:
        $ ./manage.py lms generate_course_overview --all --settings=devstack
        $ ./manage.py lms generate_course_overview 'edX/DemoX/Demo_Course' --settings=devstack
    """
    args = '<course_id course_id ...>'
    help = 'Generates and stores course overview for one or more courses.'

    option_list = BaseCommand.option_list + (
        make_option('--all',
                    action='store_true',
                    default=False,
                    help='Generate course overview for all courses.'),
    )

    def handle(self, *args, **options):

        course_keys = []
        if options['all']:
            course_keys = [course.id for course in modulestore().get_courses()]
        else:
            if len(args) < 1:
                raise CommandError('At least one course or --all must be specified.')
            try:
                course_keys = [CourseKey.from_string(arg) for arg in args]
            except InvalidKeyError:
                log.fatal('Invalid key specified.')

            if not course_keys:
                log.fatal('No courses specified.')

        CourseOverview.get_select_courses(course_keys)<commit_msg>Update this command for Django 1.8<commit_after>"""
Command to load course overviews.
"""
import logging
from optparse import make_option

from django.core.management.base import BaseCommand, CommandError
from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey
from xmodule.modulestore.django import modulestore

from openedx.core.djangoapps.content.course_overviews.models import CourseOverview


log = logging.getLogger(__name__)


class Command(BaseCommand):
    """
    Example usage:
        $ ./manage.py lms generate_course_overview --all --settings=devstack
        $ ./manage.py lms generate_course_overview 'edX/DemoX/Demo_Course' --settings=devstack
    """
    args = '<course_id course_id ...>'
    help = 'Generates and stores course overview for one or more courses.'

    def add_arguments(self, parser):
        """
        Add arguments to the command parser.
        """
        parser.add_argument(
            '--all',
            action='store_true',
            dest='all',
            default=False,
            help='Generate course overview for all courses.',
        )

    def handle(self, *args, **options):

        course_keys = []
        if options['all']:
            course_keys = [course.id for course in modulestore().get_courses()]
        else:
            if len(args) < 1:
                raise CommandError('At least one course or --all must be specified.')
            try:
                course_keys = [CourseKey.from_string(arg) for arg in args]
            except InvalidKeyError:
                log.fatal('Invalid key specified.')

            if not course_keys:
                log.fatal('No courses specified.')

        CourseOverview.get_select_courses(course_keys)
{'fraction_non_alphanumeric': 0.09388839681133747, 'fraction_numerical': 0.00118098612341305, 'mean_word_length': 2.4642126789366054, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7665487', 'n_tokens_mistral': 947, 'n_tokens_neox': 886, 'n_words': 253}