Exemplo n.º 1
0
def add_item():
    """Handle the logic related to add an item to the catalog"""
    if 'username' not in login_session:
        return redirect('/login')
    else:
        if request.method == 'POST':
            # build an item object
            item = db_helper.build_item(request.form['title'],
                                        request.form['description'],
                                        request.form['category_id'])

            # check if the item exists (the item must by unique by category)
            item_view_db = db_helper.get_item_by_title(item.title,
                                                       item.category_id)

            if not item_view_db:
                db_helper.add_item(item)
                return redirect(url_for('catalog_page'))
            else:
                categories = db_helper.get_categories()
                return render_template(
                    'addItem.html',
                    categories=categories,
                    message='An item with the same name exists')
        else:
            categories = db_helper.get_categories()
            return render_template('addItem.html',
                                   categories=categories,
                                   message='')
Exemplo n.º 2
0
def edit_item(category_name, item_title):
    """Handle the logic related to edit an item to the catalog"""
    if 'username' not in login_session:
        return redirect('/login')
    else:
        if request.method == 'POST':
            item_id = request.form['item_id']
            
            # build an item object
            item = db_helper.build_item(request.form['title'], request.form['description'], 
                                        request.form['category_id']);                                        
            
            # check if the item exists (the item must by unique by category)
            item_view_db = db_helper.get_item_by_title(item.title, item.category_id)
            
            # update the record if the item doesn't exists ot if exists is the sane
            # item that we are updating
            if not item_view_db or (item_view_db and item_view_db.id == item_id):
                db_helper.update_item(item_id, item)
                return redirect(url_for('category_page', category_name = category_name))
            else:
                categories = db_helper.get_categories()
                return render_template('editItem.html', item_view=item_view_db, 
                                       categories=categories, message = 'An item with the same name exists')
        else:
            categories = db_helper.get_categories()
            item_view = db_helper.get_item_by_title_category_view(item_title, category_name)
            return render_template('editItem.html', item_view=item_view,
                                    categories=categories, message = '') 
Exemplo n.º 3
0
def catalog_page():
    """Render the catalog page."""
    categories = db_helper.get_categories()
    lastest_items_view = db_helper.get_lastest_items_view(10)
    return render_template('catalog.html',
                           items_view=lastest_items_view,
                           categories=categories,
                           category_name=None)
Exemplo n.º 4
0
def view_item(category_name, item_title):
    """Render a page with the item information.

    Args:
      category_name: the category's name.
      item_title: the category's name.
    """
    categories = db_helper.get_categories()
    item_view = db_helper.get_item_by_title_category_view(item_title, category_name)
    return render_template('viewItem.html', item_view=item_view, categories = categories) 
Exemplo n.º 5
0
def category_page(category_name):
    """Render a page with items associated to a category.

    Args:
      category_name: the category's name.
    """
    categories = db_helper.get_categories()
    category   = db_helper.get_category_by_name(category_name)
    items_view = db_helper.get_items_view(category.id)
    return render_template('category.html', items_view=items_view,
                           categories=categories, category_name = category_name) 
Exemplo n.º 6
0
def serialize_categories_items():
    categories = get_categories()
    serializedCategories = []
    for category in categories:
        new_cat = category.serialize
        items = get_all_items_by_category_id(category.id)
        serializedItems = []
        for j in items:
            serializedItems.append(j.serialize)
        new_cat['Item'] = serializedItems
        serializedCategories.append(new_cat)
    return serializedCategories
Exemplo n.º 7
0
def edit_item(category_name, item_title):
    """Handle the logic related to edit an item to the catalog"""
    if 'username' not in login_session:
        return redirect('/login')
    else:
        if request.method == 'POST':
            item_id = request.form['item_id']

            # build an item object
            item = db_helper.build_item(request.form['title'],
                                        request.form['description'],
                                        request.form['category_id'])

            # check if the item exists (the item must by unique by category)
            item_view_db = db_helper.get_item_by_title(item.title,
                                                       item.category_id)

            # update the record if the item doesn't exists ot if exists is the sane
            # item that we are updating
            if not item_view_db or (item_view_db
                                    and item_view_db.id == item_id):
                db_helper.update_item(item_id, item)
                return redirect(
                    url_for('category_page', category_name=category_name))
            else:
                categories = db_helper.get_categories()
                return render_template(
                    'editItem.html',
                    item_view=item_view_db,
                    categories=categories,
                    message='An item with the same name exists')
        else:
            categories = db_helper.get_categories()
            item_view = db_helper.get_item_by_title_category_view(
                item_title, category_name)
            return render_template('editItem.html',
                                   item_view=item_view,
                                   categories=categories,
                                   message='')
Exemplo n.º 8
0
def view_item(category_name, item_title):
    """Render a page with the item information.

    Args:
      category_name: the category's name.
      item_title: the category's name.
    """
    categories = db_helper.get_categories()
    item_view = db_helper.get_item_by_title_category_view(
        item_title, category_name)
    return render_template('viewItem.html',
                           item_view=item_view,
                           categories=categories)
Exemplo n.º 9
0
def category_page(category_name):
    """Render a page with items associated to a category.

    Args:
      category_name: the category's name.
    """
    categories = db_helper.get_categories()
    category = db_helper.get_category_by_name(category_name)
    items_view = db_helper.get_items_view(category.id)
    return render_template('category.html',
                           items_view=items_view,
                           categories=categories,
                           category_name=category_name)
Exemplo n.º 10
0
def delete_item(category_name, item_title):
    """Handle the logic related to delete an item to the catalog"""
    if 'username' not in login_session:
        return redirect('/login')
    else:
        if request.method == 'POST':
            item_id = request.form['item_id']
            db_helper.delete_item(item_id)
            return redirect(url_for('category_page', category_name = category_name))
        else:
            categories = db_helper.get_categories()
            item_view = db_helper.get_item_by_title_category_view(item_title, category_name)
            return render_template('deleteItem.html', item_view=item_view, categories=categories)     
Exemplo n.º 11
0
def add_item():
    """Handle the logic related to add an item to the catalog"""
    if 'username' not in login_session:
        return redirect('/login')
    else:
        if request.method == 'POST':
            # build an item object
            item = db_helper.build_item(request.form['title'], request.form['description']
                                        , request.form['category_id']);
            
            # check if the item exists (the item must by unique by category)
            item_view_db = db_helper.get_item_by_title(item.title, item.category_id)
            
            if not item_view_db:
                db_helper.add_item(item)
                return redirect(url_for('catalog_page'))
            else:
                categories = db_helper.get_categories()
                return render_template('addItem.html', categories=categories,
                                        message = 'An item with the same name exists')  
        else:
            categories = db_helper.get_categories()
            return render_template('addItem.html', categories=categories, message = '') 
Exemplo n.º 12
0
def delete_item(category_name, item_title):
    """Handle the logic related to delete an item to the catalog"""
    if 'username' not in login_session:
        return redirect('/login')
    else:
        if request.method == 'POST':
            item_id = request.form['item_id']
            db_helper.delete_item(item_id)
            return redirect(
                url_for('category_page', category_name=category_name))
        else:
            categories = db_helper.get_categories()
            item_view = db_helper.get_item_by_title_category_view(
                item_title, category_name)
            return render_template('deleteItem.html',
                                   item_view=item_view,
                                   categories=categories)
Exemplo n.º 13
0
def xml_creator():
    categories = get_categories()
    feed = Element('xmlFeed')

    category = SubElement(feed, 'category')
    for categories_to_output in categories:

        SubElement(category, 'category', id=str(categories_to_output.id))
        SubElement(category, 'category', name=str(categories_to_output.name))

        itemList = get_all_items_by_category_id(categories_to_output.id)
        for items_to_output in itemList:
            items = SubElement(category, 'items')

            SubElement( items, 'item', description=str(items_to_output.description))
            SubElement( items, 'item', id=str(items_to_output.id))
            SubElement( items, 'item', title=str(items_to_output.title))

    return prettify(feed)
Exemplo n.º 14
0
def catalog_dict():
    """Return the catalog data as a dictionary"""
    categories = db_helper.get_categories()

    categories_json = []

    for category in categories:
        category_json = category.serialize
        items = db_helper.get_items(category.id)

        items_json = []

        for item in items:
            items_json.append(item.serialize)

        category_json['items'] = items_json
        categories_json.append(category_json)

    return categories_json
Exemplo n.º 15
0
def catalog_dict():
    """Return the catalog data as a dictionary"""
    categories = db_helper.get_categories()
    
    categories_json = []
    
    for category in categories:
        category_json = category.serialize
        items = db_helper.get_items(category.id)
        
        items_json = []
        
        for item in items:
            items_json.append(item.serialize)
            
        category_json['items'] = items_json
        categories_json.append(category_json)
        
    return categories_json
Exemplo n.º 16
0
def categories_dict():
    """Return the catalog's categories data as a dictionary"""
    items = db_helper.get_categories()
    return [item.serialize for item in items]
Exemplo n.º 17
0
def catalog_page():
    """Render the catalog page."""
    categories = db_helper.get_categories()
    lastest_items_view = db_helper.get_lastest_items_view(10)
    return render_template('catalog.html', items_view=lastest_items_view, 
                           categories=categories, category_name = None) 
Exemplo n.º 18
0
def categories_dict():
    """Return the catalog's categories data as a dictionary"""
    items = db_helper.get_categories()
    return [item.serialize for item in items]