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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 3 additions & 0 deletions week-0/day-3/my_todo_app/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.pythonPath": "venv/bin/python3"
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions week-0/day-3/my_todo_app/todo_app/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions week-0/day-3/my_todo_app/todo_app/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions week-0/day-3/my_todo_app/todo_app/.idea/todo_app.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions week-0/day-3/my_todo_app/todo_app/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

204 changes: 204 additions & 0 deletions week-0/day-3/my_todo_app/todo_app/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions week-0/day-3/my_todo_app/todo_app/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.pythonPath": "/bin/python3"
}
63 changes: 63 additions & 0 deletions week-0/day-3/my_todo_app/todo_app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import os

from flask import Flask, request, render_template, redirect, url_for

from . import db


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

try:
os.makedirs(app.instance_path)
except OSError:
pass

db.initialize_app_db(app)

def select_todos(name):
db1 = db.Database(app)
data = db1.get_todo(name)
print("Todos for", name, ": ", data)
return data

def insert_todo(name, todo):
db1 = db.Database(app)
db1.add_todo(name, todo)
return

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

def get_todos_by_name(name):
return select_todos(name)

@app.route('/')
def index():
return render_template('todo_view.html')

@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.route('/add_todos', methods=['POST'])
def add_todos():
name = request.form.get('name')
todo = request.form.get('todo')
add_todo_by_name(name, todo)
return redirect(url_for('todos', name=name))
# return

return app
Binary file not shown.
Binary file not shown.
48 changes: 48 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,48 @@
import pymysql

def initialize_app_db(app):
db1 = Database(app)
db1.cur.execute("DROP TABLE IF EXISTS user")
f = open("todo_app/schema.sql", "r")
sqlCommands = f.read().split(';')
db1.cur.execute("DROP TABLE IF EXISTS user")
for command in sqlCommands:
try:
print("Executing: ", command)
db1.cur.execute(command)
except:
print("Skipped: ", command)

db1.con.commit()


class Database:
def __init__(self, app):
host = "localhost"
user = "manjeet"
password = "Manjeet@12345"
db = "flask_app"
self.con = pymysql.connect(host=host, user=user, password=password, db=db,
cursorclass=pymysql.cursors.DictCursor)
self.cur = self.con.cursor()

def get_todo(self, name):
sql_get_query = """ SELECT todo from `user`
WHERE username = (%s)"""

self.cur.execute(sql_get_query, (name))
result = self.cur.fetchall()
just = []
for item in result:
just.append(item['todo'])
print("-----------------")
print(just)
print("-----------------")
return just

def add_todo(self, name, todo):
sql_insert_query = """ INSERT INTO `user`
(`username`, `todo`) VALUES (%s,%s)"""
print("Want to insert -> ", (name, todo))
self.cur.execute(sql_insert_query, (name, todo))
self.con.commit()
16 changes: 16 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,16 @@
DROP TABLE IF EXISTS user;

create table user(
username VARCHAR(100) NOT NULL,
todo VARCHAR(100) NOT NULL,
PRIMARY KEY(username,todo));

INSERT INTO user(username,todo) VALUES ('ayush','eat');
INSERT INTO user(username,todo) VALUES ('ayush','read');
INSERT INTO user(username,todo) VALUES ('ayush','sleep');
INSERT INTO user(username,todo) VALUES ('raj','code');
INSERT INTO user(username,todo) VALUES ('raj','coffee');
INSERT INTO user(username,todo) VALUES ('raj','play');
INSERT INTO user(username,todo) VALUES ('depo','sleep');
INSERT INTO user(username,todo) VALUES ('depo','brush');
INSERT INTO user(username,todo) VALUES ('depo','run');
1 change: 1 addition & 0 deletions week-0/day-3/my_todo_app/todo_app/templates/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
User Not Found
Loading