def home(): searchform = SearchForm() all_hostels = Hostel.query.all() if searchform.validate_on_submit(): query = searchform.search_input.data return redirect(url_for('search_results', query=query)) for hostels in all_hostels: single = get_name(hostels) return render_template("home.html", title='Home', searchform=SearchForm(), current_user=current_user, hostels=all_hostels, l_about='Description', c_about='Description', a_about='Description')
def update_hostel(hostel): form = Hostel_Update() hostel = Hostel.query.filter_by(name=hostel).first() form.hostel_name.data = hostel.name form.phone_number.data = hostel.phone form.cat.data = hostel.cat form.price_range.data = hostel.price_range form.location.data = hostel.location form.distance.data = hostel.distance # form.condition.data = hostel.attributes[0] # form.Intensity.data = hostel.attributes[1] if form.validate_on_submit(): hostel.name = form.hostel_name.data hostel.phone = form.phone_number.data hostel.cat = form.cat.data.lower() hostel.image = save_image(form.hostel_picture.data, hostel.cat, "Banner", hostel.name) hostel.price_range = form.price_range.data hostel.location = form.location.data hostel.distance = form.distance.data # hostel.attributes[0] = form.condition.data # hostel.attributes[1] = form.Intensity.data db.session.commit() flash('Hostel info update', 'success') return redirect(url_for('main.home')) return render_template('hostel_update.html', title='Update Hostel Info', current_hostel=hostel, upform=form, searchform=SearchForm())
def before_request(): g.search_form = SearchForm() if g.search_form.validate_on_submit(): return redirect( url_for('main.search', tags=g.search_form.search_term.data.lower()))
def search_results(query): searchform = SearchForm() # Have a 404 page saying that Hostel not found if searchform.validate_on_submit(): query = searchform.search_input.data return redirect(url_for('search_results', query=query)) Query = query.capitalize() query_name = Query.replace(" ", "_") if " " in Query else Query results = Hostel.query.filter_by(name=query_name).first_or_404() results.name = results.name.replace( "_", " ") if results else "No Hostel was found with that name." return render_template('search_results.html', title=f'Search results for {query}', results=results, searchform=searchform)
def contact(): all_hostels = Hostel.query.all() for hostels in all_hostels: single = get_name(hostels) return render_template('contact.html', title='Contact Us', hostels=all_hostels, searchform=SearchForm())
def luxury(): hostels = Hostel.query.filter_by(cat='luxury') for hostel in hostels: single = get_name(hostel) # return hostels return render_template('luxury.html', title='Luxury Hostels', posts=hostels, searchform=SearchForm())
def affordable(): hostels = Hostel.query.filter_by(cat='affordable') for hostel in hostels: single = get_name(hostel) # hostel_image = url_for('static', filename='/images/affordable/'+hostels.image) return render_template('affordable.html', title='Affordable Hostels', posts=hostels, searchform=SearchForm())
def hostel_page(hostel_name): Query = hostel_name.capitalize() query_name = Query.replace(" ", "_") if " " in Query else Query hostel = Hostel.query.filter_by(name=query_name).first_or_404() return render_template('hostel_page.html', title=f'{hostel.name} Hostel', current_hostel=hostel, attribs=hostel.attributes, searchform=SearchForm())
def comfort(): hostels = Hostel.query.filter_by(cat='comfort') for hostel in hostels: single = get_name(hostel) return render_template('comfort.html', title='Comfortable Hostels', posts=hostels, oiroomin=750, oiroommax=1750, searchform=SearchForm())
def register(): form = RegisterForm() if form.validate_on_submit(): username = form.fullname.data email = form.email.data password = bcrypt.generate_password_hash(form.confirmpassword.data) new_user = User(username=username, email=email, password=password) db.session.add(new_user) db.session.commit() flash(f'Account created for {username}', 'success') return redirect(url_for('main.home')) return render_template('register.html', legend='Register', form=form, searchform=SearchForm())
def login(): logform = LoginForm() if logform.validate_on_submit(): email = logform.email.data student = User.query.filter_by(email=email).first() if student and bcrypt.check_password_hash(student.password, logform.password.data): login_user(student) flash(f'{current_user.username} login successful', 'success') return redirect(url_for('main.home')) else: flash( 'Either email or password incorrect, please check and try again', 'warning') return render_template('login.html', form=logform, searchform=SearchForm())
def home(): form = SearchForm() if request.method == "POST": if form.validate_on_submit: print("searching") posts = Post.query.whoosh_search(form.search.data).all() news = Post.query.order_by( Post.date_posted.desc()).filter_by(category="News").first() reviews = Post.query.order_by( Post.date_posted.desc()).filter_by(category="Reviews").first() commentary = Post.query.order_by( Post.date_posted.desc()).filter_by( category="Commentary").first() print("applying") print(posts) return render_template('search.html', posts=posts, news=news, reviews=reviews, commentary=commentary, form=form) else: page = request.args.get('page', 1, type=int) posts = Post.query.order_by(Post.date_posted.desc()).paginate( page=page, per_page=1) news = Post.query.order_by( Post.date_posted.desc()).filter_by(category="News").first() reviews = Post.query.order_by( Post.date_posted.desc()).filter_by(category="Reviews").first() commentary = Post.query.order_by( Post.date_posted.desc()).filter_by(category="Commentary").first() form.search.data = '' return render_template('index.html', posts=posts, page=page, news=news, reviews=reviews, commentary=commentary, title='Home', form=form)
def hostel_crud(): form = Hostel_Crud() if form.validate_on_submit(): Hostel.image = save_image(form.hostel_picture.data, form.cat.data.lower(), 'Banner', form.hostel_name.data.capitalize()) new_hostel_name = form.hostel_name.data.replace(" ", '_') hostel = Hostel(name=new_hostel_name.capitalize(), email=form.hostel_mail.data, phone=str(form.phone_number.data), price_range=form.price_range.data, location=form.location.data, distance=form.distance.data, cat=form.cat.data.lower(), image=Hostel.image) db.session.add(hostel) db.session.commit() flash('Hostel added successfully', 'success') return redirect(url_for(f"hostel.{form.cat.data.lower()}")) return render_template('hostel_CRUD.html', title='Alter hostel info', form=form, searchform=SearchForm())
def search(): form = SearchForm() if request.method == 'POST' and form.validate_on_submit(): return redirect((url_for('main.result', text=form.search.data))) return render_template('search.html', form=form)
def about(): return render_template('about.html', title='About Us', searchform=SearchForm())
def info_posts(hostel_name): hostel = Hostel.query.filter_by(name=hostel_name).first() return render_template('hostel_page.html', title='Latest Hostel News', current_hostel=hostel, searchform=SearchForm())
def gallery(hostel_name): hostel = Hostel.query.filter_by(name=hostel_name).first() return render_template('gallery.html', title='Hostel Pictures', current_hostel=hostel, searchform=SearchForm())