def auth(): """Обработка авторизации ВКонтакте""" user_id = request.args.get('uid') app_id = current_app.config.get('VK_APP_ID') secret_key = current_app.config.get('VK_SECRET_KEY') # Проверка хэша авторизации hash_args = request.args.get('hash') hash_computed = md5(f'{app_id}{user_id}{secret_key}'.encode()).hexdigest() if hash_args == hash_computed: user = User.query.get(user_id) next = request.args.get('next') if next is None or not is_safe_url(next): next = url_for('main.index') if user is None: user = User( id=user_id, first_name=request.args.get('first_name'), last_name=request.args.get('last_name') ) db.session.add(user) db.session.commit() flash( 'Вы успешно зарегестрировались. ' 'Сейчас вы можете дополнительно настроить свой профиль.' ) return redirect(url_for('.settings', next=next)) login_user(user) flash('Вы вошли в свой аккаунт.') return redirect(next)
def logout(): """Выход из аккаунта""" if current_user.is_authenticated: logout_user() flash('Вы вышли из аккаунта.') next = request.referrer if next is None or not is_safe_url(next): next = url_for('main.index') return redirect(next)
def delete(id): """Удаление заказа""" order = Order.query.get(id) bot.notify_delete(order.client, order.volunteer) db.session.delete(order) db.session.commit() next = request.referrer if next is None or not is_safe_url(next): next = url_for('main.index') flash('Вы удалили запись.') return redirect(next)
def settings(): """Изменение настроек пользователя""" form = SettingsForm(obj=current_user) next = request.args.get('next') if next is None or not is_safe_url(next): next = url_for('.settings') if form.validate_on_submit(): current_user.phone = form.phone.data current_user.address = form.address.data db.session.add(current_user) db.session.commit() flash('Настройки успешно изменены.') return redirect(next) return render_template('auth_settings.html', form=form)
def login(): ''' logs user in if email address and password match''' form = LoginForm() if request.method == 'POST' and form.validate_on_submit(): user = mongo.db.users.find_one( {"email_address": form.email_address.data}) if user and User.validate_login(form.password.data, user['password']): user_obj = User(user['email_address'], user['first_name']) login_user(user_obj) flash("Logged in successfully!", 'success') next = request.args.get('next') if not is_safe_url(next): return abort(400) return redirect(next or url_for('home')) flash("Wrong username or password!", 'errors') return render_template('pages/login.html', title='login', form=form)
def redirect(self, endpoint='index', **values): if is_safe_url(self.next_url.data): return redirect(self.next_url.data) target = get_redirect_target() return redirect(target or url_for(endpoint, **values))