Example #1
0
def override_clear(request):
    conf = __get_config()
    if conf is None:
        # Not having a config file is acceptable here
        conf = Config(logging.getLogger('django'))

    if request.is_ajax():
        conf.clear_override()
        conf.save(CONFIG_FILE)

        return HttpResponse(json.dumps(dict(status='cleared')), content_type='application/json')
    else:
        return HttpResponse(json.dumps(dict(status='Not Ajax')), content_type='application/json')
Example #2
0
def override_post(request):
    conf = __get_config()
    if conf is None:
        # Not having a config file is acceptable here
        conf = Config(logging.getLogger('django'))

    if request.is_ajax():
        json_end_time = request.POST.get('end_time', None)
        if json_end_time is None:
            return HttpResponse(json.dumps(dict(status='Pas de date/heure')), content_type='application/json')

        try:
            end_time = json.loads(json_end_time)
        except ValueError:
            return HttpResponse(json.dumps(dict(status='Date/heure invalide : %s' % json_end_time)),
                                content_type='application/json')

        json_value = request.POST.get('value', None)
        if json_value is None:
            return HttpResponse(json.dumps(dict(status='Pas de consigne')), content_type='application/json')

        try:
            value = float(json.loads(json_value))
        except ValueError:
            return HttpResponse(json.dumps(dict(status='Consigne invalide : %s' % json_value)),
                                content_type='application/json')

        conf.clear_override()

        # Express in our local TZ and remove tzinfo -> make date naive
        begin = datetime.datetime.now()
        end = parse(end_time)
        end = end.astimezone(dateutil.tz.tzlocal()).replace(tzinfo=None)
        override = Override(begin, end, value)
        conf.set_override(override)

        # Once done, save config file and restart service
        conf.save(CONFIG_FILE)

        return HttpResponse(json.dumps(dict(status='updated')), content_type='application/json')
    else:
        return HttpResponse(json.dumps(dict(status='Not Ajax')), content_type='application/json')