Add FileService
Signed-off-by: Tiago Garcia <tiago.rgarcia@ua.pt>
This commit is contained in:
parent
5d826f409b
commit
0b0b42dac7
|
@ -9,8 +9,8 @@ class File(Base):
|
|||
__tablename__ = 'files'
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
|
||||
file_handle: Mapped[str] = mapped_column(String, nullable=False)
|
||||
document_handle: Mapped[str] = mapped_column(String, nullable=False)
|
||||
file_handle: Mapped[str] = mapped_column(String, unique=True, nullable=False)
|
||||
document_handle: Mapped[str] = mapped_column(String, unique=True, nullable=True)
|
||||
name: Mapped[str] = mapped_column(String, nullable=False)
|
||||
created_at: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
org_id: Mapped[int] = mapped_column(Integer, ForeignKey('organizations.id'), nullable=False)
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
from .orgs import OrganizationService
|
||||
from .users import UserService
|
||||
from .files import FileService
|
|
@ -0,0 +1,56 @@
|
|||
import os
|
||||
from datetime import datetime
|
||||
from typing import List, Type
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
from models import File, Organization, User
|
||||
|
||||
|
||||
class FileService:
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
self.repos = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "repository")
|
||||
|
||||
def create_dummy_file(self, org: Organization, user: User) -> File:
|
||||
file = File(
|
||||
file_handle = "dummy_file",
|
||||
document_handle = "org/dummy_file.txt",
|
||||
name = "dummy_file",
|
||||
created_at = int(datetime.now().timestamp()),
|
||||
org_id = 1,
|
||||
creator_id = 1,
|
||||
org = org,
|
||||
creator = user
|
||||
)
|
||||
|
||||
file_path = os.path.join(self.repos, file.document_handle)
|
||||
with open(file_path, "w") as f:
|
||||
f.write("Dummy file content")
|
||||
|
||||
self.db.add(file)
|
||||
self.db.commit()
|
||||
self.db.refresh(file)
|
||||
return file
|
||||
|
||||
def get_file(self, file_id: int) -> File | None:
|
||||
return self.db.query(File).filter(File.id == file_id).first()
|
||||
|
||||
def get_file_by_document_handle(self, document_handle: str) -> File | None:
|
||||
return self.db.query(File).filter(File.document_handle == document_handle).first()
|
||||
|
||||
def get_file_by_file_handle(self, file_handle: str) -> File | None:
|
||||
return self.db.query(File).filter(File.file_handle == file_handle).first()
|
||||
|
||||
def list_files(self) -> list[Type[File]]:
|
||||
return self.db.query(File).all()
|
||||
|
||||
def list_files_in_org(self, org: Organization) -> list[Type[File]]:
|
||||
return self.db.query(File).filter(File.org_id == org.id).all()
|
||||
|
||||
def delete_file(self, file: File) -> File:
|
||||
file_path = os.path.join(self.repos, file.document_handle)
|
||||
os.remove(file_path)
|
||||
file.document_handle = None
|
||||
self.db.commit()
|
||||
self.db.refresh(file)
|
||||
return file
|
|
@ -1,3 +1,5 @@
|
|||
import os.path
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
from models import Organization
|
||||
|
||||
|
@ -12,6 +14,11 @@ class OrganizationService:
|
|||
if not user:
|
||||
user = UserService(self.db).create_user(username, full_name, email, public_key)
|
||||
|
||||
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
repos = os.path.join(project_root, "repository")
|
||||
if not os.path.exists(os.path.join(repos, name)):
|
||||
os.mkdir(os.path.join(repos, name))
|
||||
|
||||
organization = Organization(
|
||||
name=name,
|
||||
owner_id=user.id,
|
||||
|
|
|
@ -1,14 +1,16 @@
|
|||
import os
|
||||
from pprint import PrettyPrinter
|
||||
|
||||
import database.setup_db
|
||||
from database import SessionLocal
|
||||
from services import OrganizationService, UserService
|
||||
from services import OrganizationService, UserService, FileService
|
||||
|
||||
database.setup_db.setup_db()
|
||||
|
||||
db = SessionLocal()
|
||||
org_service = OrganizationService(db)
|
||||
user_service = UserService(db)
|
||||
file_service = FileService(db)
|
||||
|
||||
def test_create_organization():
|
||||
org = org_service.create_organization(
|
||||
|
@ -134,3 +136,77 @@ def test_get_user():
|
|||
db.delete(org)
|
||||
db.delete(user)
|
||||
db.commit()
|
||||
|
||||
def test_create_dummy_file():
|
||||
|
||||
org = org_service.create_organization(
|
||||
name="org",
|
||||
username="testuser",
|
||||
full_name="Test User",
|
||||
email="test@mail.com",
|
||||
public_key="abc123"
|
||||
)
|
||||
|
||||
file = file_service.create_dummy_file(org, org.owner)
|
||||
|
||||
assert file.file_handle == "dummy_file"
|
||||
assert file.document_handle == "org/dummy_file.txt"
|
||||
assert file.name == "dummy_file"
|
||||
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
repos = os.path.join(project_root, "repository")
|
||||
with open(os.path.join(repos, file.document_handle), "r") as f:
|
||||
assert f.read() == "Dummy file content"
|
||||
|
||||
|
||||
os.remove(os.path.join(repos, file.document_handle))
|
||||
db.delete(file)
|
||||
db.delete(org.owner)
|
||||
db.delete(org)
|
||||
db.commit()
|
||||
|
||||
|
||||
def test_get_file():
|
||||
org = org_service.create_organization(
|
||||
name="org",
|
||||
username="testuser",
|
||||
full_name="Test User",
|
||||
email="test@mail.com",
|
||||
public_key="abc123"
|
||||
)
|
||||
|
||||
file = file_service.create_dummy_file(org, org.owner)
|
||||
file2 = file_service.get_file_by_file_handle("dummy_file")
|
||||
|
||||
assert file2.file_handle == "dummy_file"
|
||||
assert file2.document_handle == "org/dummy_file.txt"
|
||||
assert file2.name == "dummy_file"
|
||||
|
||||
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
repos = os.path.join(project_root, "repository")
|
||||
os.remove(os.path.join(repos, file.document_handle))
|
||||
db.delete(file)
|
||||
db.delete(org.owner)
|
||||
db.delete(org)
|
||||
db.commit()
|
||||
|
||||
|
||||
def test_delete_file():
|
||||
org = org_service.create_organization(
|
||||
name="org",
|
||||
username="testuser",
|
||||
full_name="Test User",
|
||||
email="test@mail.com",
|
||||
public_key="abc123"
|
||||
)
|
||||
|
||||
file = file_service.create_dummy_file(org, org.owner)
|
||||
file2 = file_service.delete_file(file)
|
||||
|
||||
assert file2.file_handle == "dummy_file"
|
||||
assert file2.document_handle is None
|
||||
assert os.path.exists(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "repository", "org", "dummy_file.txt")) == False
|
||||
|
||||
db.delete(file)
|
||||
db.delete(org.owner)
|
||||
db.delete(org)
|
||||
db.commit()
|
Loading…
Reference in New Issue