Exemplo n.º 1
0
def user_dashboard():
    sessionUser = "" if 'sessionUser' not in session else session['sessionUser']

    categories = []

    if sessionUser == "":
        abort(404)
    cursor = getCursor()[1]
    # To fetch items for a user from db
    cursor.execute(query().PRODUCTS_FOR_USER(sessionUser['u_id']))
    data = cursor.fetchall()
    # To fetch categories from db
    cursor.execute(query().fetchAllCategories())
    allCategories = cursor.fetchall()
    categories = [allCategories[i][0] for i in range(len(allCategories))]

    productList = []

    for d in data:
        if len(d) == 16:
            productObject = product.makeProduct(d)
            productList.append(productObject)

    currentSearch = ""
    categoryName = "All"
    session['categories'] = categories

    return render_template("user-dashboard.html",
                           sessionUser=sessionUser,
                           productList=productList,
                           currentSearch=currentSearch,
                           categoryName=categoryName,
                           categories=categories)
Exemplo n.º 2
0
def seller_inbox(item_id):
    sessionUser = "" if 'sessionUser' not in session else session['sessionUser']
    if sessionUser == "":
        abort(404)
    cursor = getCursor()[1]
    cursor.execute(query().GET_ITEM_MESSAGES(item_id))
    data = cursor.fetchall()

    print(data)

    messageList = []

    for d in data:
        if len(d) > 3:
            messageObject = message.makeMessage(d)
            messageList.append(messageObject)

    cursor.execute(query().APPROVED_ITEM(item_id))
    data = cursor.fetchone()
    cursor.close()

    messageProduct = product.makeProduct(data)

    return render_template('seller-inbox.html',
                           sessionUser=sessionUser,
                           messages=messageList,
                           messageProduct=messageProduct)
Exemplo n.º 3
0
def productPage(product_id):

    cursor = getCursor()[1]
    product_id = str(bleach.clean(product_id))  # sanitizing a bad redirect

    cursor.execute(query().APPROVED_ITEM(product_id))
    data = cursor.fetchall()
    if len(data) == 0:
        abort(404)

    cursor.execute(query().USER_FOR_PRODUCT(product_id))
    userObject = cursor.fetchone()
    cursor.close()

    productObject = product.makeProduct(data[0])
    try:
        if productObject.getStatus(
        ) == 0 and not session['sessionUser']['u_is_admin'] > 0:
            abort(404)
    except KeyError:
        abort(404)
    print("Redirecting to Product page", product_id)
    return render_template("products/product.html",
                           product=productObject,
                           user=userObject)
Exemplo n.º 4
0
def selectCategory(catName):
    sessionUser = "" if 'sessionUser' not in session else session['sessionUser']

    cursor = getCursor()[1]
    print(catName)
    feedback = []

    if 'previousQuery' in session:  # Some sort of filtering before
        data = session['previousQuery']
        print("data from  prev query in Category ", data)
    else:
        cursor.execute(query().APPROVED_ITEMS_FOR_CATEGORY(catName))
        data = [product.makeProduct(d).toDict() for d in cursor.fetchall()]
    cursor.close()

    data = [d for d in data if d['c_name'] == catName]

    if len(data) == 0:
        feedback.append("No Results Found, Consider these")
        data = session['previousQuery']
    else:
        feedback.append(catName)

    session['currentCategory'] = catName
    session['categoryName'] = catName
    productList = []

    if 'sortOption' in session:
        data = filter_data(data, session['sortOption'])

    currentSearch = "" if 'currentSearch' not in session else session['currentSearch']
    categoryName = "All" if 'categoryName' not in session else session['categoryName']
    return render_template("home.html", products=data, feedback=feedback, sessionUser=sessionUser, sortOption="Sort By", currentSearch=currentSearch,categoryName=categoryName)
Exemplo n.º 5
0
def home():
    n = 5  # number of most recent items to grab
    productList = []
    categories = []
    cursor = getCursor()[1]
    cursor.execute(query().MOST_RECENT_ITEMS(n))
    data = cursor.fetchall()
    cursor.execute(query().fetchAllCategories())
    allCategories = cursor.fetchall()
    categories = [allCategories[i][0] for i in range(len(allCategories))]
    print("categories fetched are: ", categories, " and type is: ")

    feedback = []
    productUsers = []
    for d in data:
        if len(d) == 16:
            productObject = product.makeProduct(d)
            productList.append(productObject)
            cursor.execute(query().FULL_USER_FOR_PRODUCT(str(productObject.i_id)))
            productUser = user.makeUser(cursor.fetchone())
            productUsers.append(productUser.toDict())

    cursor.close()
    feedback.append(
        "" if 'otherFeedback' not in session else session['otherFeedback'])
    sessionUser = "" if 'sessionUser' not in session else session['sessionUser']
    if 'sessionUser' in session:
        feedback.append("Welcome Back " +
                        session['sessionUser']['u_fname'] + " " +
                        session['sessionUser']['u_lname'])

    feedback.append("Here are the latest Items")

    try:
        session.pop('otherFeedback')
    except KeyError:
        pass
#  Reseting filter options
    if 'currentCategory' in session:
        session.pop('currentCategory')
    if 'sortOption' in session:
        session.pop('sortOption')
    if 'previousQuery' in session:
        session.pop('previousQuery')
# Storing previous query for filtering
    session['previousQuery'] = [product.toDict() for product in productList]
    currentSearch = ""
    categoryName = "All"
    session['categories'] = categories

    return render_template("home.html", products=session['previousQuery'], feedback=feedback, sessionUser=sessionUser,
                     sortOption="Sort By", currentSearch=currentSearch,categoryName=categoryName,categories=categories, productUsers=productUsers)
Exemplo n.º 6
0
def contact_seller(item_id):
    sessionUser = "" if 'sessionUser' not in session else session['sessionUser']
    # if sessionUser == "":
    #     abort(404)  # TODO lazy registration
    if 'lazyRegistration' in session:
        makeAndInsertMessageForSeller(session['buyerContact'],
                                      session['buyerMessage'], item_id,
                                      sessionUser)
        session.pop('lazyRegistration')
        session.pop('lazyPage')
        return redirect("/")

    if request.method == "GET":
        print("Got a Get")
        cursor = getCursor()[1]
        cursor.execute(query().APPROVED_ITEM(str(item_id)))
        itemObject = product.makeProduct(cursor.fetchone())
        cursor.close()
        currentItem = itemObject.toDict()
        session['contact_seller_item'] = currentItem

    if request.method == "POST":
        buyerContact = str(bleach.clean(request.form['contactType']))
        buyerMessage = str(bleach.clean(request.form['buyerMessage']))

        isRegistered = not sessionUser == ""
        session['item_id'] = item_id
        if not isRegistered:
            session['lazyRegistration'] = True
            session['lazyPage'] = 'contact-seller'
            session['buyerContact'] = buyerContact
            session['buyerMessage'] = buyerMessage
            print("going to login?")
            return redirect("/login")
        makeAndInsertMessageForSeller(buyerContact, buyerMessage, item_id,
                                      sessionUser)
        return redirect("/")

        currentItem = "" if 'contact_seller_item' not in session else session[
            'contact_seller_item']
    try:
        session.pop('contact_seller_item')
    except KeyError:
        print('yo, dat contact_seller_item was not in the session bro')
        print('Have a nice day!')

    return render_template('contact-seller.html',
                           sessionUser=sessionUser,
                           id=item_id,
                           currentItem=currentItem)
Exemplo n.º 7
0
def makeAndInsertMessageForSeller(buyerContact, buyerMessage, item_id,
                                  sessionUser):
    cursor = getCursor()[1]
    cursor.execute(query().APPROVED_ITEM(item_id))
    item = product.makeProduct(cursor.fetchone())

    cursor.execute(query().USER_FOR_PRODUCT(item_id))
    seller = cursor.fetchone()

    completeMessageList = messageForSeller(
        sessionUser['u_fname'] + " " + sessionUser['u_lname'], buyerContact,
        buyerMessage, item.i_title, item.i_create_ts, item.i_price)
    completeMessage = '\n'.join(message for message in completeMessageList)

    print(query().INSERT_MESSAGE(completeMessage, sessionUser['u_id'],
                                 seller[0], item_id))

    cursor.execute(query().INSERT_MESSAGE(completeMessage, sessionUser['u_id'],
                                          seller[0], item_id))
    db.commit()
    cursor.close()
    session['otherFeedback'] = "Message Sent"
Exemplo n.º 8
0
def admin_page(user_id):
    try:
        if session['sessionUser']['u_id'] < 1 or session['sessionUser'][
                'u_id'] != int(user_id):
            abort(404)
    except KeyError:
        abort(404)
    conncetion, cursor = getCursor()
    categories = []
    cursor.execute("SELECT * FROM user;")
    print(cursor.fetchall())
    # fetch the items for admin approval from db
    cursor.execute(query().ALL_PENDING_LISTINGS())
    data = cursor.fetchall()
    # Fetch the categories from db
    cursor.execute(query().fetchAllCategories())
    allCategories = cursor.fetchall()
    categories = [allCategories[i][0] for i in range(len(allCategories))]

    productList = []
    productListUsers = []
    for d in data:
        if len(d) == 16:
            productObject = product.makeProduct(d)
            productList.append(productObject)
            cursor.execute(query().FULL_USER_FOR_PRODUCT(
                str(productObject.i_u_id)))
            productListUsers.append(user.makeUser(cursor.fetchone()))

    cursor.execute(query().ALL_NON_ADMIN_APPROVED_USERS())
    data = cursor.fetchall()
    userList = []

    for d in data:
        if len(d) == 9:
            userObject = user.makeUser(d)
            userList.append(userObject)
    cursor.execute(query().ALL_APPROVED_LISTINGS())
    data = cursor.fetchall()

    approvedProducts = []
    approvedProductsUsers = []
    for d in data:
        if len(d) == 16:
            productObject = product.makeProduct(d)
            approvedProducts.append(productObject)
            cursor.execute(query().FULL_USER_FOR_PRODUCT(
                str(productObject.i_u_id)))
            approvedProductsUsers.append(user.makeUser(cursor.fetchone()))

    sessionUser = "" if 'sessionUser' not in session else session['sessionUser']

    currentSearch = ""
    categoryName = "All"
    session['categories'] = categories

    return render_template("admin/admin.html",
                           sessionUser=sessionUser,
                           id=user_id,
                           products=productList,
                           users=userList,
                           approvedProducts=approvedProducts,
                           currentSearch=currentSearch,
                           categoryName=categoryName,
                           categories=categories,
                           approvedProductsUsers=approvedProductsUsers,
                           productListUsers=productListUsers)
Exemplo n.º 9
0
def searchPage():

    cursor = getCursor()[1]

    print(len(request.form))
    currentSearch = "" if 'currentSearch' not in session else session['currentSearch']
    # categoryName = "Category" if 'categoryName' not in session else session['categoryName']
    categories = [] if 'categories' not in session else session['categories']
    formsLen = len(request.form)

    feedback, data = [], ""
    productList = []
    if request.method == 'GET':
        pass
        # currentSearch = "" if 'currentSearch' not in session else session['currentSearch']

    if request.method == 'POST':
        cursor = getCursor()[1]
        if categories == "":
            cursor.execute(query().fetchAllCategories())
            allCategories = cursor.fetchall()
            categories = [allCategories[i][0] for i in range(len(allCategories))]

        if formsLen > 0:
            search = request.form['text']
            catName = "All" if request.form['category'] == "All" else request.form['category']
            if catName != "":
                session['categoryName'] = catName
            print("catname is: ",catName)
            search = str(bleach.clean(search))  # sanitizing a bad search
            print("search recieved:", search)
            session['currentSearch'] = search
            print("sessions's search", session['currentSearch'])
            currentSearch = "" if 'currentSearch' not in session else session['currentSearch']
            cursor.execute(query().SEARCH_QUERY(search,catName))

            data = cursor.fetchall()
            print("All items?", data)

        if len(data) == 0:
            if formsLen > 0:
                feedback.append("No Results, Consider these Items")
            cursor.execute(query().ALL_APPROVED_LISTINGS())
            data = cursor.fetchall()
        # cursor.close()

        productUsers = []

    # if catName != "":
    #     data = [d for d in data if d['c_name'] == catName]

    for d in data:
        if len(d) > 11:
            productObject = product.makeProduct(d)
            productList.append(productObject)
            data = [productObject.toDict() for d in cursor.fetchall()]

            cursor.execute(query().FULL_USER_FOR_PRODUCT(str(productObject.i_id)))
            productUser = user.makeUser(cursor.fetchone())
            productUsers.append(productUser) 

    cursor.close()
    session['previousQuery'] = [productObject.toDict()
                                for productObject in productList]

    if 'currentCategory' in session:
        session.pop('currentCategory')
    data = session['previousQuery']

    if len(feedback) == 0 and formsLen != 0:
        if len(data) == 1:
            feedback.append(str(len(data)) + " Result Found")
        else:
            feedback.append(str(len(data)) + " Results Found")

    sessionUser = "" if 'sessionUser' not in session else session['sessionUser']
    # currentSearch = "" if 'currentSearch' not in session else session['currentSearch']
    categoryName = "All" if 'categoryName' not in session else session['categoryName']

    return render_template("home.html", products=data, feedback=feedback, sessionUser=sessionUser, sortOption="Sort By", currentSearch=currentSearch,categoryName=categoryName,categories=categories, productUsers=productUsers)