def edit(): form = EditForm() if form.validate_on_submit(): with open('../all/db/dz.csv', 'r', encoding='utf-8') as f: re = f.read().split('\n') line = re[SUBJECT_DICT[form.subject.data]] title = line.split(';')[0] old_text = line.split(';')[1] newline = title + ';' + form.text.data re[SUBJECT_DICT[form.subject.data]] = newline with open('../all/db/dz.csv', 'w', encoding='utf-8') as f: f.write('\n'.join(re)) db_sess = db_session.create_session() changes = Changes() changes.from_text = old_text changes.to_text = form.text.data changes.subject = form.subject.data current_user.changes.append(changes) db_sess.merge(current_user) db_sess.commit() return render_template( "ad_success.html", title="Изменение", title_text='Успешно', text= f"Успешно изменён текст в {form.subject.data} на {form.text.data}") return render_template('edit.html', title='Изменение', form=form)
def send_message(): if not request: return jsonify({'error': 'Empty request'}) elif not all(key in request.json for key in ['email', 'password', 'chat_id', 'message']): return jsonify({'error': 'Bad request'}) db_sess = db_session.create_session() user = db_sess.query(User).filter( User.email == request.json['email']).first() if user and user.check_password(request.json['password']): send_message_to_bot(request.json['message'], request.json['chat_id']) return jsonify({"status": "Message will be sent within 90 seconds"}) else: return jsonify({'error': 'Bad email or password'})
def login(): form = LoginForm() if form.validate_on_submit(): db_sess = db_session.create_session() user = db_sess.query(User).filter( User.email == form.email.data).first() if user and user.check_password(form.password.data): login_user(user, remember=form.remember_me.data) return redirect("/home") return render_template('login.html', title="Вход", message="Неправильный логин или пароль", form=form) return render_template('login.html', title='Вход', form=form)
def register(): form = RegisterForm() if form.validate_on_submit(): if form.password1.data != form.password2.data: return render_template('register.html', title='Регистрация', form=form, message="Пароли не совпадают") db_sess = db_session.create_session() if db_sess.query(User).filter(User.email == form.email.data).first(): return render_template('register.html', title='Регистрация', form=form, message="Такой пользователь уже есть") user = User(name=form.name.data, email=form.email.data, position=form.position.data) user.set_password(form.password1.data) db_sess.add(user) db_sess.commit() return redirect('/login') return render_template('register.html', title='Регистрация', form=form)
def load_user(user_id): db_sess = db_session.create_session() return db_sess.query(User).get(user_id)