|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +from datetime import datetime |
| 4 | +from flask import Flask, request, render_template, flash, url_for, \ |
| 5 | + redirect |
| 6 | +from flaskext import wtf |
| 7 | +from flaskext.wtf import validators |
| 8 | + |
| 9 | +from forms import NotesForm |
| 10 | + |
| 11 | +app = Flask(__name__) |
| 12 | +app.config.from_pyfile('settings.py') |
| 13 | +from flask.ext.sqlalchemy import SQLAlchemy |
| 14 | +db = SQLAlchemy(app) |
| 15 | + |
| 16 | + |
| 17 | +class Notes(db.Model): |
| 18 | + |
| 19 | + id = db.Column(db.Integer, primary_key=True) |
| 20 | + title = db.Column(db.String(80)) |
| 21 | + subject = db.Column(db.String(80)) |
| 22 | + author = db.Column(db.String(80)) |
| 23 | + description = db.Column(db.Text) |
| 24 | + pub_date = db.Column(db.Date) |
| 25 | + |
| 26 | + def __init__( |
| 27 | + self, |
| 28 | + title, |
| 29 | + author, |
| 30 | + description, |
| 31 | + subject, |
| 32 | + ): |
| 33 | + self.title = title |
| 34 | + self.author = author |
| 35 | + self.description = description |
| 36 | + self.subject = subject |
| 37 | + self.pub_date = datetime.now() |
| 38 | + |
| 39 | + |
| 40 | +@app.route('/') |
| 41 | +def redirect_to_home(): |
| 42 | + return redirect(url_for('list_notes')) |
| 43 | + |
| 44 | + |
| 45 | +@app.route('/notes/') |
| 46 | +def list_notes(): |
| 47 | + notes = Notes.query.all() |
| 48 | + return render_template('index.html', notes=notes) |
| 49 | + |
| 50 | + |
| 51 | +@app.route('/notes/create/', methods=['GET', 'POST']) |
| 52 | +def create(): |
| 53 | + form = NotesForm() |
| 54 | + if request.method == 'POST': |
| 55 | + if form.validate_on_submit(): |
| 56 | + note = Notes(form.title.data, form.author.data, |
| 57 | + form.description.data, form.subject.data) |
| 58 | + db.session.add(note) |
| 59 | + db.session.commit() |
| 60 | + flash('Note saved on database.') |
| 61 | + return redirect(url_for('list_notes')) |
| 62 | + return render_template('note.html', form=form) |
| 63 | + |
| 64 | + |
| 65 | +@app.route('/notes/delete/<int:note_id>', methods=['GET']) |
| 66 | +def delete(note_id): |
| 67 | + note = Notes.query.get_or_404(note_id) |
| 68 | + db.session.delete(note) |
| 69 | + db.session.commit() |
| 70 | + flash('Note Deleted') |
| 71 | + return redirect(url_for('list_notes')) |
| 72 | + |
| 73 | + |
| 74 | +@app.route('/notes/edit/<int:note_id>', methods=['GET', 'POST']) |
| 75 | +def edit(note_id): |
| 76 | + note = Notes.query.get_or_404(note_id) |
| 77 | + form = NotesForm(obj=note) |
| 78 | + if request.method == 'POST': |
| 79 | + print request.form |
| 80 | + if form.validate_on_submit(): |
| 81 | + print request.form['title'] |
| 82 | + note.title = request.form['title'] |
| 83 | + note.author = request.form['author'] |
| 84 | + note.description = request.form['description'] |
| 85 | + note.subject = request.form['subject'] |
| 86 | + db.session.add(note) |
| 87 | + db.session.commit() |
| 88 | + return redirect(url_for('list_notes')) |
| 89 | + else: |
| 90 | + return render_template('note.html', form=form) |
0 commit comments