mirror of
https://github.com/sissbruecker/linkding.git
synced 2025-08-08 03:08:29 +02:00
Extract access checks
This commit is contained in:
@@ -20,6 +20,7 @@ from bookmarks.api.serializers import (
|
|||||||
from bookmarks.models import Bookmark, BookmarkAsset, BookmarkSearch, Tag, User
|
from bookmarks.models import Bookmark, BookmarkAsset, BookmarkSearch, Tag, User
|
||||||
from bookmarks.services import assets, bookmarks, auto_tagging, website_loader
|
from bookmarks.services import assets, bookmarks, auto_tagging, website_loader
|
||||||
from bookmarks.type_defs import HttpRequest
|
from bookmarks.type_defs import HttpRequest
|
||||||
|
from bookmarks.views import access
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -169,11 +170,10 @@ class BookmarkAssetViewSet(
|
|||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
user = self.request.user
|
user = self.request.user
|
||||||
bookmark_id = self.kwargs["bookmark_id"]
|
# limit access to assets to the owner of the bookmark for now
|
||||||
if not Bookmark.objects.filter(id=bookmark_id, owner=user).exists():
|
bookmark = access.bookmark_write(self.request, self.kwargs["bookmark_id"])
|
||||||
raise Http404("Bookmark does not exist")
|
|
||||||
return BookmarkAsset.objects.filter(
|
return BookmarkAsset.objects.filter(
|
||||||
bookmark_id=bookmark_id, bookmark__owner=user
|
bookmark_id=bookmark.id, bookmark__owner=user
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_serializer_context(self):
|
def get_serializer_context(self):
|
||||||
@@ -214,9 +214,7 @@ class BookmarkAssetViewSet(
|
|||||||
{"error": "Asset upload is disabled."},
|
{"error": "Asset upload is disabled."},
|
||||||
status=status.HTTP_403_FORBIDDEN,
|
status=status.HTTP_403_FORBIDDEN,
|
||||||
)
|
)
|
||||||
bookmark = Bookmark.objects.filter(id=bookmark_id, owner=request.user).first()
|
bookmark = access.bookmark_write(request, bookmark_id)
|
||||||
if not bookmark:
|
|
||||||
raise Http404("Bookmark does not exist")
|
|
||||||
|
|
||||||
upload_file = request.FILES.get("file")
|
upload_file = request.FILES.get("file")
|
||||||
if not upload_file:
|
if not upload_file:
|
||||||
|
@@ -23,16 +23,18 @@ class BookmarkDetailsModalTestCase(TestCase, BookmarkFactoryMixin, HtmlTestMixin
|
|||||||
def get_index_details_modal(self, bookmark):
|
def get_index_details_modal(self, bookmark):
|
||||||
url = reverse("linkding:bookmarks.index") + f"?details={bookmark.id}"
|
url = reverse("linkding:bookmarks.index") + f"?details={bookmark.id}"
|
||||||
response = self.client.get(url)
|
response = self.client.get(url)
|
||||||
soup = self.make_soup(response.content)
|
soup = self.make_soup(response.content.decode())
|
||||||
modal = soup.find("turbo-frame", {"id": "details-modal"})
|
return soup.select_one("div.modal.bookmark-details")
|
||||||
return modal
|
|
||||||
|
|
||||||
def get_shared_details_modal(self, bookmark):
|
def get_shared_details_modal(self, bookmark):
|
||||||
url = reverse("linkding:bookmarks.shared") + f"?details={bookmark.id}"
|
url = reverse("linkding:bookmarks.shared") + f"?details={bookmark.id}"
|
||||||
response = self.client.get(url)
|
response = self.client.get(url)
|
||||||
soup = self.make_soup(response.content)
|
soup = self.make_soup(response.content.decode())
|
||||||
modal = soup.find("turbo-frame", {"id": "details-modal"})
|
return soup.select_one("div.modal.bookmark-details")
|
||||||
return modal
|
|
||||||
|
def has_details_modal(self, response):
|
||||||
|
soup = self.make_soup(response.content.decode())
|
||||||
|
return soup.select_one("div.modal.bookmark-details") is not None
|
||||||
|
|
||||||
def find_section_content(self, soup, section_name):
|
def find_section_content(self, soup, section_name):
|
||||||
h3 = soup.find("h3", string=section_name)
|
h3 = soup.find("h3", string=section_name)
|
||||||
@@ -53,35 +55,6 @@ class BookmarkDetailsModalTestCase(TestCase, BookmarkFactoryMixin, HtmlTestMixin
|
|||||||
def find_asset(self, soup, asset):
|
def find_asset(self, soup, asset):
|
||||||
return soup.find("div", {"data-asset-id": asset.id})
|
return soup.find("div", {"data-asset-id": asset.id})
|
||||||
|
|
||||||
def details_route_access_test(self):
|
|
||||||
# own bookmark
|
|
||||||
bookmark = self.setup_bookmark()
|
|
||||||
response = self.client.get(
|
|
||||||
reverse("linkding:bookmarks.index") + f"?details={bookmark.id}"
|
|
||||||
)
|
|
||||||
self.assertEqual(response.status_code, 200)
|
|
||||||
|
|
||||||
# other user's bookmark
|
|
||||||
other_user = self.setup_user()
|
|
||||||
bookmark = self.setup_bookmark(user=other_user)
|
|
||||||
response = self.client.get(
|
|
||||||
reverse("linkding:bookmarks.index") + f"?details={bookmark.id}"
|
|
||||||
)
|
|
||||||
self.assertEqual(response.status_code, 404)
|
|
||||||
|
|
||||||
# non-existent bookmark - just returns without modal in response
|
|
||||||
response = self.client.get(
|
|
||||||
reverse("linkding:bookmarks.index") + "?details=9999"
|
|
||||||
)
|
|
||||||
self.assertEqual(response.status_code, 200)
|
|
||||||
|
|
||||||
# guest user
|
|
||||||
self.client.logout()
|
|
||||||
response = self.client.get(
|
|
||||||
reverse("linkding:bookmarks.shared") + f"?details={bookmark.id}"
|
|
||||||
)
|
|
||||||
self.assertEqual(response.status_code, 404)
|
|
||||||
|
|
||||||
def test_access(self):
|
def test_access(self):
|
||||||
# own bookmark
|
# own bookmark
|
||||||
bookmark = self.setup_bookmark()
|
bookmark = self.setup_bookmark()
|
||||||
@@ -89,6 +62,7 @@ class BookmarkDetailsModalTestCase(TestCase, BookmarkFactoryMixin, HtmlTestMixin
|
|||||||
reverse("linkding:bookmarks.index") + f"?details={bookmark.id}"
|
reverse("linkding:bookmarks.index") + f"?details={bookmark.id}"
|
||||||
)
|
)
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertTrue(self.has_details_modal(response))
|
||||||
|
|
||||||
# other user's bookmark
|
# other user's bookmark
|
||||||
other_user = self.setup_user()
|
other_user = self.setup_user()
|
||||||
@@ -96,20 +70,23 @@ class BookmarkDetailsModalTestCase(TestCase, BookmarkFactoryMixin, HtmlTestMixin
|
|||||||
response = self.client.get(
|
response = self.client.get(
|
||||||
reverse("linkding:bookmarks.index") + f"?details={bookmark.id}"
|
reverse("linkding:bookmarks.index") + f"?details={bookmark.id}"
|
||||||
)
|
)
|
||||||
self.assertEqual(response.status_code, 404)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertFalse(self.has_details_modal(response))
|
||||||
|
|
||||||
# non-existent bookmark - just returns without modal in response
|
# non-existent bookmark - just returns without modal in response
|
||||||
response = self.client.get(
|
response = self.client.get(
|
||||||
reverse("linkding:bookmarks.index") + "?details=9999"
|
reverse("linkding:bookmarks.index") + "?details=9999"
|
||||||
)
|
)
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertFalse(self.has_details_modal(response))
|
||||||
|
|
||||||
# guest user
|
# guest user
|
||||||
self.client.logout()
|
self.client.logout()
|
||||||
response = self.client.get(
|
response = self.client.get(
|
||||||
reverse("linkding:bookmarks.shared") + f"?details={bookmark.id}"
|
reverse("linkding:bookmarks.shared") + f"?details={bookmark.id}"
|
||||||
)
|
)
|
||||||
self.assertEqual(response.status_code, 404)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertFalse(self.has_details_modal(response))
|
||||||
|
|
||||||
def test_access_with_sharing(self):
|
def test_access_with_sharing(self):
|
||||||
# shared bookmark, sharing disabled
|
# shared bookmark, sharing disabled
|
||||||
@@ -119,7 +96,8 @@ class BookmarkDetailsModalTestCase(TestCase, BookmarkFactoryMixin, HtmlTestMixin
|
|||||||
response = self.client.get(
|
response = self.client.get(
|
||||||
reverse("linkding:bookmarks.shared") + f"?details={bookmark.id}"
|
reverse("linkding:bookmarks.shared") + f"?details={bookmark.id}"
|
||||||
)
|
)
|
||||||
self.assertEqual(response.status_code, 404)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertFalse(self.has_details_modal(response))
|
||||||
|
|
||||||
# shared bookmark, sharing enabled
|
# shared bookmark, sharing enabled
|
||||||
profile = other_user.profile
|
profile = other_user.profile
|
||||||
@@ -130,13 +108,15 @@ class BookmarkDetailsModalTestCase(TestCase, BookmarkFactoryMixin, HtmlTestMixin
|
|||||||
reverse("linkding:bookmarks.shared") + f"?details={bookmark.id}"
|
reverse("linkding:bookmarks.shared") + f"?details={bookmark.id}"
|
||||||
)
|
)
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertTrue(self.has_details_modal(response))
|
||||||
|
|
||||||
# shared bookmark, guest user, no public sharing
|
# shared bookmark, guest user, no public sharing
|
||||||
self.client.logout()
|
self.client.logout()
|
||||||
response = self.client.get(
|
response = self.client.get(
|
||||||
reverse("linkding:bookmarks.shared") + f"?details={bookmark.id}"
|
reverse("linkding:bookmarks.shared") + f"?details={bookmark.id}"
|
||||||
)
|
)
|
||||||
self.assertEqual(response.status_code, 404)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertFalse(self.has_details_modal(response))
|
||||||
|
|
||||||
# shared bookmark, guest user, public sharing
|
# shared bookmark, guest user, public sharing
|
||||||
profile.enable_public_sharing = True
|
profile.enable_public_sharing = True
|
||||||
@@ -146,6 +126,7 @@ class BookmarkDetailsModalTestCase(TestCase, BookmarkFactoryMixin, HtmlTestMixin
|
|||||||
reverse("linkding:bookmarks.shared") + f"?details={bookmark.id}"
|
reverse("linkding:bookmarks.shared") + f"?details={bookmark.id}"
|
||||||
)
|
)
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertTrue(self.has_details_modal(response))
|
||||||
|
|
||||||
def test_displays_title(self):
|
def test_displays_title(self):
|
||||||
# with title
|
# with title
|
||||||
|
56
bookmarks/views/access.py
Normal file
56
bookmarks/views/access.py
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
from django.http import Http404
|
||||||
|
|
||||||
|
from bookmarks.models import Bookmark, BookmarkAsset, Toast
|
||||||
|
from bookmarks.type_defs import HttpRequest
|
||||||
|
|
||||||
|
|
||||||
|
def bookmark_read(request: HttpRequest, bookmark_id: int | str):
|
||||||
|
try:
|
||||||
|
bookmark = Bookmark.objects.get(pk=int(bookmark_id))
|
||||||
|
except Bookmark.DoesNotExist:
|
||||||
|
raise Http404("Bookmark does not exist")
|
||||||
|
|
||||||
|
is_owner = bookmark.owner == request.user
|
||||||
|
is_shared = (
|
||||||
|
request.user.is_authenticated
|
||||||
|
and bookmark.shared
|
||||||
|
and bookmark.owner.profile.enable_sharing
|
||||||
|
)
|
||||||
|
is_public_shared = bookmark.shared and bookmark.owner.profile.enable_public_sharing
|
||||||
|
if not is_owner and not is_shared and not is_public_shared:
|
||||||
|
raise Http404("Bookmark does not exist")
|
||||||
|
if request.method == "POST" and not is_owner:
|
||||||
|
raise Http404("Bookmark does not exist")
|
||||||
|
|
||||||
|
return bookmark
|
||||||
|
|
||||||
|
|
||||||
|
def bookmark_write(request: HttpRequest, bookmark_id: int | str):
|
||||||
|
try:
|
||||||
|
return Bookmark.objects.get(pk=bookmark_id, owner=request.user)
|
||||||
|
except Bookmark.DoesNotExist:
|
||||||
|
raise Http404("Bookmark does not exist")
|
||||||
|
|
||||||
|
|
||||||
|
def asset_read(request: HttpRequest, asset_id: int | str):
|
||||||
|
try:
|
||||||
|
asset = BookmarkAsset.objects.get(pk=asset_id)
|
||||||
|
except BookmarkAsset.DoesNotExist:
|
||||||
|
raise Http404("Asset does not exist")
|
||||||
|
|
||||||
|
bookmark_read(request, asset.bookmark_id)
|
||||||
|
return asset
|
||||||
|
|
||||||
|
|
||||||
|
def asset_write(request: HttpRequest, asset_id: int | str):
|
||||||
|
try:
|
||||||
|
return BookmarkAsset.objects.get(pk=asset_id, bookmark__owner=request.user)
|
||||||
|
except BookmarkAsset.DoesNotExist:
|
||||||
|
raise Http404("Asset does not exist")
|
||||||
|
|
||||||
|
|
||||||
|
def toast_write(request: HttpRequest, toast_id: int | str):
|
||||||
|
try:
|
||||||
|
return Toast.objects.get(pk=toast_id, owner=request.user)
|
||||||
|
except Toast.DoesNotExist:
|
||||||
|
raise Http404("Toast does not exist")
|
@@ -8,28 +8,7 @@ from django.http import (
|
|||||||
)
|
)
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
|
|
||||||
from bookmarks.models import BookmarkAsset
|
from bookmarks.views import access
|
||||||
|
|
||||||
|
|
||||||
def _access_asset(request, asset_id: int):
|
|
||||||
try:
|
|
||||||
asset = BookmarkAsset.objects.get(pk=asset_id)
|
|
||||||
except BookmarkAsset.DoesNotExist:
|
|
||||||
raise Http404("Asset does not exist")
|
|
||||||
|
|
||||||
bookmark = asset.bookmark
|
|
||||||
is_owner = bookmark.owner == request.user
|
|
||||||
is_shared = (
|
|
||||||
request.user.is_authenticated
|
|
||||||
and bookmark.shared
|
|
||||||
and bookmark.owner.profile.enable_sharing
|
|
||||||
)
|
|
||||||
is_public_shared = bookmark.shared and bookmark.owner.profile.enable_public_sharing
|
|
||||||
|
|
||||||
if not is_owner and not is_shared and not is_public_shared:
|
|
||||||
raise Http404("Bookmark does not exist")
|
|
||||||
|
|
||||||
return asset
|
|
||||||
|
|
||||||
|
|
||||||
def _get_asset_content(asset):
|
def _get_asset_content(asset):
|
||||||
@@ -49,14 +28,14 @@ def _get_asset_content(asset):
|
|||||||
|
|
||||||
|
|
||||||
def view(request, asset_id: int):
|
def view(request, asset_id: int):
|
||||||
asset = _access_asset(request, asset_id)
|
asset = access.asset_read(request, asset_id)
|
||||||
content = _get_asset_content(asset)
|
content = _get_asset_content(asset)
|
||||||
|
|
||||||
return HttpResponse(content, content_type=asset.content_type)
|
return HttpResponse(content, content_type=asset.content_type)
|
||||||
|
|
||||||
|
|
||||||
def read(request, asset_id: int):
|
def read(request, asset_id: int):
|
||||||
asset = _access_asset(request, asset_id)
|
asset = access.asset_read(request, asset_id)
|
||||||
content = _get_asset_content(asset)
|
content = _get_asset_content(asset)
|
||||||
content = content.decode("utf-8")
|
content = content.decode("utf-8")
|
||||||
|
|
||||||
|
@@ -5,7 +5,6 @@ from django.contrib.auth.decorators import login_required
|
|||||||
from django.db.models import QuerySet
|
from django.db.models import QuerySet
|
||||||
from django.http import (
|
from django.http import (
|
||||||
HttpResponseRedirect,
|
HttpResponseRedirect,
|
||||||
Http404,
|
|
||||||
HttpResponseBadRequest,
|
HttpResponseBadRequest,
|
||||||
HttpResponseForbidden,
|
HttpResponseForbidden,
|
||||||
)
|
)
|
||||||
@@ -15,7 +14,6 @@ from django.urls import reverse
|
|||||||
from bookmarks import queries, utils
|
from bookmarks import queries, utils
|
||||||
from bookmarks.models import (
|
from bookmarks.models import (
|
||||||
Bookmark,
|
Bookmark,
|
||||||
BookmarkAsset,
|
|
||||||
BookmarkForm,
|
BookmarkForm,
|
||||||
BookmarkSearch,
|
BookmarkSearch,
|
||||||
build_tag_string,
|
build_tag_string,
|
||||||
@@ -38,7 +36,7 @@ from bookmarks.services.bookmarks import (
|
|||||||
)
|
)
|
||||||
from bookmarks.type_defs import HttpRequest
|
from bookmarks.type_defs import HttpRequest
|
||||||
from bookmarks.utils import get_safe_return_url
|
from bookmarks.utils import get_safe_return_url
|
||||||
from bookmarks.views import contexts, partials, turbo
|
from bookmarks.views import access, contexts, partials, turbo
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
@@ -190,10 +188,7 @@ def new(request: HttpRequest):
|
|||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def edit(request: HttpRequest, bookmark_id: int):
|
def edit(request: HttpRequest, bookmark_id: int):
|
||||||
try:
|
bookmark = access.bookmark_write(request, bookmark_id)
|
||||||
bookmark = Bookmark.objects.get(pk=bookmark_id, owner=request.user)
|
|
||||||
except Bookmark.DoesNotExist:
|
|
||||||
raise Http404("Bookmark does not exist")
|
|
||||||
return_url = get_safe_return_url(
|
return_url = get_safe_return_url(
|
||||||
request.GET.get("return_url"), reverse("linkding:bookmarks.index")
|
request.GET.get("return_url"), reverse("linkding:bookmarks.index")
|
||||||
)
|
)
|
||||||
@@ -216,58 +211,34 @@ def edit(request: HttpRequest, bookmark_id: int):
|
|||||||
|
|
||||||
|
|
||||||
def remove(request: HttpRequest, bookmark_id: int | str):
|
def remove(request: HttpRequest, bookmark_id: int | str):
|
||||||
try:
|
bookmark = access.bookmark_write(request, bookmark_id)
|
||||||
bookmark = Bookmark.objects.get(pk=bookmark_id, owner=request.user)
|
|
||||||
except Bookmark.DoesNotExist:
|
|
||||||
raise Http404("Bookmark does not exist")
|
|
||||||
|
|
||||||
bookmark.delete()
|
bookmark.delete()
|
||||||
|
|
||||||
|
|
||||||
def archive(request: HttpRequest, bookmark_id: int | str):
|
def archive(request: HttpRequest, bookmark_id: int | str):
|
||||||
try:
|
bookmark = access.bookmark_write(request, bookmark_id)
|
||||||
bookmark = Bookmark.objects.get(pk=bookmark_id, owner=request.user)
|
|
||||||
except Bookmark.DoesNotExist:
|
|
||||||
raise Http404("Bookmark does not exist")
|
|
||||||
|
|
||||||
archive_bookmark(bookmark)
|
archive_bookmark(bookmark)
|
||||||
|
|
||||||
|
|
||||||
def unarchive(request: HttpRequest, bookmark_id: int | str):
|
def unarchive(request: HttpRequest, bookmark_id: int | str):
|
||||||
try:
|
bookmark = access.bookmark_write(request, bookmark_id)
|
||||||
bookmark = Bookmark.objects.get(pk=bookmark_id, owner=request.user)
|
|
||||||
except Bookmark.DoesNotExist:
|
|
||||||
raise Http404("Bookmark does not exist")
|
|
||||||
|
|
||||||
unarchive_bookmark(bookmark)
|
unarchive_bookmark(bookmark)
|
||||||
|
|
||||||
|
|
||||||
def unshare(request: HttpRequest, bookmark_id: int | str):
|
def unshare(request: HttpRequest, bookmark_id: int | str):
|
||||||
try:
|
bookmark = access.bookmark_write(request, bookmark_id)
|
||||||
bookmark = Bookmark.objects.get(pk=bookmark_id, owner=request.user)
|
|
||||||
except Bookmark.DoesNotExist:
|
|
||||||
raise Http404("Bookmark does not exist")
|
|
||||||
|
|
||||||
bookmark.shared = False
|
bookmark.shared = False
|
||||||
bookmark.save()
|
bookmark.save()
|
||||||
|
|
||||||
|
|
||||||
def mark_as_read(request: HttpRequest, bookmark_id: int | str):
|
def mark_as_read(request: HttpRequest, bookmark_id: int | str):
|
||||||
try:
|
bookmark = access.bookmark_write(request, bookmark_id)
|
||||||
bookmark = Bookmark.objects.get(pk=bookmark_id, owner=request.user)
|
|
||||||
except Bookmark.DoesNotExist:
|
|
||||||
raise Http404("Bookmark does not exist")
|
|
||||||
|
|
||||||
bookmark.unread = False
|
bookmark.unread = False
|
||||||
bookmark.save()
|
bookmark.save()
|
||||||
|
|
||||||
|
|
||||||
def create_html_snapshot(request: HttpRequest, bookmark_id: int | str):
|
def create_html_snapshot(request: HttpRequest, bookmark_id: int | str):
|
||||||
try:
|
bookmark = access.bookmark_write(request, bookmark_id)
|
||||||
bookmark = Bookmark.objects.get(pk=bookmark_id, owner=request.user)
|
|
||||||
except Bookmark.DoesNotExist:
|
|
||||||
raise Http404("Bookmark does not exist")
|
|
||||||
|
|
||||||
tasks.create_html_snapshot(bookmark)
|
tasks.create_html_snapshot(bookmark)
|
||||||
|
|
||||||
|
|
||||||
@@ -275,11 +246,7 @@ def upload_asset(request: HttpRequest, bookmark_id: int | str):
|
|||||||
if settings.LD_DISABLE_ASSET_UPLOAD:
|
if settings.LD_DISABLE_ASSET_UPLOAD:
|
||||||
return HttpResponseForbidden("Asset upload is disabled")
|
return HttpResponseForbidden("Asset upload is disabled")
|
||||||
|
|
||||||
try:
|
bookmark = access.bookmark_write(request, bookmark_id)
|
||||||
bookmark = Bookmark.objects.get(pk=bookmark_id, owner=request.user)
|
|
||||||
except Bookmark.DoesNotExist:
|
|
||||||
raise Http404("Bookmark does not exist")
|
|
||||||
|
|
||||||
file = request.FILES.get("upload_asset_file")
|
file = request.FILES.get("upload_asset_file")
|
||||||
if not file:
|
if not file:
|
||||||
return HttpResponseBadRequest("No file provided")
|
return HttpResponseBadRequest("No file provided")
|
||||||
@@ -288,20 +255,12 @@ def upload_asset(request: HttpRequest, bookmark_id: int | str):
|
|||||||
|
|
||||||
|
|
||||||
def remove_asset(request: HttpRequest, asset_id: int | str):
|
def remove_asset(request: HttpRequest, asset_id: int | str):
|
||||||
try:
|
asset = access.asset_write(request, asset_id)
|
||||||
asset = BookmarkAsset.objects.get(pk=asset_id, bookmark__owner=request.user)
|
|
||||||
except BookmarkAsset.DoesNotExist:
|
|
||||||
raise Http404("Asset does not exist")
|
|
||||||
|
|
||||||
asset.delete()
|
asset.delete()
|
||||||
|
|
||||||
|
|
||||||
def update_state(request: HttpRequest, bookmark_id: int | str):
|
def update_state(request: HttpRequest, bookmark_id: int | str):
|
||||||
try:
|
bookmark = access.bookmark_write(request, bookmark_id)
|
||||||
bookmark = Bookmark.objects.get(pk=bookmark_id, owner=request.user)
|
|
||||||
except Bookmark.DoesNotExist:
|
|
||||||
raise Http404("Bookmark does not exist")
|
|
||||||
|
|
||||||
bookmark.is_archived = request.POST.get("is_archived") == "on"
|
bookmark.is_archived = request.POST.get("is_archived") == "on"
|
||||||
bookmark.unread = request.POST.get("unread") == "on"
|
bookmark.unread = request.POST.get("unread") == "on"
|
||||||
bookmark.shared = request.POST.get("shared") == "on"
|
bookmark.shared = request.POST.get("shared") == "on"
|
||||||
|
@@ -20,6 +20,7 @@ from bookmarks.models import (
|
|||||||
)
|
)
|
||||||
from bookmarks.services.wayback import generate_fallback_webarchive_url
|
from bookmarks.services.wayback import generate_fallback_webarchive_url
|
||||||
from bookmarks.type_defs import HttpRequest
|
from bookmarks.type_defs import HttpRequest
|
||||||
|
from bookmarks.views import access
|
||||||
|
|
||||||
CJK_RE = re.compile(r"[\u4e00-\u9fff]+")
|
CJK_RE = re.compile(r"[\u4e00-\u9fff]+")
|
||||||
|
|
||||||
@@ -444,22 +445,10 @@ def get_details_context(
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
bookmark = Bookmark.objects.get(pk=int(bookmark_id))
|
bookmark = access.bookmark_read(request, bookmark_id)
|
||||||
except Bookmark.DoesNotExist:
|
except Http404:
|
||||||
# just ignore, might end up in a situation where the bookmark was deleted
|
# just ignore, might end up in a situation where the bookmark was deleted
|
||||||
# in between navigating back and forth
|
# in between navigating back and forth
|
||||||
return None
|
return None
|
||||||
|
|
||||||
is_owner = bookmark.owner == request.user
|
|
||||||
is_shared = (
|
|
||||||
request.user.is_authenticated
|
|
||||||
and bookmark.shared
|
|
||||||
and bookmark.owner.profile.enable_sharing
|
|
||||||
)
|
|
||||||
is_public_shared = bookmark.shared and bookmark.owner.profile.enable_public_sharing
|
|
||||||
if not is_owner and not is_shared and not is_public_shared:
|
|
||||||
raise Http404("Bookmark does not exist")
|
|
||||||
if request.method == "POST" and not is_owner:
|
|
||||||
raise Http404("Bookmark does not exist")
|
|
||||||
|
|
||||||
return context_type(request, bookmark)
|
return context_type(request, bookmark)
|
||||||
|
@@ -1,18 +1,14 @@
|
|||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
from django.http import HttpResponseRedirect, Http404
|
from django.http import HttpResponseRedirect
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
|
||||||
from bookmarks.models import Toast
|
|
||||||
from bookmarks.utils import get_safe_return_url
|
from bookmarks.utils import get_safe_return_url
|
||||||
|
from bookmarks.views import access
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def acknowledge(request):
|
def acknowledge(request):
|
||||||
toast_id = request.POST["toast"]
|
toast = access.toast_write(request, request.POST["toast"])
|
||||||
try:
|
|
||||||
toast = Toast.objects.get(pk=toast_id, owner=request.user)
|
|
||||||
except Toast.DoesNotExist:
|
|
||||||
raise Http404("Toast does not exist")
|
|
||||||
toast.acknowledged = True
|
toast.acknowledged = True
|
||||||
toast.save()
|
toast.save()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user