def new(): pcfg = {"title": "New note"} form = NoteForm() if form.validate_on_submit(): a = Note() a.title = form.title.data a.cat = form.cat.data a.note = form.note.data a.user_id = current_user.id db.session.add(a) db.session.commit() flash("Success updating note: {0}".format(a.title), "success") return redirect(url_for("bp_notes.notes")) logbooks = (db.session.query(Logbook.id, Logbook.name, func.count( Log.id)).join(Log).filter(Logbook.user_id == current_user.id).group_by( Logbook.id).all()) return render_template("notes/new.jinja2", pcfg=pcfg, form=form, logbooks=logbooks)
def handle_add_notes(username): """ Display form to add notes for the logged in user. Add new note and redirect to user detail. """ if session.get('user_id') != username: flash("You are not authorized to add notes to this user!") return redirect('/') form = NoteForm() if form.validate_on_submit(): new_note = Note() form.populate_obj(new_note) new_note.owner = username db.session.add(new_note) db.session.commit() flash("Note has been added") return redirect(f"/users/{username}") else: return render_template("add_notes.html", form=form)
def add_note(): ''' add note form ''' form = NoteForm() if form.validate_on_submit(): save(form) return redirect(url_for('notes.show_notes')) return render_template('add_note.html', form=form)
def create_note(): form = NoteForm() if form.validate_on_submit(): flash('Success') return redirect(url_for('home')) return render_template('create_note.html', title='Create note', form=form)
def edit_note(id): """ Edit a role """ add_note = False note = Note.query.get_or_404(id) form = NoteForm(obj=note) if form.validate_on_submit(): note.title = form.title.data note.body = form.body.data db.session.add(note) db.session.commit() flash('You have successfully edited the note.') # redirect to the roles page return redirect(url_for('user.list_notes')) form.body.data = note.body form.title.data = note.title return render_template('user/note.html', add_role=add_note, form=form, title="Edit Note")
def add_note(): """ Add a role to the database """ add_note = True user = current_user form = NoteForm() if form.validate_on_submit(): note = Note(title=form.title.data, body=form.body.data) try: # add role to the database user.notes.append(note) db.session.add(note) db.session.commit() flash('You have successfully added a new note to the user.') except: # in case role name already exists flash('Error: .') # redirect to the roles page return redirect(url_for('user.list_notes')) # load role template return render_template('user/note.html', add_role=add_note, form=form, title='Add Note')
def add_note(username): """ If GET, displays a form to add a new note If POST, processes new note and redirects to user's info page """ #fail fast #redirect and flash first user = User.query.get_or_404(username) if "username" not in session or username != session["username"]: flash("You cannot add a note from this user") return redirect("/") form = NoteForm() if form.validate_on_submit(): title = form.title.data content = form.content.data new_note = Note(title=title, content=content, owner=user.username) db.session.add(new_note) db.session.commit() return redirect(f"/users/{user.username}") else: return render_template("new_note_form.html", form=form)
def edit(note_id): pcfg = {"title": "Edit my notes"} a = Note.query.filter(Note.user_id == current_user.id, Note.id == note_id).first() if not a: flash("Note not found", "error") return redirect(url_for("bp_notes.notes")) form = NoteForm(request.form, obj=a) if form.validate_on_submit(): a.title = form.title.data a.cat = form.cat.data a.note = form.note.data db.session.commit() flash("Success saving note: {0}".format(a.title), "success") return redirect(url_for("bp_notes.notes")) logbooks = Logbook.query.filter(Logbook.user_id == current_user.id).all() return render_template("notes/edit.jinja2", pcfg=pcfg, form=form, note=a, note_id=note_id, logbooks=logbooks)
def update_notes_form(note_id): """ GET: Display a form to update notes. POST: update note and redirect to /users/<username> """ note = Note.query.get_or_404(note_id) username = note.user.username form = NoteForm(obj=note) if CURRENT_USER not in session: flash("You are not authorized to view this page.") return redirect("/login") elif username != session[CURRENT_USER]: flash("You are not authorized to access another user's page.") logged_in_user = session[CURRENT_USER] return redirect(f"/users/{logged_in_user}") if form.validate_on_submit(): note.title = form.title.data note.content = form.content.data db.session.commit() return redirect(f'/users/{username}') else: return render_template("update_note.html", form=form, note=note)
def add_notes_form(username): """ GET: Display a form to add notes. POST: Add a new note and redirect to /users/<username> """ if CURRENT_USER not in session: flash("You are not authorized to view this page.") return redirect("/login") elif username != session[CURRENT_USER]: flash("You are not authorized to access another user's page.") logged_in_user = session[CURRENT_USER] return redirect(f"/users/{logged_in_user}") form = NoteForm() user = User.query.get_or_404(username) if form.validate_on_submit(): title = form.title.data content = form.content.data note = Note(title=title, content=content) user.notes.append(note) db.session.commit() return redirect(f'/users/{username}') else: return render_template("new_note.html", user=user, form=form)
def edit_note(id): """ Edit a note """ add_note = False note = Note.query.get_or_404(id) form = NoteForm(obj=note) if form.validate_on_submit(): note.user_id = current_user.id note.text = form.noteText.data db.session.commit() flash('You have successfully edited the note.') # redirect to the notes page return redirect(url_for('home.list_notes')) form.noteText.data = note.text # form.userEmail.data = note.user_id return render_template('home/notes/note.html', action="Edit", add_note=add_note, form=form, note=note, title="Edit Note")
def add_note(): """ Add a note to the database """ add_note = True form = NoteForm() if form.validate_on_submit(): note = Note(user_id=current_user.id, text=form.noteText.data) try: # add note to the database db.session.add(note) db.session.commit() flash('You have successfully added a new note.') except: flash('Note cannot be added.') db.session.rollback() # redirect to notes page return redirect(url_for('home.list_notes')) # load note template return render_template('home/notes/note.html', action="Add", add_note=add_note, form=form, title="Add Note")
def addnote(language): ''' Allows a logged in user add a note for a language. ''' form = NoteForm() document_language = mongo.db.languages.find_one_or_404( {"language": language}, {"topics": 1}) topics = document_language["topics"] form.topic.choices = [("", "-select-")] + [(topic, topic) for topic in topics] if form.validate_on_submit(): mongo.db.notes.insert_one({ "user_id": ObjectId(current_user.id), "language": language, "topic": form.topic.data, "note_name": form.name.data, "content": form.content.data }) flash("Perfect - note added!") return redirect(url_for("notes", language=language)) elif request.method == "GET": pass else: flash("Oops - check fields!", "error") return render_template("addnote.html", form=form, language=language, sample1=session["sample1"], sample2=session["sample2"], sample3=session["sample3"], sample4=session["sample4"], quote=session["quote"])
def xss_stor(): notes = get_notes_xss_stor() nf = NoteForm() if nf.validate_on_submit(): add_note_xss_stor(nf.username.data, nf.note.data) flash('Note was added!') return redirect(url_for('xss_stor')) return render_template('xss_stor.html', form=nf, notes=notes)
def add_note(): form = NoteForm() if form.validate_on_submit(): user_id = current_user.id create_note(user_id, form.note.data) flash("You have successfully added a new note.") return redirect(url_for("notes")) return render_template("updateNote.html", add_note=True, form=form, title="Add Note")
def update_note(note_id): """ Update note by noteid. """ note = Note.query.get_or_404(note_id) form = NoteForm(obj=note) if form.validate_on_submit(): note.title = form.title.data note.content = form.content.data db.session.commit() return redirect(f"/users/{note.owner}") return render_template("edit_note.html", form=form, note=note)
def add_note(): note_form = NoteForm() if note_form.validate_on_submit(): title = note_form.title.data text = note_form.text.data file = note_form.file.data with sqlite3.connect(DB_NAME) as con: cur = con.cursor() cur.execute(f'''insert into Notes(title,text) values('{title}','{text}')''') con.commit() return render_template('add.html',form=note_form)
def edit_note(note_id): note = Note.get_by_id(note_id) form = NoteForm(obj=note) if request.method == 'POST': if form.validate_on_submit(): note.title = form.data.get('note_title') note.body = form.data.get('note_body') note.slug = slugify(note.title) note.put() flash(u'Note %s saved.' % note_id, 'success') return redirect(url_for('list_notes')) return render_template('edit_note.html', note=note, form=form)
def edit_note(note_id: int): user_id = current_user.id cur_note = get_note(user_id, note_id) form = NoteForm(obj=cur_note) if form.validate_on_submit(): update_note(user_id, note_id, form.note.data) flash("You have successfully updated your note.") return redirect(url_for("notes")) return render_template("updateNote.html", add_note=False, form=form, title="Edit Note")
def submit_notes(category, drug): form = NoteForm() if category == "drugclass": med = Drugclass.query.filter_by(name=drug).first() if med is None: return render_template('error_page.html', load_user=load_user) else: current_note = Note.query.filter_by(drugclass_id=med.id, user_id=current_user.get_id()).first() if form.validate_on_submit(): if current_note is None: new_note = Note(content=form.content.data, user_id=current_user.get_id(), drugclass_id=med.id) db.session.add(new_note) db.session.commit() flash('Note Added') else: current_note.content = form.content.data current_note.date_posted = datetime.datetime.now() db.session.commit() flash('Note Updated') return render_template('submit.html', form=form, medclass="drugclass", med=med.name, current_note = current_note, load_user=load_user) elif category == "medications": med = Drug.query.filter_by(name=drug).first() if med is None: return render_template('error_page.html', load_user=load_user) else: current_note = Note.query.filter_by(drug_id=med.id, user_id=current_user.get_id()).first() if form.validate_on_submit(): if current_note is None: new_note = Note(content=form.content.data, user_id=current_user.get_id(), drug_id=med.id) db.session.add(new_note) db.session.commit() flash('Note Added') else: current_note.content = form.content.data current_note.date_posted = datetime.datetime.now() db.session.commit() flash('Note Updated') return render_template('submit.html', form=form, medclass="medications", med=med.name, current_note = current_note, load_user=load_user) else: return render_template('error_page.html', load_user=load_user)
def add_note(username): """ Add a note for the logged in user redirect to users/<username> """ form = NoteForm() user = User.query.get_or_404(username) if form.validate_on_submit(): new_note = Note(owner=username, title=form.title.data, content=form.content.data) user.notes.append(new_note) db.session.commit() return redirect(f"/users/{username}") return render_template("add_note.html", form=form, user=user)
def add_notes(username): user = User.query.get(username) form = NoteForm() if form.validate_on_submit(): title = form.title.data content = form.content.data note = Note(title=title, content=content, owner=username) db.session.add(note) db.session.commit() return redirect(f"/users/{username}") return render_template("notes.html", form=form)
def list_notes(): notes = Note.query() # Convert body of each note to marked-up markdown for note in notes: note.body = Markup(markdown.markdown(note.body)) form = NoteForm() if form.validate_on_submit(): note = Note(title=form.note_title.data, slug=slugify(form.note_title.data), body=form.note_body.data, author=users.get_current_user()) try: note.put() note_id = note.key.id() flash(u'Note %s saved.' % note_id, 'success') return redirect(url_for('list_notes')) except CapabilityDisabledError: flash(u'Datastore is in read-only mode.', 'info') return redirect(url_for('list_notes')) return render_template('dynamic_notes.html', notes=notes, form=form)
def edit_note(number): """Edit a note.""" note = get_note_by_number(number) if note is None: return render_template('404.html'), 404 form = NoteForm( title=note.title, content=note.content, tags=note.tags, ) if request.method == 'POST': if form.validate_on_submit(): note.title = form.title.data note.content = form.content.data note.tags = form.tags.data note.save() return redirect(note.get_absolute_url()) action_url = url_for('edit_note', number=number) return render_template('add_note.html', form=form, action_url=action_url)
def add_note(username): """Display a form to add notes. Add a new note and redirect to /users/<username>""" print("add_note()") user = User.query.get_or_404(username) note_form = NoteForm() if note_form.validate_on_submit(): title = note_form.title.data content = note_form.content.data new_note = Note(title=title, content=content, owner=user.username) db.session.add(new_note) db.session.commit() return redirect(f'/users/{username}') else: return render_template('note_form.html', form=note_form)
def editnote(language, noteid): ''' Allows a logged in user edit a note if they own that note. ''' form = NoteForm() note = mongo.db.notes.find_one_or_404({ "_id": ObjectId(noteid), "user_id": ObjectId(current_user.id) }) document_language = mongo.db.languages.find_one_or_404( {"language": language}, {"topics": 1}) topics = document_language["topics"] form.topic.choices = [("", "-select-")] + [(topic, topic) for topic in topics] if form.validate_on_submit(): mongo.db.notes.update_one({"_id": ObjectId(noteid)}, { "$set": { "topic": form.topic.data, "note_name": form.name.data, "content": form.content.data } }) flash("Perfect - note updated!") return redirect(url_for("notes", language=language)) elif request.method == "GET": form.topic.data = note["topic"] form.name.data = note["note_name"] form.content.data = note["content"] else: flash("Oops - check fields!", "error") return render_template("editnote.html", form=form, note=note, language=language, sample1=session["sample1"], sample2=session["sample2"], sample3=session["sample3"], sample4=session["sample4"], quote=session["quote"])
def handle_update_note(note_id): """ Display form to update notes for the logged in user. Update note and redirect to user detail. """ note = Note.query.get_or_404(note_id) if session.get('user_id') != note.owner: flash("You are not authorized to update this note!") return redirect('/') form = NoteForm(obj=note) if form.validate_on_submit(): form.populate_obj(note) db.session.commit() flash("Note has been updated!") return redirect(f"/users/{note.owner}") else: return render_template("update_notes.html", form=form)
def update_note(note_id): note = Note.query.get_or_404(note_id) form = NoteForm(obj=note) if "username" not in session: flash("You must be logged in to update note!") return redirect("/") if not (session["username"] == note.user.username): flash("You're not authorized") return redirect("/") if form.validate_on_submit(): title = form.title.data content = form.content.data note.title = title note.content = content db.session.commit() return redirect(f"/users/{note.user.username}") return render_template("/update_note.html", form=form)
def view_note(note_id): ''' view individual note ''' note = Note.query.filter_by(id=note_id, uid=current_user.id).first() note_tags = note.tags tags_string = ', '.join([tag.value for tag in note_tags]) if not note: abort(404) form = NoteForm(obj=note) # hack to populate tag field # tags are a list of objects need to hack string into form if request.method == 'GET': form.tags.data = tags_string if form.validate_on_submit(): save(form, note_id=note_id) flash('Note was successfully saved') return redirect(url_for('notes.show_notes')) return render_template('view_note.html', note=note, note_tags=note_tags, form=form)
def profile(slug): #variables form = NoteForm(request.form) notes = Note.objects.all() for note in current_user.notes: for t in note.tags: tagJ = "" join = tagJ.join(note.tags) print join if request.method == 'POST': try: form = NoteForm(request.form) if form.validate() == False: flash("Something went wrong.", 'danger') return render_template('profile.html', form=form, search_form=SearchForm(request.form), delete_quote=deleteQuoteForm( request.form)) if form.validate_on_submit(): form_urlLink = form.URLLink.data if form.URLLink.data != '': if not URl_Regex.match(form_urlLink): flash('Invalid URL adress', 'danger') return render_template( "profile.html", form=form, search_form=SearchForm(request.form), regex="Invalid URL adress", delete_quote=deleteQuoteForm(request.form)) tags = form.tags.data tagList = tags.split(",") note = Note(content=form.content.data, tags=tagList, URLLink=form.URLLink.data) note.save() current_user.notes.append(note) current_user.save() flash('Quote saved successfully.', 'success') return render_template('profile.html', form=form, search_form=SearchForm(request.form), delete_quote=deleteQuoteForm( request.form)) except ValidationError: flash('UPPPS! Tags or Url was wrong', 'danger') return render_template('profile.html', form=form, search_form=SearchForm(request.form), delete_quote=deleteQuoteForm(request.form)) return render_template("profile.html", title=current_user.name + "'s Quotes", form=form, search_form=SearchForm(request.form), delete_quote=deleteQuoteForm(request.form))