|
| 1 | +from flask import Flask, render_template, request, redirect, url_for, session |
| 2 | +from db import Firebase |
| 3 | +from user import User |
| 4 | +from note import Note |
| 5 | +from flask_wtf import FlaskForm |
| 6 | +from flask_ckeditor import CKEditorField, CKEditor |
| 7 | +from wtforms import StringField, SubmitField |
| 8 | +from wtforms.validators import DataRequired |
| 9 | +from chatbot import summariz |
| 10 | + |
| 11 | +class NOTEForm(FlaskForm): |
| 12 | + title = StringField('Title', validators=[DataRequired()]) |
| 13 | + content = CKEditorField('Content') |
| 14 | + submit = SubmitField('Submit') |
| 15 | + |
| 16 | +class EditForm(FlaskForm): |
| 17 | + title = StringField('Title', validators=[DataRequired()]) |
| 18 | + content = CKEditorField('Content') |
| 19 | + submit = SubmitField('Submit') |
| 20 | + |
| 21 | +class ChatForm(FlaskForm): |
| 22 | + BomAI = CKEditorField('BomAI',validators=[DataRequired()]) |
| 23 | + submit = SubmitField('Submit') |
| 24 | + |
| 25 | +chat=summariz() |
| 26 | +ai=summariz() |
| 27 | +app = Flask(__name__) |
| 28 | +f = Firebase() |
| 29 | +app.secret_key = "secretkey" |
| 30 | +ckeditor = CKEditor(app) |
| 31 | + |
| 32 | +@app.route('/') |
| 33 | +def main(): |
| 34 | + return render_template("anasayfa.html") |
| 35 | + |
| 36 | +@app.route('/login', methods=['GET', 'POST']) |
| 37 | +def login(): |
| 38 | + if request.method == 'POST': |
| 39 | + mail = request.form['mail'] |
| 40 | + password = request.form['password'] |
| 41 | + try: |
| 42 | + f.login(mail, password) |
| 43 | + user = User(f.get_user_id(), f) |
| 44 | + user.save() |
| 45 | + session['user_id'] = f.get_user_id() |
| 46 | + return redirect(url_for('userpage', user_id=f.get_user_id())) |
| 47 | + except Exception as e: |
| 48 | + return render_template("login.html", error="Invalid User Information!") |
| 49 | + return render_template("login.html") |
| 50 | + |
| 51 | +@app.route('/signup', methods=['GET', 'POST']) |
| 52 | +def signup(): |
| 53 | + if request.method == 'POST': |
| 54 | + password = request.form['password'] |
| 55 | + mail = request.form['mail'] |
| 56 | + username = request.form['username'] |
| 57 | + f.register(mail, password) |
| 58 | + f.login(mail, password) |
| 59 | + user = User(f.get_user_id(), f) |
| 60 | + user.save() |
| 61 | + session['user_id'] = f.get_user_id() |
| 62 | + user.update({"username": username}) |
| 63 | + return redirect(url_for('userpage', user_id=f.get_user_id())) |
| 64 | + return render_template("signup.html") |
| 65 | + |
| 66 | +@app.route('/information', methods=['GET', 'POST']) |
| 67 | +def information(): |
| 68 | + user_id = session.get('user_id') |
| 69 | + if not user_id: |
| 70 | + return redirect(url_for('login')) |
| 71 | + |
| 72 | + user = User(user_id, f) |
| 73 | + user_data = user.get() |
| 74 | + if request.method == 'POST': |
| 75 | + fullname = request.form['fullname'] |
| 76 | + university = request.form['university'] |
| 77 | + department = request.form['department'] |
| 78 | + user.update({ |
| 79 | + "fullname": fullname, |
| 80 | + "university": university, |
| 81 | + "department": department, |
| 82 | + }) |
| 83 | + return render_template("information.html", user_data=user_data) |
| 84 | + |
| 85 | +@app.route('/userpage', methods=['GET', 'POST']) |
| 86 | +def userpage(): |
| 87 | + user_id = session.get('user_id') |
| 88 | + if not user_id: |
| 89 | + return redirect(url_for('login')) |
| 90 | + |
| 91 | + user = User(user_id, f) |
| 92 | + user_data = user.get() |
| 93 | + note = Note(user_id, f) |
| 94 | + notes = [note.get(note_id) for note_id in user.notes] |
| 95 | + note_id = request.args.get('note_id') |
| 96 | + selected_note = None |
| 97 | + if note_id: |
| 98 | + selected_note = f.ref.child(note.note_id) |
| 99 | + |
| 100 | + return render_template("userpage.html", user_data=user_data, note_data=notes, note=selected_note) |
| 101 | + |
| 102 | +@app.route('/userpage/<note_id>', methods=['GET', 'POST']) |
| 103 | +def show_notes(note_id): |
| 104 | + user_id = session.get('user_id') |
| 105 | + note = Note(user_id, f) |
| 106 | + user = User(user_id, f) |
| 107 | + user_data = user.get() |
| 108 | + notes = [note.get(note_id) for note_id in user.notes] |
| 109 | + selected_note_id=None |
| 110 | + for i in notes: |
| 111 | + if (note_id == i["note_idf"]): |
| 112 | + selected_note_id=i |
| 113 | + |
| 114 | + |
| 115 | + return render_template("userpage.html", user_data=user_data, note_data=notes, selected_note=selected_note_id) |
| 116 | + |
| 117 | + |
| 118 | +@app.route("/add_notes", methods=['GET', 'POST']) |
| 119 | +def add_notes(): |
| 120 | + user_id = session.get('user_id') |
| 121 | + if not user_id: |
| 122 | + return redirect(url_for('login')) |
| 123 | + |
| 124 | + user = User(user_id, f) |
| 125 | + user_data = user.get() |
| 126 | + form = NOTEForm() |
| 127 | + if request.method == 'POST': |
| 128 | + if form.validate_on_submit(): |
| 129 | + note = Note(user_id, f) |
| 130 | + note.title = form.title.data |
| 131 | + note.description = form.content.data |
| 132 | + note.create() |
| 133 | + note.note_idf = note.note_id |
| 134 | + note.update() |
| 135 | + return redirect(url_for('userpage')) |
| 136 | + return render_template("addnotes.html", form=form, user_data=user_data) |
| 137 | + |
| 138 | +@app.route("/delete/<note_id>", methods=['GET', 'POST']) |
| 139 | +def delete(note_id): |
| 140 | + user_id = session.get('user_id') |
| 141 | + ref = f.ref.child("notes").child(user_id) |
| 142 | + ref.child(note_id).delete() |
| 143 | + return redirect(url_for('userpage')) |
| 144 | + |
| 145 | +@app.route("/edit/<note_id>", methods=['GET', 'POST']) |
| 146 | +def edit(note_id): |
| 147 | + user_id = session.get('user_id') |
| 148 | + if not user_id: |
| 149 | + return redirect(url_for('login')) |
| 150 | + |
| 151 | + user = User(user_id, f) |
| 152 | + user_data = user.get() |
| 153 | + form = EditForm() |
| 154 | + note = Note(user_id, f) |
| 155 | + if request.method=="GET": |
| 156 | + notes = [note.get(note_id) for note_id in user.notes] |
| 157 | + selected_note_id=None |
| 158 | + for i in notes: |
| 159 | + if (note_id == i["note_idf"]): |
| 160 | + selected_note_id=i |
| 161 | + form.title.data=str(selected_note_id["title"]) |
| 162 | + form.content.data=str(selected_note_id["description"]) |
| 163 | + |
| 164 | + |
| 165 | + else: |
| 166 | + if form.validate_on_submit(): |
| 167 | + note = Note(user_id, f) |
| 168 | + updates = { |
| 169 | + "title": form.title.data, |
| 170 | + "description": form.content.data |
| 171 | + } |
| 172 | + f.ref.child("notes").child(user_id).child(note_id).update(updates) |
| 173 | + return redirect(url_for('userpage')) |
| 174 | + |
| 175 | + return render_template("edit-note.html",form=form, user_data=user_data,selected_note=selected_note_id) |
| 176 | +@app.route('/logout') |
| 177 | +def logout(): |
| 178 | + session.clear() |
| 179 | + return redirect(url_for('main')) |
| 180 | + |
| 181 | +@app.route("/summarize/<note_id>", methods=['GET', 'POST']) |
| 182 | +def summarize(note_id): |
| 183 | + chat=summariz() |
| 184 | + user_id = session.get('user_id') |
| 185 | + if not user_id: |
| 186 | + return redirect(url_for('login')) |
| 187 | + selected_note_id = None |
| 188 | + summarize_description = "" |
| 189 | + text = "" |
| 190 | + user = User(user_id, f) |
| 191 | + user_data = user.get() |
| 192 | + form = EditForm() |
| 193 | + note = Note(user_id, f) |
| 194 | + if request.method == "GET": |
| 195 | + notes = [note.get(note_id) for note_id in user.notes] |
| 196 | + selected_note_id = None |
| 197 | + for i in notes: |
| 198 | + if note_id == i["note_idf"]: |
| 199 | + selected_note_id = i |
| 200 | + form.title.data = str(selected_note_id["title"]) |
| 201 | + form.content.data = str(selected_note_id["description"]) |
| 202 | + text = str(selected_note_id["description"]) |
| 203 | + summarize_description = chat.model.generate_content(text + " : summarize.") |
| 204 | + elif request.method == "POST": |
| 205 | + action = request.form.get('action') |
| 206 | + if form.validate_on_submit(): |
| 207 | + if action == "save": |
| 208 | + note = Note(user_id, f) |
| 209 | + updates = { |
| 210 | + "title": form.title.data, |
| 211 | + "description": form.content.data |
| 212 | + } |
| 213 | + f.ref.child(user_id).child(note_id).update(updates) |
| 214 | + return redirect(url_for('userpage')) |
| 215 | + elif action == "cancel": |
| 216 | + return redirect(url_for('userpage')) |
| 217 | + else: # Summarize action |
| 218 | + note = Note(user_id, f) |
| 219 | + text = form.content.data |
| 220 | + summarize_description = chat.model.generate_content(text + " : verilen metni ozetle.") |
| 221 | + |
| 222 | + |
| 223 | + |
| 224 | + return render_template("sum.html", form=form, user_data=user_data, selected_note=selected_note_id, summarize_description=summarize_description.text) |
| 225 | + |
| 226 | +@app.route("/chat", methods=['GET', 'POST']) |
| 227 | +def chat(): |
| 228 | + user = User(session.get("user_id"), f) |
| 229 | + user_data = user.get() |
| 230 | + form = ChatForm() |
| 231 | + question = "" |
| 232 | + answer_description = "" # Initialize answer_description with a default value |
| 233 | + |
| 234 | + if request.method == "POST": |
| 235 | + question = form.BomAI.data |
| 236 | + if question: |
| 237 | + answer_description = ai.model.generate_content(question) |
| 238 | + |
| 239 | + return render_template("chat.html", form=form, answer_description=answer_description.text if answer_description else "",user_data=user_data) |
| 240 | + |
| 241 | +if __name__ == "__main__": |
| 242 | + app.run(debug=True, port=5500) |
0 commit comments