def delete_category_handler(catalog_id, category_id): """Deletes a category in a catalog Parameters: - catalog_id: integer id of catalog to which category belongs - category_id: integer id of category to delete Returns: - HTTP response redirecting to catalog page """ catalog_id = int(catalog_id) catalog_entity = models.get_catalog_by_id(catalog_id) category_id = int(category_id) category_entity = models.get_category_by_id(catalog_id, category_id) # Check parameters if not catalog_entity: raise BadRequest('Could not find catalog with id %d' % catalog_id) if not (category_entity): raise BadRequest('Could not find category with id %d!' % category_id) if not catalog_entity.user_can_edit(models.get_current_user()): raise Unauthorized models.delete_category(catalog_id, category_id) models.wait_for(category_entity) return flask.redirect('/catalog/%d' % catalog_entity.key.id())
def add_category_handler(catalog_id): """Adds a category to a catalog Parameters: - catalog_id: integer id of catalog to which category belongs Returns: - HTTP response """ user = models.get_current_user() catalog_id = int(catalog_id) catalog_entity = models.get_catalog_by_id(catalog_id) # Check parameters if not catalog_entity: raise BadRequest('Could not find catalog with id %d' % catalog_id) if not catalog_entity.user_can_edit(user): raise Unauthorized if flask.request.method == 'GET': # Render add category form return flask.render_template('addcategory.html', user=user, catalog=catalog_entity, category=None) elif flask.request.method == 'POST': # Handle new category submission from form new_category = models.Category( name=flask.request.form.get('name'), description=flask.request.form.get('description'), catalog=catalog_entity.key) new_category.put() models.wait_for(new_category) return flask.redirect('/catalog/%d' % catalog_entity.key.id())
def catalog_view_handler(catalog_id): """Renders a catalog page Parameters: - catalog_id: integer id of catalog to be rendered Returns: - HTTP response """ user = models.get_current_user() catalog_id = int(catalog_id) catalog = models.get_catalog_by_id(catalog_id) if not catalog: raise BadRequest('Could not find catalog with id %d!' % catalog_id) categories = models.get_categories(catalog_id) return flask.render_template('catalog.html', user=user, catalog=catalog, categories=categories)
def delete_catalog_handler(catalog_id): """Deletes a catalog Parameters: - catalog_id: integer id of catalog to delete Returns: - HTTP response redirecting to index """ catalog_id = int(catalog_id) catalog_entity = models.get_catalog_by_id(catalog_id) # Check parameteres if not catalog_entity: raise BadRequest('Could not find catalog with id %d!' % catalog_id) if not catalog_entity.user_can_edit(models.get_current_user()): raise Unauthorized models.delete_catalog(catalog_id) return flask.redirect('/')
def edit_catalog_handler(catalog_id): """Edits a catalog Parameters: - catalog_id: integer id of catalog to edit Returns: - HTTP response """ catalog_id = int(catalog_id) catalog_entity = models.get_catalog_by_id(catalog_id) user = models.get_current_user() # Check parameters if not catalog_entity: raise BadRequest('Could not find catalog with id %d!' % catalog_id) if not catalog_entity.user_can_edit(user): raise Unauthorized if flask.request.method == 'GET': # Render edit catalog form return flask.render_template('editcatalog.html', user=user, catalog=catalog_entity) elif flask.request.method == 'POST': # Handle catalog edit from form try: cover_picture_obj = flask.request.files.get('cover_picture') cover_picture_url = uploadfile.save_file(cover_picture_obj) # TODO: delete old picture catalog_entity.cover_picture = uploadfile.save_file( cover_picture_url) catalog_entity.name = flask.request.form.get('name') catalog_entity.description = flask.request.form.get('description') catalog_entity.put() models.wait_for(catalog_entity) return flask.redirect('/catalog/%d' % catalog_entity.key.id()) except IOError: return ('Failed to edit catalog', 401)
def edit_category_handler(catalog_id, category_id): """Edits a category in a catalog Parameters: - catalog_id: integer id of catalog to which category belongs - category_id: integer id of category to edit Returns: - HTTP response """ user = models.get_current_user() catalog_id = int(catalog_id) catalog_entity = models.get_catalog_by_id(catalog_id) category_id = int(category_id) category_entity = models.get_category_by_id(catalog_id, category_id) # Check parameters if not catalog_entity: raise BadRequest('Could not find catalog with id %d' % catalog_id) if not (category_entity): raise BadRequest('Could not find category with id %d!' % category_id) if not catalog_entity.user_can_edit(user): raise Unauthorized if flask.request.method == 'GET': # Render edit category form return flask.render_template('editcategory.html', user=user, catalog=catalog_entity, category=category_entity) elif flask.request.method == 'POST': # Handle category edit from form category_entity.name = flask.request.form.get('name') category_entity.description = flask.request.form.get('description') category_entity.put() models.wait_for(category_entity) return flask.redirect('/catalog/%d' % catalog_entity.key.id())
def delete_item_handler(catalog_id, item_id): """Deletes an item in a catalog Parameters: - catalog_id: integer id of catalog to which item belongs - item_id: integer id of item to delete Returns: - HTTP response redirecting to catalog page """ catalog_id = int(catalog_id) catalog_entity = models.get_catalog_by_id(catalog_id) item_id = int(item_id) item_entity = models.get_item_by_id(catalog_id, item_id) # Check parameters if not catalog_entity: raise BadRequest('Could not find catalog with id %d' % catalog_id) if not (item_entity): raise BadRequest('Could not find item with id %d!' % item_id) if not catalog_entity.user_can_edit(models.get_current_user()): raise Unauthorized models.delete_item(catalog_id, item_id) return flask.redirect('/catalog/%d' % catalog_entity.key.id())
def add_item_handler(catalog_id): """Adds an item to a catalog Parameters: - catalog_id: integer id of catalog to which item belongs Returns: - HTTP response """ user = models.get_current_user() catalog_id = int(catalog_id) catalog_entity = models.get_catalog_by_id(catalog_id) # Check parameters if not catalog_entity: raise BadRequest('Could not find catalog with id %d' % catalog_id) if not catalog_entity.user_can_edit(user): raise Unauthorized if flask.request.method == 'GET': # Render add item form # Category_id may be passed in if adding item from category edit page category_id = flask.request.args.get('category_id') if category_id: category_id = int(category_id) categories = models.get_categories(catalog_id) return flask.render_template('additem.html', user=user, catalog=catalog_entity, category_id=category_id, categories=categories, item=None) elif flask.request.method == 'POST': # Handle new item submission from form picture_obj = flask.request.files.get('picture') picture_url = uploadfile.save_file(picture_obj) try: price = float(flask.request.form.get('price')) except: price = 0.00 new_item = models.Item( name=flask.request.form.get('name'), description=flask.request.form.get('description'), price=price, picture=picture_url, owner=user.key, catalog=catalog_entity.key) # Allow possibility of putting items without category category_id = flask.request.form.get('category_id') if category_id: category_id = int(category_id) category_entity = models.get_category_by_id(catalog_id, category_id) if category_entity: new_item.category = category_entity.key new_item.put() models.wait_for(new_item) return flask.redirect('/catalog/%d' % catalog_entity.key.id())