<commit_before>from django.contrib.auth import BACKEND_SESSION_KEY, authenticate, login
from django.contrib.auth.views import redirect_to_login
from django.contrib import messages
from django.utils.translation import ugettext as _
from django.utils.html import format_html
from django.shortcuts import reverse
from django.http.response import HttpResponseForbidden, HttpResponseRedirect


class SoftLoginRequiredMixin(object):
    unlogged_redirect_url = 'oauth_redirect_view'

    def get_message_string(self, user):

        return format_html(
            _("Vous êtes connecté&middot;e&middot;s avec l'adresse &lt;{user_email}&gt;. S'il ne s'agit pas de vous,"
              " <a href=\"{login_url}\">cliquez-ici pour vous connecter</a> avec votre compte."),
            user_email=user.person.email,
            login_url=reverse('oauth_redirect_view')
        )

    def dispatch(self, request, *args, **kwargs):
        if 'p' in request.GET and 'code' in request.GET:
            # preserve other query params than p and code when we redirect
            other_params = request.GET.copy()
            del other_params['p']
            del other_params['code']
            url = '{}?{}'.format(request.path, other_params.urlencode(safe='/')) if other_params else request.path

            user = authenticate(user_pk=request.GET['p'], code=request.GET['code'])

            # case where user is already authenticated and different from user above ==> redirect with warning message
            if request.user.is_authenticated and request.user != user:
                messages.add_message(
                    request=request,
                    level=messages.WARNING,
                    message=self.get_message_string(request.user)
                )
                return HttpResponseRedirect(url)
            # case where user is being authenticated ==> we show a message but only with info level
            elif user:
                login(request, user)
                messages.add_message(
                    request=request,
                    level=messages.INFO,
                    message=self.get_message_string(user)
                )
                return HttpResponseRedirect(url)

        elif request.user.is_authenticated:
            return super().dispatch(request, *args, **kwargs)

        return redirect_to_login(request.get_full_path(), self.unlogged_redirect_url)


class HardLoginRequiredMixin(object):
    unlogged_redirect_url = 'oauth_redirect_view'

    def dispatch(self, request, *args, **kwargs):
        if request.user.is_authenticated and request.session[BACKEND_SESSION_KEY] != 'front.backend.MailLinkBackend':
            return super().dispatch(request, *args, **kwargs)

        return redirect_to_login(request.get_full_path(), self.unlogged_redirect_url)


class PermissionsRequiredMixin(object):
    permissions_required = ()

    def dispatch(self, *args, **kwargs):
        # check if there are some perms that the user does not have globally
        user = self.request.user
        local_perms = {perm for perm in self.permissions_required if not user.has_perm(perm)}

        if local_perms:
            obj = self.get_object()

            for perm in local_perms:
                if not user.has_perm(perm, obj):
                    return HttpResponseForbidden(b'Not allowed')

        return super().dispatch(*args, **kwargs)


class SimpleOpengraphMixin():
    meta_title = None
    meta_description = None
    meta_type = 'article'

    def get_context_data(self, **kwargs):
        return super().get_context_data(
            show_opengraph=True,
            meta_title=self.get_meta_title(),
            meta_description=self.get_meta_description(),
            meta_type=self.meta_type,
            **kwargs
        )

    def get_meta_title(self):
        return self.meta_title

    def get_meta_description(self):
        return self.meta_description


class ObjectOpengraphMixin(SimpleOpengraphMixin):
    title_prefix = None

    def get_meta_title(self):
        return '{} - {}'.format(self.title_prefix, self.object.name)
<commit_msg>[front] correct wording of warning messages when sorta logged-in<commit_after>from django.contrib.auth import BACKEND_SESSION_KEY, authenticate, login
from django.contrib.auth.views import redirect_to_login
from django.contrib import messages
from django.utils.translation import ugettext as _
from django.utils.html import format_html
from django.shortcuts import reverse
from django.http.response import HttpResponseForbidden, HttpResponseRedirect


class SoftLoginRequiredMixin(object):
    unlogged_redirect_url = 'oauth_redirect_view'

    def get_message_string(self, user):

        return format_html(
            _("Bonjour {person} (ce n'est pas vous ? <a href=\"{login_url}\">Cliquez-ici pour vous reconnecter"
              "</a> avec votre compte.)"),
            person=user.person.get_short_name(),
            login_url=reverse('oauth_redirect_view')
        )

    def dispatch(self, request, *args, **kwargs):
        if 'p' in request.GET and 'code' in request.GET:
            # preserve other query params than p and code when we redirect
            other_params = request.GET.copy()
            del other_params['p']
            del other_params['code']
            url = '{}?{}'.format(request.path, other_params.urlencode(safe='/')) if other_params else request.path

            user = authenticate(user_pk=request.GET['p'], code=request.GET['code'])

            # case where user is already authenticated and different from user above ==> redirect with warning message
            if request.user.is_authenticated and request.user != user:
                messages.add_message(
                    request=request,
                    level=messages.WARNING,
                    message=self.get_message_string(request.user)
                )
                return HttpResponseRedirect(url)
            # case where user is being authenticated ==> we show a message but only with info level
            elif user:
                login(request, user)
                messages.add_message(
                    request=request,
                    level=messages.INFO,
                    message=self.get_message_string(user)
                )
                return HttpResponseRedirect(url)

        elif request.user.is_authenticated:
            return super().dispatch(request, *args, **kwargs)

        return redirect_to_login(request.get_full_path(), self.unlogged_redirect_url)


class HardLoginRequiredMixin(object):
    unlogged_redirect_url = 'oauth_redirect_view'

    def dispatch(self, request, *args, **kwargs):
        if request.user.is_authenticated and request.session[BACKEND_SESSION_KEY] != 'front.backend.MailLinkBackend':
            return super().dispatch(request, *args, **kwargs)

        return redirect_to_login(request.get_full_path(), self.unlogged_redirect_url)


class PermissionsRequiredMixin(object):
    permissions_required = ()

    def dispatch(self, *args, **kwargs):
        # check if there are some perms that the user does not have globally
        user = self.request.user
        local_perms = {perm for perm in self.permissions_required if not user.has_perm(perm)}

        if local_perms:
            obj = self.get_object()

            for perm in local_perms:
                if not user.has_perm(perm, obj):
                    return HttpResponseForbidden(b'Not allowed')

        return super().dispatch(*args, **kwargs)


class SimpleOpengraphMixin():
    meta_title = None
    meta_description = None
    meta_type = 'article'

    def get_context_data(self, **kwargs):
        return super().get_context_data(
            show_opengraph=True,
            meta_title=self.get_meta_title(),
            meta_description=self.get_meta_description(),
            meta_type=self.meta_type,
            **kwargs
        )

    def get_meta_title(self):
        return self.meta_title

    def get_meta_description(self):
        return self.meta_description


class ObjectOpengraphMixin(SimpleOpengraphMixin):
    title_prefix = None

    def get_meta_title(self):
        return '{} - {}'.format(self.title_prefix, self.object.name)

{'fraction_non_alphanumeric': 0.08893208601627992, 'fraction_numerical': 0.0, 'mean_word_length': 2.6042031523642732, 'pattern_counts': {'":': 0, '<': 7, '<?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': '4790117', 'n_tokens_mistral': 2268, 'n_tokens_neox': 2189, 'n_words': 539}