Skip to content

Commit

Permalink
Olymp: update settings, formatter and linter
Browse files Browse the repository at this point in the history
  • Loading branch information
Level8Broccoli committed Mar 30, 2022
1 parent 0de8e7b commit da9b40e
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 50 deletions.
9 changes: 8 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
{
"python.linting.enabled": true,
"python.linting.mypyEnabled": true
"python.linting.pylintEnabled": true,
"python.analysis.typeCheckingMode": "basic",
"python.formatting.provider": "black",
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
"editor.formatOnSave": true,
"editor.formatOnSaveMode": "file"
}
5 changes: 1 addition & 4 deletions olymp/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@

app = FastAPI(
title="Olymp",
contact={
"name": "Gilde der Nacht",
"url": "https://gildedernacht.ch/kontakt"
}
contact={"name": "Gilde der Nacht", "url": "https://gildedernacht.ch/kontakt"},
)

origins = [
Expand Down
4 changes: 2 additions & 2 deletions olymp/app/model/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Config:
"public_body": {
"key": 3,
"key_2": None,
}
},
}
}

Expand All @@ -44,6 +44,6 @@ class Config:
"entry_uuid": uuid4(),
"created": datetime.now(),
"udpated": datetime.now(),
"status": Status.active
"status": Status.active,
}
}
17 changes: 10 additions & 7 deletions olymp/app/model/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Config:
schema_extra = {
"example": {
"name": "Name of the new resource",
"description": "Short description of the resource (optional)"
"description": "Short description of the resource (optional)",
}
}

Expand All @@ -33,14 +33,17 @@ class Config:
"example": {
"name": "Resource name",
"description": "Short description of the resource (can be empty)",
"entries": [{
"entry_uuid": uuid4(),
"created": datetime.now(),
"updated": datetime.now(),
"status": Status.active}],
"entries": [
{
"entry_uuid": uuid4(),
"created": datetime.now(),
"updated": datetime.now(),
"status": Status.active,
}
],
"resource_uuid": uuid4(),
"created": datetime.now(),
"udpated": datetime.now(),
"status": Status.active
"status": Status.active,
}
}
19 changes: 15 additions & 4 deletions olymp/app/router/entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def read_entries(resource_uuid: UUID, db: FakeDatabase = Depends(get_fake_db)):


@router.post("/", response_model=UUID, status_code=status.HTTP_201_CREATED)
def create_entry(resource_uuid: UUID, entry: EntryIn, db: FakeDatabase = Depends(get_fake_db)):
def create_entry(
resource_uuid: UUID, entry: EntryIn, db: FakeDatabase = Depends(get_fake_db)
):
"""
Create a new entry.
"""
Expand All @@ -35,7 +37,9 @@ def create_entry(resource_uuid: UUID, entry: EntryIn, db: FakeDatabase = Depends


@router.get("/{entry_uuid}/", response_model=EntryOut)
def read_entry(resource_uuid: UUID, entry_uuid: UUID, db: FakeDatabase = Depends(get_fake_db)):
def read_entry(
resource_uuid: UUID, entry_uuid: UUID, db: FakeDatabase = Depends(get_fake_db)
):
"""
Retrive one entry.
"""
Expand All @@ -46,7 +50,12 @@ def read_entry(resource_uuid: UUID, entry_uuid: UUID, db: FakeDatabase = Depends


@router.put("/{entry_uuid}/", response_model=EntryOut)
def update_entry(resource_uuid: UUID, entry_uuid: UUID, entry: EntryIn, db: FakeDatabase = Depends(get_fake_db)):
def update_entry(
resource_uuid: UUID,
entry_uuid: UUID,
entry: EntryIn,
db: FakeDatabase = Depends(get_fake_db),
):
"""
Update an existing entry.
"""
Expand All @@ -57,7 +66,9 @@ def update_entry(resource_uuid: UUID, entry_uuid: UUID, entry: EntryIn, db: Fake


@router.delete("/{entry_uuid}/", response_model=EntryOut)
def deactivate_entry(resource_uuid: UUID, entry_uuid: UUID, db: FakeDatabase = Depends(get_fake_db)):
def deactivate_entry(
resource_uuid: UUID, entry_uuid: UUID, db: FakeDatabase = Depends(get_fake_db)
):
"""
Deactivates a entry (does not delete it).
"""
Expand Down
29 changes: 17 additions & 12 deletions olymp/app/router/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session


schema.Base.metadata.create_all(bind=engine)


Expand All @@ -34,7 +33,9 @@ def read_resources(status: Optional[Status] = None, db: Session = Depends(get_db
"""
Retrieve all resources. Use the `status` query to filter only "active" or "inactive" resources.
"""
return crud.get_resources(db, status=status)
if status is None:
return crud.get_resources(db)
return crud.get_resources_by_status(db, status=status)


@router.post("/", response_model=ResourceOut, status_code=status.HTTP_201_CREATED)
Expand All @@ -43,13 +44,15 @@ def create_resource(resource: ResourceIn, db: Session = Depends(get_db)):
Create a new resource.
"""
now = datetime.now()
new_resource = ResourceOut(resource_uuid=uuid4(),
name=resource.name,
description=resource.description,
entries=[],
created=now,
updated=now,
status=Status.active)
new_resource = ResourceOut(
resource_uuid=uuid4(),
name=resource.name,
description=resource.description,
entries=[],
created=now,
updated=now,
status=Status.active,
)
return crud.create_resource(db, new_resource)


Expand All @@ -64,8 +67,10 @@ def read_resource(resource_uuid: UUID, db: Session = Depends(get_db)):
return db_resource


@ router.put("/{resource_uuid}/", response_model=ResourceOut)
def update_resource(resource_uuid: UUID, resource: ResourceIn, db: FakeDatabase = Depends(get_fake_db)):
@router.put("/{resource_uuid}/", response_model=ResourceOut)
def update_resource(
resource_uuid: UUID, resource: ResourceIn, db: FakeDatabase = Depends(get_fake_db)
):
"""
Update an existing resource.
"""
Expand All @@ -75,7 +80,7 @@ def update_resource(resource_uuid: UUID, resource: ResourceIn, db: FakeDatabase
raise HTTPException(status_code=404, detail=str(e))


@ router.delete("/{resource_uuid}/", response_model=ResourceOut)
@router.delete("/{resource_uuid}/", response_model=ResourceOut)
def deactivate_resource(resource_uuid: UUID, db: FakeDatabase = Depends(get_fake_db)):
"""
Deactivates a resource (does not delete it).
Expand Down
20 changes: 13 additions & 7 deletions olymp/app/storage/crud.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
from uuid import UUID
from sqlalchemy.orm import Session
from app.storage.schema import Resource

from app.model.resource import ResourceOut
from app.model.status import Status
from app.storage.schema import Resource
from sqlalchemy.orm import Session


def get_resources(db: Session):
return db.query(Resource).all()

def get_resources(db: Session, status: Status = None):
if status is None:
return db.query(Resource).all()

def get_resources_by_status(db: Session, status: Status):
return db.query(Resource).filter(Resource.status == status.value).all()


def create_resource(db: Session, resource: ResourceOut):
db_resource = Resource(
resource_uuid=str(resource.resource_uuid),
name=resource.name, description=resource.description,
name=resource.name,
description=resource.description,
created=resource.created,
updated=resource.updated,
status=resource.status,
Expand All @@ -27,4 +31,6 @@ def create_resource(db: Session, resource: ResourceOut):


def get_resource(db: Session, resource_uuid: UUID):
return db.query(Resource).filter(Resource.resource_uuid == str(resource_uuid)).first()
return (
db.query(Resource).filter(Resource.resource_uuid == str(resource_uuid)).first()
)
2 changes: 1 addition & 1 deletion olymp/app/storage/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@

SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base = declarative_base()
Base = declarative_base()
44 changes: 35 additions & 9 deletions olymp/app/storage/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ def resources_by_status(self, status: Status) -> List[ResourceOut]:
return [res for res in self.data if res.status == status]

def resource_by_id(self, resource_uuid: UUID):
r = next((res for res in self.data
if res.resource_uuid == resource_uuid and res.status == Status.active), None)
r = next(
(
res
for res in self.data
if res.resource_uuid == resource_uuid and res.status == Status.active
),
None,
)
if not r:
raise BaseException("Resource not found")
return r
Expand Down Expand Up @@ -77,18 +83,32 @@ def entry_by_id(self, resource_uuid, entry_uuid) -> EntryOut:
res = self.resource_by_id(resource_uuid)
if not res:
raise BaseException("Resource not found")
e = next((entry for entry in res.entries
if entry.entry_uuid == entry_uuid and entry.status == Status.active), None)
e = next(
(
entry
for entry in res.entries
if entry.entry_uuid == entry_uuid and entry.status == Status.active
),
None,
)
if not e:
raise BaseException("Entry not found")
return e

def update_entry(self, resource_uuid: UUID, entry_uuid: UUID, entry: EntryIn) -> EntryOut:
def update_entry(
self, resource_uuid: UUID, entry_uuid: UUID, entry: EntryIn
) -> EntryOut:
res = self.resource_by_id(resource_uuid)
if not res:
raise BaseException("Resource not found")
e = next((entry for entry in res.entries
if entry.entry_uuid == entry_uuid and entry.status == Status.active), None)
e = next(
(
entry
for entry in res.entries
if entry.entry_uuid == entry_uuid and entry.status == Status.active
),
None,
)
if not e:
raise BaseException("Entry not found")
e.private_body = entry.private_body
Expand All @@ -100,8 +120,14 @@ def deactivate_entry(self, resource_uuid: UUID, entry_uuid: UUID) -> EntryOut:
res = self.resource_by_id(resource_uuid)
if not res:
raise BaseException("Resource not found")
e = next((entry for entry in res.entries
if entry.entry_uuid == entry_uuid and entry.status == Status.active), None)
e = next(
(
entry
for entry in res.entries
if entry.entry_uuid == entry_uuid and entry.status == Status.active
),
None,
)
if not e:
raise BaseException("Entry not found")
e.updated = datetime.now()
Expand Down
4 changes: 1 addition & 3 deletions olymp/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
autopep8==1.6.0
black==22.3.0
fastapi==0.75.0
mypy==0.942
mypy-extensions==0.4.3
pydantic==1.9.0
SQLAlchemy==1.4.32
uvicorn==0.17.6

0 comments on commit da9b40e

Please sign in to comment.