def update(self, form_data): """Updates a category and returns a json object""" try: user = UserModel(form_data['user_token']) form_data['created_by'] = user.email category = CategoryModel(form_data).update(self.db_session) except exc.IntegrityError as e: self.db_session.rollback() category = CategoryModel({'errors': 'Error Updating Category'}) finally: self.db_session.close() return jsonify(category.attributes())
def create(self, form_data): """Creates a new category and returns a json object""" try: """form includes user_token, which is used to get user email""" user = UserModel(form_data['user_token']) form_data['created_by'] = user.email category = CategoryModel(form_data).create(self.db_session) except exc.IntegrityError as e: self.db_session.rollback() category = CategoryModel({'errors': 'failed to update database'}) finally: self.db_session.close() return jsonify(category.attributes())
def delete(self, name, form_data): """Deletes a category and returns a json object""" try: """form includes user_token, which is used to get user email""" user = UserModel(form_data['user_token']) category = CategoryModel( {'name': name, 'created_by': user.email} ).delete(self.db_session) except exc.IntegrityError as e: self.db_session.rollback() category = CategoryModel({'errors': 'Error Deleting Category'}) finally: self.db_session.close() return jsonify(category.attributes())
def get(self): product_groups = ProductGroupModel.query(ProductGroupModel.visible == True).fetch() categories = CategoryModel.query().fetch() template_values = {'product_groups': product_groups , "session": self.session, "categories" : categories} template = JINJA_ENVIRONMENT.get_template('%s.html'% self.request.path[1:]) self.response.write(template.render(template_values))
def show(self, name, headers): """Queries single category. Returns html or json view""" category = CategoryModel.find(self.db_session, name) self.db_session.close() if headers == 'application/json': return jsonify(category.attributes()) else: return render_template('categories/show.html', category=category)
def get(self, keyString): key= ndb.Key(urlsafe=keyString) product = key.get() print("---------------Blob key in get edit") print(product.imgBlobKey) print("-----------Blob key in get edit END") template_values = {'product': product, 'session':self.session, 'categories': CategoryModel.query().fetch()} template = JINJA_ENVIRONMENT.get_template('product/edit.html') self.response.write(template.render(template_values))
def index(self): """Queries categories and items, then renders index view""" categories = CategoryModel.all(self.db_session) items = ItemModel.latest(self.db_session) self.db_session.close() return render_template( 'categories/index.html', categories=categories, items=items )
def show(self, title, headers): """Queries single item. Returns html or json view""" item = ItemModel.find(self.db_session, title) self.db_session.close() if headers == 'application/json': return jsonify(item.attributes()) else: categories = CategoryModel.all(self.db_session) return render_template('items/show.html', item=item, categories=categories)
def index(self, category_name, headers): """Queries category and related items, then renders index view""" category = CategoryModel.find(self.db_session, category_name) items = ItemModel.category_group(self.db_session, category.id) self.db_session.close() if headers == 'application/json': json_items = [] for item in items: json_items.append(item.attributes()) return jsonify({ 'category': category.attributes(), 'items': json_items }) else: return render_template('items/index.html', category=category, items=items)
def get(self): template_values = {"session": self.session, "categories": CategoryModel.query().fetch()} template = JINJA_ENVIRONMENT.get_template('product_group/new.html') self.response.write(template.render(template_values))
def post(self): validation = validateForm(self.request.POST, FIELD_COND) if(not validation[0]): template_values = {"errors" : validation[1], "session": self.session, "categories": CategoryModel.query().fetch()} template = JINJA_ENVIRONMENT.get_template('/product_group/new.html') self.response.write(template.render(template_values)) else: product_group = ProductGroupModel(name=self.request.get('name'), price=float(self.request.get('price'))) product_group.visible = (self.request.get('visible') and self.request.get('visible')=="True") product_group.items = [] prod_params = getProductsFromParams(self.request.POST) #print("product params : " + str(prod_params)) for prod_key in prod_params: try: qty = float(prod_params[prod_key]) except ValueError: qty = 0 if qty > 0: prod_group_item = ProductGroupItemModel(product=ndb.Key(urlsafe=prod_key), qty=qty) product_group.items.append(prod_group_item) product_group.put() self.redirect('/product_group/view/%s' % product_group.key.urlsafe())
def get(self): return CategoryModel.getCat()
def get(self, category_id=0, page=0): return CategoryModel.getAll(category_id, page)
def get(self, keyString): key= ndb.Key(urlsafe=keyString) product_group = key.get() template_values = {'product_group': product_group, 'session':self.session, "categories": CategoryModel.query().fetch()} template = JINJA_ENVIRONMENT.get_template('product_group/edit.html') self.response.write(template.render(template_values))
def post(self): key= ndb.Key(urlsafe=self.request.get("key")) product = key.get() validation = validateForm(self.request.POST, FIELD_COND) if(not validation[0]): template_values = {"product": product, "errors" : validation[1], "session": self.session, 'categories': CategoryModel.query().fetch()} template = JINJA_ENVIRONMENT.get_template('/product/edit.html') self.response.write(template.render(template_values)) else: product.name = self.request.get('name') product.description = self.request.get('description') product.units = self.request.get('units') product.category = ndb.Key(urlsafe=self.request.get('category')) product.price = float(self.request.get('price')) product.visible = (self.request.get('visible') and self.request.get('visible')=="True") product.put() self.redirect('/')