Sort by last accessed

This commit is contained in:
Viacheslav Slinko 2024-05-19 23:58:19 +03:00
parent fa5f78cf71
commit f9357abdc2
No known key found for this signature in database
GPG key ID: 8AC133CA13886F0E
7 changed files with 46 additions and 1 deletions

View file

@ -12,6 +12,7 @@ from bookmarks.api.serializers import (
)
from bookmarks.models import Bookmark, BookmarkSearch, Tag, User
from bookmarks.services.bookmarks import (
access_bookmark,
archive_bookmark,
unarchive_bookmark,
website_loader,
@ -76,6 +77,12 @@ class BookmarkViewSet(
data = serializer(page, many=True).data
return self.get_paginated_response(data)
@action(methods=["post"], detail=True)
def access(self, request, pk):
bookmark = self.get_object()
access_bookmark(bookmark)
return Response(status=status.HTTP_204_NO_CONTENT)
@action(methods=["post"], detail=True)
def archive(self, request, pk):
bookmark = self.get_object()

View file

@ -1,6 +1,7 @@
export class ApiClient {
constructor(baseUrl) {
constructor(baseUrl, token = "") {
this.baseUrl = baseUrl;
this.token = token;
}
listBookmarks(search, options = { limit: 100, offset: 0, path: "" }) {
@ -26,4 +27,15 @@ export class ApiClient {
.then((response) => response.json())
.then((data) => data.results);
}
async markBookmarkAccessed(id) {
const url = `${this.baseUrl}bookmarks/${id}/access/`;
await fetch(url, {
method: "POST",
headers: {
"X-CSRFToken": this.token,
},
});
}
}

View file

@ -1,3 +1,4 @@
import { ApiClient } from "../api";
import { Behavior, registerBehavior } from "./index";
class BookmarkItem extends Behavior {
@ -18,6 +19,14 @@ class BookmarkItem extends Behavior {
titleAnchor.dataset.tooltip = titleSpan.textContent;
}
});
const csrftoken = element.querySelector("[name=csrfmiddlewaretoken]").value;
const id = element.querySelector("[name=bookmark_id]").value;
const apiBaseUrl = document.documentElement.dataset.apiBaseUrl || "";
const apiClient = new ApiClient(apiBaseUrl, csrftoken);
titleAnchor.addEventListener("click", async () => {
await apiClient.markBookmarkAccessed(id);
});
}
onToggleNotes(event) {

View file

@ -173,6 +173,8 @@ class BookmarkForm(forms.ModelForm):
class BookmarkSearch:
SORT_ACCESSED_ASC = "accessed_asc"
SORT_ACCESSED_DESC = "accessed_desc"
SORT_ADDED_ASC = "added_asc"
SORT_ADDED_DESC = "added_desc"
SORT_TITLE_ASC = "title_asc"
@ -262,6 +264,8 @@ class BookmarkSearch:
class BookmarkSearchForm(forms.Form):
SORT_CHOICES = [
(BookmarkSearch.SORT_ACCESSED_ASC, "Accessed ↑"),
(BookmarkSearch.SORT_ACCESSED_DESC, "Accessed ↓"),
(BookmarkSearch.SORT_ADDED_ASC, "Added ↑"),
(BookmarkSearch.SORT_ADDED_DESC, "Added ↓"),
(BookmarkSearch.SORT_TITLE_ASC, "Title ↑"),

View file

@ -87,6 +87,12 @@ def _base_bookmarks_query(
elif search.shared == BookmarkSearch.FILTER_SHARED_UNSHARED:
query_set = query_set.filter(shared=False)
# Sort by date added
if search.sort == BookmarkSearch.SORT_ACCESSED_ASC:
query_set = query_set.order_by("date_accessed", "date_modified")
elif search.sort == BookmarkSearch.SORT_ACCESSED_DESC:
query_set = query_set.order_by("-date_accessed", "-date_modified")
# Sort by date added
if search.sort == BookmarkSearch.SORT_ADDED_ASC:
query_set = query_set.order_by("date_added")

View file

@ -74,6 +74,12 @@ def update_bookmark(bookmark: Bookmark, tag_string, current_user: User):
return bookmark
def access_bookmark(bookmark: Bookmark):
bookmark.date_accessed = timezone.now()
bookmark.save()
return bookmark
def archive_bookmark(bookmark: Bookmark):
bookmark.is_archived = True
bookmark.date_modified = timezone.now()

View file

@ -10,6 +10,7 @@
data-bookmarks-total="{{ bookmark_list.bookmarks_total }}">
{% for bookmark_item in bookmark_list.items %}
<li ld-bookmark-item{% if bookmark_item.css_classes %} class="{{ bookmark_item.css_classes }}"{% endif %}>
{% csrf_token %}
<div class="content">
<div class="title">
<label class="form-checkbox bulk-edit-checkbox">