Coverage for opt/mealie/lib/python3.12/site-packages/mealie/routes/comments/__init__.py: 69%

43 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-11-25 17:29 +0000

1from functools import cached_property 1a

2 

3from fastapi import APIRouter, Depends, HTTPException 1a

4from pydantic import UUID4 1a

5 

6from mealie.routes._base.base_controllers import BaseUserController 1a

7from mealie.routes._base.controller import controller 1a

8from mealie.routes._base.mixins import HttpRepo 1a

9from mealie.schema.recipe.recipe_comments import ( 1a

10 RecipeCommentCreate, 

11 RecipeCommentOut, 

12 RecipeCommentPagination, 

13 RecipeCommentSave, 

14 RecipeCommentUpdate, 

15) 

16from mealie.schema.response.pagination import PaginationQuery 1a

17from mealie.schema.response.responses import ErrorResponse, SuccessResponse 1a

18 

19router = APIRouter(prefix="/comments", tags=["Recipe: Comments"]) 1a

20 

21 

22@controller(router) 1a

23class RecipeCommentRoutes(BaseUserController): 1a

24 @cached_property 1a

25 def repo(self): 1a

26 return self.repos.comments 1bcdefghijklmnopqBrstuvwxyCDzA

27 

28 # ======================================================================= 

29 # CRUD Operations 

30 

31 @property 1a

32 def mixins(self) -> HttpRepo: 1a

33 return HttpRepo(self.repo, self.logger, self.registered_exceptions, self.t("generic.server-error")) 

34 

35 def _check_comment_belongs_to_user(self, item_id: UUID4) -> None: 1a

36 comment = self.repo.get_one(item_id) 

37 if comment.user_id != self.user.id and not self.user.admin: 

38 raise HTTPException( 

39 status_code=403, 

40 detail=ErrorResponse(message="Comment does not belong to user"), 

41 ) 

42 

43 @router.get("", response_model=RecipeCommentPagination) 1a

44 def get_all(self, q: PaginationQuery = Depends(PaginationQuery)): 1a

45 response = self.repo.page_all( 1bcdefghijklmnopqBrstuvwxyCDzA

46 pagination=q, 

47 override=RecipeCommentOut, 

48 ) 

49 

50 response.set_pagination_guides(router.url_path_for("get_all"), q.model_dump()) 1bcdefghijklmnopqrstuvwxyzA

51 return response 1bcdefghijklmnopqrstuvwxyzA

52 

53 @router.post("", response_model=RecipeCommentOut, status_code=201) 1a

54 def create_one(self, data: RecipeCommentCreate): 1a

55 save_data = RecipeCommentSave(text=data.text, user_id=self.user.id, recipe_id=data.recipe_id) 

56 return self.mixins.create_one(save_data) 

57 

58 @router.get("/{item_id}", response_model=RecipeCommentOut) 1a

59 def get_one(self, item_id: UUID4): 1a

60 return self.mixins.get_one(item_id) 

61 

62 @router.put("/{item_id}", response_model=RecipeCommentOut) 1a

63 def update_one(self, item_id: UUID4, data: RecipeCommentUpdate): 1a

64 self._check_comment_belongs_to_user(item_id) 

65 return self.mixins.update_one(data, item_id) 

66 

67 @router.delete("/{item_id}", response_model=SuccessResponse) 1a

68 def delete_one(self, item_id: UUID4): 1a

69 self._check_comment_belongs_to_user(item_id) 

70 self.mixins.delete_one(item_id) 

71 return SuccessResponse.respond(message="Comment deleted")