def password_recovery(): times = css_js_update_time() form = PasswordRecoveryForm() if form.validate_on_submit(): user = User.objects.get(email=form.email.data) recover = PassRecovery(user=user, recovery_hash=secrets.token_urlsafe(16)).save() reset_url = 'https://рецептуарий.рф{}'.format( url_for('public.recover_reset', uid=user.id, hash=recover.recovery_hash)) recovery_msg = "<p>Добрый день!</p>" \ "<p>Мы получили запрос на восстановление пароля. Для сброса пароля и установки нового, " \ "пожалуйста, перейдите по <a href='{}' target='_blank'>ссылке</a>.</p>" \ "<br><br><p>С уважением,<br>команда Рецептуария.</p>".format(reset_url) send_email(_('Восстановление пароля'), current_app.config['SENDER'], [user.email], recovery_msg, recovery_msg) flash( _l('Ссылка на восстановление пароля успешно отправлена на указанный E-mail.' )) return redirect(url_for('public.login')) return render_template('password_recovery.html', form=form, times=times)
def recover_reset(uid, hash): try: user = User.objects.get(id=uid) except DoesNotExist: flash( 'Неверная ссылка для восстановления пароля! Пользователя не найдено в системе.' ) return redirect(url_for('public.login')) else: try: recover = PassRecovery.objects.get(recovery_hash=hash) except DoesNotExist: flash( 'Неверная ссылка для восстановления пароля! Неверный секретный код.' ) return redirect(url_for('public.login')) else: times = css_js_update_time() form = PasswordResetForm() if form.validate_on_submit(): user.set_password(form.password.data) user.save() recover.delete() flash('Пароль успешно изменён!') return redirect(url_for('public.login')) return render_template('password_reset.html', form=form, times=times)
def contacts(): times = css_js_update_time() form = ContactForm() if form.validate_on_submit(): user = None if not current_user.is_authenticated else User.objects.get( id=current_user.id) feedback = Feedback(user=user, name=form.name.data, email=form.email.data, message=form.message.data) feedback.save() msg = "<p>Добрый день!</p>" \ "<p>Кто-то оставил сообщение на сайте Рецептуария.</p>" \ "<p>Пользователь: <a href='https://xn--80ajanf7aeeqk3a.xn--p1ai{}'>{}</a></p>" \ "<p>Представился: {}</p>" \ "<p>Email: {}</p>" \ "<p>Сообщение: {}</p>" \ "<br><br><p>С уважением,<br>команда Рецептуария.</p>"\ .format( url_for('public.recipes_by_author', author_id=feedback.user.id) if feedback.user else '#', feedback.user.username if feedback.user else '-', feedback.name, feedback.email, feedback.message ) send_email(_('Сообщение с сайта'), current_app.config['SENDER'], current_app.config['ADMINS'], msg, msg) flash(_l('Сообщение было успешно отослано, спасибо!')) return redirect(url_for('public.contacts')) return render_template('contacts.html', form=form, times=times)
def profile(): times = css_js_update_time() return render_template('profile.html', user=current_user, seo={ 'title': _l('Мой профиль'), 'description': _l(''), 'keywords': _l('') }, times=times)
def register(): times = css_js_update_time() form = RegistrationForm() if form.validate_on_submit(): user = User(username=form.username.data, email=form.email.data) user.set_password(form.password.data) user.save() flash(_l('Поздравляем, Вы успешно зарегистрировались!')) return redirect(url_for('public.login')) return render_template('register.html', form=form, times=times)
def recipe(recipe_id): times = css_js_update_time() recipe = Recipe.objects.get_or_404(id=recipe_id) return render_template('recipe.html', seo={ 'title': recipe.title, 'description': recipe.title, 'keywords': recipe.title }, recipe=recipe, times=times)
def index(): times = css_js_update_time() form = SearchForm() form.init_data() if form.validate_on_submit(): return redirect( url_for('public.search', keyword=form.keyword.data, category=form.category.data)) categories = Category.objects().order_by('order') try: featured_recipe = Recipe.objects.get(is_featured=True) except DoesNotExist: featured_recipe = Recipe.objects().first() except MultipleObjectsReturned: featured_recipe = Recipe.objects(is_featured=True).first() try: featured_user = User.objects.get(is_featured=True) except DoesNotExist: featured_user = User.objects().first() except MultipleObjectsReturned: featured_user = User.objects(is_featured=True).first() top_recipe = Recipe.objects()[:1].order_by('-likes').first() recipes = Recipe.objects(is_searchable=True).order_by('-added')[:6] latest_user = User.objects().order_by('-id').first() top_user = User.objects.get( id=top_recipe.user.id) if top_recipe else latest_user return render_template( 'index.html', form=form, latest_user=latest_user, top_user=top_user, recipes=recipes, top_recipe=top_recipe, featured_user=featured_user, featured_recipe=featured_recipe, categories=categories, seo={ 'title': _l('Рецептуарий - ваш персональный сборник рецептов!'), 'description': _l('Рецептуарий - ваш персональный сборник рецептов!'), 'keywords': _l('Рецептуарий - ваш персональный сборник рецептов!') }, times=times)
def new_recipe(): times = css_js_update_time() form = RecipeForm() form.init_data() if form.validate_on_submit(): try: user = User.objects.get(id=current_user.id) if not user.recipes() or True: add_achievement('первый рецепт', current_user.id) elif len(user.recipes()) == 9: add_achievement('десятый рецепт', current_user.id) ingredients = get_recipe_ingredients( request.form.getlist('ingr[]'), request.form.getlist('qty[]')) category = Category.objects.get(id=form.category.data) recipe = Recipe(user=user, category=category, difficulty=form.difficulty.data, title=form.title.data, description=form.description.data, ingredients=ingredients, normalized_description=normalize_text( form.description.data), is_searchable=form.is_searchable.data) if form.picture.data: recipe.set_picture(form.picture.data) recipe.save() except Exception as ex: flash('Что-то пошло не так. Попробуйте еще раз :: {}'.format( str(ex))) return redirect(url_for('public.new_recipe')) else: flash('Рецепт успешно добавлен!') return redirect(url_for('public.profile')) return render_template('manage_recipe.html', form=form, seo={ 'title': _l('Новый рецепт'), 'description': _l(''), 'keywords': _l('') }, times=times)
def recipes_by_category(slug): page = request.args.get('page', 1, type=int) times = css_js_update_time() categories = Category.objects().order_by('order') category = Category.objects.get_or_404(slug=slug) recipes = Recipe.objects( category=category, is_searchable=True).order_by('-added').paginate( page=page, per_page=current_app.config['RECIPES_PER_PAGE']) return render_template('recipes_list.html', recipes=recipes, current_category=category, categories=categories, seo={ 'title': category.name, 'description': category.name, 'keywords': category.name }, times=times)
def recipes(): page = request.args.get('page', 1, type=int) times = css_js_update_time() categories = Category.objects().order_by('order') recipes = Recipe.objects(is_searchable=True).order_by('-added').paginate( page=page, per_page=current_app.config['RECIPES_PER_PAGE']) return render_template('recipes_list.html', recipes=recipes, categories=categories, seo={ 'title': _l('Рецепты пользователей рецептуария'), 'description': _l('Рецепты пользователей рецептуария'), 'keywords': _l('Рецепты пользователей рецептуария') }, times=times)
def edit_recipe(recipe_id): times = css_js_update_time() recipe = Recipe.objects.get_or_404(id=recipe_id) if recipe.user.id != current_user.id: abort(403) form = RecipeForm() form.init_data() if form.validate_on_submit(): ingredients = get_recipe_ingredients(request.form.getlist('ingr[]'), request.form.getlist('qty[]')) category = Category.objects.get(id=form.category.data) recipe.title = form.title.data recipe.category = category recipe.ingredients = ingredients recipe.difficulty = form.difficulty.data recipe.cooking_time = form.cooking_time.data recipe.description = form.description.data recipe.normalized_description = normalize_text(form.description.data) recipe.is_searchable = form.is_searchable.data recipe.updated = datetime.utcnow() if form.picture.data or request.form.get('delete_picture'): recipe.delete_picture() if form.picture.data: recipe.set_picture(form.picture.data) recipe.save() return redirect(url_for('public.edit_recipe', recipe_id=recipe.id)) return render_template('manage_recipe.html', recipe=recipe, form=form, seo={ 'title': recipe.title, 'description': _l(''), 'keywords': _l('') }, times=times)
def login(): if current_user.is_authenticated: return redirect(url_for('public.index')) form = LoginForm() if form.validate_on_submit(): try: user = User.objects.get(username=form.username.data) if not user.check_password(form.password.data): flash(_l('Неверный пароль')) return redirect(url_for('public.login')) except DoesNotExist: flash(_l('Пользователя не существует в системе')) return redirect(url_for('public.login')) else: login_user(user, form.remember_me.data) # check if user comes from any protected non-anonymous page to return him where he wanted to be before login next_page = request.args.get('next') or url_for('public.index') return redirect(next_page) times = css_js_update_time() return render_template('login.html', form=form, times=times)
def search(): times = css_js_update_time() form = SearchForm() form.init_data() page = request.args.get('page', 1, type=int) keyword = request.args.get('keyword', '') category = request.args.get('category', '') recipes = Recipe.search(keyword, request.args.get('category', ''), page) return render_template('search.html', form=form, category=category, recipes=recipes, keyword=keyword, seo={ 'title': _l('Результаты поиска'), 'description': '', 'keywords': '' }, times=times)
def recipes_by_author(author_id): page = request.args.get('page', 1, type=int) times = css_js_update_time() user = User.objects.get_or_404(id=author_id) recipes = Recipe.objects( user=user, is_searchable=True).order_by('-added').paginate( page=page, per_page=current_app.config['RECIPES_PER_PAGE']) if not recipes: abort(404) return render_template('recipes_list.html', recipes=recipes, user=user, seo={ 'title': '{} {}'.format(_l('Рецепты от'), user.username), 'description': '{} {}'.format(_l('Рецепты от'), user.username), 'keywords': '{} {}'.format(_l('Рецепты от'), user.username) }, times=times)
def bot(): times = css_js_update_time() return render_template('bot.html', times=times)