コード例 #1
0
ファイル: views.py プロジェクト: retute/Ostoslista
def items_create():
    form = ItemForm(request.form)
    form2 = CategoryForm(request.form)

    if not form.validate():
        return render_template("items/new.html", form=form)

    name = form.name.data
    it = Item.query.filter_by(name=name).first()
    if it:
        return render_template("items/new.html",
                               form=form,
                               error="Item is already listed!")
    i = Item(name)

    cid = form.category_id.data
    c = Category.query.get(cid)
    if not c:
        return render_template("categories_index")
    c.size = c.size + 1
    i.category_id = cid

    i.bought = form.bought.data
    i.account_id = current_user.id

    db.session().add(i)
    db.session().commit()

    return redirect(url_for("items_index"))
コード例 #2
0
ファイル: views.py プロジェクト: Darake/ruokaholvi
def recipes_ingredients(recipeId):
    if not recipeAuthorization(recipeId):
        return redirect(url_for("recipes_show"))

    if request.method == "GET":
        return render_template("/recipes/ingredients.html",
                                form=IngredientForm(),
                                ingredients=Recipe.list_recipes_ingredients(recipeId),
                                recipeId=recipeId)

    form = IngredientForm(request.form)
    if not form.validate():
        return render_template("/recipes/ingredients.html",
                                form=form,
                                ingredients=Recipe.list_recipes_ingredients(recipeId),
                                recipeId=recipeId)
    
    try:
        itemId = Item.get_matching_item(form.name.data).get("id")
        ingredient = Ingredient(form.amount.data, form.name.data, recipeId, itemId)
    except:
        item = Item(Item.name_to_lexeme(form.name.data))
        db.session().add(item)
        db.session().flush()
        ingredient = Ingredient(form.amount.data, form.name.data, recipeId, item.id)

    db.session().add(ingredient)
    db.session().commit()

    return redirect(url_for("recipes_ingredients", recipeId=recipeId))
コード例 #3
0
ファイル: views.py プロジェクト: thalvari/Tukkuliike
def items_create():
    form = ItemForm(request.form)
    if not form.validate():
        return render_template("items/new.html", form=form)
    item = Item(form.name.data, form.price.data, form.threshold.data)
    db.session.add(item)
    db.session.commit()
    return redirect(url_for("items_index"))
コード例 #4
0
ファイル: views.py プロジェクト: Antsax/myFridge
def items_create():
    form = ItemForm(request.form)

    if not form.validate():
        return render_template("items/new.html", form=form)

    i = Item(form.name.data)
    i.vegan = form.vegan.data
    i.account_id = current_user.id

    db.session().add(i)
    db.session().commit()

    return redirect(url_for("items_index"))
コード例 #5
0
def item_copy(item_id):
    item = Item.query.get(item_id)
    newItem = Item(item.name)
    newItem.brand = item.brand
    newItem.category = item.category
    newItem.weight = item.weight
    newItem.volume = item.volume
    newItem.description = item.description
    newItem.user_id = current_user.id

    db.session().add(newItem)
    db.session().commit()

    return redirect(url_for("items_index"))
コード例 #6
0
def items_create():
    form = ItemForm(request.form)

    if not form.validate():
        return render_template("items/new.html", form=form, warehouse_error="")

    item = Item.query.filter_by(name=form.name.data).first()

    if not item:
        item = Item(form.name.data, form.volume.data)
        db.session().add(item)
        db.session().commit()

    warehouse = Warehouse.query.filter_by(name=form.warehouse.data).first()

    if not warehouse:
        return render_template("items/new.html", form=form, warehouse_error="No such warehouse.")

    comp = Warehouse_item.query.filter_by(item_id=item.id, warehouse_id=warehouse.id).first()

    # check if item exists in warehouse

    if comp:

        new_amount = form.amount.data + comp.amount
        new_volume = new_amount * item.volume

        if not warehouse.fits(warehouse.id, new_volume):
            return render_template("items/new.html", form=form, warehouse_error="No room in warehouse.")
        elif (comp.amount + form.amount.data) < 0:
            return render_template("items/new.html", form=form, warehouse_error="Total amount can't be negative")
        
        comp.amount = new_amount

    else:

        new_amount = form.amount.data
        new_volume = new_amount * item.volume

        if not warehouse.fits(warehouse.id, new_volume):
            return render_template("items/new.html", form=form, warehouse_error="No room in warehouse.")
        
        relation = Warehouse_item(warehouse, item, new_amount)
        db.session().add(relation)
    
    db.session().commit()
    return redirect(url_for("warehouse_single", warehouse_id=warehouse.id))
コード例 #7
0
ファイル: views.py プロジェクト: petrioski/ostoslista
def create_item():
    categories = db.session.query(Category).all()
    form = CreateNewItemForm(request.form)
    category_selection = [(i.id, i.name) for i in categories]
    form.category.choices = category_selection

    if not form.validate_on_submit():
        return render_template("/items/new.html", form=form)

    new_item = Item(form.name.data)
    new_item.unit_type = form.unit_type.data
    new_item.category_id = form.category.data

    db.session().add(new_item)
    db.session().commit()

    return redirect(url_for("items_index"))
コード例 #8
0
def items_create():
    form = ItemForm(request.form)
    list = Category.query.all()

    if not form.validate():
        return render_template("items/new.html", form = form, categories = list)

    i = Item(form.name.data, form.amount.data)
    i.bought = form.bought.data
    i.account_id = current_user.id
    i.category_id = form.category.data
    i.list_id = List.find_users_defaultlist_id(current_user.id)

    db.session().add(i)
    db.session().commit()

    return redirect(url_for("items_index"))
コード例 #9
0
ファイル: views.py プロジェクト: AaaDee/Lautapelikirppis
def items_create():
    form = ItemForm(request.form)

    if not form.validate():
        return render_template('items/new.html', form = form)

    new_item = Item(form.name.data, 
                    form.description.data,
                    form.price.data)
    
    new_item.account_id = current_user.id
    gameName = form.gameName.data

    db.session().add(new_item)
    add_gameName_to_item(new_item, gameName)

    db.session().commit()
  
    return redirect(url_for('item_add_games', item_id = new_item.id))
コード例 #10
0
ファイル: views.py プロジェクト: nikomn/itemindex
def items_create():

    form = ItemForm(request.form)
    form.item_category.choices = [
        (c.id, c.name) for c in Category.query.order_by('name').filter_by(
            account_id=current_user.id)
    ]

    if not form.validate():
        return render_template("items/new.html", form=form)

    i = Item(form.name.data, form.expired.data)
    category = Category.query.get(form.item_category.data)
    i.account_id = current_user.id
    i.category_id = category.id

    db.session().add(i)
    db.session().commit()

    return redirect(url_for("items_index"))
コード例 #11
0
ファイル: views.py プロジェクト: Darake/ruokaholvi
def items_create():
    form = ItemForm(request.form)
    if not form.validate():
        return render_template("items/list.html", 
        items = UserItem.list_users_items(),
        form = form)

    ui = UserItem(form.name.data, form.best_before.data)
    ui.account_id = current_user.id
    try:
        ui.item_id = Item.get_matching_item(form.name.data).get("id")
    except:
        i = Item(Item.name_to_lexeme(form.name.data))
        db.session().add(i)
        db.session().commit()
        ui.item_id = Item.get_matching_item(form.name.data).get("id")
    
    db.session().add(ui)
    db.session().commit()

    return redirect(url_for("items_index"))
コード例 #12
0
def item_form():

    form = ItemForm(request.form)

    if request.method == "GET":
        return render_template("items/new.html", form=form)

    if not form.validate():
        return render_template("items/new.html", form=form)

    cc = Colorcode.query.filter(
        Colorcode.code == form.colorcode.data.code).first()
    ptype = Ptype.query.filter(Ptype.name == form.ptype.data.name).first()

    validProduct = Cc_ptype.query.filter(
        Cc_ptype.colorcode_id == cc.id, Cc_ptype.ptype_id == ptype.id).first()
    if not validProduct:
        return render_template("items/new.html",
                               form=form,
                               error="Product does not exist.")

    duplicate = Item.query.filter(Item.colorcode_id == cc.id,
                                  Item.ptype_id == ptype.id,
                                  Item.account_id == current_user.id).first()

    if duplicate:
        return render_template("items/new.html",
                               form=form,
                               error="Product already in collection.")

    item = Item()
    item.colorcode_id = cc.id
    item.ptype_id = ptype.id
    item.account_id = current_user.id

    db.session.add(item)
    db.session().commit()

    return redirect(url_for("index"))
コード例 #13
0
def item_create():
    # New item form
    if request.method=="GET":
        return render_template("items/new.html", form = ItemForm())

    # Form validation
    form = ItemForm(request.form)
    if not form.validate():
        return render_template("items/new.html", form = form)

    # Adding new item to db
    item = Item(form.name.data)
    item.brand = form.brand.data
    item.category = form.category.data
    item.weight = form.weight.data
    item.volume = form.volume.data
    item.description = form.description.data
    item.user_id = current_user.id

    db.session().add(item)
    db.session().commit()

    return redirect(url_for("items_index"))
コード例 #14
0
def items_create():
    form = ItemForm(request.form)

    # Fetching all qualities from the database and manually adding them to the form
    qualities = Quality.query.all()
    form.quality.choices = [(quality.id, quality.name)
                            for quality in qualities]

    categories = Category.query.all()
    form.category.choices = [(category.id, category.name)
                             for category in categories]

    if not form.validate_on_submit():
        return render_template("items/new.html", form=form)

    helsinki = pytz.timezone("Europe/Helsinki")
    bidding_end = "{} {}".format(form.bidding_end.bidding_end_date.data,
                                 form.bidding_end.bidding_end_time.data)
    bidding_end = datetime.datetime.strptime(bidding_end, "%Y-%m-%d %H:%M")
    bidding_end = helsinki.localize(bidding_end)
    # Saving datetime in UTC timezone for easier processing
    bidding_end = bidding_end.astimezone(utc)

    if 'image' not in request.files:
        form.image.errors.append("No image found")
        return render_template("items/new.html", form=form)

    image_file = request.files.get("image")

    # If not image file return form
    if image_file.filename.rsplit(
            ".", 1)[1].lower() not in ["jpg", "jpeg", "png", "bmp"]:
        form.image.errors.append("Invalid filetype")
        return render_template("items/new.html", form=form)

    img = Image.open(request.files.get("image"))

    sec_filename = secure_filename(image_file.filename)
    timecode = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
    file_key = "{}-{}".format(sec_filename.rsplit(".", 1)[0], timecode)

    thumbnail_image_key = "{}-400".format(file_key)
    full_image_key = "{}-800".format(file_key)

    thumbnail_image_name = "{}.jpg".format(thumbnail_image_key)
    full_image_name = "{}.jpg".format(full_image_key)

    # Image filename format: name-timestamp-size
    thumbnail_image = create_thumbnail(img)
    full_image = create_full(img)

    thumbnail_image = thumbnail_image.convert('RGB')
    thumbnail_image_bytes_stream = BytesIO()
    thumbnail_image.save(thumbnail_image_bytes_stream, "JPEG", quality=70)

    full_image = full_image.convert('RGB')
    full_image_bytes_stream = BytesIO()
    full_image.save(full_image_bytes_stream, "JPEG", quality=75)

    # if os.environ.get("AWS") == "huutokauppa-sovellus":
    #     # Save thumbnail image to s3
    #     put_object_to_s3(image_bytes=thumbnail_image_bytes_stream.getvalue(), filename=thumbnail_image_key)
    #     thumbnail_image_url = "{}{}".format(application.config["S3_LOCATION"], thumbnail_image_key)

    #     # Save full image to s3
    #     put_object_to_s3(image_bytes=full_image_bytes_stream.getvalue(), filename=full_image_key)
    #     full_image_url = "{}{}".format(application.config["S3_LOCATION"], full_image_key)
    # If running locally save images to static/images
    with open(
            os.path.join(application.config["UPLOAD_FOLDER"],
                         thumbnail_image_name), "wb") as f:
        f.write(thumbnail_image_bytes_stream.getvalue())
        thumbnail_image_url = url_for(
            'static', filename="images/{}".format(thumbnail_image_name))

    with open(
            os.path.join(application.config["UPLOAD_FOLDER"], full_image_name),
            "wb") as f:
        f.write(full_image_bytes_stream.getvalue())
        full_image_url = url_for('static',
                                 filename="images/{}".format(full_image_name))

    item = Item(starting_price=form.starting_price.data,
                current_price=form.starting_price.data,
                name=form.name.data,
                account_information_id=current_user.id,
                quality=form.quality.data,
                category=form.category.data,
                description=form.description.data,
                bidding_end=bidding_end,
                image_thumbnail=thumbnail_image_url,
                image_full=full_image_url)

    db.session().add(item)
    db.session().flush()

    # Adding a new task for the item that sells it when the bidding time ends
    task_id = sell_item.apply_async(args=[item.id], eta=bidding_end)
    item.celery_task_id = task_id.id
    db.session().commit()

    return redirect(url_for("item_detail", item_id=item.id))
コード例 #15
0
ファイル: schemas.py プロジェクト: marcusstockton/flask_api
 def make_object(self, data, **kwargs):
     return Item(**data)
コード例 #16
0
ファイル: schemas.py プロジェクト: marcusstockton/flask_api
 def make_object(self, data):
     return Item(**data)