Esempio n. 1
0
def post_details(id):
    post = db.find_post_by_id(id)
    user_id = post['user_id']
    user = db.find_user_by_id(user_id)
    query = ProductSearchForm(request.form)

    if hasattr(current_user, 'role'):
        role = current_user.get_role()
    else:
        role = ""

    if request.method == 'POST':
        query_list = query.search.data.lower().split(" ")
        posts = []
        for item in query_list:
            posts += db.search_products(item)
        return render_template('posts.html',
                               search_form=query,
                               posts=posts,
                               mode='results',
                               role=role)

    if post['user_id'] == current_user.get_id():
        return render_template('post-details.html',
                               search_form=query,
                               post=post,
                               role=role)
    else:
        return render_template('post-details.html',
                               search_form=query,
                               post=post,
                               user=user,
                               role=role)
Esempio n. 2
0
def edit_post(id):
    post = db.find_post_by_id(id)

    if hasattr(current_user, 'role'):
        role = current_user.get_role()
    else:
        role = ""

    if post is None:
        flash("Post doesn't exist", category='danger')
        return redirect(url_for('all_posts'))

    post_form = EditPostForm(price=post['price'],
                             quantity=post['quantity'],
                             unit=post['unit'],
                             product=post['product'],
                             description=post['description'])

    if post_form.validate_on_submit():
        rowcount = db.update_post(post_form.price.data,
                                  post_form.quantity.data, post_form.unit.data,
                                  post_form.product.data,
                                  post_form.description.data, id)

        if rowcount == 1:
            flash("'{}' post updated".format(post_form.product.data),
                  category='success')
            return redirect(url_for('all_posts'))
        else:
            flash('Post not updated', category='danger')

    return render_template('post-form.html',
                           post_form=post_form,
                           mode='update',
                           role=role)
Esempio n. 3
0
 def delete(self, post_id):
     post = find_post_by_id(post_id)
     if post is None:
         return {
             'success': 0,
             'message': 'Post not found',
         }
     else:
         delete_post(post_id)
         return {
             'success': 1,
             'message': 'Post was deleted',
         }
Esempio n. 4
0
    def test_add_post(self):
        row_count = db.create_post(5.67, 10, 'lb', 'Carrots', 'Vegetables',
                                   'Sample Description', '2018-04-18')
        self.assertEqual(row_count, 1)

        test_post = db.find_post_by_id(1)
        self.assertIsNotNone(test_post)

        self.assertEqual(test_post['price'], 5.67)
        self.assertEqual(test_post['quantity'], 10)
        self.assertEqual(test_post['unit'], 'lb')
        self.assertEqual(test_post['product'], 'Carrots')
        self.assertEqual(test_post['category'], 'Vegetables')
        self.assertEqual(test_post['description'], 'Sample Description')
        self.assertEqual(test_post['date'], '2018-04-18')
Esempio n. 5
0
def add_to_favorites(post_id):
    if session:
        user_id = session['id']

        post = db.find_post_by_id(post_id)
        favorites = db.find_duplicate_in_favorites(user_id, post_id)

        if not favorites:
            db.add_to_favorites(user_id, post_id)
            flash("{} added to favorites".format(post['product']),
                  category='success')
        else:
            flash("{} already added to favorites".format(post['product']),
                  category='danger')

    return redirect(url_for('all_posts'))
Esempio n. 6
0
def confirmation(id, amount, total):
    user_id = current_user.get_id()
    user = db.find_user_by_id(user_id)

    post = db.find_post_by_id(id)
    user_id = post['user_id']
    selling_user = db.find_user_by_id(user_id)

    if hasattr(current_user, 'role'):
        role = current_user.get_role()
    else:
        role = ""

    return render_template('confirmation.html',
                           id=id,
                           amount=amount,
                           total=total,
                           user=user,
                           post=post,
                           role=role,
                           selling_user=selling_user)
Esempio n. 7
0
def buy_product(id):
    user_id = current_user.get_id()
    buying_user = db.find_user_by_id(user_id)

    post = db.find_post_by_id(id)
    user_id = post['user_id']
    selling_user = db.find_user_by_id(user_id)

    if hasattr(current_user, 'role'):
        role = current_user.get_role()
    else:
        role = ""

    buy_product_form = BuyProductForm()

    if buy_product_form.validate_on_submit():
        quantity = db.get_quantity(id)

        amount = int(buy_product_form.amount.data)
        total = amount * post['price']

        val = db.update_quantity(id, quantity[0], amount)

        if val == 0:
            flash('You cannot buy that many!', category="danger")
            return redirect(url_for('buy_product', id=id))
        else:
            return redirect(
                url_for('confirmation', id=id, amount=amount, total=total))

    return render_template('buy-product.html',
                           form=buy_product_form,
                           selling_user=selling_user,
                           buying_user=buying_user,
                           post=post,
                           role=role)
Esempio n. 8
0
def delete_post_by_id(id):
    post = db.find_post_by_id(id)
    if post is not None:
        db.delete_post_by_id(id)
        return redirect(url_for('my_posts'))