def country(country_name): country = Country.get_country_by_name(country_name).first() if country is None: flash('Country {} not found.'.format(country_name)) return redirect(url_for('main.index')) form = SearchForm() already_picked = Post.user_has_post_in_country(current_user.id, country.id) if request.method == 'POST': button_clicked = request.form.get('submit button') if button_clicked == 'select country': if not already_picked: Post.create_empty_post(country.id) return redirect( url_for('recipes.available_posts', country_name='all')) if button_clicked == 'search user': searched_user = User.get_user_by_username( form.search_user_text.data).first() if not searched_user is None: return redirect( url_for('recipes.user_country', username=searched_user.username, country_name=country_name)) flash('User {} not found'.format(form.search_user_text.data)) if button_clicked == 'search country': searched_country = Country.get_country_by_name( form.search_country_text.data).first() if not searched_country is None: return redirect( url_for('recipes.country', country_name=searched_country.name)) flash('Country {} not found'.format(form.search_country_text.data)) page = request.args.get('page', 1, type=int) posts = Post.get_others_posts_by_country_id(country.id).paginate( page, current_app.config['POSTS_PER_PAGE'], False) next_url = url_for('recipes.country_by_user', country_name=country_name, page=posts.next_num) \ if posts.has_next else None prev_url = url_for('recipes.country_by_user', country_name=country_name, page=posts.prev_num) \ if posts.has_prev else None return render_template('recipes/country.html', title='{}'.format(country_name), country=country, form=form, already_picked=already_picked, posts=posts.items, next_url=next_url, prev_url=prev_url)
def index(): form = SearchForm() amount_of_available_countries = ( Country.count_countries() - Post.count_different_countries_with_posts()) amount_of_available_posts = Post.count_available_posts() page = request.args.get('page', 1, type=int) posts = current_user.followed_posts().paginate( page, current_app.config['POSTS_PER_PAGE'], False) next_url = url_for('main.index', page=posts.next_num) \ if posts.has_next else None prev_url = url_for('main.index', page=posts.prev_num) \ if posts.has_prev else None if request.method == 'POST': button_clicked = request.form.get('submit button') if button_clicked == 'go to available posts': return redirect( url_for('recipes.available_posts', country_name='all')) if button_clicked == 'pick country': if amount_of_available_countries: picked_countries = Post.get_my_countries_with_posts() country = Country.get_random_country(picked_countries) Post.create_empty_post(country.id) return redirect( url_for('recipes.country', country_name=country.name)) flash('You don\'t have any left country to try.') flash('Please search for that country you really want' + 'to give another try.') flash('Or, if you are brave enough, reset the whole list!') return redirect( url_for('main.user', username=current_user.username)) if button_clicked == 'search user': searched_user = User.get_user_by_username( form.search_user_text.data).first() if not searched_user is None: return redirect( url_for('main.user', username=searched_user.username)) flash('User {} not found'.format(form.search_user_text.data)) if button_clicked == 'search country': searched_country = Country.get_country_by_name( form.search_country_text.data).first() if not searched_country is None: return redirect( url_for('recipes.country', country_name=searched_country.name)) flash('Country {} not found'.format(form.search_country_text.data)) return render_template( 'index.html', title='Home', index=True, posts=posts.items, next_url=next_url, prev_url=prev_url, form=form, amount_of_available_countries=amount_of_available_countries, amount_of_available_posts=amount_of_available_posts)