<commit_before>from django.contrib.contenttypes.models import ContentType
import json

from django.http import Http404, HttpResponse
from django.contrib.auth import get_user_model
from django.contrib.auth.decorators import login_required, user_passes_test
from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404, redirect, render

from guardian.shortcuts import get_objects_for_user

from account.decorators import has_permissions
from backend.tasks import TestConnectionTask
from event.models import NotificationPreferences
from .models import Application, Department, Environment, Server, ServerRole


@login_required
def index(request):
    data = {}
    data['application_list'] = Application.objects.filter(department_id=request.current_department_id).prefetch_related('environments')
    if not data['application_list']:
        return redirect(reverse('help_page'))
    return render(request, 'page/index.html', data)


@has_permissions('core.Application')
def application_page(request, application_id):
    data = {}
    data['application'] = get_object_or_404(Application, pk=application_id)
    return render(request, 'page/application.html', data)


@has_permissions('core.Environment')
def environment_page(request, environment_id):
    data = {}
    data['environment'] = get_object_or_404(Environment, pk=environment_id)
    data['servers'] = list(Server.objects.filter(environment_id=environment_id).prefetch_related('roles'))
    return render(request, 'page/environment.html', data)


@has_permissions('core.Server')
def server_test(request, server_id):
    data = {}
    data['server'] = get_object_or_404(Server, pk=server_id)
    data['task_id'] = TestConnectionTask().delay(server_id).id
    return render(request, 'partial/server_test.html', data)


@login_required
def server_test_ajax(request, task_id):
    data = {}
    task = TestConnectionTask().AsyncResult(task_id)
    if task.status == 'SUCCESS':
        status, output = task.get()
        data['status'] = status
        data['output'] = output
    elif task.status == 'FAILED':
        data['status'] = False
    else:
        data['status'] = None
    return HttpResponse(json.dumps(data), content_type="application/json")


@login_required
def help_page(request):
    data = {}
    return render(request, 'page/help.html', data)


@login_required
def settings_page(request, section='user', subsection='profile'):
    data = {}
    data['section'] = section
    data['subsection'] = subsection
    data['department'] = Department(pk=request.current_department_id)
    data['on_settings'] = True
    handler = '_settings_%s_%s' % (section, subsection)
    if section == 'system' and request.user.is_superuser is not True:
        return redirect('index')
    if section == 'department' and not request.user.has_perm('core.edit_department', obj=data['department']):
        return redirect('index')
    if handler in globals():
        data = globals()[handler](request, data)
    else:
        raise Http404
    return render(request, 'page/settings.html', data)


def _settings_account_profile(request, data):
    data['subsection_template'] = 'partial/account_profile.html'
    from account.forms import account_create_form
    form = account_create_form('user_profile', request, request.user.id)
    form.fields['email'].widget.attrs['readonly'] = True
    data['form'] = form
    if request.method == 'POST':
        if form.is_valid():
            form.save()
            data['user'] = form.instance
    return data


def _settings_account_password(request, data):
    data['subsection_template'] = 'partial/account_password.html'
    from account.forms import account_create_form
    form = account_create_form('user_password', request, request.user.id)
    data['form'] = form
    if request.method == 'POST':
        if form.is_valid():
            user = form.save(commit=False)
            user.set_password(<PASSWORD>)
            user.save()
            data['user'] = form.instance
    return data


def _settings_account_notifications(request, data):
    data['subsection_template'] = 'partial/account_notifications.html'
    content_type = ContentType.objects.get_for_model(Application)
    if request.method == 'POST':
        departments = get_objects_for_user(request.user, 'core.view_department')
        for department in departments:
            for application in department.applications.all():
                key = 'notification[%s]' % application.id
                notification, created = NotificationPreferences.objects.get_or_create(
                    user=request.user,
                    event_type='ExecutionFinish',
                    content_type=content_type,
                    object_id=application.id)
                if notification.is_active != (key in request.POST):
                    notification.is_active = key in request.POST
                    notification.save()
    data['notifications'] = NotificationPreferences.objects.filter(
        user=request.user,
        event_type='ExecutionFinish',
        content_type=content_type.id).values_list('object_id', 'is_active')
    data['notifications'] = dict(data['notifications'])
    return data

<commit_msg>Add success messages in settings<commit_after>from django.contrib.contenttypes.models import ContentType
import json

from django.http import Http404, HttpResponse
from django.contrib import messages
from django.contrib.auth import get_user_model
from django.contrib.auth.decorators import login_required, user_passes_test
from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404, redirect, render

from guardian.shortcuts import get_objects_for_user

from account.decorators import has_permissions
from backend.tasks import TestConnectionTask
from event.models import NotificationPreferences
from .models import Application, Department, Environment, Server, ServerRole


@login_required
def index(request):
    data = {}
    data['application_list'] = Application.objects.filter(department_id=request.current_department_id).prefetch_related('environments')
    if not data['application_list']:
        return redirect(reverse('help_page'))
    return render(request, 'page/index.html', data)


@has_permissions('core.Application')
def application_page(request, application_id):
    data = {}
    data['application'] = get_object_or_404(Application, pk=application_id)
    return render(request, 'page/application.html', data)


@has_permissions('core.Environment')
def environment_page(request, environment_id):
    data = {}
    data['environment'] = get_object_or_404(Environment, pk=environment_id)
    data['servers'] = list(Server.objects.filter(environment_id=environment_id).prefetch_related('roles'))
    return render(request, 'page/environment.html', data)


@has_permissions('core.Server')
def server_test(request, server_id):
    data = {}
    data['server'] = get_object_or_404(Server, pk=server_id)
    data['task_id'] = TestConnectionTask().delay(server_id).id
    return render(request, 'partial/server_test.html', data)


@login_required
def server_test_ajax(request, task_id):
    data = {}
    task = TestConnectionTask().AsyncResult(task_id)
    if task.status == 'SUCCESS':
        status, output = task.get()
        data['status'] = status
        data['output'] = output
    elif task.status == 'FAILED':
        data['status'] = False
    else:
        data['status'] = None
    return HttpResponse(json.dumps(data), content_type="application/json")


@login_required
def help_page(request):
    data = {}
    return render(request, 'page/help.html', data)


@login_required
def settings_page(request, section='user', subsection='profile'):
    data = {}
    data['section'] = section
    data['subsection'] = subsection
    data['department'] = Department(pk=request.current_department_id)
    data['on_settings'] = True
    handler = '_settings_%s_%s' % (section, subsection)
    if section == 'system' and request.user.is_superuser is not True:
        return redirect('index')
    if section == 'department' and not request.user.has_perm('core.edit_department', obj=data['department']):
        return redirect('index')
    if handler in globals():
        data = globals()[handler](request, data)
    else:
        raise Http404
    return render(request, 'page/settings.html', data)


def _settings_account_profile(request, data):
    data['subsection_template'] = 'partial/account_profile.html'
    from account.forms import account_create_form
    form = account_create_form('user_profile', request, request.user.id)
    form.fields['email'].widget.attrs['readonly'] = True
    data['form'] = form
    if request.method == 'POST':
        if form.is_valid():
            form.save()
            data['user'] = form.instance
            messages.success(request, 'Saved')
    return data


def _settings_account_password(request, data):
    data['subsection_template'] = 'partial/account_password.html'
    from account.forms import account_create_form
    form = account_create_form('user_password', request, request.user.id)
    data['form'] = form
    if request.method == 'POST':
        if form.is_valid():
            user = form.save(commit=False)
            user.set_password(<PASSWORD>)
            user.save()
            data['user'] = form.instance
            messages.success(request, 'Saved')
    return data


def _settings_account_notifications(request, data):
    data['subsection_template'] = 'partial/account_notifications.html'
    content_type = ContentType.objects.get_for_model(Application)
    if request.method == 'POST':
        departments = get_objects_for_user(request.user, 'core.view_department')
        for department in departments:
            for application in department.applications.all():
                key = 'notification[%s]' % application.id
                notification, created = NotificationPreferences.objects.get_or_create(
                    user=request.user,
                    event_type='ExecutionFinish',
                    content_type=content_type,
                    object_id=application.id)
                if notification.is_active != (key in request.POST):
                    notification.is_active = key in request.POST
                    notification.save()
        messages.success(request, 'Saved')
    data['notifications'] = NotificationPreferences.objects.filter(
        user=request.user,
        event_type='ExecutionFinish',
        content_type=content_type.id).values_list('object_id', 'is_active')
    data['notifications'] = dict(data['notifications'])
    return data


{'fraction_non_alphanumeric': 0.11345720720720721, 'fraction_numerical': 0.0033783783783783786, 'mean_word_length': 3.7939721097615835, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2186302', 'n_tokens_mistral': 3018, 'n_tokens_neox': 2924, 'n_words': 618}