add code to convert datetime into localtime into file list and add access quarys
This commit is contained in:
+115
-28
@@ -1,9 +1,6 @@
|
||||
from my_modules.app.logger import logger
|
||||
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from zoneinfo import ZoneInfo
|
||||
from collections import Counter
|
||||
import asyncio, gel, json
|
||||
import asyncio, gel
|
||||
|
||||
class EdgeDB:
|
||||
def __init__(self, database:str=None, tls_security:str='insecure', timeout:int=1, max_retrys:int=10):
|
||||
@@ -69,7 +66,7 @@ class EdgeDB:
|
||||
""",
|
||||
file_id=file_id
|
||||
)
|
||||
|
||||
|
||||
if data:
|
||||
return {
|
||||
"file_name": data.file_name,
|
||||
@@ -78,7 +75,7 @@ class EdgeDB:
|
||||
}
|
||||
return None
|
||||
|
||||
async def get_files(self, current_datetime, user_id: str):
|
||||
async def get_files(self, current_datetime, user_id:str):
|
||||
data = await self.run_query_with_reconnection(
|
||||
self.client.query,
|
||||
"""
|
||||
@@ -100,16 +97,14 @@ class EdgeDB:
|
||||
now=current_datetime,
|
||||
user_id=user_id,
|
||||
)
|
||||
return [
|
||||
{
|
||||
"file_id": i.file_id,
|
||||
"file_name": i.file_name,
|
||||
"file_size": i.file_size,
|
||||
"note": i.note,
|
||||
"uploaded_at": i.uploaded_at,
|
||||
"expires_at": i.expires_at,
|
||||
} for i in data
|
||||
]
|
||||
return [{
|
||||
"file_id": i.file_id,
|
||||
"file_name": i.file_name,
|
||||
"file_size": i.file_size,
|
||||
"note": i.note,
|
||||
"uploaded_at": i.uploaded_at,
|
||||
"expires_at": i.expires_at if i.expires_at else '',
|
||||
} for i in data]
|
||||
|
||||
async def add_file(self, file_id, file_name, file_size, note, content_type, uploaded_at, expires_at, user_id:str):
|
||||
return await self.run_query_with_reconnection(
|
||||
@@ -180,13 +175,11 @@ class EdgeDB:
|
||||
""",
|
||||
now=current_datetime
|
||||
)
|
||||
return [
|
||||
{
|
||||
"file_id": item.file_id,
|
||||
"file_name": item.file_name,
|
||||
"expires_at": item.expires_at
|
||||
} for item in data
|
||||
]
|
||||
return [{
|
||||
"file_id": item.file_id,
|
||||
"file_name": item.file_name,
|
||||
"expires_at": item.expires_at
|
||||
} for item in data]
|
||||
|
||||
async def delete_files_by_ids(self, remove_file_ids:list[str]):
|
||||
if not remove_file_ids:
|
||||
@@ -205,11 +198,105 @@ class EdgeDB:
|
||||
pass
|
||||
|
||||
# File Access Quary Functions
|
||||
async def add_file_access(self):
|
||||
pass
|
||||
async def add_file_access(self, file_id: str, ip_address: str, status: str, user_agent: str, accessed_at):
|
||||
return await self.run_query_with_reconnection(
|
||||
self.client.query,
|
||||
"""
|
||||
WITH
|
||||
used_file := (
|
||||
SELECT files
|
||||
FILTER .file_id = <str>$file_id
|
||||
LIMIT 1
|
||||
),
|
||||
ip_obj := (
|
||||
INSERT IPAddr { value := <str>$ip_address }
|
||||
UNLESS CONFLICT ON .value
|
||||
ELSE (
|
||||
SELECT IPAddr
|
||||
FILTER .value = <str>$ip_address
|
||||
)
|
||||
),
|
||||
ua_obj := (
|
||||
INSERT UserAgent { value := <str>$user_agent }
|
||||
UNLESS CONFLICT ON .value
|
||||
ELSE (
|
||||
SELECT UserAgent
|
||||
FILTER .value = <str>$user_agent
|
||||
)
|
||||
),
|
||||
new_file_access := (
|
||||
INSERT file_access {
|
||||
at := <datetime>$accessed_at,
|
||||
status := <access_status>$status,
|
||||
ip := ip_obj,
|
||||
user_agent := ua_obj
|
||||
}
|
||||
),
|
||||
_updated_file := (
|
||||
UPDATE used_file
|
||||
SET { accesses += (SELECT new_file_access) }
|
||||
)
|
||||
|
||||
SELECT new_file_access {
|
||||
at,
|
||||
status,
|
||||
ip: { value },
|
||||
user_agent: { value },
|
||||
};
|
||||
""",
|
||||
file_id=file_id,
|
||||
accessed_at=accessed_at,
|
||||
ip_address=ip_address,
|
||||
status=status,
|
||||
user_agent=str(user_agent),
|
||||
)
|
||||
|
||||
async def get_all_file_access(self):
|
||||
pass
|
||||
data = await self.run_query_with_reconnection(
|
||||
self.client.query,
|
||||
"""
|
||||
select file_access {
|
||||
status,
|
||||
ip: {
|
||||
value
|
||||
},
|
||||
user_agent: {
|
||||
value
|
||||
},
|
||||
at
|
||||
}
|
||||
"""
|
||||
)
|
||||
return [{
|
||||
"status": str(file.status),
|
||||
"ip": file.ip.value,
|
||||
"user_agent": file.user_agent.value,
|
||||
"accessed_at": file.at,
|
||||
} for file in data]
|
||||
|
||||
async def get_file_access(self, file_id:str):
|
||||
pass
|
||||
async def get_file_access(self, file_id: str):
|
||||
data = await self.run_query_with_reconnection(
|
||||
self.client.query_single,
|
||||
"""
|
||||
SELECT files {
|
||||
accesses: {
|
||||
status,
|
||||
ip: { value },
|
||||
user_agent: { value },
|
||||
at
|
||||
}
|
||||
}
|
||||
FILTER .file_id = <str>$file_id
|
||||
LIMIT 1
|
||||
""",
|
||||
file_id=file_id,
|
||||
)
|
||||
|
||||
if data:
|
||||
return [{
|
||||
"status": str(access.status),
|
||||
"ip": access.ip.value if access.ip else None,
|
||||
"user_agent": access.user_agent.value if access.user_agent else None,
|
||||
"accessed_at": access.at,
|
||||
} for access in data.accesses]
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user