Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions week-0/day-3/my_todo_app/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export FLASK_APP=todo_app
export FLASK_ENV=development
flask init-db
flask run
65 changes: 28 additions & 37 deletions week-0/day-3/my_todo_app/todo_app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,43 @@
import os

from flask import Flask
from flask import request


def create_app(test_config=None):
# create and configure the app
app = Flask(__name__, instance_relative_config=True)

# ensure the instance folder exists
app.config.from_mapping(
SECRET_KEY='dev',
DATABASE=os.path.join(app.instance_path,'todo_app.sqlite')
)


if test_config is None:
# load the instance config, if it exists, when not testing
app.config.from_pyfile('config.py', silent=True)
else:
# load the test config if passed in
app.config.from_mapping(test_config)


try:
os.makedirs(app.instance_path)
except OSError:
pass
# a simple page that list my todos
@app.route('/shivang')
def shivang():
return ('Wake Up' + '<br/>' +
'Drink Coffee' + '<br/>' +
'Read Non-fiction Novel' + '<br/>'
)

def todo_view(todos):
the_view = 'List of my todos:' + '<br/>'
for todo in todos:
the_view += ( todo + '<br/>' )

the_view += '---- LIST ENDS HERE ---'
return the_view

def get_todos_by_name(name):
if name == 'depo':
return ['Go for run', 'Listen Rock Music']
elif name == 'shivang':
return ['Read book', 'Play Fifa', 'Drink Coffee']
elif name == 'raj':
return ['Study', 'Brush']
elif name == 'sanket':
return ['Sleep', 'Code']
elif name == 'aagam':
return ['play cricket', 'have tea']
else:
return []
from . import db
db.init_app(app)

from . import auth
app.register_blueprint(auth.bp)

# http://127.0.0.1:5000/todos?name=duster
@app.route('/todos')
def todos():
name = request.args.get('name')
print('---------')
print(name)
print('---------')

person_todo_list = get_todos_by_name(name)
return todo_view(person_todo_list)
from . import post
app.register_blueprint(post.bp)
app.add_url_rule('/', endpoint='index')

return app

Binary file removed week-0/day-3/my_todo_app/todo_app/__init__.pyc
Binary file not shown.
92 changes: 92 additions & 0 deletions week-0/day-3/my_todo_app/todo_app/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import functools

from flask import (
Blueprint, flash, g, redirect, render_template, request, session, url_for
)
from werkzeug.security import check_password_hash, generate_password_hash

from todo_app.db import get_db
from flask import current_app

bp = Blueprint('auth', __name__, url_prefix='/auth')

@bp.route('/register',methods=('GET','POST'))
def register():
if request.method=='POST':
username=request.form['username']
password=request.form['password']
db=get_db()
error=None

if not username:
error='Username is required'
elif not password:
error='Password is required'
elif db.execute(
'SELECT id FROM user WHERE username=?',(username,)
).fetchone() is not None:
error='User {} is already registered'.format(username)

if error is None:
db.execute(
'INSERT INTO user(username,password) VALUES (?,?)',
(username,generate_password_hash(password))
)
db.commit()
return redirect(url_for('auth.login'))

flash(error)
return render_template('auth/register.html')

@bp.route('/login',methods=('GET','POST'))
def login():
if request.method=='POST':
username=request.form['username']
password=request.form['password']
db=get_db()
error=None
user=db.execute(
'SELECT * FROM user WHERE username=?',(username,)
).fetchone()

if user is None:
error='Incorrect Username'
elif not check_password_hash(user['password'],password):
error='Incorrect password'

if error is None:
session.clear()
session['user_id']=user['id']
return redirect(url_for('index'))

flash(error)
return render_template('auth/login.html')

@bp.route('/logout')
def logout():
session.clear()
return redirect(url_for('auth.login'))

def login_required(view):
@functools.wraps(view)
def wrapped_view(**kwargs):
if g.user is None:
return redirect(url_for('auth.login'))

return view(**kwargs)

return wrapped_view

@bp.before_app_request
def load_logged_in_user():
user_id=session.get('user_id')

if user_id is None:
g.user=None
else:
g.user=get_db().execute(
'SELECT * FROM user WHERE id=?',(user_id,)
).fetchone()



38 changes: 38 additions & 0 deletions week-0/day-3/my_todo_app/todo_app/db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import sqlite3

import click
from flask import current_app,g
from flask.cli import with_appcontext

def get_db():
if 'db' not in g:
g.db=sqlite3.connect(
current_app.config['DATABASE'],
detect_types=sqlite3.PARSE_DECLTYPES
)
g.db.row_factory=sqlite3.Row
return g.db;

def init_app(app):
app.teardown_appcontext(close_db)
app.cli.add_command(init_db_command)

def init_db():
db = get_db()

with current_app.open_resource('schema.sql') as f:
db.executescript(f.read().decode('utf8'))


@click.command('init-db')
@with_appcontext
def init_db_command():
"""Clear the existing data and create new tables."""
init_db()
click.echo('Initialized the database.')

def close_db(e=None):
db=g.pop('db',None)

if db is not None:
db.close()
102 changes: 102 additions & 0 deletions week-0/day-3/my_todo_app/todo_app/post.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
from flask import(
Blueprint, flash, g, redirect, render_template,request,url_for,session
)
from werkzeug.exceptions import abort

from todo_app.auth import login_required
from todo_app.db import get_db
from flask import session

bp=Blueprint('post',__name__)

@bp.route('/')
def index():
user_id=session.get('user_id')
print(user_id)
if user_id is None:
return redirect('auth/login')

db=get_db()
posts = db.execute(
'SELECT * FROM post WHERE author_id=?',(user_id,)

# 'SELECT p.id, title, body, created, author_id'
# 'FROM post p WHERE p.author_id=?', (user_id,)
).fetchall()
return render_template('posts/index.html',posts=posts)


@bp.route('/create',methods=('GET','POST'))
@login_required
def create():
if request.method=='POST':
title=request.form['title']
body=request.form['body']
error=None

if not title:
error='Title required'
if error is not None:
flash(error)
else:
db=get_db()
db.execute(
'INSERT INTO post (title, body, author_id)'
' VALUES (?, ?, ?)',
(title, body, g.user['id'])
)
db.commit()
return redirect(url_for('post.index'))
return render_template('posts/create.html')

def get_post(id,check_author=True):
post=get_db().execute(
'SELECT p.id, title, body, created, author_id, username'
' FROM post p JOIN user u ON p.author_id = u.id'
' WHERE p.id = ?',
(id,)
).fetchone()

if post is None:
abort(404,"Post id {0} doesn't exist.".format(id))

if check_author and post['author_id']!=g.user['id']:
abort(403)

return post

@bp.route('/<int:id>/update',methods=('GET','POST'))
@login_required
def update(id):
post=get_post(id)

if request.method=='POST':
title=request.form['title']
body=request.form['body']
error=None

if not title:
error='Title is required'

if error is not None:
flash(error)
else:
db=get_db()
db.execute(
'UPDATE post SET title=?,body=?'
'WHERE id=?'
(title,body,id)
)
db.commit()
return redirect(url_for('post.index'))

return render_template('posts/update.html',post=post)

@bp.route('/<int:id>/delete',methods=('POST',))
@login_required
def delete(id):
get_post(id)
db=get_db()
db.execute('DELETE FROM post WHERE id=?',(id,))
db.commit()
return redirect(url_for('post.index'))
18 changes: 18 additions & 0 deletions week-0/day-3/my_todo_app/todo_app/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
DROP TABLE IF EXISTS user;
DROP TABLE IF EXISTS post;

CREATE TABLE user(
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL
);

CREATE TABLE post(
id INTEGER PRIMARY KEY AUTOINCREMENT,
author_id INTEGER NOT NULL,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
title TEXT NOT NULL,
body TEXT NOT NULL,
FOREIGN KEY(author_id) REFERENCES user (id)
);

15 changes: 15 additions & 0 deletions week-0/day-3/my_todo_app/todo_app/templates/auth/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends 'base.html' %}

{% block header %}
<h1>{% block title %}Log In{% endblock %}</h1>
{% endblock %}

{% block content %}
<form method="post">
<label for="username">Username</label>
<input name="username" id="username" required>
<label for="password">Password</label>
<input type="password" name="password" id="password" required>
<input type="submit" value="Log In">
</form>
{% endblock %}
15 changes: 15 additions & 0 deletions week-0/day-3/my_todo_app/todo_app/templates/auth/register.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends 'base.html' %}

{% block header %}
<h1>{% block title %}Register{% endblock %}</h1>
{% endblock %}

{% block content %}
<form method="post">
<label for="username">Username</label>
<input name="username" id="username" required>
<label for="password">Password</label>
<input type="password" name="password" id="password" required>
<input type="submit" value="Register">
</form>
{% endblock %}
Loading