<commit_before># -*- coding: utf-8 -*-
# This file is part of Shuup.
#
# Copyright (c) 2012-2019, Shoop Commerce Ltd. All rights reserved.
#
# This source code is licensed under the OSL-3.0 license found in the
# LICENSE file in the root directory of this source tree.
from django.conf import settings as django_settings
from django.contrib.auth import get_user_model, load_backend, login, logout
from django.core.urlresolvers import reverse
from django.http import HttpResponseForbidden, HttpResponseRedirect

from shuup.core.models import get_company_contact_for_shop_staff
from shuup.core.utils.users import (
    force_anonymous_contact_for_user, force_person_contact_for_user,
    toggle_all_seeing_for_user
)
from shuup.front.utils.user import is_admin_user


def toggle_all_seeing(request):
    return_url = request.META["HTTP_REFERER"]
    if not is_admin_user(request):
        return HttpResponseRedirect(return_url)

    toggle_all_seeing_for_user(request.user)
    return HttpResponseRedirect(return_url)


def force_anonymous_contact(request):
    return_url = request.META["HTTP_REFERER"]
    if not is_admin_user(request):
        return HttpResponseRedirect(return_url)

    user = request.user
    force_anonymous_contact_for_user(user, True)
    force_person_contact_for_user(user, False)
    return HttpResponseRedirect(return_url)


def force_person_contact(request):
    return_url = request.META["HTTP_REFERER"]
    if not is_admin_user(request):
        return HttpResponseRedirect(return_url)

    user = request.user
    force_person_contact_for_user(user, True)
    force_anonymous_contact_for_user(user, False)
    return HttpResponseRedirect(return_url)


def force_company_contact(request):
    return_url = request.META["HTTP_REFERER"]
    if not is_admin_user(request):
        return HttpResponseRedirect(return_url)

    user = request.user
    force_anonymous_contact_for_user(user, False)
    force_person_contact_for_user(user, False)

    get_company_contact_for_shop_staff(request.shop, user)
    return HttpResponseRedirect(return_url)


def stop_impersonating(request):
    if "impersonator_user_id" not in request.session:
        return HttpResponseForbidden()

    impersonator_user_id = request.session["impersonator_user_id"]
    del request.session["impersonator_user_id"]
    logout(request)

    user = get_user_model().objects.get(pk=impersonator_user_id)
    for backend in django_settings.AUTHENTICATION_BACKENDS:
        if user == load_backend(backend).get_user(user.pk):
            user.backend = backend
            break

    login(request, user)
    return HttpResponseRedirect(reverse("shuup_admin:contact.list"))<commit_msg>Back to user page after stop impersonating<commit_after># -*- coding: utf-8 -*-
# This file is part of Shuup.
#
# Copyright (c) 2012-2019, Shoop Commerce Ltd. All rights reserved.
#
# This source code is licensed under the OSL-3.0 license found in the
# LICENSE file in the root directory of this source tree.
from django.conf import settings as django_settings
from django.contrib.auth import get_user_model, load_backend, login, logout
from django.core.urlresolvers import reverse
from django.http import HttpResponseForbidden, HttpResponseRedirect

from shuup.core.models import get_company_contact_for_shop_staff
from shuup.core.utils.users import (
    force_anonymous_contact_for_user, force_person_contact_for_user,
    toggle_all_seeing_for_user
)
from shuup.front.utils.user import is_admin_user


def toggle_all_seeing(request):
    return_url = request.META["HTTP_REFERER"]
    if not is_admin_user(request):
        return HttpResponseRedirect(return_url)

    toggle_all_seeing_for_user(request.user)
    return HttpResponseRedirect(return_url)


def force_anonymous_contact(request):
    return_url = request.META["HTTP_REFERER"]
    if not is_admin_user(request):
        return HttpResponseRedirect(return_url)

    user = request.user
    force_anonymous_contact_for_user(user, True)
    force_person_contact_for_user(user, False)
    return HttpResponseRedirect(return_url)


def force_person_contact(request):
    return_url = request.META["HTTP_REFERER"]
    if not is_admin_user(request):
        return HttpResponseRedirect(return_url)

    user = request.user
    force_person_contact_for_user(user, True)
    force_anonymous_contact_for_user(user, False)
    return HttpResponseRedirect(return_url)


def force_company_contact(request):
    return_url = request.META["HTTP_REFERER"]
    if not is_admin_user(request):
        return HttpResponseRedirect(return_url)

    user = request.user
    force_anonymous_contact_for_user(user, False)
    force_person_contact_for_user(user, False)

    get_company_contact_for_shop_staff(request.shop, user)
    return HttpResponseRedirect(return_url)


def stop_impersonating(request):
    if "impersonator_user_id" not in request.session:
        return HttpResponseForbidden()

    impersonator_user_id = request.session["impersonator_user_id"]
    auth_user_id = request.session["_auth_user_id"]
    del request.session["impersonator_user_id"]
    logout(request)

    user = get_user_model().objects.get(pk=impersonator_user_id)
    for backend in django_settings.AUTHENTICATION_BACKENDS:
        if user == load_backend(backend).get_user(user.pk):
            user.backend = backend
            break

    login(request, user)
    return HttpResponseRedirect(reverse("shuup_admin:user.detail", args=[auth_user_id]))
{'fraction_non_alphanumeric': 0.0746023038946791, 'fraction_numerical': 0.004022673249222892, 'mean_word_length': 4.856531049250536, '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': '2941641', 'n_tokens_mistral': 1706, 'n_tokens_neox': 1663, 'n_words': 350}