Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Sep 9, 2024
1 parent 93a0cbb commit 2cdea48
Show file tree
Hide file tree
Showing 96 changed files with 3,965 additions and 3,271 deletions.
1 change: 1 addition & 0 deletions backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# from flask_cors import CORS
# from flask_migrate import Migrate
from requests_oauthlib import OAuth2Session

# from flask_restful import Api
import sqlalchemy
# from flask_mail import Mail
Expand Down
258 changes: 129 additions & 129 deletions backend/api/annotations/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from backend.models.postgis.task_annotation import TaskAnnotation
from backend.services.project_service import ProjectService
from backend.services.task_annotations_service import TaskAnnotationsService
from backend.services.application_service import ApplicationService
from fastapi import APIRouter, Depends, Request
from backend.db import get_session
from starlette.authentication import requires
Expand All @@ -15,146 +14,147 @@
responses={404: {"description": "Not found"}},
)


# class AnnotationsRestAPI(Resource):
@router.get("/{project_id}/annotations/{annotation_type}/")
@router.get("/{project_id}/annotations/")
async def get(request: Request, project_id: int, annotation_type: str = None):
"""
Get all task annotations for a project
---
tags:
- annotations
produces:
- application/json
parameters:
- name: project_id
in: path
description: The ID of the project
required: true
type: integer
- name: annotation_type
in: path
description: The type of annotation to fetch
required: false
type: integer
responses:
200:
description: Project Annotations
404:
description: Project or annotations not found
500:
description: Internal Server Error
"""
ProjectService.exists(project_id)
if annotation_type:
annotations = TaskAnnotation.get_task_annotations_by_project_id_type(
project_id, annotation_type
)
else:
annotations = TaskAnnotation.get_task_annotations_by_project_id(project_id)
return annotations.model_dump(by_alias=True), 200
"""
Get all task annotations for a project
---
tags:
- annotations
produces:
- application/json
parameters:
- name: project_id
in: path
description: The ID of the project
required: true
type: integer
- name: annotation_type
in: path
description: The type of annotation to fetch
required: false
type: integer
responses:
200:
description: Project Annotations
404:
description: Project or annotations not found
500:
description: Internal Server Error
"""
ProjectService.exists(project_id)
if annotation_type:
annotations = TaskAnnotation.get_task_annotations_by_project_id_type(
project_id, annotation_type
)
else:
annotations = TaskAnnotation.get_task_annotations_by_project_id(project_id)
return annotations.model_dump(by_alias=True), 200


@router.post("/{project_id}/annotations/{annotation_type}/")
@requires("authenticated")
async def post(request: Request, project_id: int, annotation_type: str):
"""
Store new task annotations for tasks of a project
---
tags:
- annotations
produces:
- application/json
parameters:
- in: header
name: Content-Type
description: Content type for post body
required: true
type: string
default: application/json
- name: project_id
in: path
description: Unique project ID
required: true
type: integer
- name: annotation_type
in: path
description: Annotation type
required: true
type: string
- name: Application-Token
in: header
description: Application token registered with TM
required: true
type: string
- in: body
name: body
required: true
description: JSON object for creating draft project
schema:
projectId:
type: integer
required: true
annotationType:
type: string
required: true
tasks:
type: array
required: true
items:
schema:
taskId:
type: integer
required: true
annotationSource:
type: string
annotationMarkdown:
type: string
properties:
description: JSON object with properties
responses:
200:
description: Project updated
400:
description: Client Error - Invalid Request
404:
description: Project or task not found
500:
description: Internal Server Error
"""
"""
Store new task annotations for tasks of a project
---
tags:
- annotations
produces:
- application/json
parameters:
- in: header
name: Content-Type
description: Content type for post body
required: true
type: string
default: application/json
- name: project_id
in: path
description: Unique project ID
required: true
type: integer
- name: annotation_type
in: path
description: Annotation type
required: true
type: string
- name: Application-Token
in: header
description: Application token registered with TM
required: true
type: string
- in: body
name: body
required: true
description: JSON object for creating draft project
schema:
projectId:
type: integer
required: true
annotationType:
type: string
required: true
tasks:
type: array
required: true
items:
schema:
taskId:
type: integer
required: true
annotationSource:
type: string
annotationMarkdown:
type: string
properties:
description: JSON object with properties
responses:
200:
description: Project updated
400:
description: Client Error - Invalid Request
404:
description: Project or task not found
500:
description: Internal Server Error
"""

# if "Application-Token" in request.headers:
# application_token = request.headers["Application-Token"]
# is_valid_token = ApplicationService.check_token(application_token) # noqa
# else:
# logger.error("No token supplied")
# return {"Error": "No token supplied", "SubCode": "NotFound"}, 500
# if "Application-Token" in request.headers:
# application_token = request.headers["Application-Token"]
# is_valid_token = ApplicationService.check_token(application_token) # noqa
# else:
# logger.error("No token supplied")
# return {"Error": "No token supplied", "SubCode": "NotFound"}, 500

try:
annotations = await request.json() or {}
except Exception as e:
logger.error(f"Error validating request: {str(e)}")
try:
annotations = await request.json() or {}
except Exception as e:
logger.error(f"Error validating request: {str(e)}")

ProjectService.exists(project_id)
ProjectService.exists(project_id)

task_ids = [t["taskId"] for t in annotations["tasks"]]
task_ids = [t["taskId"] for t in annotations["tasks"]]

# check if task ids are valid
tasks = Task.get_tasks(project_id, task_ids)
tasks_ids_db = [t.id for t in tasks]
if len(task_ids) != len(tasks_ids_db):
return {"Error": "Invalid task id"}, 500
# check if task ids are valid
tasks = Task.get_tasks(project_id, task_ids)
tasks_ids_db = [t.id for t in tasks]
if len(task_ids) != len(tasks_ids_db):
return {"Error": "Invalid task id"}, 500

for annotation in annotations["tasks"]:
try:
TaskAnnotationsService.add_or_update_annotation(
annotation, project_id, annotation_type
)
except Exception as e:
logger.error(f"Error creating annotations: {str(e)}")
return {
"Error": "Error creating annotations",
"SubCode": "InvalidData",
}, 400

return project_id, 200
for annotation in annotations["tasks"]:
try:
TaskAnnotationsService.add_or_update_annotation(
annotation, project_id, annotation_type
)
except Exception as e:
logger.error(f"Error creating annotations: {str(e)}")
return {
"Error": "Error creating annotations",
"SubCode": "InvalidData",
}, 400

return project_id, 200
43 changes: 32 additions & 11 deletions backend/api/campaigns/resources.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from backend.models.dtos.campaign_dto import CampaignDTO, CampaignListDTO, NewCampaignDTO
from backend.models.dtos.campaign_dto import (
CampaignDTO,
CampaignListDTO,
NewCampaignDTO,
)
from backend.services.campaign_service import CampaignService
from backend.services.organisation_service import OrganisationService
from fastapi import APIRouter, Depends, Request
from backend.db import get_db
from starlette.authentication import requires
from loguru import logger
from sqlalchemy.ext.asyncio import AsyncSession
from databases import Database
from fastapi import HTTPException
from backend.services.users.authentication_service import login_required
from backend.models.dtos.user_dto import AuthUserDTO

Expand All @@ -18,8 +18,11 @@
responses={404: {"description": "Not found"}},
)


@router.get("/{campaign_id}/", response_model=CampaignDTO)
async def retrieve_campaign(request: Request, campaign_id: int, db: Database = Depends(get_db)):
async def retrieve_campaign(
request: Request, campaign_id: int, db: Database = Depends(get_db)
):
"""
Get an active campaign's information
---
Expand Down Expand Up @@ -58,7 +61,13 @@ async def retrieve_campaign(request: Request, campaign_id: int, db: Database = D


@router.patch("/{campaign_id}/")
async def update_campaign(campaign_dto : CampaignDTO,request: Request, campaign_id: int, user: AuthUserDTO = Depends(login_required), db: Database = Depends(get_db)):
async def update_campaign(
campaign_dto: CampaignDTO,
request: Request,
campaign_id: int,
user: AuthUserDTO = Depends(login_required),
db: Database = Depends(get_db),
):
"""
Updates an existing campaign
---
Expand Down Expand Up @@ -122,7 +131,9 @@ async def update_campaign(campaign_dto : CampaignDTO,request: Request, campaign_
description: Internal Server Error
"""
try:
orgs_dto = await OrganisationService.get_organisations_managed_by_user_as_dto(user.id, db)
orgs_dto = await OrganisationService.get_organisations_managed_by_user_as_dto(
user.id, db
)
if len(orgs_dto.organisations) < 1:
raise ValueError("User not a Org Manager")
except ValueError as e:
Expand All @@ -134,10 +145,15 @@ async def update_campaign(campaign_dto : CampaignDTO,request: Request, campaign_
except ValueError:
error_msg = "Campaign PATCH - name already exists"
return {"Error": error_msg, "SubCode": "NameExists"}


@router.delete("/{campaign_id}/")
async def delete_campaign(request: Request, campaign_id: int, user: AuthUserDTO = Depends(login_required), db: Database = Depends(get_db)):
async def delete_campaign(
request: Request,
campaign_id: int,
user: AuthUserDTO = Depends(login_required),
db: Database = Depends(get_db),
):
"""
Deletes an existing campaign
---
Expand Down Expand Up @@ -214,7 +230,12 @@ async def list_campaigns(


@router.post("/")
async def create_campaign(campaign_dto: NewCampaignDTO,request: Request, user: AuthUserDTO = Depends(login_required), db: Database = Depends(get_db)):
async def create_campaign(
campaign_dto: NewCampaignDTO,
request: Request,
user: AuthUserDTO = Depends(login_required),
db: Database = Depends(get_db),
):
"""
Creates a new campaign
---
Expand Down
Loading

0 comments on commit 2cdea48

Please sign in to comment.