예제 #1
0
def details(id):
    if not check_auth(session):
        abort(401)

    product = requests.get(API_GET_PRODUCT.format(id)).json()

    return render_template('product/detail.html', product=product)
예제 #2
0
def create():
    if not check_auth(session):
        abort(401)

    categories = requests.get(API_GET_ALL_CATEGORIES).json()
    form = ProductForm()
    if categories:
        form.category.choices = [(categories[c]['nombre']) for c in categories]
    else:
        form.category.choices = []

    if form.validate_on_submit():
        url_img = generate_url_image(form.data['image'])

        product = {
            'nombre': (form.data['name']).strip(),
            'precio': form.data['price'],
            'categoria': form.data['category'],
            'url_imagen': url_img,
            'descripcion': (form.data['description']).strip()
        }

        try:
            req = requests.post(API_POST_PRODUCT, data=json.dumps(product))
            flash('¡Se ha creado un nuevo producto!')
        except ():
            abort(500)

        return redirect(url_for('product.index'))
    return render_template('product/create.html', form=form)
예제 #3
0
def index():
    if not check_auth(session):
        abort(401)

    products = requests.get(API_GET_ALL_PRODUCTS)

    if products.status_code == 200:
        return render_template('product/index.html', products=products.json())
    return abort(products.status_code)
예제 #4
0
def index():
    if not check_auth(session):
        abort(401)

    categories = requests.get(API_GET_ALL_CATEGORIES)

    if categories.status_code == 200:
        return render_template('categories/index.html',
                               categories=categories.json())
    return abort(categories.status_code)
def logout():
    if not check_auth(session):
        abort(401)

    try:
        del session['user']
        session.clear()
    except:
        flash('El usuario ha cerrado sesión', 'error')
    return redirect(url_for('auth.login'))
예제 #6
0
def confirm_delete(id):
    if not check_auth(session):
        abort(401)

    product = requests.get(API_GET_PRODUCT.format(id))

    if product.status_code == 200:
        requests.delete(API_DELETE_PRODUCT.format(id))
        flash('¡La eliminación ha sido exitosa!')
        return redirect(url_for('product.index'))
    return flash('¡No existe el producto indicada!', 'error')
예제 #7
0
def delete(id):
    if not check_auth(session):
        abort(401)

    product = requests.get(API_GET_PRODUCT.format(id))

    if product.status_code == 200:
        return render_template('product/delete.html',
                               product=product.json(),
                               id=id)
    return flash('¡No existe el producto indicada!', 'error')
예제 #8
0
def delete(id):
    if not check_auth(session):
        abort(401)

    category = requests.get(API_GET_CATEGORY.format(id))
    jso = category.json()
    if category.status_code == 200:
        return render_template('categories/delete.html',
                               category=category.json(),
                               id=id)
    return flash('¡No existe la categoria indicada!', 'error')
예제 #9
0
def create():
    if not check_auth(session):
        abort(401)

    form = CategoryForm()
    if form.validate_on_submit():
        category = {"nombre": str.upper(form.name.data).strip()}

        res = requests.post(API_POST_CATEGORY, data=json.dumps(category))
        flash('¡Se ha creado una nueva categoría!')
        return redirect(url_for('category.index'))

    return render_template('categories/create.html', form=form)
예제 #10
0
def edit(id):
    if not check_auth(session):
        abort(401)

    form = CategoryForm()

    req = requests.get(API_GET_CATEGORY.format(id))
    if req.status_code == 200 and form.validate_on_submit():
        category = {"nombre": str.upper(form.name.data).strip()}
        requests.patch(API_PUT_CATEGORY.format(id), json.dumps(category))
        flash('¡Se ha editado el producto con éxito!')
        return redirect(url_for('category.index'))
    form.name.data = req.json()['nombre']
    return render_template('categories/edit.html', form=form, category=id)
예제 #11
0
def edit(id):
    if not check_auth(session):
        abort(401)

    product = requests.get(API_GET_PRODUCT.format(id))
    categories = requests.get(API_GET_ALL_CATEGORIES).json()
    form = ProductEditForm()
    if categories:
        form.category.choices = [(categories[c]['nombre']) for c in categories]
    else:
        form.category.choices = []

    if form.validate_on_submit():
        if form.data['image'].filename != '':
            url_img = generate_url_image(form.data['image'])
        else:
            url_img = product.json()['url_imagen']

        product = {
            'nombre': (form.data['name']).strip(),
            'precio': form.data['price'],
            'categoria': form.data['category'],
            'url_imagen': url_img,
            'descripcion': (form.data['description']).strip()
        }

        try:
            req = requests.put(API_PUT_PRODUCT.format(id),
                               data=json.dumps(product))
            flash('¡Se ha editado el producto con éxito!')
        except ():
            abort(500)
        return redirect(url_for('product.index'))

    form.name.data = product.json()['nombre']
    form.description.data = product.json()['descripcion']
    form.price.data = product.json()['precio']
    form.category.data = product.json()['categoria']
    return render_template('product/edit.html', form=form, product=id)
예제 #12
0
def confirm_delete(id):
    if not check_auth(session):
        abort(401)

    category = requests.get(API_GET_CATEGORY.format(id))
    prod = requests.get(API_GET_ALL_PRODUCTS).json()
    if prod:
        products = [(str.upper(prod[p]['categoria'])) for p in prod]
    else:
        products = []
    try:
        if not category.json()['nombre'] in products:
            requests.delete(API_DELETE_CATEGORY.format(id))
            flash('¡La eliminación ha sido exitosa!')
        else:
            raise RuntimeError
    except:
        flash(
            '¡No se puede borrar esta categoria porque hay productos que pertenecen a ella!',
            'error')

    return redirect(url_for('category.index'))