Skip to content

Commit

Permalink
Start Postgres Heroku Migration
Browse files Browse the repository at this point in the history
  • Loading branch information
kilerhg committed Sep 27, 2021
1 parent 136d3dc commit 9138d66
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 63 deletions.
28 changes: 13 additions & 15 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import funcoes
import dao
import parameters

import logging

Expand Down Expand Up @@ -91,10 +92,10 @@ def search():
@app.route("/biblioteca")
@login_required
def library():
db, cursor = dao.connect_db()
db, cursor = dao.connection_database(user=parameters.USER, password=parameters.PWD, host=parameters.HOSTNAME, port=parameters.PORT, database=parameters.DATABASE)
id_user = dict(session)['profile']['id']
list_books_id = dao.get_id_books_by_user_id(cursor=cursor, id_user=id_user)
dict_other_infos_book = dao.get_books_by_user_id(cursor=cursor, id_user=id_user)
list_books_id = dao.get_id_books_by_user_id(cursor=cursor, id_user=id_user, schema=parameters.SCHEMA)
dict_other_infos_book = dao.get_books_by_user_id(cursor=cursor, id_user=id_user, schema=parameters.SCHEMA)
list_books = funcoes.get_books_by_id(list_books_id=list_books_id)
new_list_books = []
for x in list_books:
Expand All @@ -112,21 +113,19 @@ def library():
@app.route("/biblioteca/add/<book_id>")
@login_required
def add_book_library(book_id):
db, cursor = dao.connect_db()
db, cursor = dao.connection_database(user=parameters.USER, password=parameters.PWD, host=parameters.HOSTNAME, port=parameters.PORT, database=parameters.DATABASE)

id_user = dict(session)['profile']['id']

book_exist = dao.get_id_register(cursor=cursor, id_user=id_user, google_book_id=book_id)
book_exist = dao.get_id_register(cursor=cursor, id_user=id_user, google_book_id=book_id, schema=parameters.SCHEMA)

if not book_exist:
dao.insert_book_into_library_by_id(db=db, cursor=cursor, id_user=id_user, google_book_id=book_id)
dao.insert_book_into_library_by_id(db=db, cursor=cursor, id_user=id_user, google_book_id=book_id), schema=parameters.SCHEMA
return redirect('/biblioteca')

@app.route("/biblioteca/update", methods=['POST'])
@login_required
def update_book_library():
db, cursor = dao.connect_db()

id_user = dict(session)['profile']['id']

status = request.form["status"]
Expand All @@ -151,22 +150,21 @@ def update_book_library():




db, cursor = dao.connect_db()
id_register = dao.get_id_register(cursor=cursor, id_user=id_user, google_book_id=book_id)
dao.update_status_book_by_id(cursor, db, new_status_book=status, id_register=id_register)
dao.update_percent_book_by_id(cursor, db, new_percent=percent, id_register=id_register)
db, cursor = dao.connection_database(user=parameters.USER, password=parameters.PWD, host=parameters.HOSTNAME, port=parameters.PORT, database=parameters.DATABASE)
id_register = dao.get_id_register(cursor=cursor, id_user=id_user, google_book_id=book_id, schema=parameters.SCHEMA)
dao.update_status_book_by_id(cursor, db, new_status_book=status, id_register=id_register, schema=parameters.SCHEMA)
dao.update_percent_book_by_id(cursor, db, new_percent=percent, id_register=id_register, schema=parameters.SCHEMA)

return redirect('/biblioteca')


@app.route("/biblioteca/remove/<book_id>")
@login_required
def remove_book_library(book_id):
db, cursor = dao.connect_db()
db, cursor = dao.connection_database(user=parameters.USER, password=parameters.PWD, host=parameters.HOSTNAME, port=parameters.PORT, database=parameters.DATABASE)

id_user = dict(session)['profile']['id']
dao.remove_book_from_library_by_id(db=db, cursor=cursor, id_user=id_user, google_book_id=book_id)
dao.remove_book_from_library_by_id(db=db, cursor=cursor, id_user=id_user, google_book_id=book_id, schema=parameters.SCHEMA)
return redirect('/biblioteca')


Expand Down
75 changes: 28 additions & 47 deletions dao.py
Original file line number Diff line number Diff line change
@@ -1,69 +1,46 @@
import sqlite3
import os

def connect_db(name='database'):

if os.path.isfile(f'./{name}.db'):
db = sqlite3.connect(f"{name}.db")
import psycopg2
import parameters

def connection_database(user, password, host, port, database):
try:
db = psycopg2.connect(user=user,
password=password,
host=host,
port=port,
database=database)
cursor = db.cursor()
else:
db = sqlite3.connect(f"{name}.db")
cursor = db.cursor()

sql_query_generate_status_book = '''
CREATE TABLE status_book (
id_status INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
description TEXT(50)
);'''

sql_query_generate_library = '''
CREATE TABLE library (
id_register INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
id_user INTEGER NOT NULL,
book_unique_key TEXT(40),
id_status_book INTEGER,
percent_book FLOAT(3),
FOREIGN KEY(id_status_book) REFERENCES status_book(id_status)
);'''

sql_query_insert_status_book = '''
insert into status_book(id_status, description) values
('0', 'Not Stated'),
('1', 'Reading'),
('2', 'Readed');'''

cursor.execute(sql_query_generate_status_book)
cursor.execute(sql_query_generate_library)

cursor.execute(sql_query_insert_status_book)
db.commit()

return db, cursor

print("Successful connection")
return db, cursor
except:
print("Connection error")

def insert_book_into_library_by_id(db, cursor, id_user, google_book_id):
def insert_book_into_library_by_id(db, cursor, id_user, google_book_id, schema):
sql_query_insert_book_into_library = f'''
insert into library(id_user, book_unique_key, id_status_book, percent_book) values
('{id_user}', '{google_book_id}', '0', '0');'''
cursor.execute(f"set schema '{schema}'")
cursor.execute(sql_query_insert_book_into_library)
db.commit()


def remove_book_from_library_by_id(db, cursor, id_user, google_book_id):
def remove_book_from_library_by_id(db, cursor, id_user, google_book_id, schema):
id_register = get_id_register(cursor=cursor, id_user=id_user, google_book_id=google_book_id)
sql_query_remove_book_into_library = f'''
delete from library
where id_register='{id_register}'
'''
cursor.execute(f"set schema '{schema}'")
cursor.execute(sql_query_remove_book_into_library)
db.commit()


def get_id_register(cursor, id_user, google_book_id):
def get_id_register(cursor, id_user, google_book_id, schema):
sql_query_get_id_register = f'''
select id_register from library
where id_user = '{id_user}'
and book_unique_key = '{google_book_id}';'''
cursor.execute(f"set schema '{schema}'")
cursor.execute(sql_query_get_id_register)
register_id = cursor.fetchone()
if register_id:
Expand All @@ -73,24 +50,27 @@ def get_id_register(cursor, id_user, google_book_id):
return register_id


def update_status_book_by_id(cursor, db, new_status_book, id_register):
def update_status_book_by_id(cursor, db, new_status_book, id_register, schema):
sql_query_update_status_book = f'''
update library
set id_status_book = {new_status_book}
where id_register={id_register}'''
cursor.execute(f"set schema '{schema}'")
cursor.execute(sql_query_update_status_book)
db.commit()


def update_percent_book_by_id(cursor, db, new_percent, id_register):
def update_percent_book_by_id(cursor, db, new_percent, id_register, schema):
sql_query_update_percent_book = f'''
update library
set percent_book = {new_percent}
where id_register = {id_register};'''
cursor.execute(f"set schema '{schema}'")
cursor.execute(sql_query_update_percent_book)
db.commit()

def get_id_books_by_user_id(cursor, id_user):
def get_id_books_by_user_id(cursor, id_user, schema):
cursor.execute(f"set schema '{schema}'")
cursor.execute(f'''
select book_unique_key from library
where id_user = '{id_user}'
Expand All @@ -103,7 +83,8 @@ def get_id_books_by_user_id(cursor, id_user):
valores = []
return valores

def get_books_by_user_id(cursor, id_user):
def get_books_by_user_id(cursor, id_user, schema):
cursor.execute(f"set schema '{schema}'")
cursor.execute(f'''
select book_unique_key, id_status_book, percent_book from library
where id_user = '{id_user}'
Expand Down
8 changes: 8 additions & 0 deletions parameters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import os

DATABASE = os.getenv("DATABASE")
HOSTNAME = os.getenv("HOSTNAME")
PORT = os.getenv("PORT")
USER = os.getenv("USER")
PWD = os.getenv("PWD")
SCHEMA = os.getenv("SCHEMA")
2 changes: 1 addition & 1 deletion templates/library.html
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ <h3>{{item['titulo']}}</h3>

<div class="row">
<div class="col-md-12">
<input type="submit" value="Enviar">
<input type="submit" value="Save">
</div>
</div>
<p></p>
Expand Down

0 comments on commit 9138d66

Please sign in to comment.