def product_variants(): """ List Product variants for a product """ from openstudio.os_shop_product import ShopProduct from openstudio.os_shop_products_variants import ShopProductsVariants spID = request.vars['spID'] product = ShopProduct(spID) response.title = T('Shop') response.subtitle = T('Catalog') response.view = 'general/tabs_menu.html' variants = ShopProductsVariants(spID) content = DIV( H4(T('Variants for'), ' ', product.row.Name), variants.list_formatted() ) add = '' if not product.has_products_set(): add = os_gui.get_button('add', URL('shop_manage', 'product_variant_add', vars={'spID':spID})) back = os_gui.get_button('back', shop_products_get_return_url()) menu = catalog_get_menu('products') return dict(content=content, add=add, back=back, menu=menu)
def product_variant_edit(): """ Edit a product variant """ from openstudio.os_forms import OsForms from openstudio.os_shop_product import ShopProduct spID = request.vars['spID'] spvID = request.vars['spvID'] product = ShopProduct(spID) response.title = T('Shop') response.subtitle = T( 'Edit product - {product_name}'.format(product_name=product.row.Name)) response.view = 'general/tabs_menu.html' return_url = product_variants_get_return_url(spID) if product.has_products_set(): db.shop_products_variants.Name.writable = False os_forms = OsForms() result = os_forms.get_crud_form_update( db.shop_products_variants, return_url, spvID, ) form = result['form'] back = os_gui.get_button('back', return_url) menu = product_edit_get_menu('product_variants', spID) return dict(content=form, save=result['submit'], back=back, menu=menu)
def product_variant_add_check_products_set(spID): """ Check if a product has a set, if so, don't allow adding variants. :param spID: db.shop_products.id :return: None """ from openstudio.os_shop_product import ShopProduct product = ShopProduct(spID) if product.has_products_set(): session.flash = T("Unable to add variants for a product with a set") redirect(product_variants_get_return_url(spID))
def list_formatted(self): """ :return: HTML table with shop products variants """ from openstudio.os_shop_product import ShopProduct T = current.T os_gui = current.globalenv['os_gui'] auth = current.auth product = ShopProduct(self.shop_products_id) header = THEAD( TR(TH(), TH(T('Name')), TH(T('Price')), TH(T('Article Code')), TH(T('Keep stock')), TH(T('Stock shop')), TH(T('Stock warehouse')), TD(), TH())) table = TABLE(header, _class='table table-striped table-hover') table_disabled = TABLE(header, _class='table table-striped table-hover') permission_edit = (auth.has_membership(group_id='Admins') or auth.has_permission('update', 'shop_products_variants')) permission_delete = (auth.has_membership(group_id='Admins') or auth.has_permission('delete', 'shop_products_variants')) onclick_delete = self._list_formatted_get_onclick_delete() rows = self.list() for i, row in enumerate(rows): repr_row = list(rows[i:i + 1].render())[0] default = self._list_formatted_get_label_default(T, os_gui, row) buttons = self._list_formatted_get_buttons(permission_edit, permission_delete, onclick_delete, T, os_gui, row) tr = TR(TD(repr_row.thumbsmall), TD(os_gui.max_string_length(row.Name, 50)), TD(repr_row.Price), TD(repr_row.ArticleCode), TD(repr_row.KeepStock), TD(row.StockShop), TD(row.StockWarehouse), TD(default), TD(buttons)) if row.Enabled: table.append(tr) else: table_disabled.append(tr) if product.has_products_set(): return DIV(table, H4(T('Disabled')), table_disabled) else: return table
def _list_formatted_get_onclick_delete(self): """ :return: onclick delete for """ from openstudio.os_shop_product import ShopProduct T = current.T product = ShopProduct(self.shop_products_id) if product.has_products_set(): delete_message = T('Do you really want to disable this variant?') else: delete_message = T('Do you really want to delete this variant?') onclick_delete = "return confirm('" \ + delete_message + "');" return onclick_delete
def product_variant_sales(): """ Edit a product variant """ from openstudio.os_shop_product import ShopProduct from openstudio.os_shop_products_variant import ShopProductsVariant from openstudio.os_shop_sales import ShopSales spID = request.vars['spID'] spvID = request.vars['spvID'] product = ShopProduct(spID) variant = ShopProductsVariant(spvID) response.title = T('Shop') response.subtitle = T( 'Edit product - {product_name}'.format(product_name=product.row.Name)) response.view = 'general/tabs_menu.html' return_url = product_variants_get_return_url(spID) sales = ShopSales(spvID) content = DIV(H4(T("Sales history for variant %s" % variant.row.Name)), BR(), sales.list_formatted()) # add = os_gui.get_button('add', URL('shop_manage', 'product_add')) back = os_gui.get_button('back', return_url) menu = product_edit_get_menu('product_variants', spID) return dict(content=content, back=back, menu=menu)
def product_variant_add(): """ Add a product variant """ from openstudio.os_shop_product import ShopProduct from openstudio.os_forms import OsForms spID = request.vars['spID'] product_variant_add_check_products_set(spID) product = ShopProduct(spID) response.title = T('Shop') response.subtitle = T( 'Edit product - {product_name}'.format(product_name=product.row.Name)) response.view = 'general/tabs_menu.html' return_url = product_variants_get_return_url(spID) db.shop_products_variants.shop_products_id.default = spID os_forms = OsForms() result = os_forms.get_crud_form_create( db.shop_products_variants, return_url, ) form = result['form'] content = DIV(H4(T("Add variant")), BR(), form) back = os_gui.get_button('back', return_url) menu = product_edit_get_menu('product_variants', spID) return dict(content=content, save=result['submit'], back=back, menu=menu)
def product_onaccept(form): """ Function run when adding or editing a product If there is a product set, add all possible variants If not, add a default variant """ from openstudio.os_shop_product import ShopProduct from openstudio.os_shop_products_set import ShopProductsSet spID = form.vars.id spsID = form.vars.shop_products_sets_id if not spsID: # Check if we have > 1 variant product = ShopProduct(spID) if not product.count_variants(): product.add_default_variant() else: products_set = ShopProductsSet(spsID) products_set.insert_variants_for_product(spID)
def product_variant_delete(): """ Delete variant when not linked to a set Disable variant when linked to a set """ from openstudio.os_shop_product import ShopProduct from openstudio.os_shop_products_variant import ShopProductsVariant spID = request.vars['spID'] spvID = request.vars['spvID'] product = ShopProduct(spID) if product.has_products_set(): variant = ShopProductsVariant(spvID) variant.disable() session.flash = T('Disabled variant') else: query = (db.shop_products_variants.id == spvID) db(query).delete() session.flash = T('Deleted variant') redirect(product_variants_get_return_url(spID))
def product_categories(): """ :return: """ from general_helpers import set_form_id_and_get_submit_button from openstudio.os_shop_product import ShopProduct spID = request.vars['spID'] product = ShopProduct(spID) response.title = T('Shop') response.subtitle = T( 'Edit product - {product_name}'.format(product_name=product.row.Name)) response.view = 'general/tabs_menu.html' header = THEAD(TR(TH(), TH(T("Category")))) table = TABLE(header, _class='table table-hover') query = (db.shop_categories_products.shop_products_id == spID) # rows = db(query).select(db.teachers_classtypes.school_classtypes_id) rows = db(query).select(db.shop_categories_products.shop_categories_id) selected_ids = [] for row in rows: selected_ids.append(unicode(row.shop_categories_id)) query = (db.shop_categories.Archived == False) rows = db(query).select(db.shop_categories.id, db.shop_categories.Name, orderby=db.shop_categories.Name) for row in rows: if unicode(row.id) in selected_ids: # check the row table.append( TR( TD(INPUT(_type='checkbox', _name=row.id, _value="on", value="on"), _class='td_status_marker'), TD(row.Name))) else: table.append( TR( TD(INPUT(_type='checkbox', _name=row.id, _value="on"), _class='td_status_marker'), TD(row.Name))) form = FORM(table, _id='MainForm') return_url = URL(vars={'spID': spID}) # After submitting, check which categories are 'on' if form.accepts(request, session): # Remove all current records query = (db.shop_categories_products.shop_products_id == spID) db(query).delete() # insert new records for product for row in rows: if request.vars[unicode(row.id)] == 'on': db.shop_categories_products.insert(shop_categories_id=row.id, shop_products_id=spID) # Clear teachers (API) cache cache_clear_school_teachers() session.flash = T('Saved') redirect(return_url) back = os_gui.get_button('back', shop_products_get_return_url()) menu = product_edit_get_menu(request.function, spID) return dict(content=form, save=os_gui.get_submit_button('MainForm'), back=back, menu=menu)