mirror of
				https://github.com/sissbruecker/linkding.git
				synced 2025-11-03 20:44:05 +01:00 
			
		
		
		
	Implement tag model
This commit is contained in:
		@@ -1,9 +1,11 @@
 | 
			
		||||
from datetime import datetime
 | 
			
		||||
 | 
			
		||||
from bs4 import BeautifulSoup, Tag
 | 
			
		||||
import bs4
 | 
			
		||||
from bs4 import BeautifulSoup
 | 
			
		||||
from django.contrib.auth.models import User
 | 
			
		||||
from django.utils import timezone
 | 
			
		||||
 | 
			
		||||
from bookmarks.models import Bookmark
 | 
			
		||||
from bookmarks.models import Bookmark, Tag
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def import_netscape_html(html: str, user: User):
 | 
			
		||||
@@ -15,7 +17,7 @@ def import_netscape_html(html: str, user: User):
 | 
			
		||||
        _import_bookmark_tag(bookmark_tag, user)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def _import_bookmark_tag(bookmark_tag: Tag, user: User):
 | 
			
		||||
def _import_bookmark_tag(bookmark_tag: bs4.Tag, user: User):
 | 
			
		||||
    link_tag = bookmark_tag.a
 | 
			
		||||
 | 
			
		||||
    if link_tag is None:
 | 
			
		||||
@@ -34,9 +36,27 @@ def _import_bookmark_tag(bookmark_tag: Tag, user: User):
 | 
			
		||||
 | 
			
		||||
    bookmark.save()
 | 
			
		||||
 | 
			
		||||
    # Set tags
 | 
			
		||||
    tag_string = link_tag['tags']
 | 
			
		||||
    tag_names = tag_string.strip().split(',')
 | 
			
		||||
 | 
			
		||||
    tags = [_get_or_create_tag(tag_name, user) for tag_name in tag_names]
 | 
			
		||||
    bookmark.tags.set(tags)
 | 
			
		||||
    bookmark.save()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def _get_or_create_bookmark(url: str, user: User):
 | 
			
		||||
    try:
 | 
			
		||||
        return Bookmark.objects.get(url=url, owner=user)
 | 
			
		||||
    except Bookmark.DoesNotExist:
 | 
			
		||||
        return Bookmark()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def _get_or_create_tag(name: str, user: User):
 | 
			
		||||
    try:
 | 
			
		||||
        return Tag.objects.get(name=name, owner=user)
 | 
			
		||||
    except Tag.DoesNotExist:
 | 
			
		||||
        tag = Tag(name=name, owner=user)
 | 
			
		||||
        tag.date_added = timezone.now()
 | 
			
		||||
        tag.save()
 | 
			
		||||
        return tag
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user