瀏覽代碼

fix nft colume typos

conradlan 3 年之前
父節點
當前提交
c2b9718e81

+ 28 - 4
app/api/api_v1/endpoints/nft.py

@@ -1,10 +1,13 @@
-from typing import Any, List
+from typing import Any, List, Optional
 
-from fastapi import APIRouter, Depends, HTTPException
+from fastapi import APIRouter, Depends, HTTPException, File, UploadFile, Form
+from pydantic.errors import NotNoneError
 from sqlalchemy.orm import Session
 
 from app import crud, models, schemas
+from app.core.config import settings
 from app.api import deps
+from uuid import uuid4
 
 router = APIRouter()
 
@@ -30,15 +33,36 @@ def read_items(
 
 
 @router.post("/", response_model=schemas.NftBase)
-def create_item(
+async def create_item(
     *,
     db: Session = Depends(deps.get_db),
-    item_in: schemas.NftCreate,
+    # item_in: schemas.NftCreate,
+    hash: Optional[str] = Form(None),
+    userid: Optional[str] = Form(None),
+    title: Optional[str] = Form(None),
+    context: Optional[str] = Form(None),
+    is_active: Optional[bool] = Form(True),
+    catagory: Optional[str] = Form(None),
+    image: UploadFile = File(...),
     current_user: models.users = Depends(deps.get_current_active_user),
 ) -> Any:
     """
     Create new item.
     """
+    if image.content_type.split('/')[0] != 'image':
+        raise HTTPException(status_code=415, detail='content type error! Please upload valid image type')
+    filename = str(uuid4()) + '.' + image.content_type.split('/')[1]
+    with open(settings.IMG_PATH + filename, 'wb+') as f:
+        await f.write(image.file.read())
+        f.close()
+    item_in = schemas.NftCreate
+    item_in.hash = hash
+    item_in.userid = userid
+    item_in.title = title
+    item_in.context = context
+    item_in.is_active = is_active
+    item_in.category = catagory
+    item_in.imgurl = settings.IMG_HOST + filename
     nft = crud.nft.create_with_owner(
         db=db, obj_in=item_in, owner_id=current_user.userid)
     return nft

+ 2 - 0
app/core/config.py

@@ -26,6 +26,8 @@ class Settings(BaseSettings):
 
     PROJECT_NAME: str = "Ark Backend"
     SENTRY_DSN: Optional[HttpUrl] = ""
+    IMG_PATH: str = "/var/www/ArkCard-Linebot/ArkCard-web/img/nft/"
+    IMG_HOST: AnyHttpUrl = "https://ark.cards/img/nft/"
 
     @validator("SENTRY_DSN", pre=True)
     def sentry_dsn_can_be_blank(cls, v: str) -> Optional[str]:

+ 1 - 1
app/models/nft.py

@@ -10,5 +10,5 @@ class Nft(Base):
     userid = Column(String(200))
     title = Column(String(200))
     context = Column(String(200))
-    is_actived = Column(Boolean(), default=True)
+    is_active = Column(Boolean(), default=True)
     category = Column(String(100))

+ 25 - 0
ark_table/versions/0935df5094b3_fix_typos.py

@@ -0,0 +1,25 @@
+"""fix typos
+
+Revision ID: 0935df5094b3
+Revises: 51dfa236bcf1
+Create Date: 2021-12-24 15:22:26.954039
+
+"""
+from alembic import op
+import sqlalchemy as sa
+
+
+# revision identifiers, used by Alembic.
+revision = '0935df5094b3'
+down_revision = '51dfa236bcf1'
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+    op.add_column('nft',
+        sa.Column('is_active', sa.Boolean(), default=False, nullable=False)
+    )
+
+def downgrade():
+    op.drop_column('nft', 'is_active')

+ 1 - 1
ark_table/versions/1baf7a0c6aab_alert_table.py

@@ -21,7 +21,7 @@ def upgrade():
         sa.Column('category', sa.String(10))
     )
     op.add_column('nft',
-        sa.Column('is_actived', sa.Boolean(), default=False, nullable=False)
+        sa.Column('is_active', sa.Boolean(), default=False, nullable=False)
     )
 
 def downgrade():