def edit_usuario(id): permisos.validar_permisos('user_edit') usuario = User.get_by_id(id) roles = Rol.query.all() if request.method == 'POST': u = request.form mensaje = '' if User.existe_usuario( u['usuario']) and not u['usuario'] == usuario.usuario: mensaje = "El nombre de usuario ya existe" elif User.existe_email(u['email']) and not u['email'] == usuario.email: mensaje = "El email ya existe" else: nuevos_roles = request.form.getlist("roles") User.edit(id, u['usuario'], u['clave'], u['nombre'], u['apellido'], u['email'], u['activo'], nuevos_roles) mensaje = "Usuario creado exitosamente" usuarios = User.all() #return render_template('usuario/editar_usuario.html', mensaje=mensaje, usuario=usuario) return redirect( url_for('index_usuario', mensaje=mensaje, usuarios=usuarios)) return render_template('usuario/editar_usuario.html', usuario=usuario, mensaje_error=mensaje, roles=roles) else: return render_template('usuario/editar_usuario.html', usuario=usuario, roles=roles)
def crear_usuario(): permisos.validar_permisos('user_create') if request.method == 'POST': u = request.form mensaje_error = '' mensaje_exito = '' if User.existe_usuario(u['usuario']): mensaje_error = "El nombre de usuario ya existe" elif User.existe_email(u['email']): mensaje_error = "El email ya existe" else: res = User.create(u['usuario'], u['clave'], u['nombre'], u['apellido'], u['email']) if res: mensaje_exito = "Usuario creado exitosamente" else: mensaje_error = "Hubo algun problema" #return redirect(url_for('index_usuario', mensaje_error= mensaje_error, mensaje_exito=mensaje_exito)) return render_template('usuario/crear_usuario.html', mensaje_error=mensaje_error, mensaje_exito=mensaje_exito) return render_template('usuario/crear_usuario.html', mensaje_error=mensaje_error, mensaje_exito=mensaje_exito) else: return render_template('usuario/crear_usuario.html')
def mostrar_centro(id): permisos.validar_permisos('centro_show') centro = Centro_de_ayuda.query.get(id) nombre_municipio = api_requests.name_of_town(centro.municipio_id) return render_template('centro_de_ayuda/mostrar_centro.html', centro=centro, municipio=nombre_municipio)
def go_index(nombre=' ', estado='todos', page=1): permisos.validar_permisos('centro_index') todos_los_estados = Estado_centro.all() per_page = Configuracion.get_config().cantPagina if request.method == 'GET': nombre = nombre estado = estado if request.method == 'POST': page = 1 params = request.form nombre = params['nombre'] or ' ' estado = params['estado'] lista = Centro_de_ayuda.query.filter_by(historico=0) if estado != 'todos': lista = lista.filter_by(estado_id=estado) if nombre != ' ': lista = lista.filter(Centro_de_ayuda.nombre.like('%' + nombre + '%')) obtener_municipios(lista) lista = lista.paginate(page, per_page=per_page) return render_template('centro_de_ayuda/index_centros.html', lista_de_centros=lista, nombre=nombre, estado=estado, estados=todos_los_estados)
def crear_turno_para_fecha(): permisos.validar_permisos('turno_create') form = request.form centro = form['id_centro'] fecha = form['fecha'] print("Ok se esta abriendo el segundo template") return render_template('turnos_para_centro/crear_turno_para_fecha.html', centro=centro, fecha=fecha)
def sacar_turno(id): permisos.validar_permisos('turno_edit') turno = Turno.get_by_id(id) if request.method == 'POST': t = request.form if t['email'] == '': disponible = 1 else: disponible = 0 Turno.edit(id, t['email'], t['telefono'], disponible) return redirect(url_for('index_turno', turno=turno)) else: return render_template('turnos_para_centro/sacar_turno.html', turno=turno)
def index_turno(id='', page=1, email=' '): permisos.validar_permisos('turno_index') per_page = Configuracion.get_config().cantPagina centros = Centro_de_ayuda.all() if request.method == 'GET': email = email if request.method == 'POST': page = 1 params = request.form email = params['email'] or ' ' if email == ' ': if id: turnos_todos = Turno.get_proximos_turnos(id).filter( Turno.email.like('%' + '%')).paginate(page, per_page=per_page) return render_template('turnos_para_centro/index_turno.html', turnos=turnos_todos, centros=centros, email=email, id=id) else: turnos_todos = Turno.query.filter( Turno.email.like('%' + '%')).paginate(page, per_page=per_page) return render_template('turnos_para_centro/index_turno.html', turnos=turnos_todos, centros=centros, email=email) else: if id: turnos_todos = Turno.query.filter_by(centro_id=id).filter( Turno.email.like('%' + email + '%')).paginate( page, per_page=per_page) return render_template('turnos_para_centro/index_turno.html', turnos=turnos_todos, centros=centros, email=email, id=id) else: turnos_todos = Turno.query.filter( Turno.email.like('%' + email + '%')).paginate( page, per_page=per_page) return render_template('turnos_para_centro/index_turno.html', turnos=turnos_todos, centros=centros, email=email)
def vista_configuracion(): #configuracion = Configuracion.get_config() #esto hay que poner en algunos def para que cuando este desactivado el user no pueda entrar #if configuracion.activo == 0: # return render_template('sitioDesactivado.html') permisos.validar_permisos('site_config'); actual = Configuracion.all() if request.method == 'POST': datos = request.form #print(datos) # se muestran todos los datos por termina print (datos) Configuracion.edit(datos["titulo"], datos["descripcion"], datos["mail"], int(datos["activo"]), datos["cantPagina"]) mensaje = "Se modifico la tabla configuracion correctamente" #return render_template("configuracion/configuracion.html", actual=actual, mensaje=mensaje) return redirect(url_for('index', mensaje=mensaje)) elif request.method == 'GET': return render_template("configuracion/configuracion.html", actual=actual)
def login(): if request.method == 'POST': params = request.form usuario = User.get_by_email_and_pass(params['usuario'], params['clave']) user = User.get_by_username(params['usuario']) if user.activo == 0: return render_template('errores/usuario_deshabilitado.html') if usuario: mensaje = "Se logueo correctamente" session['usuario'] = usuario permisos.validar_permisos('') configuracion = Configuracion.get_config() return render_template('index.html', mensaje=mensaje, configuracion=configuracion) else: mensaje = "No logro autenticarse, vuelva a intentarlo." return render_template('/auth/login.html', mensaje=mensaje) return render_template('/auth/login.html')
def index_usuario(nombre=' ', estado='todos', page=1): permisos.validar_permisos('user_show') per_page = Configuracion.get_config().cantPagina usuario = User.all() if request.method == 'GET': nombre = nombre estado = estado if request.method == 'POST': page = 1 params = request.form nombre = params['nombre'] or ' ' estado = params['estado'] usuario = User.query.filter_by(historico=0) if nombre != ' ': usuario = usuario.filter(User.nombre.like('%' + nombre + '%')) if estado != 'todos': usuario = usuario.filter_by(activo=estado) usuario = usuario.paginate(page, per_page=per_page) return render_template('usuario/index_usuario.html', usuario=usuario, nombre=nombre, estado=estado)
def borrar_turno(id): permisos.validar_permisos('turno_delete') id_centro = Turno.query.filter_by(id=id).first().centro_id Turno.delete(id) turno = Turno.all() return redirect(url_for('index_turno', id=id_centro))
def centros(): permisos.validar_permisos('') return render_template('centros.html')
def desactivar(id): permisos.validar_permisos('user_edit') User.desactivar_user(id) return redirect(url_for('index_usuario'))
def agregar_rol(usuario, rol): permisos.validar_permisos('user_edit') User.agregar_rol(usuario, rol) lista_de_usuarios = User.all() return render_template('usuario/index_usuario.html', usuario=lista_de_usuarios)
def borrar_centro(id): permisos.validar_permisos('centro_delete') Centro_de_ayuda.borrar(id) return redirect(url_for('centros'))
def borrar(id): permisos.validar_permisos('user_delete') mensaje = "Se borro el usuario" User.delete(id) return redirect(url_for('index_usuario'))
def borrar_por_nombre(nombre): permisos.validar_permisos('user_delete') mensaje = "Se borro el usuario" User.delete_by_name(nombre) return redirect(url_for('index_usuario'))
def rechazar_centro(id): permisos.validar_permisos('centro_edit') Centro_de_ayuda.rechazar(id) return redirect(url_for('centros'))
def despublicar_centro(id): permisos.validar_permisos('centro_edit') Centro_de_ayuda.despublicar(id) return redirect(url_for('centros'))
def crear_turno(id_centro): permisos.validar_permisos('turno_create') centros = Centro_de_ayuda.all() turnos_todos = Turno.all() centro = Centro_de_ayuda.query.filter_by(id=id_centro).first() f = str(date.today()) if request.method == 'POST': t = request.form fecha = t['fecha'] id_centro = t['id_centro'] solo_fecha = t['solo_fecha'] if solo_fecha == "si": turnos_ocupados = Turno.query.filter_by( centro_id=id_centro).filter_by(dia=fecha).filter_by( disponible=0).filter_by(borrado=0).all() turnos_disponibles = [ "09:00 - 09:30", "09:30 - 10:00", "10:00 - 10:30", "10:30 - 11:00", "11:00 - 11:30", "11:30 - 12:00", "12:00 - 12:30", "12:30 - 13:00", "13:00 - 13:30", "13:30 - 14:00", "14:00 - 14:30", "14:30 - 15:00", "15:00 - 15:30", "15:30 - 16:00" ] for turno_ocupado in turnos_ocupados: # Armar fecha_ocupada - Tuve que cambiar hora_ini y hora_fin a VARCHAR en la BD fecha_ocupada = turno_ocupado.hora_ini + " - " + turno_ocupado.hora_fin try: turnos_disponibles.remove(fecha_ocupada) except: pass return render_template( 'turnos_para_centro/crear_turno_con_fecha.html', centro=id_centro, fecha=fecha, turnos=turnos_disponibles) hora_ini = t['bloque_horario'][0:5] hora_fin = t['bloque_horario'][8:13] email = t['email'] telefono = t['telefono'] fecha = t['fecha'] nombre = t['nombre'] apellido = t['apellido'] borrado = 0 disponible = 0 nuevo_turno = Turno(email=email, telefono=telefono, hora_ini=hora_ini, hora_fin=hora_fin, dia=fecha, borrado=borrado, centro_id=id_centro, disponible=disponible, nombre=nombre, apellido=apellido) db.session.add(nuevo_turno) db.session.commit() return redirect( url_for('index_turno', id=id_centro, turnos=turnos_todos)) else: return render_template('turnos_para_centro/crear_turno.html', centro=centro, centros=centros, f=f)
def editar_centro(id): permisos.validar_permisos('centro_edit') mensaje_error = '' mensaje_exito = '' #Obtener el centro a editar centro = Centro_de_ayuda.query.get(id) #Formateo los horarios print(centro.hora_de_apertura.hour) h1 = datetime.time(centro.hora_de_apertura.hour, centro.hora_de_apertura.minute) h2 = datetime.time(centro.hora_de_apertura.hour, centro.hora_de_apertura.minute) formated_hora_de_apertura = h1 formated_hora_de_cierre = h2 lista_de_tipos = Tipo_de_centro.all() lista_de_municipios = obtener_dic_de_municipios() if request.method == "GET": return render_template('centro_de_ayuda/editar_centro.html', centro=centro, tipos=lista_de_tipos, municipios=lista_de_municipios, mensaje_error=mensaje_error, mensaje_exito=mensaje_exito) else: form = request.form l_tipos = form.getlist("tipos") if len(l_tipos) == 0: mensaje_error = "Debe ingresar algún tipo de Centro" return render_template('centro_de_ayuda/editar_centro.html', centro=centro, tipos=lista_de_tipos, municipios=lista_de_municipios, mensaje_error=mensaje_error, mensaje_exito=mensaje_exito) h_ini = form["hora_apertura"] h_fin = form["hora_cierre"] if not horas_validas(h_ini, h_fin): mensaje_error = "La hora de apertura debe ser menor que la hora de cierre" return render_template('centro_de_ayuda/editar_centro.html', centro=centro, tipos=lista_de_tipos, municipios=lista_de_municipios, mensaje_error=mensaje_error, mensaje_exito=mensaje_exito) if not '@' in form['email']: mensaje_error = "El email debe contener '@'" return render_template('centro_de_ayuda/editar_centro.html', centro=centro, tipos=lista_de_tipos, municipios=lista_de_municipios, mensaje_error=mensaje_error, mensaje_exito=mensaje_exito) print("Hora de apertura quedo en: " + form['hora_apertura']) res = Centro_de_ayuda.editar(id=id, nombre=form['nombre'], direccion=form['direccion'], telefono=form['telefono'], hapertura=form['hora_apertura'], hcierre=form['hora_cierre'], email=form['email'], sitio_web=form['web'], corx=str(form['corx']), cory=str(form['cory']), lista_de_tipos=l_tipos, id_municipio=form['municipio'], id_estado=1, protocolo='PDF', historico=0) if res: mensaje_exito = "Centro editado exitosamente" else: mensaje_error = "Hubo algun problema" lista_de_municipios = obtener_dic_de_municipios() lista_de_tipos = Tipo_de_centro.all() return render_template('centro_de_ayuda/editar_centro.html', centro=centro, tipos=lista_de_tipos, municipios=lista_de_municipios, mensaje_error=mensaje_error, mensaje_exito=mensaje_exito)
def crear_centro(): permisos.validar_permisos('centro_create') mensaje_error = '' mensaje_exito = '' if request.method == "POST": form = request.form l_tipos = form.getlist("tipos") if len(l_tipos) == 0: mensaje_error = "Debe ingresar algún tipo de Centro" return render_template('centro_de_ayuda/crear_centro.html', mensaje_error=mensaje_error, mensaje_exito=mensaje_exito) h_ini = form["hora_apertura"] h_fin = form["hora_cierre"] if not horas_validas(h_ini, h_fin): mensaje_error = "La hora de apertura debe ser menor que la hora de cierre" return render_template('centro_de_ayuda/crear_centro.html', mensaje_error=mensaje_error, mensaje_exito=mensaje_exito) if not '@' in form['email']: mensaje_error = "El email debe contener '@'" return render_template('centro_de_ayuda/crear_centro.html', mensaje_error=mensaje_error, mensaje_exito=mensaje_exito) if Centro_de_ayuda.existe_nombre(form['municipio'], form['nombre']): print(form.getlist("tipos")) mensaje_error = "Ya existe un centro con ese nombre para el municipio indicado" return render_template('centro_de_ayuda/crear_centro.html', mensaje_error=mensaje_error, mensaje_exito=mensaje_exito) else: res = Centro_de_ayuda.crear(nombre=form['nombre'], direccion=form['direccion'], telefono=form['telefono'], hapertura=form['hora_apertura'], hcierre=form['hora_cierre'], email=form['email'], sitio_web=form['web'], corx=str(form['corx']), cory=str(form['cory']), lista_de_tipos=l_tipos, id_municipio=form['municipio'], id_estado=1, protocolo='PDF', historico=0) # Teniendo la id del nuevo centro puedo guardar su archivo file = request.files['pdf'] path = 'app/static/uploads/' filename = str(res.id) + '_' + "Protocolo_de_visita.pdf" file.save(path + filename) Centro_de_ayuda.set_protocolo(id=res.id, fn=filename) if res: mensaje_exito = "Centro creado exitosamente" return redirect(url_for('centros', mensaje_exito=mensaje_exito)) else: mensaje_error = "Hubo algun problema" return render_template('centro_de_ayuda/crear_centro.html', mensaje_error=mensaje_error, mensaje_exito=mensaje_exito) else: lista_de_municipios = obtener_dic_de_municipios() lista_de_tipos = Tipo_de_centro.all() return render_template('centro_de_ayuda/crear_centro.html', tipos=lista_de_tipos, municipios=lista_de_municipios, mensaje_error=mensaje_error, mensaje_exito=mensaje_exito)