2024-04-01 13:19:38 +00:00
|
|
|
import logging
|
2022-07-23 21:20:27 +00:00
|
|
|
import os
|
2019-07-01 20:05:38 +00:00
|
|
|
from typing import List
|
|
|
|
|
2024-04-01 13:19:38 +00:00
|
|
|
import binascii
|
2019-06-28 23:08:22 +00:00
|
|
|
from django import forms
|
2024-04-01 13:19:38 +00:00
|
|
|
from django.conf import settings
|
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
|
2024-04-01 13:19:38 +00:00
|
|
|
from django.db.models.signals import post_save, post_delete
|
2021-03-28 10:11:56 +00:00
|
|
|
from django.dispatch import receiver
|
2023-10-01 19:22:44 +00:00
|
|
|
from django.http import QueryDict
|
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
|
|
|
|
2024-04-01 13:19:38 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
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
|
2024-01-27 10:29:16 +00:00
|
|
|
return tag_name.strip().replace(" ", "-")
|
2021-12-12 21:54:22 +00:00
|
|
|
|
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
def parse_tag_string(tag_string: str, delimiter: str = ","):
|
2019-07-01 20:05:38 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
def build_tag_string(tag_names: List[str], delimiter: str = ","):
|
2019-07-01 20:05:38 +00:00
|
|
|
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)
|
2023-05-20 09:54:26 +00:00
|
|
|
notes = 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)
|
2023-01-21 15:36:10 +00:00
|
|
|
favicon_file = models.CharField(max_length=512, blank=True)
|
2024-05-10 07:19:00 +00:00
|
|
|
preview_image_file = models.CharField(max_length=512, blank=True)
|
2022-07-23 20:17:20 +00:00
|
|
|
unread = models.BooleanField(default=False)
|
2021-02-14 17:00:22 +00:00
|
|
|
is_archived = models.BooleanField(default=False)
|
2022-08-04 17:37:16 +00:00
|
|
|
shared = 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-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):
|
2022-09-09 17:46:55 +00:00
|
|
|
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):
|
2024-01-27 10:29:16 +00:00
|
|
|
return self.resolved_title + " (" + self.url[:30] + "...)"
|
2019-06-28 23:08:22 +00:00
|
|
|
|
|
|
|
|
2024-04-01 13:19:38 +00:00
|
|
|
class BookmarkAsset(models.Model):
|
|
|
|
TYPE_SNAPSHOT = "snapshot"
|
2024-04-20 10:14:11 +00:00
|
|
|
TYPE_UPLOAD = "upload"
|
2024-04-01 13:19:38 +00:00
|
|
|
|
|
|
|
CONTENT_TYPE_HTML = "text/html"
|
|
|
|
|
|
|
|
STATUS_PENDING = "pending"
|
|
|
|
STATUS_COMPLETE = "complete"
|
|
|
|
STATUS_FAILURE = "failure"
|
|
|
|
|
|
|
|
bookmark = models.ForeignKey(Bookmark, on_delete=models.CASCADE)
|
|
|
|
date_created = models.DateTimeField(auto_now_add=True, null=False)
|
|
|
|
file = models.CharField(max_length=2048, blank=True, null=False)
|
|
|
|
file_size = models.IntegerField(null=True)
|
|
|
|
asset_type = models.CharField(max_length=64, blank=False, null=False)
|
|
|
|
content_type = models.CharField(max_length=128, blank=False, null=False)
|
|
|
|
display_name = models.CharField(max_length=2048, blank=True, null=False)
|
|
|
|
status = models.CharField(max_length=64, blank=False, null=False)
|
|
|
|
gzip = models.BooleanField(default=False, null=False)
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if self.file:
|
|
|
|
try:
|
|
|
|
file_path = os.path.join(settings.LD_ASSET_FOLDER, self.file)
|
|
|
|
if os.path.isfile(file_path):
|
|
|
|
self.file_size = os.path.getsize(file_path)
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
2024-04-17 21:18:23 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.display_name or f"Bookmark Asset #{self.pk}"
|
|
|
|
|
2024-04-01 13:19:38 +00:00
|
|
|
|
|
|
|
@receiver(post_delete, sender=BookmarkAsset)
|
|
|
|
def bookmark_asset_deleted(sender, instance, **kwargs):
|
|
|
|
if instance.file:
|
|
|
|
filepath = os.path.join(settings.LD_ASSET_FOLDER, instance.file)
|
|
|
|
if os.path.isfile(filepath):
|
|
|
|
try:
|
|
|
|
os.remove(filepath)
|
|
|
|
except Exception as error:
|
|
|
|
logger.error(f"Failed to delete asset file: {filepath}", exc_info=error)
|
|
|
|
|
|
|
|
|
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
|
2024-01-27 10:29:16 +00:00
|
|
|
title = forms.CharField(max_length=512, required=False)
|
|
|
|
description = forms.CharField(required=False, widget=forms.Textarea())
|
2022-08-13 09:21:26 +00:00
|
|
|
# Include website title and description as hidden field as they only provide info when editing bookmarks
|
2024-01-27 10:29:16 +00:00
|
|
|
website_title = forms.CharField(
|
|
|
|
max_length=512, required=False, widget=forms.HiddenInput()
|
|
|
|
)
|
|
|
|
website_description = forms.CharField(required=False, widget=forms.HiddenInput())
|
2022-07-23 20:17:20 +00:00
|
|
|
unread = forms.BooleanField(required=False)
|
2022-08-04 17:37:16 +00:00
|
|
|
shared = forms.BooleanField(required=False)
|
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-08-13 09:21:26 +00:00
|
|
|
fields = [
|
2024-01-27 10:29:16 +00:00
|
|
|
"url",
|
|
|
|
"tag_string",
|
|
|
|
"title",
|
|
|
|
"description",
|
|
|
|
"notes",
|
|
|
|
"website_title",
|
|
|
|
"website_description",
|
|
|
|
"unread",
|
|
|
|
"shared",
|
|
|
|
"auto_close",
|
2022-08-13 09:21:26 +00:00
|
|
|
]
|
2022-08-04 17:37:16 +00:00
|
|
|
|
2023-05-20 09:54:26 +00:00
|
|
|
@property
|
|
|
|
def has_notes(self):
|
|
|
|
return self.instance and self.instance.notes
|
|
|
|
|
2022-08-04 17:37:16 +00:00
|
|
|
|
2023-09-01 20:48:21 +00:00
|
|
|
class BookmarkSearch:
|
2024-01-27 10:29:16 +00:00
|
|
|
SORT_ADDED_ASC = "added_asc"
|
|
|
|
SORT_ADDED_DESC = "added_desc"
|
|
|
|
SORT_TITLE_ASC = "title_asc"
|
|
|
|
SORT_TITLE_DESC = "title_desc"
|
2023-09-01 20:48:21 +00:00
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
FILTER_SHARED_OFF = "off"
|
|
|
|
FILTER_SHARED_SHARED = "yes"
|
|
|
|
FILTER_SHARED_UNSHARED = "no"
|
2023-09-10 20:14:07 +00:00
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
FILTER_UNREAD_OFF = "off"
|
|
|
|
FILTER_UNREAD_YES = "yes"
|
|
|
|
FILTER_UNREAD_NO = "no"
|
2023-09-16 08:39:27 +00:00
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
params = ["q", "user", "sort", "shared", "unread"]
|
|
|
|
preferences = ["sort", "shared", "unread"]
|
2023-09-01 20:48:21 +00:00
|
|
|
defaults = {
|
2024-01-27 10:29:16 +00:00
|
|
|
"q": "",
|
|
|
|
"user": "",
|
|
|
|
"sort": SORT_ADDED_DESC,
|
|
|
|
"shared": FILTER_SHARED_OFF,
|
|
|
|
"unread": FILTER_UNREAD_OFF,
|
2023-09-01 20:48:21 +00:00
|
|
|
}
|
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
q: str = None,
|
|
|
|
user: str = None,
|
|
|
|
sort: str = None,
|
|
|
|
shared: str = None,
|
|
|
|
unread: str = None,
|
|
|
|
preferences: dict = None,
|
|
|
|
):
|
2023-10-01 19:22:44 +00:00
|
|
|
if not preferences:
|
|
|
|
preferences = {}
|
|
|
|
self.defaults = {**BookmarkSearch.defaults, **preferences}
|
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
self.q = q or self.defaults["q"]
|
|
|
|
self.user = user or self.defaults["user"]
|
|
|
|
self.sort = sort or self.defaults["sort"]
|
|
|
|
self.shared = shared or self.defaults["shared"]
|
|
|
|
self.unread = unread or self.defaults["unread"]
|
2023-09-01 20:48:21 +00:00
|
|
|
|
|
|
|
def is_modified(self, param):
|
|
|
|
value = self.__dict__[param]
|
2023-10-01 19:22:44 +00:00
|
|
|
return value != self.defaults[param]
|
2023-09-01 20:48:21 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def modified_params(self):
|
|
|
|
return [field for field in self.params if self.is_modified(field)]
|
|
|
|
|
2023-10-01 19:22:44 +00:00
|
|
|
@property
|
|
|
|
def modified_preferences(self):
|
2024-01-27 10:29:16 +00:00
|
|
|
return [
|
|
|
|
preference
|
|
|
|
for preference in self.preferences
|
|
|
|
if self.is_modified(preference)
|
|
|
|
]
|
2023-10-01 19:22:44 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def has_modifications(self):
|
|
|
|
return len(self.modified_params) > 0
|
|
|
|
|
|
|
|
@property
|
|
|
|
def has_modified_preferences(self):
|
|
|
|
return len(self.modified_preferences) > 0
|
|
|
|
|
2023-09-01 20:48:21 +00:00
|
|
|
@property
|
|
|
|
def query_params(self):
|
|
|
|
return {param: self.__dict__[param] for param in self.modified_params}
|
|
|
|
|
2023-10-01 19:22:44 +00:00
|
|
|
@property
|
|
|
|
def preferences_dict(self):
|
2024-01-27 10:29:16 +00:00
|
|
|
return {
|
|
|
|
preference: self.__dict__[preference] for preference in self.preferences
|
|
|
|
}
|
2023-10-01 19:22:44 +00:00
|
|
|
|
2023-09-01 20:48:21 +00:00
|
|
|
@staticmethod
|
2023-10-01 19:22:44 +00:00
|
|
|
def from_request(query_dict: QueryDict, preferences: dict = None):
|
2023-09-01 20:48:21 +00:00
|
|
|
initial_values = {}
|
|
|
|
for param in BookmarkSearch.params:
|
2023-10-01 19:22:44 +00:00
|
|
|
value = query_dict.get(param)
|
2023-09-01 20:48:21 +00:00
|
|
|
if value:
|
|
|
|
initial_values[param] = value
|
|
|
|
|
2023-10-01 19:22:44 +00:00
|
|
|
return BookmarkSearch(**initial_values, preferences=preferences)
|
2023-09-01 20:48:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BookmarkSearchForm(forms.Form):
|
|
|
|
SORT_CHOICES = [
|
2024-01-27 10:29:16 +00:00
|
|
|
(BookmarkSearch.SORT_ADDED_ASC, "Added ↑"),
|
|
|
|
(BookmarkSearch.SORT_ADDED_DESC, "Added ↓"),
|
|
|
|
(BookmarkSearch.SORT_TITLE_ASC, "Title ↑"),
|
|
|
|
(BookmarkSearch.SORT_TITLE_DESC, "Title ↓"),
|
2023-09-01 20:48:21 +00:00
|
|
|
]
|
2023-09-10 20:14:07 +00:00
|
|
|
FILTER_SHARED_CHOICES = [
|
2024-01-27 10:29:16 +00:00
|
|
|
(BookmarkSearch.FILTER_SHARED_OFF, "Off"),
|
|
|
|
(BookmarkSearch.FILTER_SHARED_SHARED, "Shared"),
|
|
|
|
(BookmarkSearch.FILTER_SHARED_UNSHARED, "Unshared"),
|
2023-09-10 20:14:07 +00:00
|
|
|
]
|
2023-09-16 08:39:27 +00:00
|
|
|
FILTER_UNREAD_CHOICES = [
|
2024-01-27 10:29:16 +00:00
|
|
|
(BookmarkSearch.FILTER_UNREAD_OFF, "Off"),
|
|
|
|
(BookmarkSearch.FILTER_UNREAD_YES, "Unread"),
|
|
|
|
(BookmarkSearch.FILTER_UNREAD_NO, "Read"),
|
2023-09-16 08:39:27 +00:00
|
|
|
]
|
2023-09-01 20:48:21 +00:00
|
|
|
|
|
|
|
q = forms.CharField()
|
|
|
|
user = forms.ChoiceField()
|
|
|
|
sort = forms.ChoiceField(choices=SORT_CHOICES)
|
2023-09-10 20:14:07 +00:00
|
|
|
shared = forms.ChoiceField(choices=FILTER_SHARED_CHOICES, widget=forms.RadioSelect)
|
2023-09-16 08:39:27 +00:00
|
|
|
unread = forms.ChoiceField(choices=FILTER_UNREAD_CHOICES, widget=forms.RadioSelect)
|
2023-09-01 20:48:21 +00:00
|
|
|
|
2024-01-27 10:29:16 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
search: BookmarkSearch,
|
|
|
|
editable_fields: List[str] = None,
|
|
|
|
users: List[User] = None,
|
|
|
|
):
|
2023-09-01 20:48:21 +00:00
|
|
|
super().__init__()
|
|
|
|
editable_fields = editable_fields or []
|
2023-10-01 19:22:44 +00:00
|
|
|
self.editable_fields = editable_fields
|
2023-09-01 20:48:21 +00:00
|
|
|
|
|
|
|
# set choices for user field if users are provided
|
|
|
|
if users:
|
|
|
|
user_choices = [(user.username, user.username) for user in users]
|
2024-01-27 10:29:16 +00:00
|
|
|
user_choices.insert(0, ("", "Everyone"))
|
|
|
|
self.fields["user"].choices = user_choices
|
2023-09-01 20:48:21 +00:00
|
|
|
|
|
|
|
for param in search.params:
|
|
|
|
# set initial values for modified params
|
|
|
|
self.fields[param].initial = search.__dict__[param]
|
|
|
|
|
|
|
|
# Mark non-editable modified fields as hidden. That way, templates
|
|
|
|
# rendering a form can just loop over hidden_fields to ensure that
|
|
|
|
# all necessary search options are kept when submitting the form.
|
|
|
|
if search.is_modified(param) and param not in editable_fields:
|
|
|
|
self.fields[param].widget = forms.HiddenInput()
|
2021-03-28 10:11:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserProfile(models.Model):
|
2024-01-27 10:29:16 +00:00
|
|
|
THEME_AUTO = "auto"
|
|
|
|
THEME_LIGHT = "light"
|
|
|
|
THEME_DARK = "dark"
|
2021-03-28 10:11:56 +00:00
|
|
|
THEME_CHOICES = [
|
2024-01-27 10:29:16 +00:00
|
|
|
(THEME_AUTO, "Auto"),
|
|
|
|
(THEME_LIGHT, "Light"),
|
|
|
|
(THEME_DARK, "Dark"),
|
2021-03-28 10:11:56 +00:00
|
|
|
]
|
2024-01-27 10:29:16 +00:00
|
|
|
BOOKMARK_DATE_DISPLAY_RELATIVE = "relative"
|
|
|
|
BOOKMARK_DATE_DISPLAY_ABSOLUTE = "absolute"
|
|
|
|
BOOKMARK_DATE_DISPLAY_HIDDEN = "hidden"
|
2021-03-31 07:08:19 +00:00
|
|
|
BOOKMARK_DATE_DISPLAY_CHOICES = [
|
2024-01-27 10:29:16 +00:00
|
|
|
(BOOKMARK_DATE_DISPLAY_RELATIVE, "Relative"),
|
|
|
|
(BOOKMARK_DATE_DISPLAY_ABSOLUTE, "Absolute"),
|
|
|
|
(BOOKMARK_DATE_DISPLAY_HIDDEN, "Hidden"),
|
2021-03-31 07:08:19 +00:00
|
|
|
]
|
2024-03-24 20:31:15 +00:00
|
|
|
BOOKMARK_DESCRIPTION_DISPLAY_INLINE = "inline"
|
|
|
|
BOOKMARK_DESCRIPTION_DISPLAY_SEPARATE = "separate"
|
|
|
|
BOOKMARK_DESCRIPTION_DISPLAY_CHOICES = [
|
|
|
|
(BOOKMARK_DESCRIPTION_DISPLAY_INLINE, "Inline"),
|
|
|
|
(BOOKMARK_DESCRIPTION_DISPLAY_SEPARATE, "Separate"),
|
|
|
|
]
|
2024-01-27 10:29:16 +00:00
|
|
|
BOOKMARK_LINK_TARGET_BLANK = "_blank"
|
|
|
|
BOOKMARK_LINK_TARGET_SELF = "_self"
|
2021-10-03 07:35:59 +00:00
|
|
|
BOOKMARK_LINK_TARGET_CHOICES = [
|
2024-01-27 10:29:16 +00:00
|
|
|
(BOOKMARK_LINK_TARGET_BLANK, "New page"),
|
|
|
|
(BOOKMARK_LINK_TARGET_SELF, "Same page"),
|
2021-10-03 07:35:59 +00:00
|
|
|
]
|
2024-01-27 10:29:16 +00:00
|
|
|
WEB_ARCHIVE_INTEGRATION_DISABLED = "disabled"
|
|
|
|
WEB_ARCHIVE_INTEGRATION_ENABLED = "enabled"
|
2022-05-14 07:46:51 +00:00
|
|
|
WEB_ARCHIVE_INTEGRATION_CHOICES = [
|
2024-01-27 10:29:16 +00:00
|
|
|
(WEB_ARCHIVE_INTEGRATION_DISABLED, "Disabled"),
|
|
|
|
(WEB_ARCHIVE_INTEGRATION_ENABLED, "Enabled"),
|
2022-05-14 07:46:51 +00:00
|
|
|
]
|
2024-01-27 10:29:16 +00:00
|
|
|
TAG_SEARCH_STRICT = "strict"
|
|
|
|
TAG_SEARCH_LAX = "lax"
|
2023-05-18 07:06:22 +00:00
|
|
|
TAG_SEARCH_CHOICES = [
|
2024-01-27 10:29:16 +00:00
|
|
|
(TAG_SEARCH_STRICT, "Strict"),
|
|
|
|
(TAG_SEARCH_LAX, "Lax"),
|
2023-05-18 07:06:22 +00:00
|
|
|
]
|
2024-01-27 10:29:16 +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
|
|
|
|
)
|
|
|
|
bookmark_date_display = models.CharField(
|
|
|
|
max_length=10,
|
|
|
|
choices=BOOKMARK_DATE_DISPLAY_CHOICES,
|
|
|
|
blank=False,
|
|
|
|
default=BOOKMARK_DATE_DISPLAY_RELATIVE,
|
|
|
|
)
|
2024-03-24 20:31:15 +00:00
|
|
|
bookmark_description_display = models.CharField(
|
|
|
|
max_length=10,
|
|
|
|
choices=BOOKMARK_DESCRIPTION_DISPLAY_CHOICES,
|
|
|
|
blank=False,
|
|
|
|
default=BOOKMARK_DESCRIPTION_DISPLAY_INLINE,
|
|
|
|
)
|
|
|
|
bookmark_description_max_lines = models.IntegerField(
|
|
|
|
null=False,
|
|
|
|
default=1,
|
|
|
|
)
|
2024-01-27 10:29:16 +00:00
|
|
|
bookmark_link_target = models.CharField(
|
|
|
|
max_length=10,
|
|
|
|
choices=BOOKMARK_LINK_TARGET_CHOICES,
|
|
|
|
blank=False,
|
|
|
|
default=BOOKMARK_LINK_TARGET_BLANK,
|
|
|
|
)
|
|
|
|
web_archive_integration = models.CharField(
|
|
|
|
max_length=10,
|
|
|
|
choices=WEB_ARCHIVE_INTEGRATION_CHOICES,
|
|
|
|
blank=False,
|
|
|
|
default=WEB_ARCHIVE_INTEGRATION_DISABLED,
|
|
|
|
)
|
|
|
|
tag_search = models.CharField(
|
|
|
|
max_length=10,
|
|
|
|
choices=TAG_SEARCH_CHOICES,
|
|
|
|
blank=False,
|
|
|
|
default=TAG_SEARCH_STRICT,
|
|
|
|
)
|
2022-08-04 17:37:16 +00:00
|
|
|
enable_sharing = models.BooleanField(default=False, null=False)
|
2023-08-14 22:20:52 +00:00
|
|
|
enable_public_sharing = models.BooleanField(default=False, null=False)
|
2023-01-21 15:36:10 +00:00
|
|
|
enable_favicons = models.BooleanField(default=False, null=False)
|
2024-05-07 16:58:52 +00:00
|
|
|
enable_preview_images = models.BooleanField(default=False, null=False)
|
2023-05-18 08:18:15 +00:00
|
|
|
display_url = models.BooleanField(default=False, null=False)
|
2024-03-29 22:07:11 +00:00
|
|
|
display_view_bookmark_action = models.BooleanField(default=True, null=False)
|
|
|
|
display_edit_bookmark_action = models.BooleanField(default=True, null=False)
|
|
|
|
display_archive_bookmark_action = models.BooleanField(default=True, null=False)
|
|
|
|
display_remove_bookmark_action = models.BooleanField(default=True, null=False)
|
2023-05-20 09:54:26 +00:00
|
|
|
permanent_notes = models.BooleanField(default=False, null=False)
|
2024-03-17 00:11:59 +00:00
|
|
|
custom_css = models.TextField(blank=True, null=False)
|
2023-10-01 19:22:44 +00:00
|
|
|
search_preferences = models.JSONField(default=dict, null=False)
|
2024-04-01 13:19:38 +00:00
|
|
|
enable_automatic_html_snapshots = models.BooleanField(default=True, null=False)
|
2024-04-17 21:08:18 +00:00
|
|
|
default_mark_unread = models.BooleanField(default=False, null=False)
|
2021-03-28 10:11:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserProfileForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = UserProfile
|
2024-01-27 10:29:16 +00:00
|
|
|
fields = [
|
|
|
|
"theme",
|
|
|
|
"bookmark_date_display",
|
2024-03-24 20:31:15 +00:00
|
|
|
"bookmark_description_display",
|
|
|
|
"bookmark_description_max_lines",
|
2024-01-27 10:29:16 +00:00
|
|
|
"bookmark_link_target",
|
|
|
|
"web_archive_integration",
|
|
|
|
"tag_search",
|
|
|
|
"enable_sharing",
|
|
|
|
"enable_public_sharing",
|
|
|
|
"enable_favicons",
|
2024-05-07 16:58:52 +00:00
|
|
|
"enable_preview_images",
|
2024-04-01 13:19:38 +00:00
|
|
|
"enable_automatic_html_snapshots",
|
2024-01-27 10:29:16 +00:00
|
|
|
"display_url",
|
2024-03-29 22:07:11 +00:00
|
|
|
"display_view_bookmark_action",
|
|
|
|
"display_edit_bookmark_action",
|
|
|
|
"display_archive_bookmark_action",
|
|
|
|
"display_remove_bookmark_action",
|
2024-01-27 10:29:16 +00:00
|
|
|
"permanent_notes",
|
2024-04-17 21:08:18 +00:00
|
|
|
"default_mark_unread",
|
2024-03-17 00:11:59 +00:00
|
|
|
"custom_css",
|
2024-01-27 10:29:16 +00:00
|
|
|
]
|
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()
|
2022-05-14 07:46:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Toast(models.Model):
|
|
|
|
key = models.CharField(max_length=50)
|
|
|
|
message = models.TextField()
|
|
|
|
acknowledged = models.BooleanField(default=False)
|
|
|
|
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
|
2022-07-23 21:20:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FeedToken(models.Model):
|
|
|
|
"""
|
|
|
|
Adapted from authtoken.models.Token
|
|
|
|
"""
|
2024-01-27 10:29:16 +00:00
|
|
|
|
2022-07-23 21:20:27 +00:00
|
|
|
key = models.CharField(max_length=40, primary_key=True)
|
2024-01-27 10:29:16 +00:00
|
|
|
user = models.OneToOneField(
|
|
|
|
get_user_model(),
|
|
|
|
related_name="feed_token",
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
2022-07-23 21:20:27 +00:00
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if not self.key:
|
|
|
|
self.key = self.generate_key()
|
|
|
|
return super().save(*args, **kwargs)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def generate_key(cls):
|
|
|
|
return binascii.hexlify(os.urandom(20)).decode()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.key
|