Ejemplo n.º 1
0
def home(cid = None, item_id = None):

  #Check input: Category ID
  if cid == None:
    category_id = Category.get_root().id
  else:
    category_id = cid
  
  #Get one that is specified
  categories = [] + [Category.get_by_id(category_id)]
  
  #Get children
  if len(categories) == 1:
    children = Category.get_children(category_id, count_items = True)
    if len(children) == 0: #If no children, then get parent of it
      parent_id = categories[0].parent
      categories = [] + [Category.get_by_id(parent_id)]
      #get children of the parent
      children = Category.get_children(parent_id, count_items = True)
    
    if len(children) > 0:
      categories += children
  
    categories[0].parent_flag = True
  
  #Assign selected_flag
  for c in categories:
    if c.id == int(category_id):
      c.selected_flag = True
    else:
      c.selected_flag = False
  
  #Get item or items
  item = None
  items = []
  if item_id == None:
    items = Item.get_by_category(category_id)
  else:
    item = Item.get_by_id(item_id)
    if isinstance(item, Item) and item.category_id != category_id:
      item = None
  
  return render_template('home.html', title = 'Catalog Excercise',\
                         categories = categories,\
                         items = items,\
                         item = item
                        )
  
Ejemplo n.º 2
0
def item_json(cid = None, item_id = None):
  if item_id == None and cid == None:
    abort(404)
  if item_id != None:
    item = Item.get_by_id(item_id)
    if item == None:
      return abort(404)
    else:
      json_response = json.jsonify(Item=item.serialize)
  elif cid != None:
    items = Item.get_by_category(cid)
    if items == []:
      abort(404)
    else:
      json_response = json.jsonify(Items = [i.serialize for i in items])
  
  return json_response
Ejemplo n.º 3
0
def item_edit(item_id = None):
  item = Item.get_by_id(item_id)
  
  if request.method == 'POST':
    item.name = request.form['item-name']
    item.longtext = request.form["item-longtext"]
    file = request.files["item-image"]
    print type(file)
    item.image = file.read() #stream.getvalue()

    #file.save(os.path.join(current_app.root_path, file.filename))
    
    item.update()
    item.commit()
    return redirect(url_for('item.item_display', item_id = item_id))
  
  return render_template('item.html', title = 'Edit item',\
                         item = item,\
                         display = False
                        )
Ejemplo n.º 4
0
def item_display(item_id = None):
  return render_template('item.html', title = 'Display item',\
                         item = Item.get_by_id(item_id),\
                         display = True
                        )