2019-06-28 17:37:41 +00:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.utils import timezone
|
|
|
|
|
2020-09-27 07:34:56 +00:00
|
|
|
from bookmarks.models import Bookmark, parse_tag_string
|
2019-07-03 15:18:29 +00:00
|
|
|
from bookmarks.services.tags import get_or_create_tags
|
|
|
|
from bookmarks.services.website_loader import load_website_metadata
|
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
|
2020-09-27 07:34:56 +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()
|
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):
|
2019-06-28 22:27:20 +00:00
|
|
|
# Update website info
|
|
|
|
_update_website_metadata(bookmark)
|
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()
|
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
|
|
|
|
|
|
|
|
|
|
|
|
def unarchive_bookmark(bookmark: Bookmark):
|
|
|
|
bookmark.is_archived = False
|
|
|
|
bookmark.date_modified = timezone.now()
|
|
|
|
bookmark.save()
|
|
|
|
return bookmark
|
|
|
|
|
|
|
|
|
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
|
2019-06-28 17:37:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _update_website_metadata(bookmark: Bookmark):
|
2019-07-01 23:28:02 +00:00
|
|
|
metadata = load_website_metadata(bookmark.url)
|
|
|
|
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):
|
|
|
|
tag_names = parse_tag_string(tag_string, ' ')
|
|
|
|
tags = get_or_create_tags(tag_names, user)
|
|
|
|
bookmark.tags.set(tags)
|