From 88c45a9c4574c6ea2e98db287cd1c593773508ee Mon Sep 17 00:00:00 2001 From: Tiago vasconcelos Date: Fri, 15 Jul 2022 10:22:13 +0100 Subject: [PATCH] create and edit products --- lnbits/extensions/diagonalley/crud.py | 4 +- lnbits/extensions/diagonalley/migrations.py | 6 +- lnbits/extensions/diagonalley/models.py | 8 +- .../templates/diagonalley/index.html | 196 +++++++++--------- lnbits/extensions/diagonalley/views_api.py | 34 +-- 5 files changed, 131 insertions(+), 117 deletions(-) diff --git a/lnbits/extensions/diagonalley/crud.py b/lnbits/extensions/diagonalley/crud.py index 117a23790..44864b808 100644 --- a/lnbits/extensions/diagonalley/crud.py +++ b/lnbits/extensions/diagonalley/crud.py @@ -51,7 +51,6 @@ async def create_diagonalley_product(data: createProduct) -> Products: async def update_diagonalley_product(product_id: str, **kwargs) -> Optional[Stalls]: q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()]) - # with open_ext_db("diagonalley") as db: await db.execute( f"UPDATE diagonalley.products SET {q} WHERE id = ?", (*kwargs.values(), product_id), @@ -60,7 +59,7 @@ async def update_diagonalley_product(product_id: str, **kwargs) -> Optional[Stal "SELECT * FROM diagonalley.products WHERE id = ?", (product_id,) ) - return get_diagonalley_stall(product_id) + return Products(**row) if row else None async def get_diagonalley_product(product_id: str) -> Optional[Products]: @@ -190,6 +189,7 @@ async def get_diagonalley_stall(stall_id: str) -> Optional[Stalls]: row = await db.fetchone( "SELECT * FROM diagonalley.stalls WHERE id = ?", (stall_id,) ) + print("ROW", row) return Stalls(**row) if row else None diff --git a/lnbits/extensions/diagonalley/migrations.py b/lnbits/extensions/diagonalley/migrations.py index 11cd2d117..2500dbb09 100644 --- a/lnbits/extensions/diagonalley/migrations.py +++ b/lnbits/extensions/diagonalley/migrations.py @@ -8,9 +8,9 @@ async def m001_initial(db): id TEXT PRIMARY KEY, stall TEXT NOT NULL, product TEXT NOT NULL, - categories TEXT NOT NULL, - description TEXT NOT NULL, - image TEXT NOT NULL, + categories TEXT, + description TEXT, + image TEXT, price INTEGER NOT NULL, quantity INTEGER NOT NULL ); diff --git a/lnbits/extensions/diagonalley/models.py b/lnbits/extensions/diagonalley/models.py index 9557f6e9f..2132d72f6 100644 --- a/lnbits/extensions/diagonalley/models.py +++ b/lnbits/extensions/diagonalley/models.py @@ -25,7 +25,7 @@ class createStalls(BaseModel): class createProduct(BaseModel): stall: str = Query(...) - product: str = Query(None) + product: str = Query(...) categories: str = Query(None) description: str = Query(None) image: str = Query(None) @@ -37,9 +37,9 @@ class Products(BaseModel): id: str stall: str product: str - categories: str - description: str - image: str + categories: Optional[str] + description: Optional[str] + image: Optional[str] price: int quantity: int diff --git a/lnbits/extensions/diagonalley/templates/diagonalley/index.html b/lnbits/extensions/diagonalley/templates/diagonalley/index.html index a804898d2..7c11f31fe 100644 --- a/lnbits/extensions/diagonalley/templates/diagonalley/index.html +++ b/lnbits/extensions/diagonalley/templates/diagonalley/index.html @@ -1,6 +1,7 @@ {% extends "base.html" %} {% from "macros.jinja" import window_vars with context %} {% block page %}
+ @@ -26,28 +27,37 @@ v-model.trim="productDialog.data.description" label="Description" > -
-
+ + + + -
-
+ emit-value + v-model.trim="productDialog.data.categories" + use-input + use-chips + multiple + hide-dropdown-icon + input-debounce="0" + new-value-mode="add-unique" + label="Categories" + placeholder="crafts,robots,etc" + hint="Hit Enter to add" + > + + Create Product - Cancel
- + @@ -152,15 +162,14 @@ >Create Shipping Zone - Cancel - - + @@ -173,7 +182,8 @@ filled dense multiple - :options="stalls" + emit-value + :options="stalls.map(s => ({label: s.name, value: s.id}))" label="Stalls" v-model.trim="shopDialog.data.stalls" > @@ -195,14 +205,14 @@ >Launch - Cancel - + @@ -292,7 +302,7 @@ type="submit" >Create Store - Cancel @@ -328,8 +338,8 @@ - - + +
@@ -393,7 +403,7 @@ - +
@@ -472,8 +482,8 @@ - - + +
@@ -531,8 +541,8 @@ - - + +
@@ -658,6 +668,7 @@ const mapStalls = obj => { } const mapProducts = obj => { obj._data = _.clone(obj) + console.log(obj) return obj } const mapZone = obj => { @@ -744,7 +755,8 @@ new Vue({ 'Technology (Phones and Computers)', 'Home (furniture and accessories)', 'Gifts (flowers, cards, etc)', - 'Adult' + 'Adult', + 'Other' ], relayOptions: [ 'wss://nostr-relay.herokuapp.com/ws', @@ -924,6 +936,10 @@ new Vue({ } }, methods: { + resetDialog(dialog){ + this[dialog].show = false + this[dialog].data = {} + }, generateKeys: function(){ var self = this LNbits.api @@ -967,6 +983,7 @@ new Vue({ ////////////////STALLS////////////////// //////////////////////////////////////// getStalls: function () { + console.log(this.g.user) var self = this LNbits.api .request( @@ -1037,9 +1054,7 @@ new Vue({ return obj.id == data.id }) self.stalls.push(mapStalls(response.data)) - self.stallDialog.show = false - self.stallDialog.data = {} - data = {} + self.resetDialog('stallDialog') }) .catch(function (error) { LNbits.utils.notifyApiError(error) @@ -1058,9 +1073,10 @@ new Vue({ ) .then(function (response) { self.stalls.push(mapStalls(response.data)) - self.stallDialog.show = false - self.stallDialog.data = {} - data = {} + self.resetDialog('stallDialog') + //self.stallDialog.show = false + //self.stallDialog.data = {} + //data = {} }) .catch(function (error) { LNbits.utils.notifyApiError(error) @@ -1105,7 +1121,7 @@ new Vue({ self.g.user.wallets[0].inkey ) .then(function (response) { - console.log(response.data) + console.log("RESP DATA", response.data) if (response.data) { self.products = response.data.map(mapProducts) } @@ -1119,22 +1135,24 @@ new Vue({ var link = _.findWhere(self.products, {id: linkId}) self.productDialog.data = _.clone(link._data) + self.productDialog.data.categories = self.productDialog.data.categories.split(",") + self.productDialog.show = true }, sendProductFormData: function () { + let _data = {...this.productDialog.data} var data = { - stall: this.productDialog.data.stall, - product: this.productDialog.data.product, - categories: - this.productDialog.data.categories + - this.productDialog.categoriesextra, - description: this.productDialog.data.description, - image: this.productDialog.data.image, - price: this.productDialog.data.price, - quantity: this.productDialog.data.quantity + stall: _data.stall, + product: _data.product, + categories: _data.categories && _data.categories.toString(), + description: _data.description, + image: _data.image, + price: _data.price, + quantity: _data.quantity } - if (this.productDialog.data.id) { - this.updateProduct({...this.productDialog.data, ...data}) + if (_data.id) { + data.id = _data.id + this.updateProduct(data) } else { this.createProduct(data) } @@ -1163,80 +1181,72 @@ new Vue({ this.productDialog = {...this.productDialog} }, updateProduct: function (data) { - var self = this - LNbits.api - .request( - 'PUT', - '/diagonalley/api/v1/products' + data.id, - _.findWhere(self.g.user.wallets, { - id: self.productDialog.data.wallet - }).inkey, - _.pick( - data, - 'shopname', - 'relayaddress', - 'shippingzone1', - 'zone1cost', - 'shippingzone2', - 'zone2cost', - 'email' - ) - ) - .then(function (response) { - self.products = _.reject(self.products, function (obj) { - return obj.id == data.id - }) - self.products.push(mapProducts(response.data)) - self.productDialog.show = false - self.productDialog.data = {} - }) - .catch(function (error) { - LNbits.utils.notifyApiError(error) - }) - }, - createProduct: function (data) { var self = this let wallet = _.findWhere(this.stalls, { id: self.productDialog.data.stall }).wallet - console.log(wallet) LNbits.api .request( - 'POST', - '/diagonalley/api/v1/products', + 'PUT', + '/diagonalley/api/v1/products/' + data.id, _.findWhere(self.g.user.wallets, { id: wallet }).inkey, data ) .then(function (response) { + self.products = _.reject(self.products, function (obj) { + return obj.id == data.id + }) self.products.push(mapProducts(response.data)) - self.productDialog.show = false - self.productDialog.data = {} + self.resetDialog('productDialog') + //self.productDialog.show = false + //self.productDialog.data = {} }) .catch(function (error) { LNbits.utils.notifyApiError(error) }) }, + createProduct: function (data) { + let self = this + const walletId = _.findWhere(this.stalls, {id: data.stall}).wallet + + console.log("DATA", walletId, data) + LNbits.api + .request( + 'POST', + '/diagonalley/api/v1/products', + _.findWhere(self.g.user.wallets, {id: walletId}).inkey, + data + ) + .then((response) => { + console.log(response) + self.products.push(mapProducts(response.data)) + self.resetDialog('productDialog') + }) + .catch((error) => { + LNbits.utils.notifyApiError(error) + }) + }, deleteProduct: function (productId) { - var self = this - var product = _.findWhere(this.products, {id: productId}) - + const product = _.findWhere(this.products, {id: productId}) + const walletId = _.findWhere(this.stalls, {id: product.stall}).wallet + LNbits.utils .confirmDialog('Are you sure you want to delete this products link?') - .onOk(function () { + .onOk(() => { LNbits.api .request( 'DELETE', '/diagonalley/api/v1/products/' + productId, - _.findWhere(self.g.user.wallets, {id: product.wallet}).inkey + _.findWhere(this.g.user.wallets, {id: walletId}).adminkey ) - .then(function (response) { - self.products = _.reject(self.products, function (obj) { + .then(() => { + this.products = _.reject(this.products, (obj) => { return obj.id == productId }) }) - .catch(function (error) { + .catch((error) => { LNbits.utils.notifyApiError(error) }) }) @@ -1368,7 +1378,7 @@ new Vue({ LNbits.api .request( 'GET', - '/diagonalley/api/v1/shops?all_wallets', + '/diagonalley/api/v1/shops?all_wallets=true', this.g.user.wallets[0].inkey ) .then(function (response) { diff --git a/lnbits/extensions/diagonalley/views_api.py b/lnbits/extensions/diagonalley/views_api.py index d5e5e3826..4630b3219 100644 --- a/lnbits/extensions/diagonalley/views_api.py +++ b/lnbits/extensions/diagonalley/views_api.py @@ -82,7 +82,10 @@ async def api_diagonalley_products( wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids stalls = [stall.id for stall in await get_diagonalley_stalls(wallet_ids)] - + + if not stalls: + return + return [product.dict() for product in await get_diagonalley_products(stalls)] @@ -93,18 +96,18 @@ async def api_diagonalley_product_create( ): if product_id: - product = await get_diagonalley_product(product_id) - + product = await get_diagonalley_product(product_id) if not product: return {"message": "Withdraw product does not exist."} - - if product.wallet != wallet.wallet.id: + + stall = await get_diagonalley_stall(stall_id = product.stall) + if stall.wallet != wallet.wallet.id: return {"message": "Not your withdraw product."} product = await update_diagonalley_product(product_id, **data.dict()) else: product = await create_diagonalley_product(data=data) - + print("PRODUCT", product) return product.dict() @@ -117,7 +120,8 @@ async def api_diagonalley_products_delete( if not product: return {"message": "Product does not exist."} - if product.wallet != wallet.wallet.id: + stall = await get_diagonalley_stall(product.stall) + if stall.wallet != wallet.wallet.id: return {"message": "Not your Diagon Alley."} await delete_diagonalley_product(product_id) @@ -144,7 +148,7 @@ async def api_diagonalley_zone_create( @diagonalley_ext.post("/api/v1/zones/{zone_id}") async def api_diagonalley_zone_update( data: createZones, - zone_id: str = Query(None), + zone_id: str, wallet: WalletTypeInfo = Depends(require_admin_key), ): zone = await get_diagonalley_zone(zone_id) @@ -191,13 +195,13 @@ async def api_diagonalley_stalls( @diagonalley_ext.put("/api/v1/stalls/{stall_id}") async def api_diagonalley_stall_create( data: createStalls, - stall_id: str = Query(None), + stall_id: str = None, wallet: WalletTypeInfo = Depends(require_invoice_key), ): if stall_id: stall = await get_diagonalley_stall(stall_id) - + print("ID", stall_id) if not stall: return {"message": "Withdraw stall does not exist."} @@ -213,7 +217,7 @@ async def api_diagonalley_stall_create( @diagonalley_ext.delete("/api/v1/stalls/{stall_id}") async def api_diagonalley_stall_delete( - stall_id: str = Query(None), wallet: WalletTypeInfo = Depends(require_admin_key) + stall_id: str, wallet: WalletTypeInfo = Depends(require_admin_key) ): stall = await get_diagonalley_stall(stall_id) @@ -255,7 +259,7 @@ async def api_diagonalley_order_create( @diagonalley_ext.delete("/api/v1/orders/{order_id}") async def api_diagonalley_order_delete( - order_id: str = Query(None), wallet: WalletTypeInfo = Depends(get_key_type) + order_id: str, wallet: WalletTypeInfo = Depends(get_key_type) ): order = await get_diagonalley_order(order_id) @@ -272,7 +276,7 @@ async def api_diagonalley_order_delete( @diagonalley_ext.get("/api/v1/orders/paid/{order_id}") async def api_diagonalley_order_paid( - order_id: str = Query(None), wallet: WalletTypeInfo = Depends(require_admin_key) + order_id, wallet: WalletTypeInfo = Depends(require_admin_key) ): await db.execute( "UPDATE diagonalley.orders SET paid = ? WHERE id = ?", @@ -286,7 +290,7 @@ async def api_diagonalley_order_paid( @diagonalley_ext.get("/api/v1/orders/shipped/{order_id}") async def api_diagonalley_order_shipped( - order_id: str = Query(None), wallet: WalletTypeInfo = Depends(get_key_type) + order_id, wallet: WalletTypeInfo = Depends(get_key_type) ): await db.execute( "UPDATE diagonalley.orders SET shipped = ? WHERE id = ?", @@ -307,7 +311,7 @@ async def api_diagonalley_order_shipped( @diagonalley_ext.get("/api/v1/stall/products/{stall_id}") async def api_diagonalley_stall_products( - stall_id: str = Query(None), wallet: WalletTypeInfo = Depends(get_key_type) + stall_id, wallet: WalletTypeInfo = Depends(get_key_type) ): rows = await db.fetchone(