Esempio n. 1
0
def generate_rubric_object(grader, scores, rubric_xml):
    success, max_scores=generate_targets_from_rubric(rubric_xml)
    if not success:
        return False, "Could not parse rubric XML to extract max scores."
    for i in xrange(0,len(scores)):
        score=scores[i]
        try:
            score=int(score)
        except:
            return False, "Scores must be numeric."
        if score<0:
            return False, "Scores cannot be below zero. : {0}".format(score)
        if score>max_scores[i]:
            return False, "Score: {0} is greater than max score for this item: {1}".format(score, max_scores[i])

    try:
        rubric=Rubric(
            grader=grader,
            rubric_version=RUBRIC_VERSION,
            finished_scoring=True,
        )
        rubric.save()
        success, rubric_items=parse_rubric(rubric_xml)
        if not success:
            return False, "Could not parsed rubric items properly: {0}".format(rubric_items)
        if len(scores)!=len(rubric_items):
            return False, "Length of passed in scores: {0} does not match number of rubric items: {1}".format(len(scores), len(rubric_items))

        for i in xrange(0,len(rubric_items)):
            rubric_item=rubric_items[i]
            description = rubric_item['description']
            options = rubric_item['options']

            text=description
            score=scores[i]
            max_score=len(options)

            rubric_item=RubricItem(
                rubric=rubric,
                text=text,
                score=score,
                item_number=i,
                max_score=max_score,
                finished_scoring=True,
            )
            rubric_item.save()
            for z in xrange(0,len(options)):
                option=options[z]
                rubric_option=RubricOption(
                    points=z,
                    text = option,
                    item_number=z,
                    rubric_item=rubric_item,
                )
                rubric_option.save()
        return True, rubric
    except:
        error_message="Could not save and/or parse rubric properly"
        log.info(error_message)
        return False, error_message
Esempio n. 2
0
def create_rubric_objects(rubric_data, request):
    """
    For a given user and problem id, create a local rubric object
    rubric_data - the dictionary data associated with the rubric
    request - the request that the user has made
    """
    #Create the rubric
    rubric = Rubric(associated_problem = int(rubric_data['problem_id']), user = request.user)
    rubric.save()
    #Create each rubric option
    for option in rubric_data['options']:
        option = RubricOption(rubric=rubric, option_points =option['points'], option_text = option['text'])
        option.save()
Esempio n. 3
0
def create_rubric_objects(rubric_data, request):
    """
    For a given user and problem id, create a local rubric object
    rubric_data - the dictionary data associated with the rubric
    request - the request that the user has made
    """
    # Create the rubric
    rubric = Rubric(associated_problem=int(rubric_data['problem_id']), user=request.user)
    rubric.save()
    # Create each rubric option
    for option in rubric_data['options']:
        option = RubricOption(rubric=rubric, option_points=option['points'], option_text=option['text'])
        option.save()
Esempio n. 4
0
def generate_rubric_object(grader, scores, rubric_xml):
    success, max_scores = generate_targets_from_rubric(rubric_xml)
    if not success:
        error_message = "Could not parse rubric XML to extract max scores."
        log.error(error_message)
        return False, error_message
    for i in xrange(0, len(scores)):
        score = scores[i]
        try:
            score = int(score)
        except Exception:
            error_message = "Scores must be numeric."
            log.exception(error_message)
            return False, error_message
        if score < 0:
            error_message = "Scores cannot be below zero. : {0}".format(score)
            log.error(error_message)
            return False, error_message
        if score > max_scores[i]:
            return False, "Score: {0} is greater than max score for this item: {1}".format(
                score, max_scores[i])

    try:
        rubric = Rubric(
            grader=grader,
            rubric_version=RUBRIC_VERSION,
            finished_scoring=True,
        )
        rubric.save()
        success, rubric_items = parse_rubric(rubric_xml)
        if not success:
            error_message = "Could not parse rubric items properly: {0}".format(
                rubric_items)
            log.error(error_message)
            return False, error_message
        if len(scores) != len(rubric_items):
            error_message = "Length of passed in scores: {0} does not match number of rubric items: {1}".format(
                len(scores), len(rubric_items))
            log.error(error_message)
            return False, error_message

        for i in xrange(0, len(rubric_items)):
            rubric_item = rubric_items[i]
            description = rubric_item['description']
            options = rubric_item['options']

            text = description
            score = scores[i]
            max_score = len(options)

            rubric_item = RubricItem(
                rubric=rubric,
                text=text,
                score=score,
                item_number=i,
                max_score=max_score,
                finished_scoring=True,
            )
            rubric_item.save()
            for z in xrange(0, len(options)):
                option = options[z]
                rubric_option = RubricOption(
                    points=z,
                    text=option,
                    item_number=z,
                    rubric_item=rubric_item,
                )
                rubric_option.save()
        return True, rubric
    except Exception:
        error_message = "Could not save and/or parse rubric properly"
        log.error(error_message)
        return False, error_message
Esempio n. 5
0
def extract_rubric_handler(upload_file, rubricator, syntax, type, encoding):
    get = lambda node_id: Rubric.objects.get(pk=node_id)

    file_path = filework.save_content_to_file(upload_file, settings.SYSTEM_ROOT + 'appdata/tmp/', file_ext='mrc')
    records = []
    s = t()
    #root = Rubric.add_root(name='root', rubricator=rubricator)
    #    roots = Rubric.get_root_nodes()
    #
    #    if not roots:
    #        root = Rubric.add_root(name='root', rubricator=rubricator)
    #    else:
    #        root = roots[0]
    #    print root


    #node = get(root.id).add_child(name='Memory', rubricator=rubricator)
    #print node
    #node = get(root.id).add_child(name='CPU', rubricator=rubricator)
    #node.add_child(name='PENTIUM', rubricator=rubricator)
    #node = Rubric.objects.get(name=u'PENTIUM', rubricator=rubricator)
    #print node
    #print Rubric.dump_bulk()
    try:
        root = Rubric.objects.get(name=rubricator.name, parent=None)
    except Rubric.DoesNotExist:
        root = Rubric(name=rubricator.name, parent=None)
        root.save()

    #    rubric = Rubric(name=u'dddd')
    #    rubric.insert_at(root)
    #    root.save()
    #    return
    #    rubric = Rubric(name=u'Физика', parent=root, rubricator=rubricator)
    #    rubric.save()
    #
    #    rubric = Rubric(name=u'Ядерная', parent=rubric, rubricator=rubricator)
    #    rubric.save()
    #
    #    return

    if syntax == 'USMARC':
        reader = pymarc.MARCReader(file(file_path), encoding=encoding, to_unicode=True)
    else:
        reader = pymarc.UNIMARCReader(file(file_path), encoding=encoding, to_unicode=True)

        for i, record in enumerate(reader):
        #            rubric = None
        #            if record['606']:
        #                rubric = record['606']['a']
        #
        #            print record['100']['a'], rubric
        #            continue
            fields = record.get_fields('606')
            for field in fields:
                rubric_name = field['a'].strip(' .')

                if rubric_name:
                    try:
                        rubric = Rubric.objects.get(name=rubric_name, parent=root)
                    except Rubric.DoesNotExist:
                        rubric = Rubric(name=rubric_name, parent=root)
                        rubric.save()
                        #rubric = root.add_child(name=rubric_name,rubricator=rubricator)

                    subrubrics = field.get_subfields('x')
                    if len(subrubrics):
                        for subrubric_name in subrubrics:
                            subrubric_name = subrubric_name.strip(' .')

                            try:
                                subrubric = Rubric.objects.get(name=subrubric_name, parent=rubric)
                            except Rubric.DoesNotExist:
                                subrubric = Rubric(name=subrubric_name, parent=rubric)
                                subrubric.save()
                                #rubric.add_child(name=subrubric_name, rubricator=rubricator)

            if i % 50 == 0:
                transaction.commit()
                print 'commit'
            print i

            #                    print 'sb:', subrubric_name, rubric_name

    transaction.commit()
    print Rubric.objects.all().count()
    print 'time:', t() - s