diff --git a/__init__.py b/__init__.py index 577587e..67600e7 100644 --- a/__init__.py +++ b/__init__.py @@ -21,11 +21,12 @@ import uuid import sqlite3 from subscribie.database import database +from subscribie.schemas import Shop builder = Blueprint("builder", __name__, template_folder="templates") -class Shop(database.Model): +class ShopInDB(database.Model): __tablename__ = "builder_sites" __table_args__ = {"extend_existing": True} @@ -53,31 +54,18 @@ def submit_new_site_build( when combined with domain, becomes abc.example.com :param session: The serialized flask session data """ - payload = {} - payload["version"] = 1 - payload["users"] = [form.email.data] - payload["password"] = form.password.data - payload["login_token"] = login_token - company_name = form.company_name.data - payload["company"] = {"name": company_name, "logo": "", "start_image": ""} - # If possible, get country code and send to subscribie deployer - # Ref: https://github.com/Subscribie/module-builder/issues/33 - payload["country_code"] = session.get("country_code", "GB") - payload["theme"] = {"name": "jesmond", "static_folder": "./static/"} - - # Custom styles prepare as empty - payload["theme"]["options"] = {} - payload["theme"]["options"]["styles"] = [] - - # Pages as empty array - payload["pages"] = [] - - plans = [] + + postData = form.data + postData["users"] = [form.data.get("email", None)] + postData["version"] = 1 + postData["login_token"] = login_token + postData["plans"] = [] + postData["company"] = {"name": form.data.get("company_name", None)} + for index, plan in enumerate(form.title.data): plan = {} plan["uuid"] = str(uuid.uuid4()) plan["title"] = getPlan(form.title.data, index) - plan["sku"] = getPlan(form.title.data, index) if getPlan(form.sell_price.data, index) is None: plan["sell_price"] = 0 else: @@ -93,7 +81,6 @@ def submit_new_site_build( plan["interval_unit"] = getPlan(form.interval_unit.data, index) plan["description"] = getPlan(form.description.data, index) plan["subscription_terms"] = {"minimum_term_months": 12} - plan["primary_colour"] = "#e73b1a" # Plan requirements plan["requirements"] = {} plan["requirements"]["instant_payment"] = getPlan( @@ -103,16 +90,16 @@ def submit_new_site_build( form.subscription.data, index ) # noqa: E501 plan["requirements"]["note_to_seller_required"] = False - plan["primary_icon"] = {"src": False, "type": False} print(plan) - plans.append(plan) - payload["plans"] = plans + postData["plans"].append(plan) + + shop = Shop(**postData) + # Save to json - json.dumps(payload) with open(subdomain + ".json", "w") as fp: - fp.write(json.dumps(payload)) + fp.write(json.dumps(postData)) deploy_url = app_config.get("JAMLA_DEPLOY_URL") - deployJamla(subdomain + ".json", deploy_url=deploy_url) + deploy(shop, deploy_url=deploy_url) # Inform try: @@ -198,11 +185,9 @@ def journey_complete_subscriber(sender, **kw): @builder.route("/sendJamla") -def deployJamla(filename, deploy_url=None): - with open(filename) as fp: - payload = json.loads(fp.read()) - requests.post(deploy_url, json=payload) - return "Sent jamla file for deployment" +def deploy(shop, deploy_url=None): + requests.post(deploy_url, json=shop.json()) + return "New shop deployment requested" def create_subdomain_string(company_name=None): @@ -245,7 +230,7 @@ def shop_owner_login(): @login_required def shops(): """List all shops""" - shops = Shop.query.all() + shops = ShopInDB.query.all() shops = reversed(shops) return render_template("shops.html", shops=shops)