def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] db,c = get_db() error = None c.execute( 'select * from user where username = %s',(username,) ) user = c.fetchone() if user is None: error = 'El usuario no existe' elif not check_password_hash(user['password'],password): error = 'El usuario no existe' if error is None and user['permisos'] == 1: session.clear() session['user_id'] = user['id'] return redirect(url_for('receta.index_adm')) if error is None and user['permisos'] == 0: session.clear() session['user_id'] = user['id'] return redirect(url_for('receta.index')) flash(error) return render_template('auth/login.html')
def register(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] nombre = request.form['nombre'] apellido = request.form['apellido'] email = request.form['email'] sexo = request .form['sexo'] db, c = get_db() error = None c.execute( 'select * from user where username = %s',(username,) ) if not username: error = 'Username requerido' if not password: error = 'Password es requerido' elif c.fetchone() is not None: error = 'El usuario ya se encuentra registrado' if error is None: c.execute( 'insert into user (username,password,nombre,apellido,correo,permisos,sexo) values (%s,%s,%s,%s,%s,%s,%s)', (username,generate_password_hash(password),nombre,apellido,email,0,sexo) ) db.commit() return redirect(url_for('auth.login')) return render_template('auth/register.html')
def get_recipe(id): db, c = get_db() c.execute('select * from receta where id = %s', (id, )) receta = c.fetchone() if receta is None: abort(404, "El todo de id {0} no existe".format(id)) return receta
def load_logged_in_user(): user_id = session.get('user_id') if user_id is None: g.user = None else: db, c = get_db() c.execute( 'select * from user where id = %s', (user_id,) ) g.user = c.fetchone()
def create(): if request.method == 'POST': titulo = request.form['titulo'] descripcion = request.form['descripcion'] ingredientes = request.form['ingredientes'] preparacion = request.form['preparacion'] categoria = request.form['categoria'] url = request.form['url'] db, c = get_db() c.execute( 'insert into receta (titulo,descripcion,ingredientes,preparacion,categoria,url) values (%s,%s,%s,%s,%s,%s)', (titulo, descripcion, ingredientes, preparacion, categoria, url)) db.commit() return redirect(url_for('receta.index_adm')) return render_template('recetas/create.html')
def index(): db, c = get_db() c.execute('select r.ingredientes from receta r') ingredientes = c.fetchall() rows = 0 rows_list = [] linea = 0 for i in range(len(ingredientes)): for j in ingredientes[i]['ingredientes']: if j == '\n': rows += 1 rows_list.append(rows) rows = 0 c.execute('select * from receta') receta = c.fetchall() size = len(receta) return render_template('recetas/index.html', receta=receta, rows=rows_list, size=size)
def mostrar_categoria_user(categoria): db, c = get_db() c.execute('select r.ingredientes from receta r where categoria = %s', (categoria, )) ingredientes = c.fetchall() rows = 0 rows_list = [] linea = 0 for i in range(len(ingredientes)): for j in ingredientes[i]['ingredientes']: if j == '\n': rows += 1 rows_list.append(rows) rows = 0 c.execute('select * from receta where categoria = %s', (categoria, )) receta = c.fetchall() size = len(receta) return render_template('recetas/mostrar_categoria_user.html', receta=receta, rows=rows_list, size=size)
def categorias(): db, c = get_db() c.execute('select * from receta') receta = c.fetchall() return render_template('recetas/categorias.html', receta=receta)
def recipe_user(): db, c = get_db() c.execute('select * from receta') receta = c.fetchall() return render_template('recetas/recipe_user.html', receta=receta)