def choose(): jamla = get_jamla() # Filter archived items jamlaApp = Jamla() jamla = jamlaApp.filter_archived_items(jamla) return render_template("choose.html", jamla=jamla, pages=jamla['pages'])
def delete_item_by_sku(sku): """Archive (dont actually delete) an item""" jamla = get_jamla() # Filter archived items jamlaApp = Jamla() jamla = jamlaApp.filter_archived_items(jamla) jamlaApp = Jamla() jamlaApp.load(jamla=get_jamla()) itemIndex = jamlaApp.sku_get_index(sku) if "confirm" in request.args: confirm = False return render_template( "admin/delete_jamla_item_choose.html", jamla=jamla, itemSKU=sku, confirm=False, ) if itemIndex is not False: # Perform removal jamla["items"][itemIndex]['archived'] = True fp = open(current_app.config["JAMLA_PATH"], "w") yaml.safe_dump(jamla, fp, default_flow_style=False) flash("Item deleted.") return render_template("admin/delete_jamla_item_choose.html", jamla=jamla)
def delete_jamla_item(): jamla = get_jamla() # Filter archived items jamlaApp = Jamla() jamla = jamlaApp.filter_archived_items(jamla) return render_template("admin/delete_jamla_item_choose.html", jamla=jamla)
def edit_jamla(): """Update jamla items Note sku items are immutable, when a change is made to an item, its old item is archived and a new sku item is created with a new uuid. This is to protect data integriry and make sure plan history is retained, via its uuid. If a user needs to change a subscription, they should change to a different plan with a different uuid. """ form = ItemsForm() jamla = get_jamla() # Filter archived items jamlaApp = Jamla() jamla = jamlaApp.filter_archived_items(jamla) if form.validate_on_submit(): jamla["company"]["name"] = request.form["company_name"] jamla["company"]["slogan"] = request.form["slogan"] jamla["users"][0] = request.form["email"] # Loop items for index in request.form.getlist("itemIndex", type=int): # Archive existing item then create new sku item # (remember, edit edits create new items because # skus are immutable) jamla["items"][index]["archived"] = True # Archive item # Build new sku draftItem = {} draftItem["uuid"] = str(uuid.uuid4()) draftItem["requirements"] = {} # Preserve primary icon if exists draftItem["primary_icon"] = jamla["items"][index]["primary_icon"] draftItem["title"] = getItem(form.title.data, index, default="").strip() draftItem["sku"] = draftItem["title"].replace(" ", "").strip() draftItem["requirements"]["subscription"] = bool( getItem(form.subscription.data, index)) if getItem(form.monthly_price.data, index, default=0) is None: monthly_price = 0.00 else: monthly_price = getItem( form.monthly_price.data, index, default=0) * 100 draftItem["monthly_price"] = monthly_price draftItem["requirements"]["instant_payment"] = bool( getItem(form.instant_payment.data, index)) draftItem["requirements"]["note_to_seller_required"] = bool( getItem(form.note_to_seller_required.data, index)) draftItem["requirements"]["note_to_buyer_message"] = str( getItem(form.note_to_buyer_message, index, default="").data) try: days_before_first_charge = int( form.days_before_first_charge[index].data) except ValueError: days_before_first_charge = 0 draftItem["days_before_first_charge"] = days_before_first_charge if getItem(form.sell_price.data, index, default=0) is None: sell_price = 0.00 else: sell_price = getItem(form.sell_price.data, index, default=0) * 100 draftItem["sell_price"] = sell_price draftItem["selling_points"] = getItem(form.selling_points.data, index, default="") # Primary icon image storage f = getItem(form.image.data, index) if f: images = UploadSet("images", IMAGES) filename = images.save(f) # symlink to active theme static directory img_src = "".join( [current_app.config["UPLOADED_IMAGES_DEST"], filename]) link = "".join([current_app.config["STATIC_FOLDER"], filename]) symlink(img_src, link, overwrite=True) src = url_for("static", filename=filename) draftItem["primary_icon"] = {"src": src, "type": ""} # Add new sku to items jamla["items"].append(draftItem) fp = open(current_app.config["JAMLA_PATH"], "w") yaml.safe_dump(jamla, fp, default_flow_style=False) flash("Item(s) updated.") # Trigger a reload by touching the wsgi file. # Which file we need to touch depends on the wsgi config # e.g. on uwsgi to set it to subscribie.wsgi on uwsgi we pass: # uwsgi --http :9090 --workers 2 --wsgi-file subscribie.wsgi \ # --touch-chain-reload subscribie.wsgi # To uwsgi. The `--touch-chain-reload` option tells uwsgi to perform # a graceful reload. "When triggered, it will restart one worker at # time, and the following worker is not reloaded until the previous one # is ready to accept new requests. We must use more than one worker for # this to work. See: # https://uwsgi-docs.readthedocs.io/en/latest/articles/TheArtOfGracefulReloading.html#chain-reloading-lazy-apps wsgiFile = os.path.abspath(''.join([os.getcwd(), '/subscribie.wsgi'])) p = Path(wsgiFile) p.touch(exist_ok=True) # Triggers the graceful reload return redirect(url_for("admin.dashboard")) return render_template("admin/edit_jamla.html", jamla=jamla, form=form)