Skip to content

Commit

Permalink
[revert] linterの修正箇所の切り戻し
Browse files Browse the repository at this point in the history
  • Loading branch information
PigeonsHouse committed Jul 11, 2022
1 parent ed342b8 commit 1cc762f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 38 deletions.
33 changes: 17 additions & 16 deletions cruds/works/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@

# TODO: CASCADEを導入する

def set_work(db: Session, title: str, description: str, user_id: str,
def set_work(db: Session, title: str, description: str, user_id: str,
visibility: str, thumbnail_asset_id: str,
assets_id: List[str], urls: List[BaseUrlInfo], tags_id: List[str]) -> Work:


if title == '':
raise HTTPException(status_code=400, detail="Title is empty")

# DB書き込み
md = markdown.Markdown(extensions=['tables'])
work_orm = models.Work(
title=title,
description=description,
description_html=md.convert(description),
user_id=user_id,
visibility=visibility,
title = title,
description = description,
description_html = md.convert(description),
user_id = user_id,
visibility = visibility,
)
db.add(work_orm)
db.commit()
Expand Down Expand Up @@ -58,8 +59,8 @@ def set_work(db: Session, title: str, description: str, user_id: str,
if thumbnail is None:
raise HTTPException(status_code=400, detail='This thumbnail asset id is invalid.')
thumbnail_orm = models.Thumbnail(
work_id=work_orm.id,
asset_id=thumbnail.id
work_id = work_orm.id,
asset_id = thumbnail.id
)
db.add(thumbnail_orm)
db.commit()
Expand Down Expand Up @@ -98,10 +99,10 @@ def get_work_by_id(db: Session, work_id: str, auth: bool = False) -> Work:
raise HTTPException(status_code=403, detail="This work is a private work. You need to sign in.")
return work

def replace_work(db: Session, work_id: str, title: str, description: str, user_id: str,
visibility: str, thumbnail_asset_id: str, assets_id: List[str],
def replace_work(db: Session, work_id: str, title: str, description: str, user_id: str,
visibility: str, thumbnail_asset_id: str, assets_id: List[str],
urls: List[BaseUrlInfo], tags_id: List[str]) -> Work:

work_orm = db.query(models.Work).get(work_id)

# 自分のWorkでなければ弾く
Expand All @@ -128,7 +129,7 @@ def replace_work(db: Session, work_id: str, title: str, description: str, user_i
asset_orm.work_id = None
db.commit()
db.refresh(asset_orm)

# 使われなくなったassetの削除
old_asset_ids = [asset_orm.id for asset_orm in assets_orm]
old_thumbnail_orm = db.query(models.Thumbnail).filter(models.Thumbnail.work_id == work_id).first()
Expand All @@ -152,7 +153,7 @@ def replace_work(db: Session, work_id: str, title: str, description: str, user_i
urls_orm = db.query(models.UrlInfo).filter(models.UrlInfo.work_id == work_id).all()
for url_orm in urls_orm:
delete_url_info(db, url_orm.id)

# url_infoテーブルへのインスタンスの作成
for url in urls:
create_url_info(db, url.get('url'), url.get('url_type', 'other'), work_id, user_id)
Expand All @@ -166,8 +167,8 @@ def replace_work(db: Session, work_id: str, title: str, description: str, user_i
# tagの中間テーブルへのインスタンスの作成
for tag_id in tags_id:
tagging_orm = models.Tagging(
work_id=work_id,
tag_id=tag_id
work_id = work_id,
tag_id = tag_id
)
db.add(tagging_orm)
db.commit()
Expand Down Expand Up @@ -227,7 +228,7 @@ def get_works_by_user_id(db: Session, user_id: str, at_me: bool = False, auth: b
user_orm = db.query(models.User).get(user_id)
if user_orm is None:
raise HTTPException(status_code=404, detail='this user is not exist')

works_orm = db.query(models.Work).filter(models.Work.user_id == user_id)

if at_me:
Expand Down
8 changes: 4 additions & 4 deletions routers/works/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

@work_router.post('', response_model=Work)
async def post_work(payload: PostWork, db: Session = Depends(get_db), user: User = Depends(GetCurrentUser())):
work = set_work(db, payload.title, payload.description, user.id,
work = set_work(db, payload.title, payload.description, user.id,
payload.visibility, payload.thumbnail_asset_id, payload.assets_id, payload.urls, payload.tags_id)
return work

Expand All @@ -37,12 +37,12 @@ async def get_work(work_id: str, db: Session = Depends(get_db), user: User = Dep

@work_router.put('/{work_id}', response_model=Work)
async def put_work(work_id: str, payload: PostWork, db: Session = Depends(get_db), user: User = Depends(GetCurrentUser())):
work = replace_work(db, work_id, payload.title, payload.description, user.id,
payload.visibility, payload.thumbnail_asset_id, payload.assets_id, payload.urls,
work = replace_work(db, work_id, payload.title, payload.description, user.id,
payload.visibility, payload.thumbnail_asset_id, payload.assets_id, payload.urls,
payload.tags_id)
return work

@work_router.delete('/{work_id}', response_model=DeleteStatus, dependencies=[Depends(GetCurrentUser())])
async def delete_work(work_id: str, db: Session = Depends(get_db)):
result = delete_work_by_id(db, work_id)
return result
return result
37 changes: 21 additions & 16 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
import os
import json
from fastapi.datastructures import UploadFile
import sqlalchemy
import sqlalchemy_utils
import pytest
from datetime import timedelta
from typing import Callable, List, Optional
from fastapi.testclient import TestClient
from fastapi.datastructures import UploadFile
from sqlalchemy.orm import sessionmaker
import sqlalchemy_utils
from fastapi.testclient import TestClient
from pytest import fixture
from sqlalchemy.orm.session import Session
from main import app
from db import Base, get_db
from db.models import User, Visibility
from cruds.assets import create_asset
from cruds.tags.tag import create_tag
from cruds.users import auth
from sqlalchemy_utils.view import refresh_materialized_view
from cruds.works import set_work
from schemas.asset import Asset as AssetSchema
from schemas.tag import GetTag as TagSchema

from db.models import User, Visibility
from schemas.url_info import BaseUrlInfo
from schemas.user import User as UserSchema, TokenResponse as TokenResponseSchema
from schemas.user import User as UserSchema, Token as TokenSchema, TokenResponse as TokenResponseSchema
from schemas.work import Work as WorkSchema
from schemas.asset import Asset as AssetSchema
from schemas.tag import GetTag as TagSchema
from db import Base, get_db
from main import app
import os
from datetime import timedelta
from cruds.users import auth
# from cruds.works import create_work
from cruds.assets import create_asset
from typing import Callable, List, Optional
from cruds.tags.tag import create_tag

import json

DATABASE = 'postgresql'
USER = os.environ.get('POSTGRES_USER')
Expand Down
3 changes: 1 addition & 2 deletions tests/test_work.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import json
import pytest

from db.models import Visibility
from .fixtures import client, use_test_db_fixture, session_for_test, user_factory_for_test, user_token_factory_for_test, asset_factory_for_test, user_for_test, tag_for_test, work_factory_for_test, tag_factory_for_test
from db.models import Visibility

@pytest.mark.usefixtures('use_test_db_fixture')
class TestWork:
Expand Down

0 comments on commit 1cc762f

Please sign in to comment.