<commit_before>from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect, JsonResponse
from django.shortcuts import render, Http404, render_to_response
from django.template import RequestContext

from PiPool.models import Pin
from PiPool.pin_controller import PinController

# Create the pin controller instance
controller = PinController()


@login_required(login_url="login/")
def dashboard(request):
    return render(request, "dashboard.html", controller.get_dashboard_data())


def pins(request):
    return render(request, "pins.html", controller.get_all_pins())


def pin_create(request):
    return render(request, "pin-create.html", {})


def pin_delete(request, id):
    try:
        pin = controller.my_pins.get(id=id)
    except Pin.DoesNotExist:
        raise Http404("Could not find that pin!")

    result = pin.delete()
    controller.set_all_pins()

    return JsonResponse({'success': result})


def pin_create_post(request):
    if request.method != 'POST':
        return HttpResponseRedirect("/")

    name = request.POST.get('name')
    pin_number = request.POST.get('pin_number')
    description = request.POST.get('description')
    is_thermometer = request.POST.get('is_thermometer', False)

    try:
        pin = Pin()
        pin.pin_number = pin_number
        pin.description = description
        pin.is_thermometer = is_thermometer
        pin.name = name
        pin.save()

        controller.set_all_pins()

        return HttpResponseRedirect("/pins")
    except:
        # todo send them back and try again
        return HttpResponseRedirect("/")


def pin_edit(request, id):
    try:
        pin = controller.my_pins.get(id=id)
    except Pin.DoesNotExist:
        raise Http404("Could not find that pin!")

    return render(request, "pin-create.html", {"pin": pin})


def pin_set(request):
    if request.method != 'POST':
        return HttpResponseRedirect("/")

    pin_id = request.POST['pin']
    state = request.POST['state']
    result = False

    try:
        pin = controller.my_pins.get(id=pin_id)
    except Pin.DoesNotExist:
        return JsonResponse({'success': False, 'state': state})

    pin.set_state(state)
    new_state = pin.get_state()<commit_msg>better return error message<commit_after>from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect, JsonResponse
from django.shortcuts import render, Http404, render_to_response
from django.template import RequestContext

from PiPool.models import Pin
from PiPool.pin_controller import PinController

# Create the pin controller instance
controller = PinController()


@login_required(login_url="login/")
def dashboard(request):
    return render(request, "dashboard.html", controller.get_dashboard_data())


def pins(request):
    return render(request, "pins.html", controller.get_all_pins())


def pin_create(request):
    return render(request, "pin-create.html", {})


def pin_delete(request, id):
    try:
        pin = controller.my_pins.get(id=id)
    except Pin.DoesNotExist:
        raise Http404("Could not find that pin!")

    result = pin.delete()
    controller.set_all_pins()

    return JsonResponse({'success': result})


def pin_create_post(request):
    if request.method != 'POST':
        return HttpResponseRedirect("/")

    name = request.POST.get('name')
    pin_number = request.POST.get('pin_number')
    description = request.POST.get('description')
    is_thermometer = request.POST.get('is_thermometer', False)

    try:
        pin = Pin()
        pin.pin_number = pin_number
        pin.description = description
        pin.is_thermometer = is_thermometer
        pin.name = name
        pin.save()

        controller.set_all_pins()

        return HttpResponseRedirect("/pins")
    except:
        # todo send them back and try again
        return HttpResponseRedirect("/")


def pin_edit(request, id):
    try:
        pin = controller.my_pins.get(id=id)
    except Pin.DoesNotExist:
        raise Http404("Could not find that pin!")

    return render(request, "pin-create.html", {"pin": pin})


def pin_set(request):
    if request.method != 'POST':
        return HttpResponseRedirect("/")

    pin_id = request.POST['pin']
    state = request.POST['state']
    result = False

    try:
        pin = controller.my_pins.get(id=pin_id)
    except Pin.DoesNotExist:
        return JsonResponse({'success': False, 'state': state, 'message': 'Can not find pin'})

    pin.set_state(state)
    new_state = pin.get_state()
{'fraction_non_alphanumeric': 0.11274402281202019, 'fraction_numerical': 0.0039482342618995395, 'mean_word_length': 3.466209598432909, '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': '4689720', 'n_tokens_mistral': 1385, 'n_tokens_neox': 1321, 'n_words': 323}