def item_edit(item_id): item = Item.query.get(item_id) form = ItemForm() 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] form.category.default = item.category_id form.quality.default = item.quality_id form.process() form.name.data = item.name form.description.data = item.description if not item.sold and not item.hidden: if current_user.is_authenticated and (current_user.id is item.account_information_id): return render_template("items/edit.html", item=item, form=form) else: return redirect(url_for('items_index')) else: return redirect(url_for('items_index'))
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"))
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"))
def items_edit(item_id): form = ItemForm(request.form) item = Item.query.get(item_id) form.item_id = item.id if not form.validate(): return render_template("items/edit.html", form=form, item=item) item.name = form.name.data item.price = form.price.data item.threshold = form.threshold.data db.session().commit() return redirect(url_for("items_index"))
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"))
def item_update(item_id): form = ItemForm(request.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] # Validating only certain fields from the form if not (form.name.validate(form) and (form.description.validate(form)) and (form.quality.validate(form))): return render_template("items/edit.html", form=form, item=Item.query.get(item_id)) item = Item.query.get_or_404(item_id) if not item.sold and not item.hidden: if current_user.is_authenticated and (current_user.id == item.account_information.id): item.name = form.name.data item.description = form.description.data item.quality_id = form.quality.data item.category_id = form.category.data db.session().commit() return redirect(url_for("item_detail", item_id=item.id))
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))
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"))
def cc_form(): form = CcForm(request.form) if not form.validate(): return render_template("dbnew.html", formItem = ItemForm(), formCc = form, formPtype = PtypeForm()) duplicate = Colorcode.query.filter(Colorcode.code == form.code.data).first() if duplicate: return render_template("dbnew.html", formItem = ItemForm(), formCc = CcForm(), formPtype = PtypeForm(), errorCc = "Colorcode already in database.") cc = Colorcode(form.code.data, form.name.data) db.session().add(cc) db.session().commit() return redirect(url_for("cc_only"))
def ptype_form(): form = PtypeForm(request.form) if not form.validate(): return render_template("dbnew.html", formItem = ItemForm(), formCc = CcForm(), formPtype = form) duplicate = Ptype.query.filter_by(name=form.pname.data).first() if duplicate: return render_template("dbnew.html", formItem = ItemForm(), formCc = CcForm(), formPtype = PtypeForm(), errorPtype = "Product type already in database.") ptype = Ptype(form.pname.data) db.session().add(ptype) db.session().commit() return redirect(url_for("ptype_only"))
def items_form(): form = ItemForm() # Adding qualities and categories manually 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] return render_template("items/new.html", form=form)
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))
def cc_ptype_form(): form = ItemForm(request.form) if not form.validate(): return render_template("dbnew.html", formItem = form, formCc = CcForm(), formPtype = PtypeForm()) cc = Colorcode.query.filter(Colorcode.code == form.colorcode.data.code).first() ptype = Ptype.query.filter(Ptype.name == form.ptype.data.name).first() duplicate = Cc_ptype.query.filter(Cc_ptype.colorcode_id == cc.id, Cc_ptype.ptype_id == ptype.id).first() if duplicate: return render_template("dbnew.html", formItem = ItemForm(), formCc = CcForm(), formPtype = PtypeForm(), errorItem = "Product already in database.") ccptype = Cc_ptype(cc.id, ptype.id) db.session.add(ccptype) db.session().commit() return redirect(url_for("cc_ptype_index"))
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"))
def item_update(item_id): item = Item.query.get(item_id) form = ItemForm(request.form) form.id.data = item_id # Form validation if not form.validate(): return render_template("items/modify.html", form = form) # Update item in db item.name = 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 db.session().commit() return redirect(url_for("items_index"))
def delete_game_in_item(item_id, game_id): item = Item.query.get(item_id) if (len(item.games) == 1): errorMessage = 'Myyntikohteessa on oltava vähintään yksi peli' form = ItemForm() edit_defaults(item, form) return edit_template(item, form, errorMessage = errorMessage) Item.delete_game(item_id, game_id) db.session.commit() form = ItemForm() edit_defaults(item, form) return edit_template(item, form)
def item_edit(item_id): edited_item = Item.query.get(item_id) edit_form = ItemForm() if request.method == 'GET': edit_defaults(edited_item, edit_form) else: edit_form = ItemForm(request.form) if not edit_form.validate(): return edit_template(edited_item, edit_form) edited_item.name = edit_form.name.data edited_item.price = edit_form.price.data edited_item.description = edit_form.description.data # Return to the same edit page in both methods return edit_template(edited_item, edit_form)
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"))
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"))
def items_modify(item_id): item = Item.query.get(item_id) category = Category.query.get(item.category_id) form = ItemForm() form.item_category.choices = [ (c.id, c.name) for c in Category.query.order_by('name').filter_by( account_id=current_user.id) ] form.item_category.data = category.id form.expired.data = item.expired return render_template("items/modify.html", form=form, item=item)
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"))
def item_edit(item_id): item = Item.query.get(item_id) form = ItemForm() form.name.data = item.name form.brand.data = item.brand form.category.data = item.category form.weight.data = item.weight form.volume.data = item.volume form.description.data = item.description form.user_id.data = item.user_id form.id.data = item_id return render_template("items/modify.html", form = form)
def item_submit_game_edit(item_id): gameForm = GameToItemForm(request.form) item = Item.query.get(item_id) itemForm = ItemForm() edit_defaults(item, itemForm) if not gameForm.validate(): return edit_template(item, itemForm, gameForm = gameForm) gameName = gameForm.name.data if Item.check_game_in_item(item, gameName): errorMessage = game_already_in_item_error_message() return edit_template(item, itemForm, gameForm = gameForm, errorMessage = errorMessage) add_gameName_to_item(item, gameName) return edit_template(item, itemForm , gameForm = gameForm)
def items_commit_changes(item_id): form = ItemForm(request.form) item = Item.query.get(item_id) 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/modify.html", form=form, item=item) item.name = form.name.data category = Category.query.get(form.item_category.data) item.category_id = category.id item.expired = form.expired.data db.session().commit() return redirect(url_for("items_index"))
def items_form(): category_list = Category.query.order_by('name').filter_by( account_id=current_user.id) c_list = [] for c in category_list: c_list.append(c) if len(c_list) == 0: c = Category("Ei kategoriaa") c.account_id = current_user.id db.session().add(c) db.session().commit() form = ItemForm() form.item_category.choices = [ (c.id, c.name) for c in Category.query.order_by('name').filter_by( account_id=current_user.id) ] return render_template('items/new.html', form=form)
def warehouse_single(warehouse_id): form = ItemForm(request.form) return render_template("warehouses/single.html", warehouse=Warehouse.query.get(warehouse_id), form=form)
def items_form(): return render_template('items/new.html', form = ItemForm())
def db_form(): return render_template("dbnew.html", formItem = ItemForm(), formCc = CcForm(), formPtype = PtypeForm())
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))
def items_form(): default = List.user_has_default_list(current_user.id) if default != "default": List.create_default_list_for_user(current_user.id) return render_template("items/new.html", form = ItemForm(), categories = Category.query.all())