Beispiel #1
0
def alarm_save():
    fields = ['id_', 'name', 'volume', 'stream', 'action']
    data = dict([ (k, request.POST.get(k)) for k in fields  ])
    data['volume'] = int(data['volume'])
    data['type'] = 'radio'

    try:
        date = request.POST.get('date')
        hour = request.POST.get('hour')
        data['at'] = time.mktime(time.strptime("%s %s" % (date, hour),
                                               "%Y-%m-%d %H:%M:%S"))
        dt = datetime.datetime.fromtimestamp(data['at'])
        data['date'] = dt.strftime('%Y-%m-%d')
        data['hour'] = dt.strftime('%H:%M:%S')
    except:
        return "Problem with the date... Chek it, please"

    if data['id_']:
        data['id_'] = int(data['id_'])
        alarms_data = storage.replace('alarms', data)
        storage.save_table('alarms', alarms_data)
    else:
        # TODO: All this logic of getting a new ID for the given table should
        # be handled by the storage lib
        stored = storage.read()
        ids = map(lambda x: x['id_'], stored['alarms'])
        data['id_'] = max(ids)+1 if ids else 1

        stored['alarms'].append(data)
        storage.save(stored)

    alarms.set_alarms(storage.read('alarms'))

    redirect('/alarm/edit/%s' % data['id_'])
Beispiel #2
0
def radio_save():
    radios = {}
    post = dict(request.POST)
    for key in post:
        name, value = key.split('_')
        if name != "name": continue
        radio_name = post.get('name_' + value)
        radio_stream = post.get('stream_' + value)
        if radio_name == "" or radio_stream == "": continue
        radios[radio_name] = radio_stream
    storage.save_table('radio', radios)
    return radio(successfully_saved=True)
Beispiel #3
0
def change_password():
    old = request.POST.get('old_password')
    new = request.POST.get('new_password')
    repeat = request.POST.get('repeat_password')

    user = storage.get_by_id('user', 'admin')
    sha_password = hashlib.sha256(old).hexdigest()
    if sha_password != user['password']:
        return config_edit(err_msg="Bad password")
    elif new != repeat:
        return config_edit(err_msg="Password missmatches")

    user['password'] = hashlib.sha256(new).hexdigest()

    storage.save_table('user', [user])

    return config_edit(err_msg="Password changed successfully")
Beispiel #4
0
def command_save():
    id_ = request.POST.get('id')
    class_ = request.POST.get('class')
    action = request.POST.get('action')
    command = request.POST.get('command', '')

    if not class_ or not action:
        return "Invalid data. CLASS and ACTION are required fields."

    if id_:
        new_command = {"id_": int(id_), "class_": class_, "action": action, "command": command}
        commands = storage.replace('commands', new_command)
        storage.save_table('commands', commands)
    else:
        data = storage.read()
        ids = map(lambda x: x['id_'], data['commands'])
        id_ = max(ids)+1 if ids else 1

        new_command = {"id_": int(id_), "class_": class_, "action": action, "command": command}
        data['commands'].append(new_command)
        storage.save(data)

    redirect("/command/edit/%s" % id_)