Skip to content
Snippets Groups Projects
Commit 51011bf5 authored by Tim Repke's avatar Tim Repke
Browse files

Merge branch 'master' into 'production'

Master

See merge request !37
parents d5f8a698 bea803f0
No related branches found
No related tags found
1 merge request!37Master
Pipeline #1298 passed
flake8==6.0.0
tox==4.4.6
tox==4.4.8
pytest==7.2.2
pytest-cov==4.0.0
mypy==1.1.1
types-toml==0.10.8.5
types-PyYAML==6.0.12.8
\ No newline at end of file
types-toml==0.10.8.6
types-PyYAML==6.0.12.9
\ No newline at end of file
......@@ -7,6 +7,7 @@ from .routes import projects
from .routes import project
from .routes import imports
from .routes import events
from .routes import highlight
# this router proxies all /api endpoints
router = APIRouter()
......@@ -34,3 +35,6 @@ router.include_router(imports.router, prefix='/imports', tags=['imports'])
# route for triggering events in the system
router.include_router(events.router, prefix='/events', tags=['events'])
# route for listing and editing highlighters
router.include_router(highlight.router, prefix='/highlighters', tags=['highlighters'])
......@@ -24,7 +24,10 @@ from nacsos_data.models.bot_annotations import \
BotAnnotationModel, \
AnnotationCollection, \
BotMetaResolve, \
GroupedBotAnnotation, AnnotationCollectionDB, BotKind, BotAnnotationMetaDataBaseModel
GroupedBotAnnotation, \
AnnotationCollectionDB, \
BotKind, \
BotAnnotationMetaDataBaseModel
from nacsos_data.models.users import UserModel
from nacsos_data.models.items import AnyItemModel
from nacsos_data.db.crud.items import read_any_item_by_item_id
......@@ -52,15 +55,18 @@ from nacsos_data.db.crud.annotations import \
AssignmentCounts, \
UserProjectAssignmentScope, \
store_assignments, \
store_resolved_bot_annotations, update_resolved_bot_annotations
store_resolved_bot_annotations, \
update_resolved_bot_annotations
from nacsos_data.util.annotations.resolve import \
AnnotationFilterObject, \
get_resolved_item_annotations, read_bot_annotations
get_resolved_item_annotations, \
read_bot_annotations
from nacsos_data.util.annotations.validation import \
merge_scheme_and_annotations, \
annotated_scheme_to_annotations, \
flatten_annotation_scheme
from nacsos_data.util.annotations.assignments.random import random_assignments
from nacsos_data.util.annotations.assignments.random_exclusion import random_assignments_with_exclusion
from server.api.errors import \
SaveFailedError, \
......@@ -356,6 +362,16 @@ async def make_assignments(payload: MakeAssignmentsRequestModel,
except ValueError as e:
raise HTTPException(status_code=http_status.HTTP_400_BAD_REQUEST,
detail=str(e))
elif payload.config.config_type == 'random_exclusion':
try:
assignments = await random_assignments_with_exclusion(assignment_scope_id=payload.scope_id,
annotation_scheme_id=payload.annotation_scheme_id,
project_id=permissions.permissions.project_id,
config=payload.config, # type: ignore[arg-type] # FIXME
engine=db_engine)
except ValueError as e:
raise HTTPException(status_code=http_status.HTTP_400_BAD_REQUEST,
detail=str(e))
else:
raise HTTPException(status_code=http_status.HTTP_501_NOT_IMPLEMENTED,
detail=f'Method "{payload.config.config_type}" is unknown.')
......
from typing import TYPE_CHECKING
from nacsos_data.util.auth import UserPermissions
from sqlalchemy import select
from fastapi import APIRouter, Depends
from nacsos_data.db.schemas.annotations import AssignmentScope
from nacsos_data.db.schemas.highlight import Highlighter
from nacsos_data.models.highlight import HighlighterModel
from server.api.errors import \
NoDataForKeyError, \
DataNotFoundWarning
from server.util.security import UserPermissionChecker, InsufficientPermissions
from server.data import db_engine
if TYPE_CHECKING:
from sqlalchemy.ext.asyncio import AsyncSession # noqa F401
router = APIRouter()
@router.get('/scope/{assignment_scope_id}', response_model=HighlighterModel | None)
async def get_scope_highlighter(assignment_scope_id: str,
permissions: UserPermissions = Depends(UserPermissionChecker('annotations_read'))) \
-> HighlighterModel:
async with db_engine.session() as session: # type: AsyncSession
stmt = select(Highlighter) \
.join(AssignmentScope,
AssignmentScope.highlighter_id == Highlighter.highlighter_id) \
.where(Highlighter.project_id == permissions.permissions.project_id,
AssignmentScope.assignment_scope_id == assignment_scope_id)
result = (await session.scalars(stmt)).one_or_none()
if result is not None:
return HighlighterModel.parse_obj(result.__dict__)
raise DataNotFoundWarning(f'No highlighter in project {permissions.permissions.project_id} '
f'for scope with id {assignment_scope_id}!')
@router.get('/project', response_model=list[HighlighterModel])
async def get_project_highlighters(permissions: UserPermissions = Depends(UserPermissionChecker('annotations_read'))) \
-> list[HighlighterModel]:
print('HERE!')
async with db_engine.session() as session: # type: AsyncSession
stmt = select(Highlighter).where(Highlighter.project_id == permissions.permissions.project_id)
results = (await session.scalars(stmt)).all()
return [HighlighterModel.parse_obj(r.__dict__) for r in results]
@router.put('/project', response_model=str)
async def upsert_highlighter(highlighter: HighlighterModel,
permissions: UserPermissions = Depends(UserPermissionChecker('annotations_edit'))) \
-> str:
if str(permissions.permissions.project_id) != str(highlighter.project_id):
raise InsufficientPermissions('Project IDs don\'t match!')
async with db_engine.session() as session: # type: AsyncSession
stmt = select(Highlighter).where(Highlighter.project_id == permissions.permissions.project_id,
Highlighter.highlighter_id == highlighter.highlighter_id)
result: Highlighter | None = (await session.scalars(stmt)).one_or_none()
if result is not None:
result.name = highlighter.name
result.style = highlighter.style
result.keywords = highlighter.keywords
else:
new_highlighter = Highlighter(**highlighter.dict())
session.add(new_highlighter)
await session.commit()
return str(highlighter.highlighter_id)
@router.get('/{highlighter_id}', response_model=HighlighterModel | None)
async def get_highlighter(highlighter_id: str,
permissions: UserPermissions = Depends(UserPermissionChecker('annotations_read'))) \
-> HighlighterModel:
async with db_engine.session() as session: # type: AsyncSession
stmt = select(Highlighter).where(Highlighter.project_id == permissions.permissions.project_id,
Highlighter.highlighter_id == highlighter_id)
result = (await session.scalars(stmt)).one_or_none()
if result is not None:
return HighlighterModel.parse_obj(result.__dict__)
raise NoDataForKeyError(f'No highlighter in project {permissions.permissions.project_id} '
f'with id {highlighter_id}!')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment