def mynotes(): form = NoteForm() noteID = request.args.get('noteID') #if creating a new note if form.validate_on_submit() and noteID is None: if 'submit' in request.form: note = Note(body=form.note.data, due_date=form.due_date.data, author=current_user, priority=form.priorityLevel.data, title=form.title.data) #changed db.session.add(note) db.session.commit() return redirect(url_for('mynotes')) notes = current_user.get_notes().all() #will run when click submit after editing an existing note noteData = Note.query.filter_by(id=noteID).first() if form.validate_on_submit() and noteID is not None: if 'submit' in request.form: noteData.body = form.note.data noteData.due_date = form.due_date.data noteData.priority = form.priorityLevel.data noteData.author = current_user noteData.title = form.title.data #changed db.session.commit() return redirect(url_for('mynotes')) #This puts the existing notes data into the note field for editing if noteID is not None: form.note.data = noteData.body form.due_date.data = noteData.due_date form.priorityLevel.data = noteData.priority form.title.data = noteData.title #display note to edit return render_template('mynotes.html', form=form, notes=notes)
def ajax_form(): form = NoteForm(request.form, csrf_enabled=False) if request.is_xhr and request.method == 'POST': if form.validate_on_submit(): note = form.new_note.data if request.files.get('image') is None: filename = None else: image = request.files.get('image') filename = '' if image and allowed_file(image.filename): filename = secure_filename(image.filename) image.save(os.path.join( app.config['UPLOAD_FOLDER'], filename )) add_new_note = Note(notes=note, image_path=filename) response_data = {} try: db.session.add(add_new_note) db.session.commit() response_data['msg'] = u'Your note is now live!' except: response_data['msg'] = u'Failed to add you note.' return Response( json.dumps(response_data), content_type='application/json' ) return render_template('ajax-form.html', form=form)
def edit(note_id): note = Note.query.filter_by(id=note_id).first() if isinstance(note, NoneType) or current_user.id != note.id_user: flash('This note does not exists !', 'warning') return redirect(url_for('index')) form = NoteForm(obj=note) if form.validate_on_submit(): edit_date = datetime.now() # update note in postgresql note.title = form.title.data note.content = form.content.data note.is_public = form.is_public.data note.edit_date = edit_date db.session.commit() # update note in redis redis_client.hmset( note_id, { 'title': form.title.data, 'content': form.content.data, 'is_public': str(form.is_public.data), 'edit_date': str(edit_date) }) flash('Your note has been updated !', 'success') return redirect(url_for('index')) return render_template('note_form.html', title='Update a note', form=form)
def submit_note(): form = NoteForm() if form.validate_on_submit(): flash('Message {} received! Redirecting ...'.format(form.message.data)) note = Note(name=form.name.data, message=form.message.data) db.session.add(note) db.session.commit() return redirect(url_for('index')) return render_template('note.html', title='Leave a note', form=form)
def add_note(): form = NoteForm() if form.validate_on_submit(): count = len(set(form.body.data.split())) note = Note(body=form.body.data, counter_unique_words=count) db.session.add(note) db.session.commit() return redirect(url_for('add_note')) return render_template('add_note.html', title='Add Note', form=form)
def transfer(transfer_id): if g.current_user: transfer = db.session.query(Transfer).join(User).filter(User.id == g.current_user.id, Transfer.id == int(transfer_id)).first() if transfer: form = NoteForm() if form.validate_on_submit(): note = Note(body=form.note.data, transfer=transfer) db.session.add(note) db.session.commit() return redirect(url_for('transfer', transfer_id=transfer.id)) return render_template("transfer.html", transfer=transfer, form=form)
def create_note(): form = NoteForm() if request.method == 'POST': if form.validate_on_submit(): new_note = dict(note=form.note.data) post_entry(DB_NOTE_URL, new_note) return redirect(url_for('notes')) return render_template('create_note.html', legend='New Note', form=form, DB_NOTE_URL=DB_NOTE_URL)
def list_notes(): form = NoteForm() if form.validate_on_submit(): add_new_note = Note(notes=form.new_note.data) db.session.add(add_new_note) db.session.commit() flash('Your note is now live!') return redirect('list-notes') notes = Note.query.all() return render_template('list_notes.html', form=form, notes=notes)
def create_note(): form = NoteForm() if form.validate_on_submit(): note = Notes(note=form.note.data) db.session.add(note) db.session.commit() return redirect(url_for('notes')) elif request.method == 'GET': return render_template('create_note.html', title='New Note', form=form, legend='New Note', date=date)
def edit_note(id): note = Notes.query.get_or_404(id) form = NoteForm() if form.validate_on_submit(): note.note = form.note.data db.session.commit() return redirect(url_for('notes')) elif request.method == 'GET': form.note.data = note.note return render_template('create_note.html', title='Edit Note', form=form, legend='Edit Note', date=date)
def edit_note(id): new_url = f"{DB_NOTE_URL}/{id}" note = requests.get(new_url) form = NoteForm() if request.method == 'POST': if form.validate_on_submit(): new_content = dict(note=form.note.data) update = requests.put(new_url, new_content) return redirect(url_for('notes')) elif request.method == 'GET': form.note.data = json.loads(note.text)['note'] return render_template('input.html', title='Edit Note', form=form, legend='Edit Note')
def post_note(): form = NoteForm() body = request.get_json() user = current_user form['csrf_token'].data = request.cookies['csrf_token'] if form.validate_on_submit(): note = Note() form.populate_obj(note) note.userId = user.id note.createdAt = datetime.date.today() note.updatedAt = datetime.date.today() tagIds = body['tags'] for tagId in tagIds: tag = Tag.query.get(tagId) note.tags.append(tag) db.session.add(note) db.session.commit() return note.to_dict()
def index(): """ Home page """ form = NoteForm() if form.validate_on_submit(): notes.append({ 'date': datetime.utcnow(), 'title': form.title.data, 'message': form.message.data }) return redirect(url_for('index')) else: print("No valid form") sorted_notes = sorted(notes, key=lambda k: k['date'], reverse=True) return render_template('index.html', form=form, notes=sorted_notes)
def index(): form = NoteForm() if form.validate_on_submit(): note = Note(content=form.content.data) db.session.add(note) db.session.commit() flash('Note saved') return redirect(url_for('index')) page = request.args.get('page', 1, type=int) notes = Note.query.order_by(Note.timestamp.desc()).filter_by( deleted=False).paginate(page, app.config['NOTES_PER_PAGE'], False) next_url = url_for('index', page=notes.next_num) \ if notes.has_next else None prev_url = url_for('index', page=notes.prev_num) \ if notes.has_prev else None return render_template('index.html', notes=notes.items, form=form, title='HomePage', next_url=next_url, prev_url=prev_url)
def index(): name = request.args.get("name", "stranger") note = request.args.get("note") noteBody = "" if note is not None and note.isdigit(): dispNote = Note.query.get(int(note)) if dispNote is not None: noteBody = dispNote.body nameDisp = name[0].upper() + name[1:] user = {"username": nameDisp} form = NoteForm() if form.validate_on_submit(): app.logger.info("Note submitted: {}".format(form.noteField.data)) notes = Note.query.filter_by(username=name.lower()) return render_template("index.html", user=user, form=form, notes=notes, noteBody=noteBody)
def new(): form = NoteForm() if form.validate_on_submit(): note = Note(title=form.title.data, content=form.content.data, creation_date=datetime.now(), edit_date=datetime.now(), author=current_user, is_public=form.is_public.data, uuid=uuid.uuid4()) # insert into postgresql db.session.add(note) db.session.flush() # get id of inserted note db.session.refresh(note) db.session.commit() # insert into redis redis_client.lpush('notes_id', note.id) redis_client.hmset(note.id, NoteConverter.note_to_dict(note)) flash('Your note has been created !', 'success') return redirect(url_for('index')) return render_template('note_form.html', title='New note', form=form)
def add_note(): form = NoteForm(request.form, csrf_enabled=False) if form.validate_on_submit(): note = form.new_note.data if request.files.get('image') is None: filename = None else: image = request.files['image'] filename = '' if image and allowed_file(image.filename): filename = secure_filename(image.filename) image.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) add_new_note = Note(notes=note, image_path=filename) db.session.add(add_new_note) db.session.commit() flash('Your post "%s" is now live!' % note, 'success') return redirect(url_for('list_notes')) if form.errors: flash(form.errors, 'danger') widget_link = request.url.replace("add-note", "widget") widget_link = widget_link.replace("http", "https") return render_template('add_note.html', form=form, widget_link=widget_link)