-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Davide Arcuri
committed
Apr 16, 2024
1 parent
131cc57
commit f4296b2
Showing
14 changed files
with
191 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from typing import List | ||
|
||
from django.shortcuts import get_object_or_404 | ||
from ninja import Router | ||
from ninja.security import django_auth | ||
|
||
from orochi.api.models import BookmarksSchema, ErrorsOut, SuccessResponse | ||
from orochi.website.models import Bookmark | ||
|
||
router = Router() | ||
|
||
|
||
@router.get("/", auth=django_auth, response=List[BookmarksSchema]) | ||
def list_bookmarks(request): | ||
""" | ||
Retrieves a list of bookmarks for the current user. | ||
Returns: | ||
QuerySet: A queryset of bookmarks belonging to the current user. | ||
""" | ||
return Bookmark.objects.filter(user=request.user) | ||
|
||
|
||
@router.delete( | ||
"/{int:id}", | ||
auth=django_auth, | ||
url_name="delete_bookmark", | ||
response={200: SuccessResponse, 400: ErrorsOut}, | ||
) | ||
def delete_bookmarks(request, id: int): | ||
""" | ||
Deletes a bookmark by its ID. | ||
Args: | ||
id (int): The ID of the bookmark to delete. | ||
Returns: | ||
tuple: A tuple containing the status code and a message dictionary. | ||
Raises: | ||
Exception: If an error occurs during the deletion process. | ||
""" | ||
bookmark = get_object_or_404(Bookmark, pk=id, user=request.user) | ||
name = bookmark.name | ||
try: | ||
bookmark.delete() | ||
return 200, {"message": f"Bookmark {name} deleted"} | ||
except Exception as excp: | ||
return 400, {"errors": str(excp)} | ||
|
||
|
||
@router.post( | ||
"/{int:id}/star/{star}", | ||
auth=django_auth, | ||
url_name="star_bookmark", | ||
response={200: SuccessResponse, 400: ErrorsOut}, | ||
) | ||
def star_bookmark(request, id: int, star: bool): | ||
""" | ||
Stars or unstars a bookmark. | ||
Args: | ||
id (int): The ID of the bookmark to star/unstar. | ||
star (bool): True to star the bookmark, False to unstar it. | ||
Returns: | ||
tuple: A tuple containing the HTTP status code and a message dict. | ||
Raises: | ||
Exception: If an error occurs during the process. | ||
""" | ||
try: | ||
bookmark = get_object_or_404(Bookmark, pk=id, user=request.user) | ||
name = bookmark.name | ||
bookmark.star = star | ||
bookmark.save() | ||
return 200, { | ||
"message": ( | ||
f"Bookmark {name} starred" if star else f"Bookmark {name} unstarred" | ||
) | ||
} | ||
except Exception as excp: | ||
return 400, {"errors": str(excp)} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.