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
|
2021-03-28 10:11:56 +00:00
|
|
|
from django.contrib.auth.models import User
|
2019-06-27 06:09:51 +00:00
|
|
|
from django.db import models
|
2021-03-28 10:11:56 +00:00
|
|
|
from django.db.models.signals import post_save
|
|
|
|
from django.dispatch import receiver
|
2019-06-27 06:09:51 +00:00
|
|
|
|
2021-01-02 10:30:20 +00:00
|
|
|
from bookmarks.utils import unique
|
2021-02-06 15:27:19 +00:00
|
|
|
from bookmarks.validators import BookmarkURLValidator
|
2021-01-02 10:30:20 +00:00
|
|
|
|
2019-06-27 06:09:51 +00:00
|
|
|
|
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
|
|
|
|
2021-12-12 21:54:22 +00:00
|
|
|
def sanitize_tag_name(tag_name: str):
|
|
|
|
# strip leading/trailing spaces
|
|
|
|
# replace inner spaces with replacement char
|
|
|
|
return tag_name.strip().replace(' ', '-')
|
|
|
|
|
|
|
|
|
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)
|
2021-12-12 21:54:22 +00:00
|
|
|
# remove empty names, sanitize remaining names
|
|
|
|
names = [sanitize_tag_name(name) for name in names if name]
|
|
|
|
# remove duplicates
|
2021-01-02 10:30:20 +00:00
|
|
|
names = unique(names, str.lower)
|
2019-07-01 20:05:38 +00:00
|
|
|
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):
|
2021-02-06 15:27:19 +00:00
|
|
|
url = models.CharField(max_length=2048, validators=[BookmarkURLValidator()])
|
2020-09-27 07:34:56 +00:00
|
|
|
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)
|
2021-09-04 20:31:04 +00:00
|
|
|
web_archive_snapshot_url = models.CharField(max_length=2048, blank=True)
|
2019-06-27 06:09:51 +00:00
|
|
|
unread = models.BooleanField(default=True)
|
2021-02-14 17:00:22 +00:00
|
|
|
is_archived = models.BooleanField(default=False)
|
2019-06-27 06:09:51 +00:00
|
|
|
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
|
2019-07-01 23:28:02 +00:00
|
|
|
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):
|
2021-01-15 23:57:57 +00:00
|
|
|
if self.title:
|
|
|
|
return self.title
|
|
|
|
elif self.website_title:
|
|
|
|
return self.website_title
|
|
|
|
else:
|
|
|
|
return self.url
|
2019-06-28 17:37:41 +00:00
|
|
|
|
|
|
|
@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):
|
2019-07-01 23:28:02 +00:00
|
|
|
# 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
|
2021-02-06 15:27:19 +00:00
|
|
|
url = forms.CharField(validators=[BookmarkURLValidator()])
|
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)
|
2019-06-28 23:08:22 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Bookmark
|
2022-03-25 17:29:54 +00:00
|
|
|
fields = ['url', 'tag_string', 'title', 'description', 'auto_close']
|
2021-03-28 10:11:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserProfile(models.Model):
|
|
|
|
THEME_AUTO = 'auto'
|
|
|
|
THEME_LIGHT = 'light'
|
|
|
|
THEME_DARK = 'dark'
|
|
|
|
THEME_CHOICES = [
|
|
|
|
(THEME_AUTO, 'Auto'),
|
|
|
|
(THEME_LIGHT, 'Light'),
|
|
|
|
(THEME_DARK, 'Dark'),
|
|
|
|
]
|
2021-03-31 07:08:19 +00:00
|
|
|
BOOKMARK_DATE_DISPLAY_RELATIVE = 'relative'
|
|
|
|
BOOKMARK_DATE_DISPLAY_ABSOLUTE = 'absolute'
|
|
|
|
BOOKMARK_DATE_DISPLAY_HIDDEN = 'hidden'
|
|
|
|
BOOKMARK_DATE_DISPLAY_CHOICES = [
|
|
|
|
(BOOKMARK_DATE_DISPLAY_RELATIVE, 'Relative'),
|
|
|
|
(BOOKMARK_DATE_DISPLAY_ABSOLUTE, 'Absolute'),
|
|
|
|
(BOOKMARK_DATE_DISPLAY_HIDDEN, 'Hidden'),
|
|
|
|
]
|
2021-10-03 07:35:59 +00:00
|
|
|
BOOKMARK_LINK_TARGET_BLANK = '_blank'
|
|
|
|
BOOKMARK_LINK_TARGET_SELF = '_self'
|
|
|
|
BOOKMARK_LINK_TARGET_CHOICES = [
|
|
|
|
(BOOKMARK_LINK_TARGET_BLANK, 'New page'),
|
|
|
|
(BOOKMARK_LINK_TARGET_SELF, 'Same page'),
|
|
|
|
]
|
2021-03-28 10:11:56 +00:00
|
|
|
user = models.OneToOneField(get_user_model(), related_name='profile', on_delete=models.CASCADE)
|
|
|
|
theme = models.CharField(max_length=10, choices=THEME_CHOICES, blank=False, default=THEME_AUTO)
|
2021-03-31 07:08:19 +00:00
|
|
|
bookmark_date_display = models.CharField(max_length=10, choices=BOOKMARK_DATE_DISPLAY_CHOICES, blank=False,
|
|
|
|
default=BOOKMARK_DATE_DISPLAY_RELATIVE)
|
2021-10-03 07:35:59 +00:00
|
|
|
bookmark_link_target = models.CharField(max_length=10, choices=BOOKMARK_LINK_TARGET_CHOICES, blank=False,
|
|
|
|
default=BOOKMARK_LINK_TARGET_BLANK)
|
2021-03-28 10:11:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserProfileForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = UserProfile
|
2021-10-03 07:35:59 +00:00
|
|
|
fields = ['theme', 'bookmark_date_display', 'bookmark_link_target']
|
2021-03-28 10:11:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=get_user_model())
|
|
|
|
def create_user_profile(sender, instance, created, **kwargs):
|
|
|
|
if created:
|
|
|
|
UserProfile.objects.create(user=instance)
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=get_user_model())
|
|
|
|
def save_user_profile(sender, instance, **kwargs):
|
|
|
|
instance.profile.save()
|