Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions db_models/news_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class News_DB(BaseModel_DB):

pinned_to: Mapped[Optional[datetime_utc]] = mapped_column(default=None)

image_exist: Mapped[bool] = mapped_column(default=False)

news_tags: Mapped[list["NewsTag_DB"]] = relationship(
back_populates="news", cascade="all, delete-orphan", init=False
)
8 changes: 6 additions & 2 deletions routes/news_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from services.news_service import create_new_news, update_existing_news, bump_existing_news
from user.permission import Permission


news_router = APIRouter()


Expand Down Expand Up @@ -60,8 +59,13 @@ async def post_news_image(news_id: int, db: DB_dependency, image: UploadFile = F
raise HTTPException(400, "file extension not allowed")

dest_path = Path(f"{ASSETS_BASE_PATH}/news/{news.id}{ext}")
news.image_exist = True

dest_path.write_bytes(image.file.read())
try:
dest_path.write_bytes(image.file.read())
except:
news.image_exist = False
db.commit()


@news_router.get("/{news_id}/image/stream", dependencies=[Depends(rate_limit(limit=100))])
Expand Down
3 changes: 1 addition & 2 deletions routes/song_category_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from fastapi import APIRouter, HTTPException, status
from user.permission import Permission


song_category_router = APIRouter()


Expand All @@ -26,7 +25,7 @@ def get_song_category(category_id: int, db: DB_dependency):
def create_song_category(song_category_data: SongCategoryCreate, db: DB_dependency):
num_existing = db.query(SongCategory_DB).filter(SongCategory_DB.name == song_category_data.name).count()
if num_existing > 0:
raise HTTPException(status.HTTP_400_BAD_REQUEST, detail="Ths post already exists")
raise HTTPException(status.HTTP_400_BAD_REQUEST, detail="This song category already exists")
songcategory = SongCategory_DB(name=song_category_data.name)
db.add(songcategory)
db.commit()
Expand Down
3 changes: 3 additions & 0 deletions seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,15 @@ def seed_news(db: Session, user: User_DB):
content_sv="Oj här var det ju en massa spännande saker man kunde läsa!",
content_en="Whoops here there was a lot of content to read!",
author_id=user.id,
image_exist=False,
),
News_DB(
title_sv="En annan nyhet",
title_en="Another news",
content_sv="Lite mer content",
content_en="A bit more content",
author_id=user.id,
image_exist=False,
),
]

Expand All @@ -284,6 +286,7 @@ def seed_news(db: Session, user: User_DB):
content_sv="Lite mer content",
content_en="A bit more content",
author_id=user.id,
image_exist=False,
)
)

Expand Down
1 change: 1 addition & 0 deletions services/news_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def create_new_news(data: NewsCreate, author_id: int, db: Session):
author_id=author_id,
pinned_from=data.pinned_from,
pinned_to=data.pinned_to,
image_exist=False,
)
db.add(news)
db.commit()
Expand Down