<commit_before>import logging

from django.core.management.base import BaseCommand
from django.conf import settings
from optparse import make_option
from queue.models import Submission
from queue.consumer import post_failure_to_lms

log = logging.getLogger(__name__)


class Command(BaseCommand):
    args = "<queue_name>"
    help = "Retire submissions that have more than settings.MAX_NUMBER_OF_FAILURES failures. Notify the LMS/student that the queue will no longer attempt to process the submission. Optional <queue_name>"

    option_list = BaseCommand.option_list + (
        make_option('-f','--force',
            action='store_true',
            dest='force',
            default=False,
            help='Force retire submissions'),
        )

    def handle(self, *args, **options):
        log.info(' [*] Scanning Submission database to retire failed submissions...')
        
        if len(args) > 1:
            queue_name = args[0]
            failed_submissions = Submission.objects.select_for_update().filter(queue_name=queue_name, lms_ack=False)
        else:
            failed_submissions = Submission.objects.select_for_update().filter(lms_ack=False)

        failed_submissions = failed_submissions.exclude(num_failures=0)
        
        for failed_submission in failed_submissions:
            if failed_submission.num_failures >= settings.MAX_NUMBER_OF_FAILURES:
                log.info(" [ ] Retiring submission id=%d from queue '%s' with num_failures=%d" %\
                            (failed_submission.id, failed_submission.queue_name, failed_submission.num_failures))
                failed_submission.lms_ack = post_failure_to_lms(failed_submission.xqueue_header)
                if not failed_submission.lms_ack:
                    log.error(' [ ] Could not contact LMS to retire submission id=%d' % failed_submission.id)
                    if options['force']:
                        failed_submission.lms_ack = True # Forcibly mark as done
                failed_submission.save()<commit_msg>Force option marks submissions as done without contacting LMS<commit_after>import logging

from django.core.management.base import BaseCommand
from django.conf import settings
from optparse import make_option
from queue.models import Submission
from queue.consumer import post_failure_to_lms

log = logging.getLogger(__name__)


class Command(BaseCommand):
    args = "<queue_name>"
    help = "Retire submissions that have more than settings.MAX_NUMBER_OF_FAILURES failures. Notify the LMS/student that the queue will no longer attempt to process the submission. Optional <queue_name>"

    option_list = BaseCommand.option_list + (
        make_option('-f','--force',
            action='store_true',
            dest='force',
            default=False,
            help='Force retire submissions'),
        )

    def handle(self, *args, **options):
        log.info(' [*] Scanning Submission database to retire failed submissions...')
        
        if len(args) > 1:
            queue_name = args[0]
            failed_submissions = Submission.objects.select_for_update().filter(queue_name=queue_name, lms_ack=False)
        else:
            failed_submissions = Submission.objects.select_for_update().filter(lms_ack=False)

        failed_submissions = failed_submissions.exclude(num_failures=0)
        
        for failed_submission in failed_submissions:
            if failed_submission.num_failures >= settings.MAX_NUMBER_OF_FAILURES:
                log.info(" [ ] Retiring submission id=%d from queue '%s' with num_failures=%d" %\
                            (failed_submission.id, failed_submission.queue_name, failed_submission.num_failures))
                if options['force']:
                    log.info(" [ ] Force retiring all failed submissions...")
                    failed_submission.lms_ack = True # Mark as done without contacting LMS
                else:
                    failed_submission.lms_ack = post_failure_to_lms(failed_submission.xqueue_header)
                    if not failed_submission.lms_ack:
                        log.error(' [ ] Could not contact LMS to retire submission id=%d' % failed_submission.id)
                failed_submission.save()
{'fraction_non_alphanumeric': 0.08297872340425531, 'fraction_numerical': 0.0014184397163120568, 'mean_word_length': 2.6919720767888307, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '1362453', 'n_tokens_mistral': 1169, 'n_tokens_neox': 1114, 'n_words': 276}