Exemplo n.º 1
0
def clear_search():
    search = Search.query.get(1)
    if search is None:
        search = Search(search_key = '')
        db.session.add(search)
        db.session.commit()
    else:
        search.search_key = ''
        db.session.commit()
    return redirect('/index')
Exemplo n.º 2
0
def index():

    sortForm=SortForm()
    if sortForm.validate_on_submit():
        newSort = int(sortForm.sort_by.data)
        if (newSort == 4): 
            items=Item.query.order_by(Item.posted_on.desc())
        elif (newSort == 3): 
            items=Item.query.order_by(Item.rating.desc())
        elif (newSort == 2):
            items=Item.query.order_by(Item.price.asc())
        elif (newSort == 1): 
            items=Item.query.order_by(Item.price.desc())
        else: 
            items = Item.query.order_by(Item.item_name.desc())
        item_list = items.all()
    else:    
        items = Item.query.order_by(Item.item_name.desc())
        item_list = items.all()

    searchForm = SearchForm()
    if searchForm.validate_on_submit():
        search = Search.query.get(1)
        if search is None:
            search = Search(search_key = searchForm.search_key.data)
            db.session.add(search)
            db.session.commit()
        else:
            search.search_key = searchForm.search_key.data
            db.session.commit()

    search = Search.query.get(1)
    if search is None:
        search = Search(search_key = '')
        db.session.add(search)
        db.session.commit()

    search_key = search.search_key
    searchForm.search_key.data = search_key

    if search_key != '':
        for item in items:
            item_name = re.split(', |_| |-|!|\.|\n', item.item_name)
            if not search_key in item_name:
                item_description = re.split(', |_| |-|!|\.|\n', item.item_description)
                if not search_key in item_description:
                    item_list.remove(item)

    return render_template('index.html', searchForm=searchForm, sortForm=sortForm, title="Covid Commerce", items=item_list, current_user = current_user)