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)
def user_profile(id): query = ProductSearchForm(request.form) if hasattr(current_user, 'role'): role = current_user.get_role() else: role = "" user = db.find_user_by_id(id) if id is None: flash('User does not exist!', category='danger') posts = [] else: posts = db.posts_by_user(id) 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) stars = int(user['rating']) return render_template('user-profile.html', search_form=query, posts=posts, user=user, role=role, stars=stars)
def user_posts(user_id): query = ProductSearchForm(request.form) user = db.find_user_by_id(user_id) if hasattr(current_user, 'role'): role = current_user.get_role() else: role = "" if user_id is None: flash('No user with id {}'.format(user_id), category='danger') posts = [] else: posts = db.posts_by_user(user_id) 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) return render_template('posts.html', search_form=query, user=user, posts=posts, mode='user', role=role)
def user(id): user = find_user_by_id(id) followeds= user.followed.all() if id == session.get('id') else None if user is None: flash(u'用户不存在', 'alert-warning') return redirect(url_for('index')) return render_template('user.html', user=user, followeds=followeds)
def follow(id): followed = find_user_by_id(id) if followed is None: flash('User not found', 'alert-warning') return redirect(url_for('index')) curuser = find_user_by_id(session.get('id')) if curuser == followed: flash('You can\'t follow yourself!', 'alert-warning') return redirect(url_for('user', id=id)) u = curuser.follow(followed) if u is None: flash('Cannot follow the user') return redirect(url_for('user', id=id)) db.session.add(u) db.session.commit() # db.session.close() flash('You are now following the user', 'alert-success') follower_notification(followed, curuser) return redirect(url_for('user', id=id))
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)
def before_request(): """pull user info from the database based on session id""" g.user = None if 'id' in session: try: try: current_user = find_user_by_id(session.get('id')) g.user = current_user except TypeError: # session probably expired pass except KeyError: pass
def edit_profile(): id = current_user.get_id() user = db.find_user_by_id(id) if hasattr(current_user, 'role'): role = current_user.get_role() else: role = "" if user is None: flash("User doesn't exist", category='danger') return redirect(url_for('all_users')) user_form = UserForm(name=user['name'], email=user['email'], zip=user['zip'], password=user['password'], bio=user['bio']) if user_form.validate_on_submit(): user_dict = db.update_user(user_form.name.data, user_form.email.data, user_form.zip.data, user_form.password.data, user_form.bio.data, id) # uploaded_photo = user_form.image.data # # photo_row = db.init_user_photo(user_dict['id']) # # file_name = "file{:04d}".format(photo_row['id']) # # extension = PurePath(uploaded_photo.filename).suffix # file_name += extension # # file_path = os.path.join('static/user-photos', file_name) # # file_path2 = os.path.join('user-photos', file_name) # # save_path = os.path.join(app.static_folder, file_path2) # uploaded_photo.save(save_path) # # db.set_user_photo(photo_row['id'], file_path) if user_dict['rowcount'] == 1: flash("Profile updated!", category='success') return redirect(url_for('profile')) else: flash('User not updated', category='danger') return render_template('user-form.html', form=user_form, mode='edit-profile', role=role)
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)
def unfollow(id): followed = find_user_by_id(id) if followed is None: flash('User not found.', 'alert-warning') return redirect(url_for('index')) if followed == g.user: flash('You can\'t unfollow yourself!', 'alert-warning') return redirect(url_for('user', id=id)) u = g.user.unfollow(followed) if u is None: flash('Cannot unfollow the user.', 'alert-warning') return redirect(url_for('user', id=id)) db.session.add(u) db.session.commit() # db.session.close() flash('You have stopped following the user.', 'alert-success') return redirect(url_for('user', id=id))
def __init__(self, id): self.id = id user = db.find_user_by_id(self.id) if user is not None: self.id = user['id'] self.name = user['name'] self.email = user['email'] self.role = user['role'] self.is_authenticated = True else: self.id = 'no id' self.name = 'no name' self.email = 'no email' self.role = 'no role' self.is_authenticated = False self.is_active = True
def edit_user(id): user = db.find_user_by_id(id) if hasattr(current_user, 'role'): role = current_user.get_role() else: role = "" if user is None: flash("User doesn't exist", category='danger') return redirect(url_for('all_users')) user_form = AdminUserForm(name=user['name'], email=user['email'], zip=user['zip'], password=user['password'], rating=user['rating'], bio=user['bio']) if user_form.validate_on_submit(): rowcount = db.admin_update_user(user_form.name.data, user_form.email.data, user_form.password.data, user_form.bio.data, user_form.rating.data, id) if rowcount == 1: flash("User '{}' updated!".format(user_form.name.data), category='success') return redirect(url_for('admin_dashboard')) else: flash('User not updated', category='danger') return render_template('user-form.html', form=user_form, mode='update', role=role)
def enable_user_by_id(id): user = db.find_user_by_id(id) db.enable_user_by_id(id) flash("User {} enabled".format(user['name']), category='success') return redirect(url_for('all_users'))
def load_user(u): return db.find_user_by_id(u) # should return none if none