<commit_before>from django.shortcuts import render_to_response, get_object_or_404, redirect
from django.core.urlresolvers import reverse
from django_sprinkler.models import Context, Sprinkler, Program
from django.http import HttpResponse, HttpResponseServerError
from django_sprinkler.settings import *
import logging
from django.conf import settings


logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG if settings.DEBUG else logging.INFO)

logger_watering = logging.getLogger("watering")
logger_watering.setLevel(logger.level)


def watering_logs(request):
    f = open(settings.LOGGING["handlers"]["watering"], "r")

    return render_to_response(
        "sprinkler/log.html",
        {"log": f.readlines()}
    )


def get_context(request):
    ctxt = Context.objects.get_context()

    response = HttpResponse(
        content=ctxt.to_json(),
        content_type="application/json")
    response['Cache-Control'] = 'no-cache'
    return response


def toggle_valve(request, valve_id):

    sprinkler = get_object_or_404(Sprinkler, id=valve_id)
    try:
        sprinkler.toggle()
        return get_context(request)
    except Exception as ex:
        logger.error("Error at toggle_valve: %s" % ex)
        return HttpResponseServerError(ex)


def activate_program(request, program_id):
    program = get_object_or_404(Program, id=program_id)
    try:
        ctxt = Context.objects.get_or_create(state='manual')[0]
        ctxt.active_program = program
        ctxt.save()
        logger_watering.info("Changing active program to: %s" % program)
        return get_context(request)
    except Exception as et:
        logger.error("Error at activate_program: %s" % et)
        return HttpResponseServerError(et)


def set_state(request, new_state):
    try:
        #print "new_state: %s" % new_state
        ctxt = Context.objects.get_context()
        if new_state in ("3min_cicle", "cicle"):
            pass
            #TODO activate manual cicle
        ctxt.state = new_state
        if new_state == "manual":
            ctxt.start_at = None
        ctxt.save()

        #resetamos todos los riegos
        [s.toggle(False) for s in Sprinkler.objects.all()]
        logger_watering.info("Changing state to %s" % ctxt.state)
        return get_context(request)
    except Exception as et:
        logger.error("Error at set_state: %s" % et)
        return HttpResponseServerError(et)


def home(request):
    return render_to_response(
        "sprinkler/home.html",
        {}
    )<commit_msg>added watering_logs views<commit_after>from django.shortcuts import render_to_response, get_object_or_404, redirect
from django.core.urlresolvers import reverse
from django_sprinkler.models import Context, Sprinkler, Program
from django.http import HttpResponse, HttpResponseServerError
from django_sprinkler.settings import *
import logging
from django.conf import settings


logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG if settings.DEBUG else logging.INFO)

logger_watering = logging.getLogger("watering")
logger_watering.setLevel(logger.level)


def watering_logs(request):
    print settings.LOGGING["handlers"]["watering"]
    f = open(settings.LOGGING["handlers"]["watering"], "r")

    return render_to_response(
        "sprinkler/log.html",
        {"log": f.readlines()}
    )


def get_context(request):
    ctxt = Context.objects.get_context()

    response = HttpResponse(
        content=ctxt.to_json(),
        content_type="application/json")
    response['Cache-Control'] = 'no-cache'
    return response


def toggle_valve(request, valve_id):

    sprinkler = get_object_or_404(Sprinkler, id=valve_id)
    try:
        sprinkler.toggle()
        return get_context(request)
    except Exception as ex:
        logger.error("Error at toggle_valve: %s" % ex)
        return HttpResponseServerError(ex)


def activate_program(request, program_id):
    program = get_object_or_404(Program, id=program_id)
    try:
        ctxt = Context.objects.get_or_create(state='manual')[0]
        ctxt.active_program = program
        ctxt.save()
        logger_watering.info("Changing active program to: %s" % program)
        return get_context(request)
    except Exception as et:
        logger.error("Error at activate_program: %s" % et)
        return HttpResponseServerError(et)


def set_state(request, new_state):
    try:
        #print "new_state: %s" % new_state
        ctxt = Context.objects.get_context()
        if new_state in ("3min_cicle", "cicle"):
            pass
            #TODO activate manual cicle
        ctxt.state = new_state
        if new_state == "manual":
            ctxt.start_at = None
        ctxt.save()

        #resetamos todos los riegos
        [s.toggle(False) for s in Sprinkler.objects.all()]
        logger_watering.info("Changing state to %s" % ctxt.state)
        return get_context(request)
    except Exception as et:
        logger.error("Error at set_state: %s" % et)
        return HttpResponseServerError(et)


def home(request):
    return render_to_response(
        "sprinkler/home.html",
        {}
    )
{'fraction_non_alphanumeric': 0.09951124144672532, 'fraction_numerical': 0.004301075268817204, 'mean_word_length': 3.24212271973466, 'pattern_counts': {'":': 4, '<': 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': '7525088', 'n_tokens_mistral': 1583, 'n_tokens_neox': 1551, 'n_words': 352}