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
63 changes: 10 additions & 53 deletions week-0/day-3/my_todo_app/todo_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,26 @@
from flask import request

from flask import render_template
from todo_app.extensions import mysql

# our fake db
todo_store = {}
todo_store['depo'] = ['Go for run', 'Listen Rock Music']
todo_store['shivang'] = ['Read book', 'Play Fifa', 'Drink Coffee']
todo_store['raj'] = ['Study', 'Brush']
todo_store['sanket'] = ['Sleep', 'Code']
todo_store['aagam'] = ['play cricket', 'have tea']

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

# ensure the instance folder exists
try:
os.makedirs(app.instance_path)
except OSError:
pass

def select_todos(name):
global todo_store
return todo_store[name]

def insert_todo(name, todo):
global todo_store
current_todos = todo_store[name]
current_todos.append(todo)
todo_store[name] = current_todos
return

def add_todo_by_name(name, todo):
# call DB function
insert_todo(name, todo)
return

def get_todos_by_name(name):
try:
return select_todos(name)
except:
return None


# 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)
if person_todo_list == None:
return render_template('404.html'), 404
else:
return render_template('todo_view.html',todos=person_todo_list)

app.config['MYSQL_DATABASE_HOST'] = 'localhost'
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'swapnil159'
app.config['MYSQL_DATABASE_DB'] = 'todo_app'
#app.config['MYSQL_DATABASE_CHARSET'] = 'utf-8'
mysql.init_app(app)

@app.route('/add_todos')
def add_todos():
name = request.args.get('name')
todo = request.args.get('todo')
add_todo_by_name(name, todo)
return 'Added Successfully'
from . import todo
app.register_blueprint(todo.bp)

return app

Binary file modified week-0/day-3/my_todo_app/todo_app/__init__.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
3 changes: 3 additions & 0 deletions week-0/day-3/my_todo_app/todo_app/extensions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from flaskext.mysql import MySQL

mysql = MySQL()
Binary file added week-0/day-3/my_todo_app/todo_app/extensions.pyc
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion week-0/day-3/my_todo_app/todo_app/templates/todo_view.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
List of my todos:<br/>

{% for todo in todos %}
{{todo}}<br/>
{{todo[0]}}<br/>
{% endfor %}

---- LIST ENDS HERE ---
53 changes: 53 additions & 0 deletions week-0/day-3/my_todo_app/todo_app/todo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from flask import (
Blueprint, flash, g, redirect, render_template, request, url_for
)
from werkzeug.exceptions import abort

bp = Blueprint('todo', __name__)

from todo_app.extensions import mysql

def select_todos(name,num):
cur = mysql.get_db().cursor()
cur.execute("SELECT todos FROM todo WHERE name = %s LIMIT %s",(name,num))
todo_list = cur.fetchall()
return todo_list

def insert_todo(name, todo):
print(name,todo)
conn = mysql.connect()
cur = conn.cursor()
cur.execute("INSERT INTO todo (name, todos) VALUES (%s, %s)",(name,todo))
conn.commit()
cur.close()
conn.close()
return

def add_todo_by_name(name, todo):
# call DB function
insert_todo(name, todo)
return

def get_todos_by_name(name,num):
return select_todos(name,num)



# http://127.0.0.1:5000/todos?name=duster
@bp.route('/todos')
def todos():
name = request.args.get('name')
num = int(request.args.get('num'))
person_todo_list = get_todos_by_name(name,num)
if person_todo_list == None:
return render_template('404.html'), 404
else:
return render_template('todo_view.html',todos=person_todo_list)


@bp.route('/add_todos')
def add_todos():
name = request.args.get('name')
todo = request.args.get('todo')
add_todo_by_name(name, todo)
return 'Added Successfully'
Binary file added week-0/day-3/my_todo_app/todo_app/todo.pyc
Binary file not shown.