<commit_before>from django import template
from django.conf import settings
from django.core.urlresolvers import reverse

from django.contrib.contenttypes.models import ContentType

from phileo.models import Like

register = template.Library()


class LikesNode(template.Node):
    
    def __init__(self, user, model_list, varname):
        self.user = template.Variable(user)
        self.model_list = [template.Variable(m) for m in model_list]
        self.varname = varname
    
    def render(self, context):
        user = self.user.resolve(context)
        content_types = []
        for model_name in self.model_list:
            app, model = model_name.resolve(context).split(".")
            content_type = ContentType.objects.get(app_label=app, model__iexact=model)
            content_types.append(content_type)
        context[self.varname] = Like.objects.filter(
            sender=user,
            receiver_content_type__in=content_types
        )
        return ""


@register.tag
def likes(parser, token):
    """
    {% likes user "app.Model" "app.Model" "app.Model" as like_objs %}
    """
    tokens = token.split_contents()
    user = tokens[1]
    varname = tokens[-1]
    model_list = tokens[2:-2]
    return LikesNode(user, model_list, varname)


@register.filter
def likes_count(obj):
    """
    Something like:
    
        <div class="likes_count">{{ obj|likes_count }}</div>
    
    will render:
    
        <div class="likes_count">34</div>
    """
    return Like.objects.filter(
        receiver_content_type=ContentType.objects.get_for_model(obj),
        receiver_object_id=obj.pk
    ).count()


@register.inclusion_tag("phileo/_css.html")
def phileo_css():
    return {"STATIC_URL": settings.STATIC_URL}


@register.inclusion_tag("phileo/_js.html")
def phileo_js():
    return {"STATIC_URL": settings.STATIC_URL}


@register.inclusion_tag("phileo/_widget.html")
def phileo_widget(user, obj, widget_id=None, like_type="like", toggle_class="phileo-liked"):
    ct = ContentType.objects.get_for_model(obj)

    like_count = Like.objects.filter(
       receiver_content_type = ct,
       receiver_object_id = obj.pk
    ).count()

    if widget_id == None:
        widget_id = "phileo_%s_%s_%s" % (like_type, ct.pk, obj.pk)

    like_count_id = "%s_count" % widget_id

    if user.is_anonymous():
        liked = False
        like_url = settings.LOGIN_URL
    else:
        like_url = reverse("phileo_like_toggle", kwargs={
            "content_type_id": ct.id,
            "object_id": obj.pk
        })
        liked = Like.objects.filter(
           sender = user,
           receiver_content_type = ContentType.objects.get_for_model(obj),
           receiver_object_id = obj.pk
        ).exists()

    return {
        "user": user,
        "like_url": like_url,
        "widget_id": widget_id,
        "like_type": like_type,
        "like_count": like_count,<commit_msg>Removed an extra ContentType lookup<commit_after>from django import template
from django.conf import settings
from django.core.urlresolvers import reverse

from django.contrib.contenttypes.models import ContentType

from phileo.models import Like

register = template.Library()


class LikesNode(template.Node):
    
    def __init__(self, user, model_list, varname):
        self.user = template.Variable(user)
        self.model_list = [template.Variable(m) for m in model_list]
        self.varname = varname
    
    def render(self, context):
        user = self.user.resolve(context)
        content_types = []
        for model_name in self.model_list:
            app, model = model_name.resolve(context).split(".")
            content_type = ContentType.objects.get(app_label=app, model__iexact=model)
            content_types.append(content_type)
        context[self.varname] = Like.objects.filter(
            sender=user,
            receiver_content_type__in=content_types
        )
        return ""


@register.tag
def likes(parser, token):
    """
    {% likes user "app.Model" "app.Model" "app.Model" as like_objs %}
    """
    tokens = token.split_contents()
    user = tokens[1]
    varname = tokens[-1]
    model_list = tokens[2:-2]
    return LikesNode(user, model_list, varname)


@register.filter
def likes_count(obj):
    """
    Something like:
    
        <div class="likes_count">{{ obj|likes_count }}</div>
    
    will render:
    
        <div class="likes_count">34</div>
    """
    return Like.objects.filter(
        receiver_content_type=ContentType.objects.get_for_model(obj),
        receiver_object_id=obj.pk
    ).count()


@register.inclusion_tag("phileo/_css.html")
def phileo_css():
    return {"STATIC_URL": settings.STATIC_URL}


@register.inclusion_tag("phileo/_js.html")
def phileo_js():
    return {"STATIC_URL": settings.STATIC_URL}


@register.inclusion_tag("phileo/_widget.html")
def phileo_widget(user, obj, widget_id=None, like_type="like", toggle_class="phileo-liked"):
    ct = ContentType.objects.get_for_model(obj)

    like_count = Like.objects.filter(
       receiver_content_type = ct,
       receiver_object_id = obj.pk
    ).count()

    if widget_id == None:
        widget_id = "phileo_%s_%s_%s" % (like_type, ct.pk, obj.pk)

    like_count_id = "%s_count" % widget_id

    if user.is_anonymous():
        liked = False
        like_url = settings.LOGIN_URL
    else:
        like_url = reverse("phileo_like_toggle", kwargs={
            "content_type_id": ct.id,
            "object_id": obj.pk
        })
        liked = Like.objects.filter(
           sender = user,
           receiver_content_type = ct,
           receiver_object_id = obj.pk
        ).exists()

    return {
        "user": user,
        "like_url": like_url,
        "widget_id": widget_id,
        "like_type": like_type,
        "like_count": like_count,
{'fraction_non_alphanumeric': 0.11669535283993115, 'fraction_numerical': 0.002065404475043029, 'mean_word_length': 2.785667752442997, 'pattern_counts': {'":': 18, '<': 11, '<?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': '6795908', 'n_tokens_mistral': 1897, 'n_tokens_neox': 1867, 'n_words': 353}