예제 #1
0
def doDatabaseWrite(categories, previous_category, user_data, item=None):
    """
    Método utilitário que checa se os campos do formulário estão preenchidos
    antes de salvar o item no banco de dados. Em caso de erro, retorna os
    redirects e erros correspondente à ação sendo executada (criação ou edição)

    Também checa se um item já existe na categoria informada com o mesmo nome.
    """
    if item:
        form_name = "edititem.html"
        message = "Item edited"
        isInsert = False
    else:
        item = Items(title=None,
                     description=None,
                     category_id=None,
                     user_id=login_session["user_id"])
        form_name = "newitem.html"
        message = "New item created"
        isInsert = True

    if user_data["title"] \
            and user_data["description"] \
            and user_data["category_id"]:
        item.title = user_data["title"]
        item.description = user_data["description"]
        item.category_id = user_data["category_id"]

        try:
            session.add(item)
            session.commit()
            flash(message)

            category_name = session.query(Categories) \
                .filter_by(id=user_data["category_id"]) \
                .first().name

            if isInsert:
                return redirect(url_for("showAllItems",
                                        category_name=category_name))
            else:
                return redirect(url_for("showItem",
                                category_name=category_name,
                                item_name=user_data["title"]))
        except IntegrityError:
            session.rollback()
            flash("Item '%s' already exists in selected category." %
                  user_data["title"])
            return render_template(form_name,
                                   categories=categories,
                                   item=user_data,
                                   previous_category=previous_category,
                                   previous_item=item.title)
    else:
        flash("There is missing data")
        return render_template(form_name,
                               categories=categories,
                               item=user_data,
                               previous_category=previous_category,
                               previous_item=item.title)
예제 #2
0
파일: views.py 프로젝트: DiyarS/CMPSC431W
def submit_item_to_sell(request, user_email):
    """
    View for filling out information about an item to sell
    """
    if request.method == 'POST':
        new_item = Items()

        max_id_value = 0
        for existing_item in Items.objects.all():
            last_id = existing_item.item_id
            if int(last_id) > max_id_value:
                max_id_value = int(last_id)

        new_id = str(max_id_value+1)
        new_item.item_id = new_id
        new_item.item_name = request.POST.get('item_name')
        new_item.description = request.POST.get('description')
        new_item.location = request.POST.get('location')
        new_item.save()



        new_user = True
        #search for users
        for user in Sellers.objects.all():
            if user.seller_id == request.POST.get('user_email'):
                user.item_id_id = new_item.item_id
                new_user = False
                user.save()

        if new_user == True:
            new_seller = Sellers()
            new_seller.username_id = retrieve_id_of_registered_user(request.POST.get('user_email'))
            idd = new_seller.username_id

            if request.POST.get('user_email') == None:
                new_seller.seller_id = 'empty'
            else:
                new_seller.seller_id = request.POST.get('user_email')
            new_seller.item_id_id = new_item.item_id
            use = request.GET.get('user_email')
            new_seller.save()



        price_entry_for_new_item = Fixed_Price()
        price_entry_for_new_item.item_id = new_item
        price_entry_for_new_item.price = request.POST.get('price')
        price_entry_for_new_item.save()

        item_belongs_to = Belongs_to()
        item_belongs_to.item_id = new_item
        item_belongs_to.category = 'Books'
        item_belongs_to.save()


    #return render_to_response('add_item_for_sell.html')
    return HttpResponseRedirect('/online_store/submitted_item_to_sell', {'user_email': user_email} )
예제 #3
0
def add_item():
    if request.method == 'GET':
        if (login_session['username'] is not None):
            categories = db.query(Category).all()
            return render_template('add_item.html',
                                   login_session=login_session,
                                   categories=categories)
        else:
            return render_template('login.html',
                                   error="You must be logged in to do that!")
    elif request.method == 'POST':
        if login_session['username'] is not None:
            categories = db.query(Category).all()
            item_name = request.form.get('item_name')
            price = request.form.get('price')
            description = request.form.get('description')
            category = request.form.get('category')
            new_category = request.form.get('new_category')

            if item_name is not None and item_name != '':
                item = Items(name=item_name)
                if price is not None and item.price != '':
                    item.price = price
                if description is not None and description != '':
                    item.description = description
                item.author = getUserInfo(login_session['user_id'])
            else:
                flash("You need to give an item!")
                return redirect(url_for('add_item'))

            if category is not None and category != '':
                c = db.query(Category).filter_by(name=category).first()
                item.category = c
            elif new_category is not None and new_category != '':
                c = db.query(Category).filter_by(name=new_category).first()
                if (c):
                    flash("You cannot add a category that already exists!")
                    return redirect(url_for('add_item'))
                new_category = Category(name=new_category)
                item.category = new_category
                db.add(new_category)
            else:
                flash("You need to provide a category!")
                return redirect(url_for('add_item'))

            db.add(item)
            db.commit()
            return redirect(url_for('index'))
        else:
            flash("Please log in to add items")
            return redirect(url_for('index'))