2021-02-24 02:36:27 +00:00
|
|
|
from django.contrib import admin, messages
|
|
|
|
from django.contrib.admin import AdminSite
|
|
|
|
from django.contrib.auth.admin import UserAdmin
|
|
|
|
from django.contrib.auth.models import User
|
2024-04-07 09:11:14 +00:00
|
|
|
from django.core.paginator import Paginator
|
2021-02-24 02:36:27 +00:00
|
|
|
from django.db.models import Count, QuerySet
|
2024-04-07 09:11:14 +00:00
|
|
|
from django.shortcuts import render
|
|
|
|
from django.urls import path
|
2021-02-24 02:36:27 +00:00
|
|
|
from django.utils.translation import ngettext, gettext
|
2024-04-07 09:11:14 +00:00
|
|
|
from huey.contrib.djhuey import HUEY as huey
|
2021-02-24 02:36:27 +00:00
|
|
|
from rest_framework.authtoken.admin import TokenAdmin
|
2021-08-17 03:48:45 +00:00
|
|
|
from rest_framework.authtoken.models import TokenProxy
|
2019-06-27 06:09:51 +00:00
|
|
|
|
2024-04-07 09:11:14 +00:00
|
|
|
from bookmarks.models import Bookmark, BookmarkAsset, Tag, UserProfile, Toast, FeedToken
|
2021-02-24 02:36:27 +00:00
|
|
|
from bookmarks.services.bookmarks import archive_bookmark, unarchive_bookmark
|
|
|
|
|
|
|
|
|
2024-04-07 09:11:14 +00:00
|
|
|
# Custom paginator to paginate through Huey tasks
|
|
|
|
class TaskPaginator(Paginator):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__(self, 100)
|
|
|
|
self.task_count = huey.storage.queue_size()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def count(self):
|
|
|
|
return self.task_count
|
|
|
|
|
|
|
|
def page(self, number):
|
|
|
|
limit = self.per_page
|
|
|
|
offset = (number - 1) * self.per_page
|
|
|
|
return self._get_page(
|
|
|
|
self.enqueued_items(limit, offset),
|
|
|
|
number,
|
|
|
|
self,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Copied from Huey's SqliteStorage with some modifications to allow pagination
|
|
|
|
def enqueued_items(self, limit, offset):
|
|
|
|
to_bytes = lambda b: bytes(b) if not isinstance(b, bytes) else b
|
|
|
|
sql = "select data from task where queue=? order by priority desc, id limit ? offset ?"
|
|
|
|
params = (huey.storage.name, limit, offset)
|
|
|
|
|
|
|
|
serialized_tasks = [
|
|
|
|
to_bytes(i) for i, in huey.storage.sql(sql, params, results=True)
|
|
|
|
]
|
|
|
|
return [huey.deserialize_task(task) for task in serialized_tasks]
|
|
|
|
|
|
|
|
|
|
|
|
# Custom view to display Huey tasks in the admin
|
|
|
|
def background_task_view(request):
|
|
|
|
page_number = int(request.GET.get("p", 1))
|
|
|
|
paginator = TaskPaginator()
|
|
|
|
page = paginator.get_page(page_number)
|
|
|
|
page_range = paginator.get_elided_page_range(page_number, on_each_side=2, on_ends=2)
|
|
|
|
context = {
|
|
|
|
**linkding_admin_site.each_context(request),
|
|
|
|
"title": "Background tasks",
|
|
|
|
"page": page,
|
|
|
|
"page_range": page_range,
|
|
|
|
"tasks": page.object_list,
|
|
|
|
}
|
|
|
|
return render(request, "admin/background_tasks.html", context)
|
|
|
|
|
|
|
|
|
2021-02-24 02:36:27 +00:00
|
|
|
class LinkdingAdminSite(AdminSite):
|
2024-01-27 10:29:16 +00:00
|
|
|
site_header = "linkding administration"
|
|
|
|
site_title = "linkding Admin"
|
2021-02-24 02:36:27 +00:00
|
|
|
|
2024-04-07 09:11:14 +00:00
|
|
|
def get_urls(self):
|
|
|
|
urls = super().get_urls()
|
|
|
|
custom_urls = [
|
|
|
|
path("tasks/", background_task_view, name="background_tasks"),
|
|
|
|
]
|
|
|
|
return custom_urls + urls
|
|
|
|
|
|
|
|
def get_app_list(self, request, app_label=None):
|
|
|
|
app_list = super().get_app_list(request, app_label)
|
|
|
|
app_list += [
|
|
|
|
{
|
|
|
|
"name": "Huey",
|
|
|
|
"app_label": "huey_app",
|
|
|
|
"models": [
|
|
|
|
{
|
|
|
|
"name": "Queued tasks",
|
|
|
|
"object_name": "background_tasks",
|
|
|
|
"admin_url": "/admin/tasks/",
|
|
|
|
"view_only": True,
|
|
|
|
}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
]
|
|
|
|
return app_list
|
|
|
|
|
2019-06-27 06:09:51 +00:00
|
|
|
|
2019-12-28 21:44:14 +00:00
|
|
|
class AdminBookmark(admin.ModelAdmin):
|
2024-01-27 10:29:16 +00:00
|
|
|
list_display = ("resolved_title", "url", "is_archived", "owner", "date_added")
|
|
|
|
search_fields = (
|
|
|
|
"title",
|
|
|
|
"description",
|
|
|
|
"website_title",
|
|
|
|
"website_description",
|
|
|
|
"url",
|
|
|
|
"tags__name",
|
|
|
|
)
|
|
|
|
list_filter = (
|
|
|
|
"owner__username",
|
|
|
|
"is_archived",
|
|
|
|
"unread",
|
|
|
|
"tags",
|
|
|
|
)
|
|
|
|
ordering = ("-date_added",)
|
|
|
|
actions = [
|
|
|
|
"delete_selected_bookmarks",
|
|
|
|
"archive_selected_bookmarks",
|
|
|
|
"unarchive_selected_bookmarks",
|
|
|
|
"mark_as_read",
|
|
|
|
"mark_as_unread",
|
|
|
|
]
|
2022-09-09 06:31:28 +00:00
|
|
|
|
|
|
|
def get_actions(self, request):
|
|
|
|
actions = super().get_actions(request)
|
|
|
|
# Remove default delete action, which gets replaced by delete_selected_bookmarks below
|
|
|
|
# The default action shows a confirmation page which can fail in production when selecting all bookmarks and the
|
|
|
|
# number of objects to delete exceeds the value in DATA_UPLOAD_MAX_NUMBER_FIELDS (1000 by default)
|
2024-01-27 10:29:16 +00:00
|
|
|
del actions["delete_selected"]
|
2022-09-09 06:31:28 +00:00
|
|
|
return actions
|
|
|
|
|
|
|
|
def delete_selected_bookmarks(self, request, queryset: QuerySet):
|
|
|
|
bookmarks_count = queryset.count()
|
|
|
|
for bookmark in queryset:
|
|
|
|
bookmark.delete()
|
2024-01-27 10:29:16 +00:00
|
|
|
self.message_user(
|
|
|
|
request,
|
|
|
|
ngettext(
|
|
|
|
"%d bookmark was successfully deleted.",
|
|
|
|
"%d bookmarks were successfully deleted.",
|
|
|
|
bookmarks_count,
|
|
|
|
)
|
|
|
|
% bookmarks_count,
|
|
|
|
messages.SUCCESS,
|
|
|
|
)
|
2021-02-24 02:36:27 +00:00
|
|
|
|
|
|
|
def archive_selected_bookmarks(self, request, queryset: QuerySet):
|
|
|
|
for bookmark in queryset:
|
|
|
|
archive_bookmark(bookmark)
|
|
|
|
bookmarks_count = queryset.count()
|
2024-01-27 10:29:16 +00:00
|
|
|
self.message_user(
|
|
|
|
request,
|
|
|
|
ngettext(
|
|
|
|
"%d bookmark was successfully archived.",
|
|
|
|
"%d bookmarks were successfully archived.",
|
|
|
|
bookmarks_count,
|
|
|
|
)
|
|
|
|
% bookmarks_count,
|
|
|
|
messages.SUCCESS,
|
|
|
|
)
|
2021-02-24 02:36:27 +00:00
|
|
|
|
|
|
|
def unarchive_selected_bookmarks(self, request, queryset: QuerySet):
|
|
|
|
for bookmark in queryset:
|
|
|
|
unarchive_bookmark(bookmark)
|
|
|
|
bookmarks_count = queryset.count()
|
2024-01-27 10:29:16 +00:00
|
|
|
self.message_user(
|
|
|
|
request,
|
|
|
|
ngettext(
|
|
|
|
"%d bookmark was successfully unarchived.",
|
|
|
|
"%d bookmarks were successfully unarchived.",
|
|
|
|
bookmarks_count,
|
|
|
|
)
|
|
|
|
% bookmarks_count,
|
|
|
|
messages.SUCCESS,
|
|
|
|
)
|
2021-02-24 02:36:27 +00:00
|
|
|
|
2022-07-23 20:17:20 +00:00
|
|
|
def mark_as_read(self, request, queryset: QuerySet):
|
|
|
|
bookmarks_count = queryset.count()
|
|
|
|
queryset.update(unread=False)
|
2024-01-27 10:29:16 +00:00
|
|
|
self.message_user(
|
|
|
|
request,
|
|
|
|
ngettext(
|
|
|
|
"%d bookmark marked as read.",
|
|
|
|
"%d bookmarks marked as read.",
|
|
|
|
bookmarks_count,
|
|
|
|
)
|
|
|
|
% bookmarks_count,
|
|
|
|
messages.SUCCESS,
|
|
|
|
)
|
2022-07-23 20:17:20 +00:00
|
|
|
|
|
|
|
def mark_as_unread(self, request, queryset: QuerySet):
|
|
|
|
bookmarks_count = queryset.count()
|
|
|
|
queryset.update(unread=True)
|
2024-01-27 10:29:16 +00:00
|
|
|
self.message_user(
|
|
|
|
request,
|
|
|
|
ngettext(
|
|
|
|
"%d bookmark marked as unread.",
|
|
|
|
"%d bookmarks marked as unread.",
|
|
|
|
bookmarks_count,
|
|
|
|
)
|
|
|
|
% bookmarks_count,
|
|
|
|
messages.SUCCESS,
|
|
|
|
)
|
2022-07-23 20:17:20 +00:00
|
|
|
|
2019-12-28 21:44:14 +00:00
|
|
|
|
2024-04-07 09:11:14 +00:00
|
|
|
class AdminBookmarkAsset(admin.ModelAdmin):
|
|
|
|
list_display = ("display_name", "date_created", "status")
|
|
|
|
search_fields = (
|
|
|
|
"display_name",
|
|
|
|
"file",
|
|
|
|
)
|
|
|
|
list_filter = ("status",)
|
|
|
|
|
|
|
|
|
2019-12-28 21:44:14 +00:00
|
|
|
class AdminTag(admin.ModelAdmin):
|
2024-01-27 10:29:16 +00:00
|
|
|
list_display = ("name", "bookmarks_count", "owner", "date_added")
|
|
|
|
search_fields = ("name", "owner__username")
|
|
|
|
list_filter = ("owner__username",)
|
|
|
|
ordering = ("-date_added",)
|
|
|
|
actions = ["delete_unused_tags"]
|
2021-02-24 02:36:27 +00:00
|
|
|
|
|
|
|
def get_queryset(self, request):
|
|
|
|
queryset = super().get_queryset(request)
|
|
|
|
queryset = queryset.annotate(bookmarks_count=Count("bookmark"))
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
def bookmarks_count(self, obj):
|
|
|
|
return obj.bookmarks_count
|
2022-05-14 07:46:51 +00:00
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
bookmarks_count.admin_order_field = "bookmarks_count"
|
2021-02-24 02:36:27 +00:00
|
|
|
|
|
|
|
def delete_unused_tags(self, request, queryset: QuerySet):
|
|
|
|
unused_tags = queryset.filter(bookmark__isnull=True)
|
|
|
|
unused_tags_count = unused_tags.count()
|
|
|
|
for tag in unused_tags:
|
|
|
|
tag.delete()
|
|
|
|
|
|
|
|
if unused_tags_count > 0:
|
2024-01-27 10:29:16 +00:00
|
|
|
self.message_user(
|
|
|
|
request,
|
|
|
|
ngettext(
|
|
|
|
"%d unused tag was successfully deleted.",
|
|
|
|
"%d unused tags were successfully deleted.",
|
|
|
|
unused_tags_count,
|
|
|
|
)
|
|
|
|
% unused_tags_count,
|
|
|
|
messages.SUCCESS,
|
|
|
|
)
|
2021-02-24 02:36:27 +00:00
|
|
|
else:
|
2024-01-27 10:29:16 +00:00
|
|
|
self.message_user(
|
|
|
|
request,
|
|
|
|
gettext(
|
|
|
|
"There were no unused tags in the selection",
|
|
|
|
),
|
|
|
|
messages.SUCCESS,
|
|
|
|
)
|
2021-02-24 02:36:27 +00:00
|
|
|
|
|
|
|
|
2021-03-28 10:11:56 +00:00
|
|
|
class AdminUserProfileInline(admin.StackedInline):
|
|
|
|
model = UserProfile
|
|
|
|
can_delete = False
|
2024-01-27 10:29:16 +00:00
|
|
|
verbose_name_plural = "Profile"
|
|
|
|
fk_name = "user"
|
|
|
|
readonly_fields = ("search_preferences",)
|
|
|
|
|
2021-03-28 10:11:56 +00:00
|
|
|
|
|
|
|
class AdminCustomUser(UserAdmin):
|
|
|
|
inlines = (AdminUserProfileInline,)
|
|
|
|
|
|
|
|
def get_inline_instances(self, request, obj=None):
|
|
|
|
if not obj:
|
|
|
|
return list()
|
|
|
|
return super(AdminCustomUser, self).get_inline_instances(request, obj)
|
|
|
|
|
|
|
|
|
2022-05-14 07:46:51 +00:00
|
|
|
class AdminToast(admin.ModelAdmin):
|
2024-01-27 10:29:16 +00:00
|
|
|
list_display = ("key", "message", "owner", "acknowledged")
|
|
|
|
search_fields = ("key", "message")
|
|
|
|
list_filter = ("owner__username",)
|
2022-05-14 07:46:51 +00:00
|
|
|
|
|
|
|
|
2022-07-23 21:20:27 +00:00
|
|
|
class AdminFeedToken(admin.ModelAdmin):
|
2024-01-27 10:29:16 +00:00
|
|
|
list_display = ("key", "user")
|
|
|
|
search_fields = ["key"]
|
|
|
|
list_filter = ("user__username",)
|
2022-07-23 21:20:27 +00:00
|
|
|
|
|
|
|
|
2021-02-24 02:36:27 +00:00
|
|
|
linkding_admin_site = LinkdingAdminSite()
|
|
|
|
linkding_admin_site.register(Bookmark, AdminBookmark)
|
2024-04-07 09:11:14 +00:00
|
|
|
linkding_admin_site.register(BookmarkAsset, AdminBookmarkAsset)
|
2021-02-24 02:36:27 +00:00
|
|
|
linkding_admin_site.register(Tag, AdminTag)
|
2021-03-28 10:11:56 +00:00
|
|
|
linkding_admin_site.register(User, AdminCustomUser)
|
2021-08-17 03:48:45 +00:00
|
|
|
linkding_admin_site.register(TokenProxy, TokenAdmin)
|
2022-05-14 07:46:51 +00:00
|
|
|
linkding_admin_site.register(Toast, AdminToast)
|
2022-07-23 21:20:27 +00:00
|
|
|
linkding_admin_site.register(FeedToken, AdminFeedToken)
|