Skip to content

Commit a7dd4f3

Browse files
committed
Initial commit.
0 parents  commit a7dd4f3

File tree

12 files changed

+206
-0
lines changed

12 files changed

+206
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea/
2+
__pycache__/
3+
*.pyc
4+
my_database.db

Procfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: python main.py

main.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from datetime import datetime
2+
3+
from flask import Flask, render_template, request, redirect, url_for, session
4+
from passlib.hash import pbkdf2_sha256
5+
6+
from model import Task, User
7+
8+
app = Flask(__name__)
9+
10+
11+
@app.route('/all')
12+
def all_tasks():
13+
return render_template('all.jinja2', tasks=Task.select())
14+
15+
16+
@app.route('/incomplete', methods=['GET', 'POST'])
17+
def incomplete_tasks():
18+
if 'username' not in session:
19+
return redirect(url_for('login'))
20+
21+
if request.method == 'POST':
22+
user = User.select().where(User.name == session['username']).get()
23+
24+
Task.update(performed=datetime.now(), performed_by=user)\
25+
.where(Task.id == request.form['task_id'])\
26+
.execute()
27+
28+
return render_template('incomplete.jinja2', tasks=Task.select().where(Task.performed.is_null()))
29+
30+
31+
@app.route('/create', methods=['GET', 'POST'])
32+
def create():
33+
if 'username' not in session:
34+
return redirect(url_for('login'))
35+
36+
if request.method == 'POST':
37+
task = Task(name=request.form['name'])
38+
task.save()
39+
40+
return redirect(url_for('all_tasks'))
41+
else:
42+
return render_template('create.jinja2')
43+
44+
45+
@app.route('/login', methods=['GET', 'POST'])
46+
def login():
47+
if request.method == 'POST':
48+
user = User.select().where(User.name == request.form['name']).get()
49+
50+
if user and pbkdf2_sha256.verify(request.form['password'], user.password):
51+
session['username'] = request.form['name']
52+
return redirect(url_for('all_tasks'))
53+
54+
return render_template('login.jinja2', error="Incorrect username or password.")
55+
56+
else:
57+
return render_template('login.jinja2')
58+
59+
60+
@app.route('/logout')
61+
def logout():
62+
session.pop('username', None)
63+
return redirect(url_for('all_tasks'))
64+
65+
app.secret_key = b'\x9d\xb1u\x08%\xe0\xd0p\x9bEL\xf8JC\xa3\xf4J(hAh\xa4\xcdw\x12S*,u\xec\xb8\xb8'
66+
67+
68+
if __name__ == "__main__":
69+
app.run()

model.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from peewee import Model, CharField, DateTimeField, ForeignKeyField
2+
from playhouse.sqlite_ext import SqliteExtDatabase
3+
4+
db = SqliteExtDatabase('my_database.db')
5+
6+
7+
class User(Model):
8+
name = CharField(max_length=255, unique=True)
9+
password = CharField(max_length=255)
10+
11+
class Meta:
12+
database = db
13+
14+
15+
class Task(Model):
16+
name = CharField(max_length=255)
17+
performed = DateTimeField(null=True)
18+
performed_by = ForeignKeyField(rel_model=User, null=True)
19+
20+
class Meta:
21+
database = db
22+
23+
24+
# class Context(BaseModel):
25+
# name = CharField(max_length=255)
26+
#
27+
#
28+
# class ContextTask(BaseModel):
29+
# context_id = IntegerField() # foreign key into context
30+
# task_id = IntegerField() # foreign key into task
31+
#
32+
# class Meta:
33+
# primary_key = CompositeKey(
34+
# 'context_id', 'task_id'
35+
# )

requirements.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
click==6.7
2+
Flask==0.12.2
3+
itsdangerous==0.24
4+
Jinja2==2.9.6
5+
MarkupSafe==1.0
6+
passlib==1.7.1
7+
peewee==2.10.1
8+
Werkzeug==0.12.2

setup.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
Scripts to run to set up our database
3+
"""
4+
5+
from model import db, User, Task
6+
from passlib.hash import pbkdf2_sha256
7+
8+
9+
# Create the database tables for our model
10+
db.connect()
11+
db.create_tables([User, Task])
12+
13+
# We need a user to log in as! Create it here.
14+
first_user_name = "admin"
15+
first_user_password = "password"
16+
17+
first_user = User(name=first_user_name, password=pbkdf2_sha256.hash(first_user_password))
18+
first_user.save()

templates/all.jinja2

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{% extends 'base.jinja2' %}
2+
3+
{% block subtitle %}All the things{% endblock subtitle %}
4+
5+
{% block content %}
6+
<ul>
7+
{% for task in tasks %}
8+
<li>
9+
{{ task.name }} {% if task.performed %}Done on {{ task.performed }} by {{ task.performed_by.name }}{% else %} Undone {% endif %}
10+
</li>
11+
{% endfor %}
12+
</ul>
13+
{% endblock content %}

templates/base.jinja2

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<html>
2+
<head>
3+
<title>My Todo List</title>
4+
</head>
5+
<body>
6+
<nav>
7+
<div style="float:left">
8+
<a href="/all">All tasks</a>
9+
<a href="/incomplete">Do incomplete tasks</a>
10+
<a href="/create">Create a task</a>
11+
</div>
12+
<div style="float:right">
13+
{% if session.username %}{{ session.username }} (<a href="/logout">logout</a>)
14+
{% else %}<a href="/login">login</a>
15+
{% endif %}
16+
</div>
17+
</nav>
18+
<br style="clear:both">
19+
<h1>My Todo List</h1>
20+
<h2>{% block subtitle %} {% endblock subtitle %}</h2>
21+
{% block content %} {% endblock content %}
22+
</body>
23+
</html>

templates/create.jinja2

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{% extends 'base.jinja2' %}
2+
3+
{% block subtitle %}Add a task{% endblock subtitle %}
4+
5+
{% block content %}
6+
<form method="POST">
7+
<label for="name-input">Name:</label><input id="name-input" type="text" name="name">
8+
<input type="submit" value="Create Task">
9+
</form>
10+
{% endblock content %}

templates/incomplete.jinja2

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{% extends 'base.jinja2' %}
2+
3+
{% block subtitle %}Incomplete Tasks{% endblock subtitle %}
4+
5+
{% block content %}
6+
<ul>
7+
{% for task in tasks %}
8+
<li>
9+
{{ task.name }}: <form method="POST"><input type="hidden" name="task_id" value="{{ task.id }}"><input type="submit" value="Mark done"></form>
10+
</li>
11+
{% endfor %}
12+
</ul>
13+
{% endblock content %}

0 commit comments

Comments
 (0)