from django import template
from collections import Iterable
register = template.Library()

@register.filter
def format_result(value):
    if not isinstance(value, Iterable):
        value = [value]
    output = ""
    for result in value:
        if result.grade == "A+":
            labelclass="label-success"
        elif result.grade == "A":
            labelclass="label-success"
        elif result.grade == "A-":
            labelclass="label-success"
        elif result.grade == "B":
            labelclass="label-warning"
        elif result.grade == "C":
            labelclass="label-warning"
        elif result.grade == "D":
            labelclass="label-danger"
        elif result.grade == "E":
            labelclass="label-danger"
        elif result.grade == "F":
            labelclass="label-danger"
        else:
            labelclass="label-default"
            result.grade = "<i class='fa fa-frown-o'></i>"
            if result.status_message and result.status_message != "":
                output += "<div style='font-size: 36px'><span data-content='%s' data-placement='right' data-original-title='Error Message' data-trigger='hover click' class='popoverlabel label %s'>%s</span></div>" % (result.status_message, labelclass, result.grade)
                continue
        output += "<div style='font-size: 36px'><span class='label %s'>%s</span></div>" % (labelclass, result.grade)
    return output


from django import template
from collections import Iterable
register = template.Library()

@register.filter
def format_result(value):
    if not isinstance(value, Iterable):
        value = [value]
    output = ""
    for result in value:
        if result.grade == "A+":
            labelclass="label-success"
            gradenumber = 8
        elif result.grade == "A":
            labelclass="label-success"
            gradenumber = 7
        elif result.grade == "A-":
            labelclass="label-success"
            gradenumber = 6
        elif result.grade == "B":
            labelclass="label-warning"
            gradenumber = 5
        elif result.grade == "C":
            labelclass="label-warning"
            gradenumber = 4
        elif result.grade == "D":
            labelclass="label-danger"
            gradenumber = 3
        elif result.grade == "E":
            labelclass="label-danger"
            gradenumber = 2
        elif result.grade == "F":
            labelclass="label-danger"
            gradenumber = 1
        else:
            labelclass="label-default"
            gradenumber = 0
            result.grade = "X"
            if result.status_message and result.status_message != "":
                output += "<span style='display: none'>%s</span><div style='font-size: 36px'><span data-content='%s' data-placement='right' data-original-title='Error Message' data-trigger='hover click' class='popoverlabel label %s'>%s</span></div>" % (gradenumber, result.status_message, labelclass, result.grade)
                continue
        output += "<span style='display: none'>%s</span><div style='font-size: 36px'><span class='label %s'>%s</span></div>" % (gradenumber, labelclass, result.grade)
    return output


fix sorting