def edit(request):
    """device edit """
    id = request.matchdict['id']
    dbsession = DBSession()
    device = dbsession.query(Device).filter_by(id=id).one()
    if device is None:
        request.session.flash("error;No se encontro el dispositivo!")
        return HTTPFound(location=request.route_url("device_list"))        
    

    form = Form(request, schema=DeviceForm)    
    if "form_submitted" in request.POST and form.validate():
        form.bind(device)
        dbsession.add(device)
        request.session.flash("warning;Se guardo el dispositivo!")
        s = SuperControl()
	s.restart('condor')
	return HTTPFound(location = request.route_url("device_list"))

    storage = request.registry.settings['storage']
    sp = path.join(storage, str(device.id))
    last = max(glob.iglob(path.join(sp, '*.png')), key=path.getctime)
    sample = '/store/{0}/{1}'.format(device.id, path.split(last)[1])
    action_url = request.route_url("device_edit", id=id)
    if device.roi is None: device.roi = '400,400,650,650'
    return dict(form=FormRenderer(form), 
                action_url=action_url, obj=device,sample = sample )
def new(request):
    """new country """
    form = Form(request, schema=BrandForm)
    if "form_submitted" in request.POST and form.validate():
        dbsession = DBSession()
        brand = form.bind(Brand())
        # TODO: db error control?
        dbsession.add(brand)
        request.session.flash("warning;Se guardo la marca!")
        return HTTPFound(location=request.route_url("brand_list"))

    return dict(form=FormRenderer(form), action_url=request.route_url("brand_new"))
def new(request):
    """new country """
    form = Form(request, schema=DeviceForm)    
    if "form_submitted" in request.POST and form.validate():
        dbsession = DBSession()
        device = form.bind(Device())
        # TODO: db error control?
        dbsession.add(device)
        request.session.flash("warning;Se agrego el dispositivo!")
        return HTTPFound(location = request.route_url("device_list"))
        
    return dict(form=FormRenderer(form), 
                action_url=request.route_url("device_new"))
def edit(request):
    """brand edit """
    id = request.matchdict["id"]
    dbsession = DBSession()
    brand = dbsession.query(Brand).filter_by(id=id).one()
    if brand is None:
        request.session.flash("error;No se encontro la marca!")
        return HTTPFound(location=request.route_url("brand_list"))

    form = Form(request, schema=BrandForm, obj=brand)
    if "form_submitted" in request.POST and form.validate():
        form.bind(brand)
        dbsession.add(brand)
        request.session.flash("warning;Se guardo la marca!")
        return HTTPFound(location=request.route_url("brand_list"))

    action_url = request.route_url("brand_edit", id=id)
    return dict(form=FormRenderer(form), action_url=action_url)
def new(request):
    """new plate """
    brands = get_brands()
    
    form = Form(request, schema=PlateForm)    
    if "form_submitted" in request.POST and form.validate():
        dbsession = DBSession()
        plate = form.bind(Plate())
        existing = dbsession.query(Plate).filter_by(code=plate.code).count()
        if not existing:
            dbsession.add(plate)
            request.session.flash("warning;Nueva Patente guardada!")
        else:
            request.session.flash("error; Ya existe la patente!")
        return HTTPFound(location = request.route_url("plate_list"))
        
    return dict(form=FormRenderer(form),
                brands=brands, 
                action_url=request.route_url("plate_new"))
def edit(request):
    """plate edit """
    id = request.matchdict['id']
    dbsession = DBSession()
    plate = dbsession.query(Plate).filter_by(id=id).first()
    if plate is None:
        request.session.flash("error;Patente no encontrada!")
        return HTTPFound(location=request.route_url("plate_list"))        
    
    brands = get_brands()
    
    form = Form(request, schema=PlateForm, obj=plate)    
    if "form_submitted" in request.POST and form.validate():
        form.bind(plate)
        dbsession.add(plate)
        request.session.flash("warning;Se guardo la patente!")
        return HTTPFound(location = request.route_url("plate_list"))

    action_url = request.route_url("plate_edit", id=id)
    return dict(form=FormRenderer(form),
                brands=brands, 
                action_url=action_url)