add project files
This commit is contained in:
188
back/crud.py
Normal file
188
back/crud.py
Normal file
@@ -0,0 +1,188 @@
|
||||
from database import connection
|
||||
|
||||
def get_books(page, limit=50, order="Auteur", search=""):
|
||||
"""
|
||||
:param limit: Item limit
|
||||
:type limit: int
|
||||
:param page: current page
|
||||
:type page: int
|
||||
:param order: Book fields are : Auteur, Titre, Editeur, Type
|
||||
:type order: str
|
||||
"""
|
||||
cursor = connection.cursor()
|
||||
connection.ping(reconnect=True)
|
||||
if search == "":
|
||||
t = f"""
|
||||
SELECT * from Books order by %s asc LIMIT %s OFFSET %s;
|
||||
"""
|
||||
nb = f"""
|
||||
SELECT COUNT(*) as count from Books order by %s asc;
|
||||
"""
|
||||
try:
|
||||
cursor.execute(t, (order, limit, limit * page))
|
||||
r = cursor.fetchall()
|
||||
cursor.execute(nb, (order))
|
||||
n = cursor.fetchall()
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return False
|
||||
else:
|
||||
search = search.replace(' ', ', ')
|
||||
t = f"""
|
||||
SELECT * FROM `Books`
|
||||
WHERE `Titre` LIKE %s
|
||||
OR `Auteur` LIKE %s
|
||||
OR `Editeur` LIKE %s
|
||||
OR `Type` LIKE %s
|
||||
ORDER BY %s asc LIMIT %s OFFSET %s;
|
||||
"""
|
||||
nb = f"""
|
||||
SELECT COUNT(*) as count from `Books`
|
||||
WHERE `Titre` LIKE %s
|
||||
OR `Auteur` LIKE %s
|
||||
OR `Editeur` LIKE %s
|
||||
OR `Type` LIKE %s
|
||||
ORDER BY %s asc;
|
||||
"""
|
||||
try:
|
||||
cursor.execute(t, (f'%{search}%', f'%{search}%', f'%{search}%', f'%{search}%', order, limit, limit * page))
|
||||
r = cursor.fetchall()
|
||||
cursor.execute(nb, (f'%{search}%', f'%{search}%', f'%{search}%', f'%{search}%', order))
|
||||
n = cursor.fetchall()
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return False
|
||||
return r, n
|
||||
|
||||
def remove_book(id):
|
||||
cursor = connection.cursor()
|
||||
connection.ping(reconnect=True)
|
||||
t = f"""
|
||||
DELETE FROM Books where biblio_Index=%s;
|
||||
"""
|
||||
try:
|
||||
cursor.execute(t, (id))
|
||||
except:
|
||||
return False
|
||||
connection.commit()
|
||||
return True
|
||||
|
||||
def edit_book(id, newContent):
|
||||
"""
|
||||
:param id: item to update
|
||||
:type id: str
|
||||
:param newContent: New values to update
|
||||
:type newContent: dict
|
||||
"""
|
||||
cursor = connection.cursor()
|
||||
connection.ping(reconnect=True)
|
||||
t = f"""
|
||||
UPDATE Books
|
||||
SET Auteur=%s, Titre=%s, Editeur=%s, Type=%s
|
||||
WHERE biblio_Index=%s;
|
||||
"""
|
||||
try:
|
||||
cursor.execute(t, (newContent['author'], newContent['title'], newContent['editor'], newContent['type'], id))
|
||||
except:
|
||||
return False
|
||||
connection.commit()
|
||||
return True
|
||||
|
||||
def add_book(newContent):
|
||||
"""
|
||||
:param newContent: New values to add
|
||||
:type newContent: dict
|
||||
"""
|
||||
connection.ping(reconnect=True)
|
||||
cursor = connection.cursor()
|
||||
t = f"""
|
||||
INSERT INTO Books (Auteur, Titre, Editeur, Type)
|
||||
VALUES (%s, %s, %s, %s);
|
||||
"""
|
||||
try:
|
||||
cursor.execute(t, (newContent['author'], newContent['title'], newContent['editor'], newContent['type']))
|
||||
except:
|
||||
return False
|
||||
connection.commit()
|
||||
return True
|
||||
|
||||
def add_film(newContent):
|
||||
connection.ping(reconnect=True)
|
||||
cursor = connection.cursor()
|
||||
t = f"""
|
||||
INSERT INTO Films (Title, Director, Producer, Actors, Length, Type)
|
||||
VALUES (%s, %s, %s, %s, %s, %s);
|
||||
"""
|
||||
try:
|
||||
cursor.execute(t, (newContent['title'], newContent['director'], newContent['producer'], newContent['actors'], newContent['length'], newContent['type']))
|
||||
except:
|
||||
return False
|
||||
connection.commit()
|
||||
return True
|
||||
|
||||
def remove_film(id):
|
||||
cursor = connection.cursor()
|
||||
t = f"""
|
||||
DELETE FROM Films where Number=%s;
|
||||
"""
|
||||
try:
|
||||
cursor.execute(t, (id))
|
||||
except:
|
||||
return False
|
||||
connection.commit()
|
||||
return True
|
||||
|
||||
def edit_film(id, newContent):
|
||||
connection.ping(reconnect=True)
|
||||
cursor = connection.cursor()
|
||||
t = f"""
|
||||
UPDATE Films
|
||||
SET Title=%s, Director=%s, Producer=%s, Actors=%s, Length=%s, Type=%s
|
||||
WHERE Number=%s;
|
||||
"""
|
||||
try:
|
||||
cursor.execute(t, (newContent['title'], newContent['director'], newContent['producer'], newContent['actors'], newContent['length'], newContent['type'], id))
|
||||
except:
|
||||
return False
|
||||
connection.commit()
|
||||
return True
|
||||
|
||||
def get_films(page, limit=50, order="Director"):
|
||||
connection.ping(reconnect=True)
|
||||
cursor = connection.cursor()
|
||||
t = f"""
|
||||
SELECT * from Films order by %s asc LIMIT %s OFFSET %s;
|
||||
"""
|
||||
try:
|
||||
cursor.execute(t, (order, limit, limit * page))
|
||||
except:
|
||||
return False
|
||||
return cursor.fetchall()
|
||||
|
||||
def get_field_values(table, field):
|
||||
connection.ping(reconnect=True)
|
||||
cursor = connection.cursor()
|
||||
t = f"""
|
||||
SELECT DISTINCT {field} FROM {table} ORDER BY {field} asc;
|
||||
"""
|
||||
try:
|
||||
cursor.execute(t)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return False
|
||||
res = cursor.fetchall()
|
||||
return [a[field] for a in res]
|
||||
|
||||
def get_user(username):
|
||||
connection.ping(reconnect=True)
|
||||
cursor = connection.cursor()
|
||||
t = f"""
|
||||
SELECT * from Users WHERE username=%s;
|
||||
"""
|
||||
try:
|
||||
cursor.execute(t, (username))
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return False
|
||||
res = cursor.fetchall()
|
||||
return res
|
||||
Reference in New Issue
Block a user