Skip to content

Commit d03c4c4

Browse files
author
fRui Apps
committed
Added flask framework
1 parent 7f3da96 commit d03c4c4

13 files changed

+224
-0
lines changed

flasknotes/README

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
This is a simple flask application, that performs CRUD operations.
2+
3+
1. make a virtualenv
4+
2. ``pip install -r requirements.txt``
5+
3. ``python manage.py initdb``
6+
4. ``python manage.py runserver``
7+
8+
the documentation is available at blog.fruiapps.com

flasknotes/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

flasknotes/forms.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
from flaskext import wtf
4+
from flaskext.wtf import validators
5+
6+
7+
class NotesForm(wtf.Form):
8+
9+
subject_list = [('0', 'English'), ('1', 'Philosophy'), ('2',
10+
'Theology'), ('3', 'Mathematics')]
11+
12+
title = wtf.StringField('Title', validators=[validators.Required()])
13+
author = wtf.StringField('Author',
14+
validators=[validators.Required()])
15+
description = wtf.TextAreaField('Description',
16+
validators=[validators.Required()])
17+
subject = wtf.SelectField(choices=subject_list)

flasknotes/manage.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from flask.ext.script import Manager
2+
from notes import app, db
3+
4+
manager = Manager(app)
5+
6+
7+
@manager.command
8+
def initdb():
9+
"""Creates all database tables."""
10+
db.create_all()
11+
12+
13+
@manager.command
14+
def dropdb():
15+
"""Drops all database tables."""
16+
db.drop_all()
17+
18+
19+
if __name__ == '__main__':
20+
manager.run()

flasknotes/models.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-

flasknotes/notes.py

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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)

flasknotes/requirements.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Flask
2+
Flask-SQLAlchemy
3+
Flask-Script
4+
Flask-WTF
5+

flasknotes/settings.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
DEBUG=False
2+
SQLALCHEMY_DATABASE_URI='sqlite:///notes_.db'
3+
SECRET_KEY='development-key'
4+
CSRF_ENABLED=True
5+
6+
7+

flasknotes/static/style.css

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.sticky {
2+
-webkit-box-shadow: #DDD 0px 1px 2px;
3+
position: relative;
4+
background-color: #F4F39E;
5+
border-color: #DEE184;
6+
text-align: center;
7+
margin: 2.5em 0px;
8+
padding: 1.5em 1em;
9+
-webkit-box-shadow: 0px 1px 3px rgba(0,0,0,0.25);
10+
-moz-box-shadow: 0px 1px 3px rgba(0,0,0,0.25);
11+
box-shadow: 0px 1px 3px rgba(0,0,0,0.25);
12+
font-family: Chalkboard, 'Comic Sans';
13+
}

flasknotes/templates/base.html

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!doctype html>
2+
<head>
3+
<title>Notes Application</title>
4+
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
5+
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
6+
</head>
7+
<body>
8+
{% for message in get_flashed_messages() %}
9+
<p class=flash>{{ message }}</p>
10+
{% endfor %}
11+
{% block content %}
12+
{% endblock content %}
13+
</body>
14+
</html>
15+

flasknotes/templates/index.html

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{% extends "base.html" %}
2+
{% block content %}
3+
<h1>Hello World! </h1>
4+
<p>You can <a href="{{ url_for('create') }}">create notes</a>, delete notes and edit created notes.
5+
Thats all!</p>
6+
7+
{% for note in notes %}
8+
<div class="sticky">
9+
<b> {{ note.title }} </b>
10+
<p>{{ note.author }}</p>
11+
<p>{{ note.description }}</p>
12+
<a href="{{ url_for('edit', note_id=note.id) }}">Edit, this!</a>
13+
<a href="{{ url_for('delete', note_id=note.id) }}">Delete, this!</a>
14+
</div>
15+
{% endfor %}
16+
{% endblock content %}

flasknotes/templates/note.html

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{% extends "base.html" %}
2+
{% block content %}
3+
4+
<form action="" method="POST" accept-charset="utf-8">
5+
6+
<table>
7+
{% for field in form %}
8+
9+
<tr>
10+
<th>{{ field.label }}</th>
11+
<td>{{ field }}</td>
12+
{% if field.errors %}
13+
<td>
14+
<ul class=errors>
15+
{% for error in field.errors %}
16+
<li>{{ error }}</li>
17+
{% endfor %}
18+
</ul>
19+
{% endif %}
20+
</td>
21+
</tr>
22+
{% endfor %}
23+
</table>
24+
25+
26+
<p><input type="submit" value="Save post"/></p>
27+
</form>
28+
29+
{% endblock content %}

tgnotes/README

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a simple tutorial for turbogears mvc application.

0 commit comments

Comments
 (0)