##// END OF EJS Templates
Version bump
Version bump

File last commit:

r2065:770356bf default
r2085:7c9be4c6 4.9.2 default
Show More
settingsmanager.py
256 lines | 8.2 KiB | text/x-python | PythonLexer
neko259
New backend for fav threads. Now only last post ids are saved, no thread ids
r2044 import boards
neko259
Added local stickers feature
r1940 from boards.models import Tag, TagAlias, Attachment
from boards.models.attachment import AttachmentSticker
neko259
Store user settings apart from session in the database
r2041 from boards.models.user import UserSettings
neko259
Removed user and settings mode. Added settings manager to manage settings and keep them in the session (or any other backend like cookie in the future
r728
neko259
Tripcode collision preventer
r1297 MAX_TRIPCODE_COLLISIONS = 50
neko259
Removed user and settings mode. Added settings manager to manage settings and keep them in the session (or any other backend like cookie in the future
r728 __author__ = 'neko259'
SESSION_SETTING = 'setting'
SETTING_THEME = 'theme'
SETTING_FAVORITE_TAGS = 'favorite_tags'
neko259
Favorite threads with new posts counter
r1323 SETTING_FAVORITE_THREADS = 'favorite_threads'
neko259
Removed user and settings mode. Added settings manager to manage settings and keep them in the session (or any other backend like cookie in the future
r728 SETTING_HIDDEN_TAGS = 'hidden_tags'
neko259
User notifications (BB-59)
r990 SETTING_USERNAME = 'username'
SETTING_LAST_NOTIFICATION_ID = 'last_notification'
neko259
Setting for image view mode: in post (simple) or in popup
r1122 SETTING_IMAGE_VIEWER = 'image_viewer'
neko259
Added image aliases to upload the same images (like "fake" or "gtfo")
r1500 SETTING_IMAGES = 'images_aliases'
neko259
Switch to show only favorite tags (BB-94)
r1691 SETTING_ONLY_FAVORITES = 'only_favorites'
neko259
New backend for fav threads. Now only last post ids are saved, no thread ids
r2044 SETTING_LAST_POSTS = 'last_posts'
neko259
Move posting validation to class-based validators for PoW and Time-based
r2065 SETTING_CONFIRMED_USER = 'confirmed_user'
neko259
Removed user and settings mode. Added settings manager to manage settings and keep them in the session (or any other backend like cookie in the future
r728
DEFAULT_THEME = 'md'
neko259
Divided settings manager into base settings manager class and session-based settings manager. This allowes to add other backends to the settings manager
r730 class SettingsManager:
"""
Base settings manager class. get_setting and set_setting methods should
be overriden.
"""
def __init__(self):
pass
neko259
Removed user and settings mode. Added settings manager to manage settings and keep them in the session (or any other backend like cookie in the future
r728
neko259
If we are trying to open a hidden tag page, show its threads
r1064 def get_theme(self) -> str:
neko259
Removed user and settings mode. Added settings manager to manage settings and keep them in the session (or any other backend like cookie in the future
r728 theme = self.get_setting(SETTING_THEME)
if not theme:
theme = DEFAULT_THEME
self.set_setting(SETTING_THEME, theme)
return theme
def set_theme(self, theme):
self.set_setting(SETTING_THEME, theme)
neko259
Setting for image view mode: in post (simple) or in popup
r1122 def get_setting(self, setting, default=None):
neko259
Divided settings manager into base settings manager class and session-based settings manager. This allowes to add other backends to the settings manager
r730 pass
neko259
Removed user and settings mode. Added settings manager to manage settings and keep them in the session (or any other backend like cookie in the future
r728
def set_setting(self, setting, value):
neko259
Divided settings manager into base settings manager class and session-based settings manager. This allowes to add other backends to the settings manager
r730 pass
neko259
Removed user and settings mode. Added settings manager to manage settings and keep them in the session (or any other backend like cookie in the future
r728
neko259
If we are trying to open a hidden tag page, show its threads
r1064 def get_fav_tags(self) -> list:
neko259
Removed user and settings mode. Added settings manager to manage settings and keep them in the session (or any other backend like cookie in the future
r728 tag_names = self.get_setting(SETTING_FAVORITE_TAGS)
tags = []
if tag_names:
neko259
Show only localized tags
r1891 tags = list(Tag.objects.filter(aliases__in=TagAlias.objects
.filter_localized(parent__aliases__name__in=tag_names))
.order_by('aliases__name'))
neko259
Removed user and settings mode. Added settings manager to manage settings and keep them in the session (or any other backend like cookie in the future
r728 return tags
def add_fav_tag(self, tag):
tags = self.get_setting(SETTING_FAVORITE_TAGS)
if not tags:
neko259
Tag name is now stored in the alias with default locale
r1874 tags = [tag.get_name()]
neko259
Removed user and settings mode. Added settings manager to manage settings and keep them in the session (or any other backend like cookie in the future
r728 else:
neko259
Tag name is now stored in the alias with default locale
r1874 if not tag.get_name() in tags:
tags.append(tag.get_name())
neko259
Sort favorite and hidden tags when updating the list
r789
tags.sort()
neko259
Removed user and settings mode. Added settings manager to manage settings and keep them in the session (or any other backend like cookie in the future
r728 self.set_setting(SETTING_FAVORITE_TAGS, tags)
def del_fav_tag(self, tag):
tags = self.get_setting(SETTING_FAVORITE_TAGS)
neko259
Tag name is now stored in the alias with default locale
r1874 if tag.get_name() in tags:
tags.remove(tag.get_name())
neko259
Removed user and settings mode. Added settings manager to manage settings and keep them in the session (or any other backend like cookie in the future
r728 self.set_setting(SETTING_FAVORITE_TAGS, tags)
neko259
If we are trying to open a hidden tag page, show its threads
r1064 def get_hidden_tags(self) -> list:
neko259
Removed user and settings mode. Added settings manager to manage settings and keep them in the session (or any other backend like cookie in the future
r728 tag_names = self.get_setting(SETTING_HIDDEN_TAGS)
tags = []
if tag_names:
neko259
Show only localized tags
r1891 tags = list(Tag.objects.filter(aliases__in=TagAlias.objects
.filter_localized(parent__aliases__name__in=tag_names))
.order_by('aliases__name'))
neko259
Removed user and settings mode. Added settings manager to manage settings and keep them in the session (or any other backend like cookie in the future
r728
return tags
def add_hidden_tag(self, tag):
tags = self.get_setting(SETTING_HIDDEN_TAGS)
if not tags:
neko259
Tag name is now stored in the alias with default locale
r1874 tags = [tag.get_name()]
neko259
Removed user and settings mode. Added settings manager to manage settings and keep them in the session (or any other backend like cookie in the future
r728 else:
neko259
Tag name is now stored in the alias with default locale
r1874 if not tag.get_name() in tags:
tags.append(tag.get_name())
neko259
Sort favorite and hidden tags when updating the list
r789
tags.sort()
neko259
Removed user and settings mode. Added settings manager to manage settings and keep them in the session (or any other backend like cookie in the future
r728 self.set_setting(SETTING_HIDDEN_TAGS, tags)
def del_hidden_tag(self, tag):
tags = self.get_setting(SETTING_HIDDEN_TAGS)
neko259
Tag name is now stored in the alias with default locale
r1874 if tag.get_name() in tags:
tags.remove(tag.get_name())
neko259
Removed user and settings mode. Added settings manager to manage settings and keep them in the session (or any other backend like cookie in the future
r728 self.set_setting(SETTING_HIDDEN_TAGS, tags)
neko259
Divided settings manager into base settings manager class and session-based settings manager. This allowes to add other backends to the settings manager
r730
neko259
Favorite threads with new posts counter
r1323 def add_or_read_fav_thread(self, opening_post):
neko259
New backend for fav threads. Now only last post ids are saved, no thread ids
r2044 last_post_ids = self.get_setting(SETTING_LAST_POSTS)
if not last_post_ids:
last_post_ids = []
neko259
Limit favorite threads count
r1626
neko259
New backend for fav threads. Now only last post ids are saved, no thread ids
r2044 self.del_fav_thread(opening_post)
last_post_id = opening_post.get_thread().get_replies().last().id
last_post_ids.append(last_post_id)
self.set_setting(SETTING_LAST_POSTS, last_post_ids)
neko259
Favorite threads with new posts counter
r1323
def del_fav_thread(self, opening_post):
neko259
New backend for fav threads. Now only last post ids are saved, no thread ids
r2044 last_posts_ids = self.get_setting(SETTING_LAST_POSTS)
for post in self.get_last_posts():
if post.get_thread() == opening_post.get_thread():
last_posts_ids.remove(post.id)
self.set_setting(SETTING_LAST_POSTS, last_posts_ids)
neko259
Favorite threads with new posts counter
r1323
def thread_is_fav(self, opening_post):
neko259
New backend for fav threads. Now only last post ids are saved, no thread ids
r2044 for post in self.get_last_posts():
if post.get_thread() == opening_post.get_thread():
return True
return False
neko259
Divided settings manager into base settings manager class and session-based settings manager. This allowes to add other backends to the settings manager
r730
neko259
Subscribe to a multiple of users for notifications
r1429 def get_notification_usernames(self):
neko259
Allow spaces in the notification usernames setting
r1452 names = set()
neko259
Fixed error 500 if the user does not have notification username
r1453 name_list = self.get_setting(SETTING_USERNAME)
if name_list is not None:
name_list = name_list.strip()
if len(name_list) > 0:
names = name_list.lower().split(',')
names = set(name.strip() for name in names)
neko259
Allow spaces in the notification usernames setting
r1452 return names
neko259
Subscribe to a multiple of users for notifications
r1429
neko259
Added local stickers feature
r1940 def get_attachment_by_alias(self, alias):
neko259
Added image aliases to upload the same images (like "fake" or "gtfo")
r1500 images = self.get_setting(SETTING_IMAGES)
neko259
Added local stickers feature
r1940 if images and alias in images:
neko259
Handle local stickers which attachments were deleted
r1941 try:
return Attachment.objects.get(id=images.get(alias))
except Attachment.DoesNotExist:
self.remove_attachment_alias(alias)
neko259
Added image aliases to upload the same images (like "fake" or "gtfo")
r1500
neko259
Added local stickers feature
r1940 def add_attachment_alias(self, alias, attachment):
neko259
Added image aliases to upload the same images (like "fake" or "gtfo")
r1500 images = self.get_setting(SETTING_IMAGES)
if images is None:
images = dict()
neko259
Added local stickers feature
r1940 images[alias] = attachment.id
self.set_setting(SETTING_IMAGES, images)
def remove_attachment_alias(self, alias):
images = self.get_setting(SETTING_IMAGES)
del images[alias]
self.set_setting(SETTING_IMAGES, images)
def get_stickers(self):
images = self.get_setting(SETTING_IMAGES)
neko259
Handle local stickers which attachments were deleted
r1941 stickers = []
neko259
Added local stickers feature
r1940 if images:
neko259
Handle local stickers which attachments were deleted
r1941 for key, value in images.items():
try:
attachment = Attachment.objects.get(id=value)
stickers.append(AttachmentSticker(name=key, attachment=attachment))
except Attachment.DoesNotExist:
self.remove_attachment_alias(key)
return stickers
neko259
Added image aliases to upload the same images (like "fake" or "gtfo")
r1500
neko259
Store user settings apart from session in the database
r2041 def tag_is_fav(self, tag):
fav_tag_names = self.get_setting(SETTING_FAVORITE_TAGS)
return fav_tag_names is not None and tag.get_name() in fav_tag_names
def tag_is_hidden(self, tag):
hidden_tag_names = self.get_setting(SETTING_HIDDEN_TAGS)
return hidden_tag_names is not None and tag.get_name() in hidden_tag_names
neko259
New backend for fav threads. Now only last post ids are saved, no thread ids
r2044 def get_last_posts(self):
post_ids = self.get_setting(SETTING_LAST_POSTS) or []
neko259
Order fav threads in list
r2050 return list(boards.models.Post.objects.filter(id__in=post_ids).order_by('thread__id'))
neko259
New backend for fav threads. Now only last post ids are saved, no thread ids
r2044
neko259
Subscribe to a multiple of users for notifications
r1429
neko259
Divided settings manager into base settings manager class and session-based settings manager. This allowes to add other backends to the settings manager
r730 class SessionSettingsManager(SettingsManager):
"""
Session-based settings manager. All settings are saved to the user's
session.
"""
def __init__(self, session):
SettingsManager.__init__(self)
self.session = session
neko259
Setting for image view mode: in post (simple) or in popup
r1122 def get_setting(self, setting, default=None):
neko259
Divided settings manager into base settings manager class and session-based settings manager. This allowes to add other backends to the settings manager
r730 if setting in self.session:
return self.session[setting]
else:
neko259
Added ability to reset tripcode
r1296 self.set_setting(setting, default)
neko259
Setting for image view mode: in post (simple) or in popup
r1122 return default
neko259
Divided settings manager into base settings manager class and session-based settings manager. This allowes to add other backends to the settings manager
r730
def set_setting(self, setting, value):
self.session[setting] = value
neko259
User notifications (BB-59)
r990
neko259
Store user settings apart from session in the database
r2041 class DatabaseSettingsManager(SessionSettingsManager):
def __init__(self, session):
super().__init__(session)
neko259
Some comments to the settings manager
r2046
# First time a user accesses the server, his session is not saved
# and does not have the key yet. In order to create the settings object
# we need to save it manually
neko259
New backend for fav threads. Now only last post ids are saved, no thread ids
r2044 if not session.session_key:
session.save()
neko259
Some comments to the settings manager
r2046
neko259
Store user settings apart from session in the database
r2041 self.settings, created = UserSettings.objects.get_or_create(session_key=session.session_key)
def add_fav_tag(self, tag):
self.settings.fav_tags.add(tag)
def del_fav_tag(self, tag):
self.settings.fav_tags.remove(tag)
def get_fav_tags(self) -> list:
return self.settings.fav_tags.filter(
aliases__in=TagAlias.objects.filter_localized())\
.order_by('aliases__name')
def get_hidden_tags(self) -> list:
return self.settings.hidden_tags.all()
def add_hidden_tag(self, tag):
self.settings.hidden_tags.add(tag)
def del_hidden_tag(self, tag):
self.settings.hidden_tags.remove(tag)
def tag_is_fav(self, tag):
return self.settings.fav_tags.filter(id=tag.id).exists()
def tag_is_hidden(self, tag):
return self.settings.hidden_tags.filter(id=tag.id).exists()
neko259
Do not load fav tags when need to only check notifications
r2056 def get_user_settings(self):
return self.settings
neko259
Store user settings apart from session in the database
r2041
neko259
User notifications (BB-59)
r990 def get_settings_manager(request) -> SettingsManager:
"""
neko259
Some comments to the settings manager
r2046 Get settings manager based on the request object. Currently database-based
settings manager is implemented over the session-based one (settings that
are not connected to the database in any way are stored in session). Pure
session-based manager is also supported but not used by default.
neko259
User notifications (BB-59)
r990 """
neko259
Store user settings apart from session in the database
r2041 return DatabaseSettingsManager(request.session)