mirror of
https://github.com/StepanovPlaton/torrent_backend.git
synced 2026-04-03 20:30:38 +04:00
Update game models
This commit is contained in:
@@ -11,6 +11,7 @@ async def add_game(db: AsyncSession,
|
|||||||
game_info: sch.GameCreate,
|
game_info: sch.GameCreate,
|
||||||
user_id: int):
|
user_id: int):
|
||||||
game = mdl.Game(**game_info.model_dump(),
|
game = mdl.Game(**game_info.model_dump(),
|
||||||
|
update_date=strftime("%Y-%m-%d %H:%M:%S"),
|
||||||
upload_date=strftime("%Y-%m-%d %H:%M:%S"),
|
upload_date=strftime("%Y-%m-%d %H:%M:%S"),
|
||||||
owner_id=user_id)
|
owner_id=user_id)
|
||||||
return await add_transaction(db, game)
|
return await add_transaction(db, game)
|
||||||
|
|||||||
2
database/models/__init__.py
Normal file
2
database/models/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
from .games import Game as Game
|
||||||
|
from .users import User as User
|
||||||
@@ -1,23 +1,18 @@
|
|||||||
|
|
||||||
from sqlalchemy import Column, ForeignKey, Integer, String
|
from sqlalchemy import Column, ForeignKey, Integer, String
|
||||||
from sqlalchemy.orm import relationship
|
from sqlalchemy.orm import relationship
|
||||||
|
|
||||||
from .database import Base
|
from ..database import Base
|
||||||
|
|
||||||
|
|
||||||
class Game(Base):
|
class Game(Base):
|
||||||
__tablename__ = "games"
|
__tablename__ = "games"
|
||||||
|
|
||||||
id = Column(Integer, primary_key=True)
|
id = Column(Integer, primary_key=True)
|
||||||
cover = Column(String)
|
|
||||||
title = Column(String, nullable=False, unique=True)
|
title = Column(String, nullable=False, unique=True)
|
||||||
|
cover = Column(String)
|
||||||
description = Column(String)
|
description = Column(String)
|
||||||
torrent_file = Column(String, nullable=False)
|
torrent_file = Column(String, nullable=False)
|
||||||
language = Column(String)
|
|
||||||
version = Column(String)
|
|
||||||
download_size = Column(String)
|
|
||||||
upload_date = Column(String, nullable=False)
|
upload_date = Column(String, nullable=False)
|
||||||
release_date = Column(String)
|
trailer = Column(String)
|
||||||
|
|
||||||
system = Column(String)
|
system = Column(String)
|
||||||
processor = Column(String)
|
processor = Column(String)
|
||||||
@@ -25,16 +20,12 @@ class Game(Base):
|
|||||||
graphics = Column(String)
|
graphics = Column(String)
|
||||||
storage = Column(String)
|
storage = Column(String)
|
||||||
|
|
||||||
|
version = Column(String)
|
||||||
|
update_date = Column(String, nullable=False)
|
||||||
|
developer = Column(String)
|
||||||
|
language = Column(String)
|
||||||
|
release_date = Column(String)
|
||||||
|
download_size = Column(String)
|
||||||
|
|
||||||
owner_id = Column(Integer, ForeignKey("users.id"))
|
owner_id = Column(Integer, ForeignKey("users.id"))
|
||||||
owner = relationship("User", back_populates="games")
|
owner = relationship("User", back_populates="games")
|
||||||
|
|
||||||
|
|
||||||
class User(Base):
|
|
||||||
__tablename__ = "users"
|
|
||||||
|
|
||||||
id = Column(Integer, primary_key=True)
|
|
||||||
email = Column(String, nullable=False, unique=True)
|
|
||||||
name = Column(String, nullable=False)
|
|
||||||
hash_of_password = Column(String, nullable=False)
|
|
||||||
|
|
||||||
games = relationship("Game", back_populates="owner")
|
|
||||||
14
database/models/users.py
Normal file
14
database/models/users.py
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
from sqlalchemy import Column, Integer, String
|
||||||
|
from sqlalchemy.orm import relationship
|
||||||
|
|
||||||
|
from ..database import Base
|
||||||
|
|
||||||
|
class User(Base):
|
||||||
|
__tablename__ = "users"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True)
|
||||||
|
email = Column(String, nullable=False, unique=True)
|
||||||
|
name = Column(String, nullable=False)
|
||||||
|
hash_of_password = Column(String, nullable=False)
|
||||||
|
|
||||||
|
games = relationship("Game", back_populates="owner")
|
||||||
@@ -6,7 +6,7 @@ class GameCardBase(BaseModel):
|
|||||||
title: str
|
title: str
|
||||||
cover: Optional[str] = None
|
cover: Optional[str] = None
|
||||||
description: Optional[str] = None
|
description: Optional[str] = None
|
||||||
release_date: Optional[str] = None
|
version: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
class GameCard(GameCardBase):
|
class GameCard(GameCardBase):
|
||||||
@@ -15,9 +15,7 @@ class GameCard(GameCardBase):
|
|||||||
|
|
||||||
class GameBase(GameCardBase):
|
class GameBase(GameCardBase):
|
||||||
torrent_file: str
|
torrent_file: str
|
||||||
language: Optional[str] = None
|
trailer: Optional[str] = None
|
||||||
version: Optional[str] = None
|
|
||||||
download_size: Optional[str] = None
|
|
||||||
|
|
||||||
system: Optional[str] = None
|
system: Optional[str] = None
|
||||||
processor: Optional[str] = None
|
processor: Optional[str] = None
|
||||||
@@ -25,6 +23,12 @@ class GameBase(GameCardBase):
|
|||||||
graphics: Optional[str] = None
|
graphics: Optional[str] = None
|
||||||
storage: Optional[str] = None
|
storage: Optional[str] = None
|
||||||
|
|
||||||
|
developer: Optional[str] = None
|
||||||
|
language: Optional[str] = None
|
||||||
|
release_date: Optional[str] = None
|
||||||
|
download_size: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class GameCreate(GameBase):
|
class GameCreate(GameBase):
|
||||||
pass
|
pass
|
||||||
@@ -32,7 +36,8 @@ class GameCreate(GameBase):
|
|||||||
|
|
||||||
class Game(GameBase):
|
class Game(GameBase):
|
||||||
id: int
|
id: int
|
||||||
upload_date: str | None
|
update_date: str
|
||||||
|
upload_date: str
|
||||||
owner_id: int
|
owner_id: int
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
|
|||||||
@@ -46,18 +46,34 @@ async def save_image(cover: UploadFile, type: Literal["cover", "screenshot"]):
|
|||||||
cover_data = await cover.read()
|
cover_data = await cover.read()
|
||||||
if (isinstance(cover_data, str)):
|
if (isinstance(cover_data, str)):
|
||||||
raise ValueError("Invalid image file")
|
raise ValueError("Invalid image file")
|
||||||
await full_size_file.write(cover_data)
|
|
||||||
image = Image.open(BytesIO(cover_data))
|
cover_full_size = Image.open(BytesIO(cover_data))
|
||||||
compressed_coefficient = (image.size[0] * image.size[1]) / (1280*720/4)
|
compressed_coefficient = (cover_full_size.size[0] *
|
||||||
|
cover_full_size.size[1]) / (1920*1080)
|
||||||
if (compressed_coefficient < 1):
|
if (compressed_coefficient < 1):
|
||||||
compressed_coefficient = 1
|
compressed_coefficient = 1
|
||||||
compressed_image = image.resize(
|
|
||||||
(int(image.size[0] / compressed_coefficient),
|
cover_full_size = cover_full_size.resize(
|
||||||
int(image.size[1] / compressed_coefficient))
|
(int(cover_full_size.size[0] / compressed_coefficient),
|
||||||
|
int(cover_full_size.size[1] / compressed_coefficient))
|
||||||
|
)
|
||||||
|
buf = BytesIO()
|
||||||
|
cover_full_size.save(
|
||||||
|
buf, format=cover.content_type.upper().replace("IMAGE/", ""))
|
||||||
|
await full_size_file.write(buf.getbuffer())
|
||||||
|
|
||||||
|
cover_preview = Image.open(BytesIO(cover_data))
|
||||||
|
compressed_coefficient /= 4
|
||||||
|
if (compressed_coefficient < 1):
|
||||||
|
compressed_coefficient = 1
|
||||||
|
|
||||||
|
cover_preview = cover_preview.resize(
|
||||||
|
(int(cover_preview.size[0] / compressed_coefficient),
|
||||||
|
int(cover_preview.size[1] / compressed_coefficient))
|
||||||
)
|
)
|
||||||
|
|
||||||
buf = BytesIO()
|
buf = BytesIO()
|
||||||
compressed_image.save(
|
cover_preview.save(
|
||||||
buf, format=cover.content_type.upper().replace("IMAGE/", ""))
|
buf, format=cover.content_type.upper().replace("IMAGE/", ""))
|
||||||
await preview_file.write(buf.getbuffer())
|
await preview_file.write(buf.getbuffer())
|
||||||
return hash_filename
|
return hash_filename
|
||||||
|
|||||||
Reference in New Issue
Block a user