Updated Backend
This commit is contained in:
@ -1,12 +1,13 @@
|
|||||||
from minio import Minio
|
from minio import Minio
|
||||||
from random import randint
|
from random import randint
|
||||||
import dbwork
|
import DBwork
|
||||||
|
import config
|
||||||
|
|
||||||
def _setClient():
|
def _setClient():
|
||||||
minio_client = Minio(
|
minio_client = Minio(
|
||||||
"localhost:9000",
|
config.IS_address,
|
||||||
access_key="minioadmin",
|
access_key = config.acc_key,
|
||||||
secret_key="minioadmin",
|
secret_key = config.sec_key,
|
||||||
secure = False
|
secure = False
|
||||||
)
|
)
|
||||||
return minio_client
|
return minio_client
|
||||||
@ -23,12 +24,12 @@ def downloadImage(currentDay, username):
|
|||||||
client.fget_object(bucket_name, getImageName(currentDay), username + '.jpeg')
|
client.fget_object(bucket_name, getImageName(currentDay), username + '.jpeg')
|
||||||
|
|
||||||
def downloadForAll(currentDay):
|
def downloadForAll(currentDay):
|
||||||
|
cur, conn = DBwork.set_connection()
|
||||||
counter = 1
|
counter = 1
|
||||||
user = dbwork.get_user(counter)
|
user = DBwork.get_user(counter, cur)
|
||||||
while(user != 'Error'):
|
while(user != 'Error'):
|
||||||
downloadImage(currentDay, user)
|
downloadImage(currentDay, user)
|
||||||
counter += 1
|
counter += 1
|
||||||
user = dbwork.get_user(counter)
|
user = DBwork.get_user(counter, cur)
|
||||||
|
DBwork.close_connection(conn, cur)
|
||||||
|
|
||||||
Day = 'Tuesday'
|
|
||||||
downloadForAll(Day)
|
|
||||||
8
src/Backend/config.py
Normal file
8
src/Backend/config.py
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
IS_address = "localhost:9000"
|
||||||
|
acc_key = "minioadmin"
|
||||||
|
sec_key = "minioadmin"
|
||||||
|
db_name = "postgres_db"
|
||||||
|
postgres_user = "postgres_user"
|
||||||
|
postgres_password = "postgres_password"
|
||||||
|
host_name = "localhost"
|
||||||
|
port = "5430"
|
||||||
@ -1,67 +1,47 @@
|
|||||||
import psycopg2
|
import psycopg2
|
||||||
|
import config
|
||||||
|
|
||||||
def _get_last_id(cursor):
|
def _get_last_id(cursor):
|
||||||
cursor.execute("SELECT MAX(id) FROM usernames")
|
cursor.execute("SELECT MAX(id) FROM usernames")
|
||||||
id = cursor.fetchall()[0][0]
|
id = cursor.fetchall()[0][0]
|
||||||
return id
|
return id
|
||||||
|
|
||||||
def add_user(username):
|
def set_connection():
|
||||||
connection = psycopg2.connect(
|
connection = psycopg2.connect(
|
||||||
dbname="postgres_db",
|
dbname = config.db_name,
|
||||||
user="postgres_user",
|
user = config.postgres_user,
|
||||||
password="postgres_password",
|
password = config.postgres_password,
|
||||||
host="localhost",
|
host = config.host_name,
|
||||||
port="5430"
|
port = config.port
|
||||||
)
|
)
|
||||||
|
cursor = connection.cursor()
|
||||||
|
return cursor, connection
|
||||||
|
|
||||||
|
def close_connection(connection, cursor):
|
||||||
|
cursor.close()
|
||||||
|
connection.close()
|
||||||
|
|
||||||
|
#Functions don't close connection automatically, it has to be closed manually
|
||||||
|
def add_user(username, connection, cursor):
|
||||||
cursor = connection.cursor()
|
cursor = connection.cursor()
|
||||||
cursor.execute("INSERT INTO usernames VALUES (%s, %s);", (_get_last_id(cursor) + 1, username))
|
cursor.execute("INSERT INTO usernames VALUES (%s, %s);", (_get_last_id(cursor) + 1, username))
|
||||||
connection.commit()
|
connection.commit()
|
||||||
cursor.close()
|
|
||||||
connection.close()
|
|
||||||
|
|
||||||
def delete_user(username):
|
def delete_user(username, connection, cursor):
|
||||||
connection = psycopg2.connect(
|
|
||||||
dbname="postgres_db",
|
|
||||||
user="postgres_user",
|
|
||||||
password="postgres_password",
|
|
||||||
host="localhost",
|
|
||||||
port="5430"
|
|
||||||
)
|
|
||||||
cursor = connection.cursor()
|
cursor = connection.cursor()
|
||||||
cursor.execute("DELETE FROM usernames WHERE username = %s;", (username,))
|
cursor.execute("DELETE FROM usernames WHERE username = %s;", (username,))
|
||||||
connection.commit()
|
connection.commit()
|
||||||
cursor.close()
|
|
||||||
connection.close()
|
|
||||||
|
|
||||||
def change_name(old_username, new_username):
|
def change_name(old_username, new_username, connection, cursor):
|
||||||
connection = psycopg2.connect(
|
|
||||||
dbname="postgres_db",
|
|
||||||
user="postgres_user",
|
|
||||||
password="postgres_password",
|
|
||||||
host="localhost",
|
|
||||||
port="5430"
|
|
||||||
)
|
|
||||||
cursor = connection.cursor()
|
cursor = connection.cursor()
|
||||||
cursor.execute("UPDATE usernames SET username = %s WHERE username = %s;", (new_username, old_username))
|
cursor.execute("UPDATE usernames SET username = %s WHERE username = %s;", (new_username, old_username))
|
||||||
connection.commit()
|
connection.commit()
|
||||||
cursor.close()
|
|
||||||
connection.close()
|
|
||||||
|
|
||||||
def get_user(id):
|
def get_user(id, cursor):
|
||||||
connection = psycopg2.connect(
|
|
||||||
dbname="postgres_db",
|
|
||||||
user="postgres_user",
|
|
||||||
password="postgres_password",
|
|
||||||
host="localhost",
|
|
||||||
port="5430"
|
|
||||||
)
|
|
||||||
cursor = connection.cursor()
|
|
||||||
max_id = _get_last_id(cursor)
|
max_id = _get_last_id(cursor)
|
||||||
if id <= max_id:
|
if id <= max_id:
|
||||||
cursor.execute("SELECT username FROM usernames WHERE id = %s", (id,))
|
cursor.execute("SELECT username FROM usernames WHERE id = %s", (id,))
|
||||||
username = cursor.fetchall()[0][0]
|
username = cursor.fetchall()[0][0]
|
||||||
else:
|
else:
|
||||||
username = 'Error'
|
username = 'Error'
|
||||||
cursor.close()
|
return username, cursor
|
||||||
connection.close()
|
|
||||||
return username
|
|
||||||
|
|||||||
Reference in New Issue
Block a user