-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] Add ability to create reports (#3189)
- Loading branch information
Showing
191 changed files
with
11,249 additions
and
482 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import uuid | ||
|
||
import sqlalchemy as sa | ||
|
||
from aim.web.api.db import Base | ||
from aim.web.api.utils import datetime_now | ||
|
||
|
||
class Report(Base): | ||
__tablename__ = 'reports' | ||
uuid = sa.Column(sa.Text, primary_key=True) | ||
code = sa.Column(sa.Text) | ||
name = sa.Column(sa.Text) | ||
description = sa.Column(sa.Text) | ||
|
||
created_at = sa.Column(sa.DateTime, default=datetime_now) | ||
updated_at = sa.Column(sa.DateTime, default=datetime_now, onupdate=datetime_now) | ||
|
||
def __init__(self, code, name, description): | ||
self.uuid = str(uuid.uuid1()) | ||
self.code = code | ||
self.name = name | ||
self.description = description |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from datetime import datetime | ||
from typing import List, Optional | ||
from uuid import UUID | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
# response models | ||
class ReportOut(BaseModel): | ||
id: UUID | ||
name: str | ||
code: Optional[str] = None | ||
description: Optional[str] = None | ||
updated_at: datetime = 'Wed, 01 Jan 2021 16:12:07 GMT' | ||
created_at: datetime = 'Wed, 01 Jan 2021 16:12:07 GMT' | ||
|
||
|
||
# request models | ||
class ReportUpdateIn(BaseModel): | ||
name: Optional[str] = None | ||
code: Optional[str] = None | ||
description: Optional[str] = None | ||
|
||
|
||
class ReportCreateIn(BaseModel): | ||
name: str | ||
code: Optional[str] = None | ||
description: Optional[str] = None | ||
|
||
|
||
ReportListOut = List[ReportOut] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from aim.web.api.reports.models import Report | ||
|
||
|
||
def report_response_serializer(report_object): | ||
if not isinstance(report_object, Report): | ||
return None | ||
|
||
response = { | ||
'id': report_object.uuid, | ||
'code': report_object.code, | ||
'name': report_object.name, | ||
'description': report_object.description, | ||
'updated_at': report_object.updated_at, | ||
'created_at': report_object.created_at, | ||
} | ||
|
||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from aim.web.api.db import get_session | ||
from aim.web.api.reports.models import Report | ||
from aim.web.api.reports.pydantic_models import ( | ||
ReportCreateIn, | ||
ReportListOut, | ||
ReportOut, | ||
ReportUpdateIn, | ||
) | ||
from aim.web.api.reports.serializers import report_response_serializer | ||
from aim.web.api.utils import APIRouter # wrapper for fastapi.APIRouter | ||
from fastapi import Depends, HTTPException | ||
from sqlalchemy.orm import Session | ||
|
||
|
||
reports_router = APIRouter() | ||
|
||
|
||
@reports_router.get('/', response_model=ReportListOut) | ||
async def reports_list_api(session: Session = Depends(get_session)): | ||
reports_query = session.query(Report).order_by(Report.updated_at) | ||
result = [report_response_serializer(report) for report in reports_query] | ||
return result | ||
|
||
|
||
@reports_router.post('/', status_code=201, response_model=ReportOut) | ||
async def reports_post_api(request_data: ReportCreateIn, session: Session = Depends(get_session)): | ||
report = Report(request_data.code, request_data.name, request_data.description) | ||
session.add(report) | ||
session.commit() | ||
return report_response_serializer(report) | ||
|
||
|
||
@reports_router.get('/{report_id}/', response_model=ReportOut) | ||
async def reports_get_api(report_id: str, session: Session = Depends(get_session)): | ||
report = session.query(Report).filter(Report.uuid == report_id).first() | ||
if not report: | ||
raise HTTPException(status_code=404) | ||
return report_response_serializer(report) | ||
|
||
|
||
@reports_router.put('/{report_id}/', response_model=ReportOut) | ||
async def reports_put_api(report_id: str, request_data: ReportUpdateIn, session: Session = Depends(get_session)): | ||
report = session.query(Report).filter(Report.uuid == report_id).first() | ||
if not report: | ||
raise HTTPException(status_code=404) | ||
if request_data.code is not None: | ||
report.code = request_data.code | ||
if request_data.name is not None: | ||
report.name = request_data.name | ||
if request_data.description is not None: | ||
report.description = request_data.description | ||
session.commit() | ||
return report_response_serializer(report) | ||
|
||
|
||
@reports_router.delete('/{report_id}/') | ||
async def reports_delete_api(report_id: str, session: Session = Depends(get_session)): | ||
report = session.query(Report).filter(Report.uuid == report_id).first() | ||
if not report: | ||
raise HTTPException(status_code=404) | ||
session.delete(report) | ||
session.commit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"""empty message | ||
Revision ID: 3d5fd76e8485 | ||
Revises: 517a45b2e62c | ||
Create Date: 2024-07-03 20:56:10.622375 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = '3d5fd76e8485' | ||
down_revision = '517a45b2e62c' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table('reports', | ||
sa.Column('uuid', sa.Text(), nullable=False), | ||
sa.Column('code', sa.Text(), nullable=True), | ||
sa.Column('name', sa.Text(), nullable=True), | ||
sa.Column('description', sa.Text(), nullable=True), | ||
sa.Column('created_at', sa.DateTime(), nullable=True), | ||
sa.Column('updated_at', sa.DateTime(), nullable=True), | ||
sa.PrimaryKeyConstraint('uuid') | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_table('reports') | ||
# ### end Alembic commands ### |
Oops, something went wrong.