New commit 2.0:
- Added test file to gitignore
- Created table of users with chat_ids(UserTable.sql)
- Added check of empty table into get_last_id
- Modified get_user to return a chat_id instead of username
- Changed download function in ISwork to send URL instead
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -5,3 +5,4 @@
|
|||||||
.DS_Store
|
.DS_Store
|
||||||
.vscode/
|
.vscode/
|
||||||
__pycache__/
|
__pycache__/
|
||||||
|
test.py
|
||||||
|
|||||||
@ -3,8 +3,10 @@ from src 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 Users")
|
||||||
id = cursor.fetchall()[0][0]
|
id = cursor.fetchall()[0][0]
|
||||||
|
if id == None:
|
||||||
|
return 0
|
||||||
return id
|
return id
|
||||||
|
|
||||||
def set_connection():
|
def set_connection():
|
||||||
@ -23,19 +25,19 @@ def close_connection(connection, cursor):
|
|||||||
connection.close()
|
connection.close()
|
||||||
|
|
||||||
#Functions don't close connection automatically, it has to be closed manually
|
#Functions don't close connection automatically, it has to be closed manually
|
||||||
def add_user(username, connection, cursor):
|
def add_user(username, chat_id, connection, cursor):
|
||||||
cursor.execute("INSERT INTO usernames VALUES (%s, %s);", (get_last_id(cursor) + 1, username))
|
cursor.execute("INSERT INTO Users VALUES (%s, %s, %s);", (get_last_id(cursor) + 1, username, chat_id))
|
||||||
connection.commit()
|
connection.commit()
|
||||||
|
|
||||||
def delete_user(username, connection, cursor):
|
def delete_user(username, connection, cursor):
|
||||||
cursor.execute("DELETE FROM usernames WHERE username = %s;", (username,))
|
cursor.execute("DELETE FROM Users WHERE username = %s;", (username,))
|
||||||
connection.commit()
|
connection.commit()
|
||||||
|
|
||||||
def change_name(old_username, new_username, connection, cursor):
|
def change_name(old_username, new_username, connection, cursor):
|
||||||
cursor.execute("UPDATE usernames SET username = %s WHERE username = %s;", (new_username, old_username))
|
cursor.execute("UPDATE Users SET username = %s WHERE username = %s;", (new_username, old_username))
|
||||||
connection.commit()
|
connection.commit()
|
||||||
|
|
||||||
def get_user(id, cursor):
|
def get_chat_id(id, cursor):
|
||||||
cursor.execute("SELECT username FROM usernames WHERE id = %s", (id,))
|
cursor.execute("SELECT chatid FROM Users WHERE id = %s", (id,))
|
||||||
username = cursor.fetchall()[0][0]
|
chat_id = cursor.fetchall()[0][0]
|
||||||
return username
|
return chat_id
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
from minio import Minio
|
from minio import Minio
|
||||||
from random import randint
|
from random import randint
|
||||||
import DBwork
|
from datetime import timedelta
|
||||||
|
from src.Backend import DBwork
|
||||||
from src import config
|
from src import config
|
||||||
|
|
||||||
|
|
||||||
@ -23,24 +24,27 @@ def getObjectExtension(client, currentDay, fileNumber):
|
|||||||
if counter == fileNumber:
|
if counter == fileNumber:
|
||||||
return obj.object_name.split('.')[-1]
|
return obj.object_name.split('.')[-1]
|
||||||
|
|
||||||
def getFileNames(currentDay, client, username):
|
def getImageName(currentDay, client):
|
||||||
maxFiles = getNumberofObjects(client, currentDay)
|
maxFiles = getNumberofObjects(client, currentDay)
|
||||||
fileNumber = randint(1, maxFiles)
|
fileNumber = randint(1, maxFiles)
|
||||||
fileExtension = getObjectExtension(client, currentDay, fileNumber)
|
fileExtension = '.' + getObjectExtension(client, currentDay, fileNumber)
|
||||||
desiredFile = currentDay + '/' + str(fileNumber) + '.' + fileExtension
|
desiredFile = currentDay + '/' + str(fileNumber) + fileExtension
|
||||||
downloadName = username + '.' + fileExtension
|
return desiredFile
|
||||||
return desiredFile, downloadName
|
|
||||||
|
|
||||||
def downloadImage(currentDay, username):
|
def getDownloadURL(currentDay):
|
||||||
client = _setClient()
|
client = _setClient()
|
||||||
object_name, file_name = getFileNames(currentDay, client, username)
|
object_name = getImageName(currentDay, client)
|
||||||
client.fget_object(config.bucket_name, object_name, file_name)
|
url = client.presigned_get_object(
|
||||||
|
config.bucket_name,
|
||||||
|
object_name,
|
||||||
|
expires=timedelta(days=1))
|
||||||
|
return url
|
||||||
|
|
||||||
def downloadForAll(currentDay):
|
def downloadForAll(currentDay):
|
||||||
cur, conn = DBwork.set_connection()
|
cur, conn = DBwork.set_connection()
|
||||||
max_id = DBwork.get_last_id(cur)
|
max_id = DBwork.get_last_id(cur)
|
||||||
for id in range(1, max_id + 1):
|
for id in range(1, max_id + 1):
|
||||||
user = DBwork.get_user(id, cur)
|
chat_id = DBwork.get_chat_id(id, cur)
|
||||||
downloadImage(currentDay, user)
|
image_URL = getDownloadURL(currentDay)
|
||||||
DBwork.close_connection(conn, cur)
|
DBwork.close_connection(conn, cur)
|
||||||
|
|
||||||
|
|||||||
6
src/Backend/UserTable.sql
Normal file
6
src/Backend/UserTable.sql
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
create table Users(
|
||||||
|
Id serial primary key,
|
||||||
|
username character varying(32),
|
||||||
|
ChatId bigint not null,
|
||||||
|
constraint chat_id_unique unique(ChatId)
|
||||||
|
);
|
||||||
Reference in New Issue
Block a user