コード例 #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_'])
コード例 #2
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_)