def register(): if current_user.is_authenticated: return redirect(url_for('main.index')) form = RegistrationForm() if form.validate_on_submit(): user = User(username=form.username.data.capitalize(), email=form.email.data) user.set_password(form.password.data) db.session.add(user) db.session.commit() if not Country.count_countries(): Country.create_country_table() flash('Congratulations, you are now a registered user!') return redirect(url_for('auth.login')) return render_template('auth/register.html', title='Register', form=form)
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)