linkding/bookmarks/models.py

86 lines
3.0 KiB
Python
Raw Normal View History

2019-07-01 20:05:38 +00:00
from typing import List
2019-06-28 23:08:22 +00:00
from django import forms
2019-06-27 06:09:51 +00:00
from django.contrib.auth import get_user_model
from django.db import models
2019-06-30 05:15:46 +00:00
class Tag(models.Model):
name = models.CharField(max_length=64)
date_added = models.DateTimeField()
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
2019-06-30 06:24:21 +00:00
def __str__(self):
return self.name
2019-06-30 05:15:46 +00:00
2019-07-01 20:05:38 +00:00
def parse_tag_string(tag_string: str, delimiter: str = ','):
if not tag_string:
return []
names = tag_string.strip().split(delimiter)
names = [name for name in names if name]
names.sort(key=str.lower)
return names
def build_tag_string(tag_names: List[str], delimiter: str = ','):
return delimiter.join(tag_names)
2019-06-27 06:09:51 +00:00
class Bookmark(models.Model):
2020-09-13 07:05:50 +00:00
url = models.URLField(max_length=2048)
title = models.CharField(max_length=512, blank=True)
description = models.TextField(blank=True)
2019-06-29 00:01:26 +00:00
website_title = models.CharField(max_length=512, blank=True, null=True)
website_description = models.TextField(blank=True, null=True)
2019-06-27 06:09:51 +00:00
unread = models.BooleanField(default=True)
date_added = models.DateTimeField()
2019-06-28 22:27:20 +00:00
date_modified = models.DateTimeField()
date_accessed = models.DateTimeField(blank=True, null=True)
2019-06-27 06:09:51 +00:00
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
2019-06-30 05:15:46 +00:00
tags = models.ManyToManyField(Tag)
2019-06-27 06:09:51 +00:00
2019-06-30 06:24:21 +00:00
# Attributes might be calculated in query
tag_count = 0 # Projection for number of associated tags
tag_string = '' # Projection for list of tag names, comma-separated
tag_projection = False # Tracks if the above projections were loaded
2019-06-30 06:24:21 +00:00
2019-06-28 17:37:41 +00:00
@property
def resolved_title(self):
return self.website_title if not self.title else self.title
@property
def resolved_description(self):
return self.website_description if not self.description else self.description
2019-06-30 06:24:21 +00:00
@property
def tag_names(self):
# If tag projections were loaded then avoid querying all tags (=executing further selects)
2019-07-02 00:05:09 +00:00
if self.tag_projection:
2019-07-01 20:05:38 +00:00
return parse_tag_string(self.tag_string)
else:
return [tag.name for tag in self.tags.all()]
2019-06-30 06:24:21 +00:00
2019-06-27 06:09:51 +00:00
def __str__(self):
2019-06-28 22:27:20 +00:00
return self.resolved_title + ' (' + self.url[:30] + '...)'
2019-06-28 23:08:22 +00:00
class BookmarkForm(forms.ModelForm):
# Use URLField for URL
url = forms.URLField()
2019-07-01 20:05:38 +00:00
tag_string = forms.CharField(required=False)
2019-06-28 23:08:22 +00:00
# Do not require title and description in form as we fill these automatically if they are empty
title = forms.CharField(max_length=512,
2019-07-01 19:03:27 +00:00
required=False)
2019-06-28 23:08:22 +00:00
description = forms.CharField(required=False,
2019-07-01 19:03:27 +00:00
widget=forms.Textarea())
2019-07-05 20:29:21 +00:00
# Hidden field that determines whether to close window/tab after saving the bookmark
auto_close = forms.CharField(required=False)
# Hidden field that determines where to redirect after saving the form
return_url = forms.CharField(required=False)
2019-06-28 23:08:22 +00:00
class Meta:
model = Bookmark
fields = ['url', 'tag_string', 'title', 'description', 'auto_close', 'return_url']