<commit_before>from django import template
from django.contrib.auth.models import User
from friends.models import FriendshipRequest, Friendship, UserBlocks


register = template.Library()


class AddToFriendsNode(template.Node):
    def __init__(self, target_user, current_user='user',
                       template_name='friends/_add_to.html'):
        self.target_user = template.Variable(target_user)
        self.current_user = template.Variable(current_user)
        self.template_name = template_name

    def render(self, context):
        target_user = self.target_user.resolve(context)
        current_user = self.current_user.resolve(context)
        if current_user.is_authenticated():
            ctx = {'target_user': target_user,
                   'current_user': current_user}
            if not target_user is current_user:
                ctx['are_friends'] = Friendship.objects.are_friends(
                                                    target_user, current_user)
                ctx['is_invited'] = bool(FriendshipRequest.objects.filter(
                                                    from_user=current_user,
                                                    to_user=target_user,
                                                    accepted=False).count())
            return template.loader.render_to_string(self.template_name,
                                                    ctx,
                                                    context)
        else:
            return u''


class BlockUserLinkNode(template.Node):
    def __init__(self, target_user, current_user='user',
                       template_name='friends/_block.html'):
        self.target_user = template.Variable(target_user)
        self.current_user = template.Variable(current_user)
        self.template_name = template_name

    def render(self, context):
        target_user = self.target_user.resolve(context)
        current_user = self.current_user.resolve(context)
        if current_user.is_authenticated():
            ctx = {'target_user': target_user,
                   'current_user': current_user}
            if not target_user is current_user:
                ctx['is_blocked'] = bool(UserBlocks.objects.filter(
                         user=current_user, blocks__pk=target_user.pk).count())
            return template.loader.render_to_string(self.template_name,
                                                    ctx,
                                                    context)
        else:
            return u''


def add_to_friends(parser, token):
    bits = token.split_contents()
    tag_name, bits = bits[0], bits[1:]
    if not bits:
        raise template.TemplateSyntaxError(
                           '%s tag requires at least one argument' % tag_name)
    elif len(bits) > 3:
        raise template.TemplateSyntaxError(
                            '%s tag takes at most three arguments' % tag_name)
    if len(bits) == 3:
        if bits[2].startswith('"') and bits[2].endswith('"'):
            bits[2] = bits[2][1:-1]
        else:
            raise template.TemplateSyntaxError(
                      'Third argument for %s tag must be a string' % tag_name)
    return AddToFriendsNode(*bits)


def block_user(parser, token):
    bits = token.split_contents()
    tag_name, bits = bits[0], bits[1:]
    if not bits:
        raise template.TemplateSyntaxError(
                           '%s tag requires at least one argument' % tag_name)
    elif len(bits) > 3:
        raise template.TemplateSyntaxError(
                            '%s tag takes at most three arguments' % tag_name)
    if len(bits) == 3:
        if bits[2].startswith('"') and bits[2].endswith('"'):
            bits[2] = bits[2][1:-1]
        else:
            raise template.TemplateSyntaxError(
                      'Third argument for %s tag must be a string' % tag_name)
    return BlockUserLinkNode(*bits)


def friends_(value):
    try:
        user = _get_user(value)
    except ValueError:
        raise template.TemplateSyntaxError('friends filter can only be ' \
                                           'applied to User\'s or objects ' \
                                           'with a `user` attribute.')
    return Friendship.objects.friends_of(user)


def is_blocked_by(value, arg):
    try:
        user = _get_user(value)
    except ValueError:
        raise template.TemplateSyntaxError('isblockedby filter can only be ' \
                                           'applied to User\'s or objects ' \
                                           'with a `user` attribute.')
    try:
        target = _get_user(arg)
    except ValueError:
        raise template.TemplateSyntaxError('isblockedby filter\'s argument ' \
                                           'must be a User or an object ' \
                                           'with a `user` attribute.')
    return UserBlocks.objects.filter(user=target, blocks=user).exists()


def is_friends_with(value, arg):
    try:
        user = _get_user(value)
    except ValueError:
        raise template.TemplateSyntaxError('isfriendswith filter can only ' \
                                           'be applied to User\'s or ' \
                                           'objects with a `user` attribute.')
    try:
        target = _get_user(arg)
    except ValueError:
        raise template.TemplateSyntaxError('isfriendswith filter\'s ' \
                                           'argument must be a User or an ' \
                                           'object with a `user` attribute.')
    return Friendship.objects.are_friends(user, target)


def _get_user(value):
    if isinstance(value, User):
        return value
    elif hasattr(value, 'user') and isinstance(value.user, User):
        return value.user
    else:
        raise ValueError


register.filter('friends', friends_)
register.filter('isblockedby', is_blocked_by)
register.filter('isfriendswith', is_friends_with)
register.tag('addtofriends', add_to_friends)
register.tag('blockuser', block_user)<commit_msg>Refactored User resolution in filters<commit_after>from django import template
from django.contrib.auth.models import User
from friends.models import FriendshipRequest, Friendship, UserBlocks


register = template.Library()


class AddToFriendsNode(template.Node):
    def __init__(self, target_user, current_user='user',
                       template_name='friends/_add_to.html'):
        self.target_user = template.Variable(target_user)
        self.current_user = template.Variable(current_user)
        self.template_name = template_name

    def render(self, context):
        target_user = self.target_user.resolve(context)
        current_user = self.current_user.resolve(context)
        if current_user.is_authenticated():
            ctx = {'target_user': target_user,
                   'current_user': current_user}
            if not target_user is current_user:
                ctx['are_friends'] = Friendship.objects.are_friends(
                                                    target_user, current_user)
                ctx['is_invited'] = bool(FriendshipRequest.objects.filter(
                                                    from_user=current_user,
                                                    to_user=target_user,
                                                    accepted=False).count())
            return template.loader.render_to_string(self.template_name,
                                                    ctx,
                                                    context)
        else:
            return u''


class BlockUserLinkNode(template.Node):
    def __init__(self, target_user, current_user='user',
                       template_name='friends/_block.html'):
        self.target_user = template.Variable(target_user)
        self.current_user = template.Variable(current_user)
        self.template_name = template_name

    def render(self, context):
        target_user = self.target_user.resolve(context)
        current_user = self.current_user.resolve(context)
        if current_user.is_authenticated():
            ctx = {'target_user': target_user,
                   'current_user': current_user}
            if not target_user is current_user:
                ctx['is_blocked'] = bool(UserBlocks.objects.filter(
                         user=current_user, blocks__pk=target_user.pk).count())
            return template.loader.render_to_string(self.template_name,
                                                    ctx,
                                                    context)
        else:
            return u''


def add_to_friends(parser, token):
    bits = token.split_contents()
    tag_name, bits = bits[0], bits[1:]
    if not bits:
        raise template.TemplateSyntaxError(
                           '%s tag requires at least one argument' % tag_name)
    elif len(bits) > 3:
        raise template.TemplateSyntaxError(
                            '%s tag takes at most three arguments' % tag_name)
    if len(bits) == 3:
        if bits[2].startswith('"') and bits[2].endswith('"'):
            bits[2] = bits[2][1:-1]
        else:
            raise template.TemplateSyntaxError(
                      'Third argument for %s tag must be a string' % tag_name)
    return AddToFriendsNode(*bits)


def block_user(parser, token):
    bits = token.split_contents()
    tag_name, bits = bits[0], bits[1:]
    if not bits:
        raise template.TemplateSyntaxError(
                           '%s tag requires at least one argument' % tag_name)
    elif len(bits) > 3:
        raise template.TemplateSyntaxError(
                            '%s tag takes at most three arguments' % tag_name)
    if len(bits) == 3:
        if bits[2].startswith('"') and bits[2].endswith('"'):
            bits[2] = bits[2][1:-1]
        else:
            raise template.TemplateSyntaxError(
                      'Third argument for %s tag must be a string' % tag_name)
    return BlockUserLinkNode(*bits)


def friends_(value):
    user = _get_user_from_value('friends', value)
    return Friendship.objects.friends_of(user)


def is_blocked_by(value, arg):
    user = _get_user_from_value('isblockedby', value)
    target = _get_user_from_argument('isblockedby', arg)
    return UserBlocks.objects.filter(user=target, blocks=user).exists()


def is_friends_with(value, arg):
    user = _get_user_from_value('isfriendswith', value)
    target = _get_user_from_argument('isfriendswith', arg)
    return Friendship.objects.are_friends(user, target)


def _get_user(value):
    if isinstance(value, User):
        return value
    elif hasattr(value, 'user') and isinstance(value.user, User):
        return value.user
    else:
        raise ValueError


def _get_user_from_argument(filter_name, arg):
    try:
        return _get_user(arg)
    except ValueError:
        message = '%s filter\'s argument must be a User or ' \
                  'an object with a `user` attribute.' % filter_name
        raise template.TemplateSyntaxError(message)


def _get_user_from_value(filter_name, value):
    try:
        return _get_user(value)
    except ValueError:
        message = '%s filter can only be applied to User\'s or ' \
                  'objects with a `user` attribute.' % filter_name
        raise template.TemplateSyntaxError(message)


register.filter('friends', friends_)
register.filter('isblockedby', is_blocked_by)
register.filter('isfriendswith', is_friends_with)
register.tag('addtofriends', add_to_friends)
register.tag('blockuser', block_user)
{'fraction_non_alphanumeric': 0.08996986655187258, 'fraction_numerical': 0.0034438226431338786, 'mean_word_length': 1.8845294263719892, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 60, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3501708', 'n_tokens_mistral': 3023, 'n_tokens_neox': 2932, 'n_words': 708}