def store(user_id): """ Store item to database. params: user_id: foreign key that should exists in table users.id """ form = ItemForm() if not form.validate(): return ItemController.message(False, form.errors) item = Item() item.name = form.name.data.strip() item.description = form.description.data item.category_id = CategoryHash.decode(form.category_id.data) item.user_id = user_id db.session.add(item) if item.commit_changes(): return ItemController.message(True, item) else: return ItemController.message( False, 'Could not save \ the given item.')
def new_item_save(): """ saves the new item to db """ form = request.form item = Item() item.category_id = int(form['category']) item.title = form['title'] item.description = form['description'] item.created_at = datetime.now() item.user_id = current_user.id db.session.add(item) db.session.commit() return redirect(url_for('index'))
def item_save(request, item_id): categories = Category.objects.all().order_by('name') error = False error_message = [] if(item_id == 0 or item_id == '0'): item = Item() item.state = STATE_AVAILABLE else: item = get_object_or_404(Item, pk=item_id) item.name = request.POST['name'] item.description = request.POST['description'] item.condition = request.POST['condition'] if len(item.name) == 0: error = True error_message.append("El nombre es requerido") if len(item.condition) == 0: error = True error_message.append("La condicion es requerida") article_id = request.POST['article'] if article_id != 0 and article_id != '0': item.article = Article.objects.get(id=article_id) else: item.article = None if error: articles = Article.objects.all().order_by('name') context = { 'categories': categories, 'item': item, 'error_message': error_message, 'articles': articles, } return render(request, 'item_add.html', context) else: item.save() article = Article.objects.get(pk=item.article.id) context = { 'categories': categories, 'article': article, 'success_message': 'El item ' + item.name + ' ha sido eliminado exitosamente.', } return render(request, 'article.html', context)
def item_create(category_id): """ This creates a new item only if the user is logged in """ if not logged_in(): flash('Please log in to create items') return redirect(url_for('category_list')) form = ItemForm() if form.validate_on_submit(): item = Item() item.title = form.title.data item.description = form.description.data item.category_id = category_id item.ctlg_user = get_current_user() db.session.add(item) db.session.commit() return redirect(url_for('item_create', category_id=category_id)) return render_template('item_create.html', form=form, category_id=category_id)
print "Creating Sample Category Football ..." category = Category() category.title = "Football" category.user_id = user.id category.created = datetime.utcnow() category.modified = datetime.utcnow() db.session.add(category) db.session.commit() print "Category - Football created ID=" + str(category.id) print "Creating Sample Items in Football ..." item = Item() item.category_id = category.id item.title = "Soccer Ball" item.description = "High Quality ,Tournament grade" item.created = datetime.utcnow() item.modified = datetime.utcnow() item.user_id = user.id db.session.add(item) db.session.commit() print "Item - %s created ID=%d" % (item.title, item.id) print "Creating Sample Items in Football ..." item = Item() item.category_id = category.id item.title = "Studs - Reebok" item.description = "Imported Studs from Taiwan" item.created = datetime.utcnow() item.modified = datetime.utcnow() item.user_id = user.id