<commit_before>"""
django-critic: utils
"""

from django.contrib.contenttypes.models import ContentType
from django.template.loader import get_template, render_to_string

from critic.modules import method_for_instance

def render(obj, template=None, **extra_context):
    t = None
    try:
        ct = ContentType.objects.get_for_model(obj)
        method = method_for_instance(obj)
    except:
        return None

    model = ct.model.lower()
    app = ct.app_label.lower()
       
    try:
        # Retreive the template specified
        t = get_template(template)
    except:
        try:
            # Retrieve the template based of off the content object
            t = get_template('critic/%s__%s.html' % (app, model)) 
        except:
            t = get_template('critic/render.html')
            
    if not t: 
        return None
    
    # The conext that will be passed to the rendered template.
    from critic.models import RatingData
    context = {
        'instance': obj, 
        'method': method, 
        'content_type_id': ct.pk,
        'average': RatingData.objects.average(obj),
        'total': RatingData.objects.total(obj)}
        
    context.update(extra_context)<commit_msg>FIxed up the render method<commit_after>"""
django-critic: utils
"""

from django.contrib.contenttypes.models import ContentType
from django.template.loader import get_template, render_to_string

from critic.modules import method_for_instance

def render(obj, **extra_context):
    t = None
    try:
        ct = ContentType.objects.get_for_model(obj)
        method = method_for_instance(obj)
    except:
        return None

    model = ct.model.lower()
    app = ct.app_label.lower()
       
    try:
        # Retreive the template from the method.
        t = get_template(method.template)
    except:
        try:
            # Retrieve the template based of off the content object
            t = get_template('critic/%s__%s.html' % (app, model)) 
        except:
            # Use the default if no template was found.
            t = get_template('critic/render.html')
            
    if not t: 
        return None
    
    # The conext that will be passed to the rendered template.
    from critic.models import RatingData
    context = {
        'obj': obj, 
        'method': method, 
        'content_type_id': ct.pk,
        'average': RatingData.objects.average(obj),
        'total': RatingData.objects.total(obj)}
        
    context.update(extra_context)
{'fraction_non_alphanumeric': 0.09248090068355448, 'fraction_numerical': 0.0, 'mean_word_length': 2.3173333333333335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7171183', 'n_tokens_mistral': 730, 'n_tokens_neox': 708, 'n_words': 192}