def validate_python(self, values, state): voucher = Voucher.find_by_code(values['voucher']['code']) error_dict = {} if voucher is not None: message = "Duplicate Voucher Code" error_dict = {'voucher.code': "Code already exists!"} raise Invalid(message, values, state, error_dict=error_dict)
def index(self): c.admin = h.auth.authorized(h.auth.has_organiser_role) if c.admin: c.vouchers = Voucher.find_all() else: c.vouchers = h.signed_in_person().vouchers return render('/voucher/list.mako')
def delete(self, id): """Delete the voucher GET will return a form asking for approval. POST requests will delete the item. """ c.voucher = Voucher.find_by_id(id) return render('/voucher/confirm_delete.mako')
def _delete(self, id): c.voucher = Voucher.find_by_id(id) if not c.voucher.registration: meta.Session.delete(c.voucher) meta.Session.commit() h.flash("Voucher has been deleted.") else: h.flash("Cannot delete a voucher which has already been used.", 'error') redirect_to('index')
def _new(self): results = self.form_result['voucher'] count = results['count'] # Number of voucher codes to generate del(results['count']) for i in xrange(count): if 'products' in results: del(results['products']) c.voucher = Voucher(**results) if c.voucher.code !='': c.voucher.code += '-' #add a dash between prefix and random c.voucher.code += generate_code() meta.Session.add(c.voucher) # save voucher to DB results['products'] = self.form_result['products'] for category in c.product_categories: if category.name in allowed_categories: # depending on "display" type of product, handle the input appropriately if category.display == 'radio': if 'category_' + str(category.id) in results['products']: product = Product.find_by_id(results['products']['category_' + str(category.id)]) vproduct = VoucherProduct() vproduct.voucher = c.voucher vproduct.product = product vproduct.qty = 1 vproduct.percentage = results['products']['category_' + str(category.id) + '_percentage'] meta.Session.add(vproduct) # Save product to DB c.voucher.products.append(vproduct) # Assign individual product discount to voucher elif category.display == 'checkbox': for product in category.products: if 'product_' + str(product.id) in results['products']: vproduct = VoucherProduct() vproduct.voucher = c.voucher vproduct.product = product vproduct.qty = 1 vproduct.percentage = results['products']['product_' + str(product.id) + '_percentage'] meta.Session.add(vproduct) c.voucher.products.append(vproduct) else: for product in category.products: if 'product_' + str(product.id) + '_qty' in results['products']: if results['products']['product_' + str(product.id) + '_qty'] not in (0, None): vproduct = VoucherProduct() vproduct.voucher = c.voucher vproduct.product = product vproduct.qty = results['products']['product_' + str(product.id) + '_qty'] vproduct.percentage = results['products']['product_' + str(product.id) + '_percentage'] meta.Session.add(vproduct) c.voucher.products.append(vproduct) meta.Session.commit() #save all updates h.flash("Voucher created") return redirect_to(controller='voucher', action='index')