2024-04-20 10:14:11 +00:00
|
|
|
import logging
|
|
|
|
import os
|
2021-03-28 22:43:50 +00:00
|
|
|
from typing import Union
|
|
|
|
|
2024-04-20 10:14:11 +00:00
|
|
|
from django.conf import settings
|
2019-06-28 17:37:41 +00:00
|
|
|
from django.contrib.auth.models import User
|
2024-04-20 10:14:11 +00:00
|
|
|
from django.core.files.uploadedfile import UploadedFile
|
2019-06-28 17:37:41 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
|
2024-04-20 10:14:11 +00:00
|
|
|
from bookmarks.models import Bookmark, BookmarkAsset, parse_tag_string
|
2021-09-04 20:31:04 +00:00
|
|
|
from bookmarks.services import tasks
|
2024-04-20 10:14:11 +00:00
|
|
|
from bookmarks.services import website_loader
|
2024-05-17 07:39:46 +00:00
|
|
|
from bookmarks.services import auto_tagging
|
2024-04-20 10:14:11 +00:00
|
|
|
from bookmarks.services.tags import get_or_create_tags
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2019-06-28 17:37:41 +00:00
|
|
|
|
|
|
|
|
2020-09-27 07:34:56 +00:00
|
|
|
def create_bookmark(bookmark: Bookmark, tag_string: str, current_user: User):
|
2020-09-13 06:46:07 +00:00
|
|
|
# If URL is already bookmarked, then update it
|
2024-01-27 10:29:16 +00:00
|
|
|
existing_bookmark: Bookmark = Bookmark.objects.filter(
|
|
|
|
owner=current_user, url=bookmark.url
|
|
|
|
).first()
|
2020-09-13 06:46:07 +00:00
|
|
|
|
|
|
|
if existing_bookmark is not None:
|
2020-09-27 07:34:56 +00:00
|
|
|
_merge_bookmark_data(bookmark, existing_bookmark)
|
|
|
|
return update_bookmark(existing_bookmark, tag_string, current_user)
|
2020-09-13 06:46:07 +00:00
|
|
|
|
2019-06-28 17:37:41 +00:00
|
|
|
# Update website info
|
|
|
|
_update_website_metadata(bookmark)
|
|
|
|
# Set currently logged in user as owner
|
|
|
|
bookmark.owner = current_user
|
|
|
|
# Set dates
|
|
|
|
bookmark.date_added = timezone.now()
|
2019-06-28 22:27:20 +00:00
|
|
|
bookmark.date_modified = timezone.now()
|
|
|
|
bookmark.save()
|
2019-07-01 20:05:38 +00:00
|
|
|
# Update tag list
|
2020-09-27 07:34:56 +00:00
|
|
|
_update_bookmark_tags(bookmark, tag_string, current_user)
|
2019-07-01 20:05:38 +00:00
|
|
|
bookmark.save()
|
2021-09-04 20:31:04 +00:00
|
|
|
# Create snapshot on web archive
|
2022-05-14 07:46:51 +00:00
|
|
|
tasks.create_web_archive_snapshot(current_user, bookmark, False)
|
2023-01-21 15:36:10 +00:00
|
|
|
# Load favicon
|
|
|
|
tasks.load_favicon(current_user, bookmark)
|
2024-05-07 16:58:52 +00:00
|
|
|
# Load preview image
|
|
|
|
tasks.load_preview_image(current_user, bookmark)
|
2024-04-01 13:19:38 +00:00
|
|
|
# Create HTML snapshot
|
|
|
|
if current_user.profile.enable_automatic_html_snapshots:
|
|
|
|
tasks.create_html_snapshot(bookmark)
|
2021-09-04 20:31:04 +00:00
|
|
|
|
2020-09-27 07:34:56 +00:00
|
|
|
return bookmark
|
2019-06-28 22:27:20 +00:00
|
|
|
|
|
|
|
|
2020-09-27 07:34:56 +00:00
|
|
|
def update_bookmark(bookmark: Bookmark, tag_string, current_user: User):
|
2021-09-04 20:31:04 +00:00
|
|
|
# Detect URL change
|
|
|
|
original_bookmark = Bookmark.objects.get(id=bookmark.id)
|
|
|
|
has_url_changed = original_bookmark.url != bookmark.url
|
2019-07-01 20:05:38 +00:00
|
|
|
# Update tag list
|
2020-09-27 07:34:56 +00:00
|
|
|
_update_bookmark_tags(bookmark, tag_string, current_user)
|
2019-06-28 22:27:20 +00:00
|
|
|
# Update dates
|
|
|
|
bookmark.date_modified = timezone.now()
|
2019-06-28 17:37:41 +00:00
|
|
|
bookmark.save()
|
2023-01-21 15:36:10 +00:00
|
|
|
# Update favicon
|
|
|
|
tasks.load_favicon(current_user, bookmark)
|
2024-05-07 16:58:52 +00:00
|
|
|
# Update preview image
|
|
|
|
tasks.load_preview_image(current_user, bookmark)
|
2023-01-21 15:36:10 +00:00
|
|
|
|
2021-09-04 20:31:04 +00:00
|
|
|
if has_url_changed:
|
2022-08-13 09:21:26 +00:00
|
|
|
# Update web archive snapshot, if URL changed
|
2022-05-14 07:46:51 +00:00
|
|
|
tasks.create_web_archive_snapshot(current_user, bookmark, True)
|
2022-08-13 09:21:26 +00:00
|
|
|
# Only update website metadata if URL changed
|
|
|
|
_update_website_metadata(bookmark)
|
2023-01-20 19:59:09 +00:00
|
|
|
bookmark.save()
|
2021-09-04 20:31:04 +00:00
|
|
|
|
2020-09-27 07:34:56 +00:00
|
|
|
return bookmark
|
|
|
|
|
|
|
|
|
2021-02-14 17:00:22 +00:00
|
|
|
def archive_bookmark(bookmark: Bookmark):
|
|
|
|
bookmark.is_archived = True
|
|
|
|
bookmark.date_modified = timezone.now()
|
|
|
|
bookmark.save()
|
|
|
|
return bookmark
|
|
|
|
|
|
|
|
|
2021-03-28 22:43:50 +00:00
|
|
|
def archive_bookmarks(bookmark_ids: [Union[int, str]], current_user: User):
|
|
|
|
sanitized_bookmark_ids = _sanitize_id_list(bookmark_ids)
|
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
Bookmark.objects.filter(owner=current_user, id__in=sanitized_bookmark_ids).update(
|
|
|
|
is_archived=True, date_modified=timezone.now()
|
|
|
|
)
|
2021-03-28 22:43:50 +00:00
|
|
|
|
|
|
|
|
2021-02-14 17:00:22 +00:00
|
|
|
def unarchive_bookmark(bookmark: Bookmark):
|
|
|
|
bookmark.is_archived = False
|
|
|
|
bookmark.date_modified = timezone.now()
|
|
|
|
bookmark.save()
|
|
|
|
return bookmark
|
|
|
|
|
|
|
|
|
2021-03-28 22:43:50 +00:00
|
|
|
def unarchive_bookmarks(bookmark_ids: [Union[int, str]], current_user: User):
|
|
|
|
sanitized_bookmark_ids = _sanitize_id_list(bookmark_ids)
|
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
Bookmark.objects.filter(owner=current_user, id__in=sanitized_bookmark_ids).update(
|
|
|
|
is_archived=False, date_modified=timezone.now()
|
|
|
|
)
|
2021-03-28 22:43:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def delete_bookmarks(bookmark_ids: [Union[int, str]], current_user: User):
|
|
|
|
sanitized_bookmark_ids = _sanitize_id_list(bookmark_ids)
|
|
|
|
|
2024-01-27 08:13:21 +00:00
|
|
|
Bookmark.objects.filter(owner=current_user, id__in=sanitized_bookmark_ids).delete()
|
2021-03-28 22:43:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def tag_bookmarks(bookmark_ids: [Union[int, str]], tag_string: str, current_user: User):
|
|
|
|
sanitized_bookmark_ids = _sanitize_id_list(bookmark_ids)
|
2024-01-27 10:29:16 +00:00
|
|
|
owned_bookmark_ids = Bookmark.objects.filter(
|
|
|
|
owner=current_user, id__in=sanitized_bookmark_ids
|
|
|
|
).values_list("id", flat=True)
|
2021-12-12 21:54:22 +00:00
|
|
|
tag_names = parse_tag_string(tag_string)
|
2021-03-28 22:43:50 +00:00
|
|
|
tags = get_or_create_tags(tag_names, current_user)
|
|
|
|
|
2024-01-27 08:13:21 +00:00
|
|
|
BookmarkToTagRelationShip = Bookmark.tags.through
|
|
|
|
relationships = []
|
|
|
|
for tag in tags:
|
|
|
|
for bookmark_id in owned_bookmark_ids:
|
2024-01-27 10:29:16 +00:00
|
|
|
relationships.append(
|
|
|
|
BookmarkToTagRelationShip(bookmark_id=bookmark_id, tag=tag)
|
|
|
|
)
|
2021-03-28 22:43:50 +00:00
|
|
|
|
2024-01-27 08:13:21 +00:00
|
|
|
# Insert all bookmark -> tag associations at once, should ignore errors if association already exists
|
|
|
|
BookmarkToTagRelationShip.objects.bulk_create(relationships, ignore_conflicts=True)
|
2024-01-27 10:29:16 +00:00
|
|
|
Bookmark.objects.filter(id__in=owned_bookmark_ids).update(
|
|
|
|
date_modified=timezone.now()
|
|
|
|
)
|
2021-03-28 22:43:50 +00:00
|
|
|
|
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
def untag_bookmarks(
|
|
|
|
bookmark_ids: [Union[int, str]], tag_string: str, current_user: User
|
|
|
|
):
|
2021-03-28 22:43:50 +00:00
|
|
|
sanitized_bookmark_ids = _sanitize_id_list(bookmark_ids)
|
2024-01-27 10:29:16 +00:00
|
|
|
owned_bookmark_ids = Bookmark.objects.filter(
|
|
|
|
owner=current_user, id__in=sanitized_bookmark_ids
|
|
|
|
).values_list("id", flat=True)
|
2021-12-12 21:54:22 +00:00
|
|
|
tag_names = parse_tag_string(tag_string)
|
2021-03-28 22:43:50 +00:00
|
|
|
tags = get_or_create_tags(tag_names, current_user)
|
|
|
|
|
2024-01-27 08:13:21 +00:00
|
|
|
BookmarkToTagRelationShip = Bookmark.tags.through
|
|
|
|
for tag in tags:
|
|
|
|
# Remove all bookmark -> tag associations for the owned bookmarks and the current tag
|
2024-01-27 10:29:16 +00:00
|
|
|
BookmarkToTagRelationShip.objects.filter(
|
|
|
|
bookmark_id__in=owned_bookmark_ids, tag=tag
|
|
|
|
).delete()
|
2021-03-28 22:43:50 +00:00
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
Bookmark.objects.filter(id__in=owned_bookmark_ids).update(
|
|
|
|
date_modified=timezone.now()
|
|
|
|
)
|
2021-03-28 22:43:50 +00:00
|
|
|
|
|
|
|
|
2023-08-25 11:54:23 +00:00
|
|
|
def mark_bookmarks_as_read(bookmark_ids: [Union[int, str]], current_user: User):
|
|
|
|
sanitized_bookmark_ids = _sanitize_id_list(bookmark_ids)
|
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
Bookmark.objects.filter(owner=current_user, id__in=sanitized_bookmark_ids).update(
|
|
|
|
unread=False, date_modified=timezone.now()
|
|
|
|
)
|
2023-08-25 11:54:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
def mark_bookmarks_as_unread(bookmark_ids: [Union[int, str]], current_user: User):
|
|
|
|
sanitized_bookmark_ids = _sanitize_id_list(bookmark_ids)
|
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
Bookmark.objects.filter(owner=current_user, id__in=sanitized_bookmark_ids).update(
|
|
|
|
unread=True, date_modified=timezone.now()
|
|
|
|
)
|
2023-08-25 11:54:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
def share_bookmarks(bookmark_ids: [Union[int, str]], current_user: User):
|
|
|
|
sanitized_bookmark_ids = _sanitize_id_list(bookmark_ids)
|
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
Bookmark.objects.filter(owner=current_user, id__in=sanitized_bookmark_ids).update(
|
|
|
|
shared=True, date_modified=timezone.now()
|
|
|
|
)
|
2023-08-25 11:54:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
def unshare_bookmarks(bookmark_ids: [Union[int, str]], current_user: User):
|
|
|
|
sanitized_bookmark_ids = _sanitize_id_list(bookmark_ids)
|
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
Bookmark.objects.filter(owner=current_user, id__in=sanitized_bookmark_ids).update(
|
|
|
|
shared=False, date_modified=timezone.now()
|
|
|
|
)
|
2023-08-25 11:54:23 +00:00
|
|
|
|
|
|
|
|
2024-04-20 10:14:11 +00:00
|
|
|
def _generate_upload_asset_filename(asset: BookmarkAsset, filename: str):
|
|
|
|
formatted_datetime = asset.date_created.strftime("%Y-%m-%d_%H%M%S")
|
|
|
|
return f"{asset.asset_type}_{formatted_datetime}_{filename}"
|
|
|
|
|
|
|
|
|
|
|
|
def upload_asset(bookmark: Bookmark, upload_file: UploadedFile) -> BookmarkAsset:
|
|
|
|
asset = BookmarkAsset(
|
|
|
|
bookmark=bookmark,
|
|
|
|
asset_type=BookmarkAsset.TYPE_UPLOAD,
|
|
|
|
content_type=upload_file.content_type,
|
|
|
|
display_name=upload_file.name,
|
|
|
|
status=BookmarkAsset.STATUS_PENDING,
|
|
|
|
gzip=False,
|
|
|
|
)
|
|
|
|
asset.save()
|
|
|
|
|
|
|
|
try:
|
|
|
|
filename = _generate_upload_asset_filename(asset, upload_file.name)
|
|
|
|
filepath = os.path.join(settings.LD_ASSET_FOLDER, filename)
|
|
|
|
with open(filepath, "wb") as f:
|
|
|
|
for chunk in upload_file.chunks():
|
|
|
|
f.write(chunk)
|
|
|
|
asset.status = BookmarkAsset.STATUS_COMPLETE
|
|
|
|
asset.file = filename
|
|
|
|
asset.file_size = upload_file.size
|
|
|
|
logger.info(
|
|
|
|
f"Successfully uploaded asset file. bookmark={bookmark} file={upload_file.name}"
|
|
|
|
)
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(
|
|
|
|
f"Failed to upload asset file. bookmark={bookmark} file={upload_file.name}",
|
|
|
|
exc_info=e,
|
|
|
|
)
|
|
|
|
asset.status = BookmarkAsset.STATUS_FAILURE
|
|
|
|
|
|
|
|
asset.save()
|
|
|
|
|
|
|
|
return asset
|
|
|
|
|
|
|
|
|
2020-09-27 07:34:56 +00:00
|
|
|
def _merge_bookmark_data(from_bookmark: Bookmark, to_bookmark: Bookmark):
|
|
|
|
to_bookmark.title = from_bookmark.title
|
|
|
|
to_bookmark.description = from_bookmark.description
|
2023-05-20 09:54:26 +00:00
|
|
|
to_bookmark.notes = from_bookmark.notes
|
2022-07-25 22:13:41 +00:00
|
|
|
to_bookmark.unread = from_bookmark.unread
|
2022-08-04 17:37:16 +00:00
|
|
|
to_bookmark.shared = from_bookmark.shared
|
2019-06-28 17:37:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _update_website_metadata(bookmark: Bookmark):
|
2022-08-13 09:21:26 +00:00
|
|
|
metadata = website_loader.load_website_metadata(bookmark.url)
|
2019-07-01 23:28:02 +00:00
|
|
|
bookmark.website_title = metadata.title
|
|
|
|
bookmark.website_description = metadata.description
|
2019-06-29 00:01:26 +00:00
|
|
|
|
|
|
|
|
2019-07-01 20:05:38 +00:00
|
|
|
def _update_bookmark_tags(bookmark: Bookmark, tag_string: str, user: User):
|
2021-12-12 21:54:22 +00:00
|
|
|
tag_names = parse_tag_string(tag_string)
|
2024-05-17 07:39:46 +00:00
|
|
|
|
|
|
|
if user.profile.auto_tagging_rules:
|
|
|
|
auto_tag_names = auto_tagging.get_tags(
|
|
|
|
user.profile.auto_tagging_rules, bookmark.url
|
|
|
|
)
|
|
|
|
for auto_tag_name in auto_tag_names:
|
|
|
|
if auto_tag_name not in tag_names:
|
|
|
|
tag_names.append(auto_tag_name)
|
|
|
|
|
2019-07-01 20:05:38 +00:00
|
|
|
tags = get_or_create_tags(tag_names, user)
|
|
|
|
bookmark.tags.set(tags)
|
2021-03-28 22:43:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _sanitize_id_list(bookmark_ids: [Union[int, str]]) -> [int]:
|
|
|
|
# Convert string ids to int if necessary
|
|
|
|
return [int(bm_id) if isinstance(bm_id, str) else bm_id for bm_id in bookmark_ids]
|