<commit_before>from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect,HttpResponse
from django.core.urlresolvers import reverse
from django.utils.functional import lazy
from bcore.apps.administrator.views import AUTH_FAILED_URL
from django.contrib.auth.decorators import login_required, user_passes_test
from bcore.apps.authorization.models import in_supportuser_or_programmanager_group
from bcore.apps.common.utils import create_custom_JSON_error_response
import json

from .utils import (
    get_blocked_ips, get_blocked_usernames, unblock_ip, unblock_username)


# Enable reverse function for url namespace:name in the user_passes_test decorator
# (standard reverse function can NOT be used outside view functions
reverse_lazy = lambda name = None, *args: lazy(reverse, str)(name, args=args)

@login_required
@user_passes_test(in_supportuser_or_programmanager_group, login_url=reverse_lazy(AUTH_FAILED_URL), redirect_field_name='')
def block_view(request):
    """ List the blocked IP and Usernames """
    blocked_ip_list = get_blocked_ips()
    blocked_username_list = get_blocked_usernames()

    context = {'blocked_ip_list': blocked_ip_list,
               'blocked_username_list': blocked_username_list}
    return render_to_response(
        'defender/admin/blocks.html',
        context, context_instance=RequestContext(request))


@login_required
@user_passes_test(in_supportuser_or_programmanager_group, login_url=reverse_lazy(AUTH_FAILED_URL), redirect_field_name='')
def unblock_ip_view(request, ip):
    """ unblock the given ip """
    if request.method == 'POST':
        unblock_ip(ip)
    return HttpResponseRedirect(reverse("defender_blocks_view"))


@login_required
@user_passes_test(in_supportuser_or_programmanager_group, login_url=reverse_lazy(AUTH_FAILED_URL), redirect_field_name='')
def unblock_username_view(request, username):
    """ unblock he given username """
    if request.method == 'POST':
        unblock_username(username)
        return HttpResponse(json.dumps({"status": "unlocked"}), content_type='application/json')
    else:
        return create_custom_JSON_error_response(422, "Wrong request method")<commit_msg>Added audit support for user unlock<commit_after>from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect,HttpResponse
from django.core.urlresolvers import reverse
from django.utils.functional import lazy
from bcore.apps.administrator.views import AUTH_FAILED_URL
from django.contrib.auth.decorators import login_required, user_passes_test
from bcore.apps.authorization.models import in_supportuser_or_programmanager_group
from bcore.apps.common.utils import create_custom_JSON_error_response
from bcore.apps.audit.hbx_signals import log_user_unlock
import json

from .utils import (
    get_blocked_ips, get_blocked_usernames, unblock_ip, unblock_username)


# Enable reverse function for url namespace:name in the user_passes_test decorator
# (standard reverse function can NOT be used outside view functions
reverse_lazy = lambda name = None, *args: lazy(reverse, str)(name, args=args)

@login_required
@user_passes_test(in_supportuser_or_programmanager_group, login_url=reverse_lazy(AUTH_FAILED_URL), redirect_field_name='')
def block_view(request):
    """ List the blocked IP and Usernames """
    blocked_ip_list = get_blocked_ips()
    blocked_username_list = get_blocked_usernames()

    context = {'blocked_ip_list': blocked_ip_list,
               'blocked_username_list': blocked_username_list}
    return render_to_response(
        'defender/admin/blocks.html',
        context, context_instance=RequestContext(request))


@login_required
@user_passes_test(in_supportuser_or_programmanager_group, login_url=reverse_lazy(AUTH_FAILED_URL), redirect_field_name='')
def unblock_ip_view(request, ip):
    """ unblock the given ip """
    if request.method == 'POST':
        unblock_ip(ip)
    return HttpResponseRedirect(reverse("defender_blocks_view"))


@login_required
@user_passes_test(in_supportuser_or_programmanager_group, login_url=reverse_lazy(AUTH_FAILED_URL), redirect_field_name='')
def unblock_username_view(request, username):
    """ unblock he given username """
    if request.method == 'POST':
        unblock_username(username)
        log_user_unlock.send(sender=unblock_username_view, request=request, username=username)
        return HttpResponse(json.dumps({"status": "unlocked"}), content_type='application/json')
    else:
        return create_custom_JSON_error_response(422, "Wrong request method")
{'fraction_non_alphanumeric': 0.08140034364261169, 'fraction_numerical': 0.001288659793814433, 'mean_word_length': 6.357030015797788, 'pattern_counts': {'":': 2, '<': 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': '4964416', 'n_tokens_mistral': 1381, 'n_tokens_neox': 1375, 'n_words': 295}