<commit_before>#-*- coding: utf-8 -*-
from django.conf import settings
from django.contrib.sites.models import Site
from django.utils.translation import get_language, activate
from django.template.context import Context


def get_language_for_user(user):
    return user.get_profile().preferred_language


class BaseDeliveryBackend(object):
    """
    Base backend for all other backends
    """
    supports_anonymous_users = False

    def __init__(self, notice):
        self.notice = notice

    def can_send(self, user, notice_type):
        """
        Determines if this backend should send a notification for the given combination of user and notice_type
        """
        return True

    def default_context(self):
        return Context({
            'default_http_protocol': getattr(settings, "DEFAULT_HTTP_PROTOCOL", "http"),
            'current_site': Site.objects.get_current(),
        })

    def deliver(self):
        """
        deliver the given Notice to_users.
        """
        current_language = get_language()

        for user in self.notice.to_users:
            if not self.supports_anonymous_users and user.is_anonymous():
                continue
            elif user.is_anonymous():
                languages = [language_code for language_code, language in settings.LANGUAGES]
                user = None
            else:
                languages = [get_language_for_user(user)]
            if not (self.notice.can_send(user, self) and self.can_send(user, self.notice)):
                continue
            for language in languages:
                activate(language)
                # build the context
                context = self.default_context()
                context.update(self.notice.get_context(language=language))
                context.update(self.notice.get_user_context(user))
                self.deliver_to(user=user,
                                context=context,
                                notice=self.notice,
                                language=language)
        activate(current_language)

<commit_msg>smarter current site<commit_after>#-*- coding: utf-8 -*-
from django.conf import settings
from django.contrib.sites.models import Site
from django.utils.translation import get_language, activate
from django.template.context import Context


def get_language_for_user(user):
    return user.get_profile().preferred_language


class BaseDeliveryBackend(object):
    """
    Base backend for all other backends
    """
    supports_anonymous_users = False

    def __init__(self, notice):
        self.notice = notice

    def can_send(self, user, notice_type):
        """
        Determines if this backend should send a notification for the given combination of user and notice_type
        """
        return True

    def get_site(self):
        return Site.objects.get_current()

    def default_context(self):
        return Context({
            'default_http_protocol': getattr(settings, "DEFAULT_HTTP_PROTOCOL", "http"),
            'site': self.get_site(),
            'notice': self.notice,
        })

    def deliver(self):
        """
        deliver the given Notice to_users.
        """
        current_language = get_language()

        for user in self.notice.to_users:
            if not self.supports_anonymous_users and user.is_anonymous():
                continue
            elif user.is_anonymous():
                languages = [language_code for language_code, language in settings.LANGUAGES]
                user = None
            else:
                languages = [get_language_for_user(user)]
            if not (self.notice.can_send(user, self) and self.can_send(user, self.notice)):
                continue
            for language in languages:
                activate(language)
                # build the context
                context = self.default_context()
                context.update(self.notice.get_context(language=language))
                context.update(self.notice.get_user_context(user))
                self.deliver_to(user=user,
                                context=context,
                                notice=self.notice,
                                language=language)
        activate(current_language)


{'fraction_non_alphanumeric': 0.08160865475070556, 'fraction_numerical': 0.00047036688617121356, 'mean_word_length': 2.0077793493635077, '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': '4979124', 'n_tokens_mistral': 1092, 'n_tokens_neox': 1055, 'n_words': 266}