<commit_before>#!/usr/bin/env python

from optparse import OptionParser
import logging

from common import (setup_logging, load_devilry_plugins, add_debug_opt,
    add_quiet_opt, add_settings_option, set_django_settings_module)


extra_help = """


"""


p = OptionParser(
        usage = "%prog [options] <action>")
add_settings_option(p)
p.add_option("--djangoadmin-script", dest="djangoadmin",
        default='django-admin.py',
        help="Path to the django-admin.py script. Defaults to "\
            "'django-admin.py', which means that it must be on the PATH.",
            metavar="PATH")
p.add_option("--no-backup-on-restore", action="store_false",
    dest="backup_on_restore", default=True,
    help="Normally a backup is made before a restore (because restore "\
        "destroys data). Use this to skip the backup.")
add_quiet_opt(p)
add_debug_opt(p)
(opt, args) = p.parse_args()
setup_logging(opt)

# Django must be imported after setting DJANGO_SETTINGS_MODULE
set_django_settings_module(opt)
load_devilry_plugins()
from devilry.core.models import Feedback, Assignment
from devilry.core.gradeplugin import GradePluginDoesNotExistError, \
    WrongContentTypeError, GradePluginError


def exit_help(msg=""):
    p.print_help()
    print
    print extra_help
    raise SystemExit(msg)

def validate_gradeplugins():
    for assignment in Assignment.objects.all():
        try:
            assignment.validate_gradeplugin()
        except GradePluginDoesNotExistError, e:
            logging.error("%s: %s" % (assignment, str(e)))
        else:
            logging.info('%-70s  [ OK ]' % assignment)

def validate_gradeplugin_contenttypes():
    for feedback in Feedback.objects.all():
        try:
            feedback.validate_gradeobj()
        except GradePluginError, e:
            logging.error("%s: %s" % (feedback, str(e)))
        else:
            logging.info("%-70s  [ OK ]" % feedback)


if len(args) == 0:
    exit_help()
action = args[0]

if action == "fix-gradeplugin-content-type-errors":
    for feedback in Feedback.objects.all():
        try:
            feedback.validate_gradeobj()
        except GradePluginDoesNotExistError, e:
            logging.error("%s: %s" % (feedback, str(e)))
        except WrongContentTypeError, e:
            assignment = feedback.delivery.assignment_group.parentnode
            correct_ct = assignment.get_gradeplugin_registryitem().get_content_type()
            feedback.content_type = correct_ct
            feedback.save()<commit_msg>dbadmin help<commit_after>#!/usr/bin/env python

from optparse import OptionParser
import logging

from common import (setup_logging, load_devilry_plugins, add_debug_opt,
    add_quiet_opt, add_settings_option, set_django_settings_module)


extra_help = """
Actions:
    validate-gradeplugins
        Check that all grade plugins used in the database is
        available/correct.
    
    validate-gradeplugin-content-types
        AssignmentGroup objects have a grade attribute that should be a
        instance of the grade plugin of the Assignment. This that this is
        correct onchecks all AssignmentGroups.

    fix-gradeplugin-content-type-errors
        Fixes any of the errors explained in the
        <validate-gradeplugin-content-types> action.
"""


p = OptionParser(
        usage = "%prog [options] <action>")
add_settings_option(p)
p.add_option("--djangoadmin-script", dest="djangoadmin",
        default='django-admin.py',
        help="Path to the django-admin.py script. Defaults to "\
            "'django-admin.py', which means that it must be on the PATH.",
            metavar="PATH")
p.add_option("--no-backup-on-restore", action="store_false",
    dest="backup_on_restore", default=True,
    help="Normally a backup is made before a restore (because restore "\
        "destroys data). Use this to skip the backup.")
add_quiet_opt(p)
add_debug_opt(p)
(opt, args) = p.parse_args()
setup_logging(opt)

# Django must be imported after setting DJANGO_SETTINGS_MODULE
set_django_settings_module(opt)
load_devilry_plugins()
from devilry.core.models import Feedback, Assignment, AssignmentGroup
from devilry.core.gradeplugin import (GradePluginDoesNotExistError,
        WrongContentTypeError, GradePluginError)


def exit_help(msg=""):
    p.print_help()
    print
    print extra_help
    raise SystemExit(msg)

def validate_gradeplugins():
    for assignment in Assignment.objects.all():
        try:
            assignment.validate_gradeplugin()
        except GradePluginDoesNotExistError, e:
            logging.error("%s: %s" % (assignment, str(e)))
        else:
            logging.info('%-70s  [ OK ]' % assignment)

def validate_gradeplugin_contenttypes():
    for feedback in Feedback.objects.all():
        try:
            feedback.validate_gradeobj()
        except GradePluginError, e:
            logging.error("%s: %s" % (feedback, str(e)))
        else:
            logging.info("%-70s  [ OK ]" % feedback)

def check_assignmentgroup_status():
    for ag in AssignmentGroup.objects.all():
        correct_status = ag._get_status_from_qry()
        if ag.status == correct_status:
            logging.info("%-70s  [ OK ]" % ag)
        else:
            logging.error("%-70s  [ WRONG STATUS ]" % ag)
            logging.error("%s status is %s, should be %s." % (ag, ag.status,
                correct_status))


if len(args) == 0:
    exit_help()
action = args[0]

if action == "fix-gradeplugin-content-type-errors":
    for feedback in Feedback.objects.all():
        try:
            feedback.validate_gradeobj()
        except GradePluginDoesNotExistError, e:
            logging.error("%s: %s" % (feedback, str(e)))
        except WrongContentTypeError, e:
            assignment = feedback.delivery.assignment_group.parentnode
            correct_ct = assignment.get_gradeplugin_registryitem().get_content_type()
            feedback.content_type = correct_ct
            feedback.save()
{'fraction_non_alphanumeric': 0.10705268473320989, 'fraction_numerical': 0.0026931493014644, 'mean_word_length': 3.064295485636115, 'pattern_counts': {'":': 2, '<': 6, '<?xml version=': 0, '>': 6, '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': '4540409', 'n_tokens_mistral': 1786, 'n_tokens_neox': 1738, 'n_words': 425}