Repository navigation
Expand file tree
/
Copy pathviews.py
More file actions
106 lines (90 loc) · 3.27 KB
/
Copy pathviews.py
File metadata and controls
106 lines (90 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from typing import List
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.orm import Session
from dispatch.common.utils.views import create_pydantic_include
from dispatch.auth.models import DispatchUser
from dispatch.auth.service import get_current_user
from dispatch.database import get_db, search_filter_sort_paginate
from .models import TaskCreate, TaskUpdate, TaskRead, TaskPagination
from .service import get, update, create, delete
router = APIRouter()
@router.get("/", summary="Retrieve a list of all tasks.")
def get_tasks(
db_session: Session = Depends(get_db),
page: int = 1,
items_per_page: int = Query(5, alias="itemsPerPage"),
query_str: str = Query(None, alias="q"),
sort_by: List[str] = Query([], alias="sortBy[]"),
descending: List[bool] = Query([], alias="descending[]"),
fields: List[str] = Query([], alias="fields[]"),
ops: List[str] = Query([], alias="ops[]"),
values: List[str] = Query([], alias="values[]"),
include: List[str] = Query([], alias="include[]"),
):
"""
Retrieve all tasks.
"""
pagination = search_filter_sort_paginate(
db_session=db_session,
model="Task",
query_str=query_str,
page=page,
items_per_page=items_per_page,
sort_by=sort_by,
descending=descending,
fields=fields,
values=values,
ops=ops,
join_attrs=[
("incident", "incident"),
("incident_type", "incident"),
("incident_priority", "incident"),
("tags", "tag"),
("creator", "creator"),
("owner", "owner"),
],
)
if include:
# only allow two levels for now
include_sets = create_pydantic_include(include)
include_fields = {
"items": {"__all__": include_sets},
"itemsPerPage": ...,
"page": ...,
"total": ...,
}
return TaskPagination(**pagination).dict(include=include_fields)
return TaskPagination(**pagination).dict()
@router.post("/", response_model=TaskRead, tags=["tasks"])
def create_task(
*,
db_session: Session = Depends(get_db),
task_in: TaskCreate,
current_user: DispatchUser = Depends(get_current_user),
):
"""
Creates a new task.
"""
task_in.creator = {"individual": {"email": current_user.email}}
task = create(db_session=db_session, task_in=task_in)
return task
@router.put("/{task_id}", response_model=TaskRead, tags=["tasks"])
def update_task(*, db_session: Session = Depends(get_db), task_id: int, task_in: TaskUpdate):
"""
Updates an existing task.
"""
task = get(db_session=db_session, task_id=task_id)
if not task:
raise HTTPException(status_code=404, detail="A task with this id does not exist.")
task = update(db_session=db_session, task=task, task_in=task_in)
return task
@router.delete("/{task_id}", response_model=TaskRead, tags=["tasks"])
def delete_task(*, db_session: Session = Depends(get_db), task_id: int):
"""
Deletes an existing task.
"""
task = get(db_session=db_session, task_id=task_id)
if not task:
raise HTTPException(status_code=404, detail="A task with this id does not exist.")
delete(db_session=db_session, task_id=task_id)
return task