Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/alembic/script.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ from alembic import op
import sqlalchemy as sa
${imports if imports else ""}

# pylint: disable=E1101

# revision identifiers, used by Alembic.
revision: str = ${repr(up_revision)}
down_revision: Union[str, None] = ${repr(down_revision)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import sqlalchemy as sa


# pylint: disable=E1101

# revision identifiers, used by Alembic.
revision: str = "004d29805949"
down_revision: Union[str, None] = "ebad9129920d"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
Create Date: 2023-12-27 21:17:14.641409

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# pylint: disable=E1101

# revision identifiers, used by Alembic.
revision: str = "0ba6579fbbca"
down_revision: Union[str, None] = "d2f116d3976e"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import sqlalchemy as sa


# pylint: disable=E1101

# revision identifiers, used by Alembic.
revision: str = "68219fe4ad46"
down_revision: Union[str, None] = "f83699ff2dd0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from alembic import op
import sqlalchemy as sa


# pylint: disable=E1101

# revision identifiers, used by Alembic.
Expand Down
2 changes: 1 addition & 1 deletion backend/alembic/versions/94eb09ac97fe_added_user_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
from alembic import op
import sqlalchemy as sa

# pylint: disable=E1101

# pylint: disable=E1101

# revision identifiers, used by Alembic.
revision: str = "94eb09ac97fe"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""Add links between users and documents

Revision ID: a61da93aeb2b
Revises: 9356f0b5cdd9
Create Date: 2024-06-29 14:53:17.965879

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# pylint: disable=E1101

# revision identifiers, used by Alembic.
revision: str = "a61da93aeb2b"
down_revision: Union[str, None] = "9356f0b5cdd9"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# TMX part
op.add_column("tmx_document", sa.Column("created_by", sa.Integer()))
update = sa.update(sa.table("tmx_document", sa.Column("created_by"))).values(
created_by=1 # first administrator account
)
op.execute(update)
op.alter_column("tmx_document", "created_by", nullable=False)
op.create_foreign_key(
"tmx_document_user_id_key", "tmx_document", "user", ["created_by"], ["id"]
)

# XLIFF part
op.add_column("xliff_document", sa.Column("created_by", sa.Integer()))
update = sa.update(sa.table("xliff_document", sa.Column("created_by"))).values(
created_by=1 # first administrator account
)
op.execute(update)
op.alter_column("xliff_document", "created_by", nullable=False)
op.create_foreign_key(
"xliff_document_user_id_key", "xliff_document", "user", ["created_by"], ["id"]
)


def downgrade() -> None:
op.drop_constraint(
"xliff_document_user_id_key", "xliff_document", type_="foreignkey"
)
op.drop_column("xliff_document", "created_by")
op.drop_constraint("tmx_document_user_id_key", "tmx_document", type_="foreignkey")
op.drop_column("tmx_document", "created_by")
3 changes: 3 additions & 0 deletions backend/alembic/versions/ab1574d1a1da_added_tmx_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
Create Date: 2023-12-06 21:40:05.738555

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# pylint: disable=E1101

# revision identifiers, used by Alembic.
revision: str = "ab1574d1a1da"
down_revision: Union[str, None] = None
Expand Down
3 changes: 3 additions & 0 deletions backend/alembic/versions/d2f116d3976e_added_xliff_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
Create Date: 2023-12-07 00:56:49.742016

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# pylint: disable=E1101

# revision identifiers, used by Alembic.
revision: str = "d2f116d3976e"
down_revision: Union[str, None] = "ab1574d1a1da"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from alembic import op
import sqlalchemy as sa

# pylint: disable=E1101

# pylint: disable=E1101

# revision identifiers, used by Alembic.
revision: str = "dc95266888cd"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
Create Date: 2024-01-02 20:17:06.133690

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# pylint: disable=E1101

# revision identifiers, used by Alembic.
revision: str = "ebad9129920d"
down_revision: Union[str, None] = "0ba6579fbbca"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import sqlalchemy as sa


# pylint: disable=E1101

# revision identifiers, used by Alembic.
revision: str = "f83699ff2dd0"
down_revision: Union[str, None] = "004d29805949"
Expand Down
2 changes: 2 additions & 0 deletions backend/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Identified(BaseModel):

class TmxFile(Identified):
name: str
created_by: int


class TmxFileRecord(Identified):
Expand All @@ -47,6 +48,7 @@ class TmxFileWithRecords(TmxFile):
class XliffFile(Identified):
name: str
status: DocumentStatus
created_by: int


class XliffFileRecord(Identified):
Expand Down
18 changes: 13 additions & 5 deletions backend/app/routers/tmx.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from sqlalchemy.orm import Session

from app import schema
from app.auth import has_user_role
from app.auth import has_user_role, get_current_user_id
from app.db import get_db
from app.tmx import extract_tmx_content
from app.models import TmxFile, TmxFileWithRecords, TmxFileRecord, StatusMessage
Expand All @@ -16,7 +16,9 @@
@router.get("/")
def get_tmxs(db: Annotated[Session, Depends(get_db)]) -> list[TmxFile]:
docs = db.query(schema.TmxDocument).order_by(schema.TmxDocument.id).all()
return [TmxFile(id=doc.id, name=doc.name) for doc in docs]
return [
TmxFile(id=doc.id, name=doc.name, created_by=doc.created_by) for doc in docs
]


@router.get("/{tmx_id}")
Expand All @@ -30,6 +32,7 @@ def get_tmx(tmx_id: int, db: Annotated[Session, Depends(get_db)]) -> TmxFileWith
return TmxFileWithRecords(
id=doc.id,
name=doc.name,
created_by=doc.created_by,
records=[
TmxFileRecord(id=record.id, source=record.source, target=record.target)
for record in doc.records
Expand All @@ -39,13 +42,18 @@ def get_tmx(tmx_id: int, db: Annotated[Session, Depends(get_db)]) -> TmxFileWith

@router.post("/")
async def create_tmx(
file: Annotated[UploadFile, File()], db: Annotated[Session, Depends(get_db)]
file: Annotated[UploadFile, File()],
db: Annotated[Session, Depends(get_db)],
current_user: Annotated[int, Depends(get_current_user_id)],
) -> TmxFile:
name = file.filename
tmx_data = await file.read()
segments = extract_tmx_content(tmx_data)

doc = schema.TmxDocument(name=name)
doc = schema.TmxDocument(
name=name,
created_by=current_user,
)
db.add(doc)
db.commit()

Expand All @@ -65,7 +73,7 @@ async def create_tmx(
)
assert new_doc

return TmxFile(id=new_doc.id, name=new_doc.name)
return TmxFile(id=new_doc.id, name=new_doc.name, created_by=doc.created_by)


@router.delete("/{tmx_id}")
Expand Down
10 changes: 8 additions & 2 deletions backend/app/routers/xliff.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from sqlalchemy.orm import Session

from app import schema, models
from app.auth import has_user_role
from app.auth import has_user_role, get_current_user_id
from app.db import get_db
from app.xliff import extract_xliff_content

Expand All @@ -32,6 +32,7 @@ def get_xliffs(db: Annotated[Session, Depends(get_db)]) -> list[models.XliffFile
id=xliff.id,
name=xliff.name,
status=models.DocumentStatus(xliff.processing_status),
created_by=xliff.created_by,
)
for xliff in xliffs
]
Expand All @@ -51,6 +52,7 @@ def get_xliff(doc_id: int, db: Annotated[Session, Depends(get_db)]) -> models.Xl
id=doc.id,
name=doc.name,
status=models.DocumentStatus(doc.processing_status),
created_by=doc.created_by,
)


Expand Down Expand Up @@ -96,7 +98,9 @@ def delete_xliff(

@router.post("/")
async def create_xliff(
file: Annotated[UploadFile, File()], db: Annotated[Session, Depends(get_db)]
file: Annotated[UploadFile, File()],
db: Annotated[Session, Depends(get_db)],
current_user: Annotated[int, Depends(get_current_user_id)],
) -> models.XliffFile:
cutoff_date = datetime.now() - timedelta(days=1)

Expand All @@ -120,6 +124,7 @@ async def create_xliff(
original_document=original_document,
processing_status=models.DocumentStatus.UPLOADED.value,
upload_time=datetime.now(),
created_by=current_user,
)
db.add(doc)
db.commit()
Expand All @@ -131,6 +136,7 @@ async def create_xliff(
id=new_doc.id,
name=new_doc.name,
status=models.DocumentStatus(new_doc.processing_status),
created_by=new_doc.created_by,
)


Expand Down
11 changes: 11 additions & 0 deletions backend/app/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ class TmxDocument(Base):

id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column()
created_by: Mapped[int] = mapped_column(ForeignKey("user.id"))

records: Mapped[list["TmxRecord"]] = relationship(
back_populates="document", cascade="all, delete-orphan", order_by="TmxRecord.id"
)
user: Mapped["User"] = relationship(back_populates="tmxs")


class TmxRecord(Base):
Expand All @@ -38,6 +40,7 @@ class XliffDocument(Base):

id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column()
created_by: Mapped[int] = mapped_column(ForeignKey("user.id"))
original_document: Mapped[str] = mapped_column()
processing_status: Mapped[str] = mapped_column()
upload_time: Mapped[datetime] = mapped_column(default=datetime.now(UTC))
Expand All @@ -47,6 +50,7 @@ class XliffDocument(Base):
cascade="all, delete-orphan",
order_by="XliffRecord.id",
)
user: Mapped["User"] = relationship(back_populates="xliffs")


class XliffRecord(Base):
Expand Down Expand Up @@ -78,3 +82,10 @@ class User(Base):
email: Mapped[str] = mapped_column(unique=True)
role: Mapped[str] = mapped_column(default="user")
disabled: Mapped[bool] = mapped_column(default=False)

tmxs: Mapped[list["TmxDocument"]] = relationship(
back_populates="user", cascade="all, delete-orphan", order_by="TmxDocument.id"
)
xliffs: Mapped[list["XliffDocument"]] = relationship(
back_populates="user", cascade="all, delete-orphan", order_by="XliffDocument.id"
)
11 changes: 11 additions & 0 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from app import create_app, db, schema, models
from app.db import init_connection, get_db

# pylint: disable=C0116


@contextmanager
def session():
Expand Down Expand Up @@ -50,6 +52,15 @@ def user_logged_client(fastapi_client: TestClient):
disabled=False,
)
)
s.add(
schema.User(
username="test-admin",
password="$pbkdf2-sha256$29000$R4gxRkjpnXNOqXXundP6Xw$pzr2kyXZjurvt6sUv7NF4dQhpHdv9RBtlGbOStnFyUM",
email="admin@test.com",
role=models.UserRole.ADMIN.value,
disabled=False,
)
)
s.commit()

fastapi_client.post(
Expand Down
2 changes: 2 additions & 0 deletions backend/tests/test_routes_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from app import schema, models
from app.db import get_db

# pylint: disable=C0116


@contextmanager
def session():
Expand Down
Loading