Esempio n. 1
0
def admin_item_edit(request, id=None):
    item = id and Item.get_by_id(int(id)) or None

    if request.POST:
        form = FormItem(request.POST, files=request.FILES, instance=item)

        if form.is_valid():
            item = form.save(False)
            
            if form.cleaned_data.get('file_to_upload', None):
                # Get image to field file
                img = db.Blob(form.cleaned_data['file_to_upload'].read())
                item.file = img

                # Builds image thumbnail
                item.thumb = images.resize(img, 120, 120)

                item.mimetype = form.cleaned_data['file_to_upload'].content_type

            item.save()

            return HttpResponseRedirect(item.get_absolute_url())
    else:
        form = FormItem(instance=item)

    return locals()
def editCatalogItem(category_name, item_name):
    """Edit the attributes of an item in the database

    Args:
        category_name(str): the name of the category the item belongs to
        item_name(str): the name of the item

    Returns:
        render the editcatalogitem template if a GET request is sent
        redirect to the category page if the POST request succeeds"""
    if 'provider' in login_session:
        editedItem = session.query(CatalogItem).filter_by(name=item_name).one()
        form = FormItem()
        if form.validate_on_submit():
            if hasattr(form, 'name'):
                editedItem.name = form.name.data
            if hasattr(form, 'description'):
                editedItem.description = form.description.data
            if hasattr(form, 'image_loc'):
                editedItem.image_loc = form.image_loc.data
            session.add(editedItem)
            session.commit()
            return redirect(
                url_for('showCategory', category_name=category_name))
        return render_template('editcatalogitem.html',
                               category_name=category_name,
                               item_name=item_name,
                               item=editedItem,
                               form=form)
    else:
        flash("Please login to be able to edit an item")
        return redirect(url_for('showCategory', category_name=category_name))
def newCatalogItem(category_name):
    """Create a new item in the database

    Args:
        category_name(str): the name of the category the item belongs to

    Returns:
        render the newcatalogitem template if a GET request is sent
        redirect to the category page if the POST request succeeds"""
    if 'provider' in login_session:
        form = FormItem()
        if form.validate_on_submit():
            category = session.query(Category).filter_by(
                name=category_name).one()
            newItem = CatalogItem(name=form.name.data,
                                  description=form.description.data,
                                  category_id=category.id,
                                  image_loc=form.image_loc.data)
            session.add(newItem)
            session.commit()
            return redirect(
                url_for('showCategory', category_name=category_name))
        return render_template('newcatalogitem.html',
                               category_name=category_name,
                               form=form)
    else:
        flash("Please login to be able to add a new item")
        return redirect(url_for('showCategory', category_name=category_name))