settingsmanager.py
205 lines
| 6.5 KiB
| text/x-python
|
PythonLexer
|
r1626 | from boards import settings | ||
|
r1891 | from boards.models import Tag, TagAlias | ||
|
r1346 | from boards.models.thread import FAV_THREAD_NO_UPDATES | ||
|
r1875 | from boards.models.tag import DEFAULT_LOCALE | ||
|
r728 | |||
|
r1297 | MAX_TRIPCODE_COLLISIONS = 50 | ||
|
r728 | __author__ = 'neko259' | ||
SESSION_SETTING = 'setting' | ||||
|
r905 | # Remove this, it is not used any more cause there is a user's permission | ||
|
r728 | PERMISSION_MODERATE = 'moderator' | ||
SETTING_THEME = 'theme' | ||||
SETTING_FAVORITE_TAGS = 'favorite_tags' | ||||
|
r1323 | SETTING_FAVORITE_THREADS = 'favorite_threads' | ||
|
r728 | SETTING_HIDDEN_TAGS = 'hidden_tags' | ||
SETTING_PERMISSIONS = 'permissions' | ||||
|
r990 | SETTING_USERNAME = 'username' | ||
SETTING_LAST_NOTIFICATION_ID = 'last_notification' | ||||
|
r1122 | SETTING_IMAGE_VIEWER = 'image_viewer' | ||
|
r1296 | SETTING_TRIPCODE = 'tripcode' | ||
|
r1500 | SETTING_IMAGES = 'images_aliases' | ||
|
r1691 | SETTING_ONLY_FAVORITES = 'only_favorites' | ||
|
r728 | |||
DEFAULT_THEME = 'md' | ||||
|
r730 | class SettingsManager: | ||
""" | ||||
Base settings manager class. get_setting and set_setting methods should | ||||
be overriden. | ||||
""" | ||||
def __init__(self): | ||||
pass | ||||
|
r728 | |||
|
r1064 | def get_theme(self) -> str: | ||
|
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) | ||||
def has_permission(self, permission): | ||||
permissions = self.get_setting(SETTING_PERMISSIONS) | ||||
if permissions: | ||||
return permission in permissions | ||||
else: | ||||
return False | ||||
|
r1122 | def get_setting(self, setting, default=None): | ||
|
r730 | pass | ||
|
r728 | |||
def set_setting(self, setting, value): | ||||
|
r730 | pass | ||
|
r728 | |||
def add_permission(self, permission): | ||||
permissions = self.get_setting(SETTING_PERMISSIONS) | ||||
if not permissions: | ||||
permissions = [permission] | ||||
else: | ||||
|
r729 | permissions.append(permission) | ||
self.set_setting(SETTING_PERMISSIONS, permissions) | ||||
def del_permission(self, permission): | ||||
permissions = self.get_setting(SETTING_PERMISSIONS) | ||||
if not permissions: | ||||
permissions = [] | ||||
else: | ||||
permissions.remove(permission) | ||||
|
r728 | self.set_setting(SETTING_PERMISSIONS, permissions) | ||
|
r1064 | def get_fav_tags(self) -> list: | ||
|
r728 | tag_names = self.get_setting(SETTING_FAVORITE_TAGS) | ||
tags = [] | ||||
if tag_names: | ||||
|
r1891 | tags = list(Tag.objects.filter(aliases__in=TagAlias.objects | ||
.filter_localized(parent__aliases__name__in=tag_names)) | ||||
.order_by('aliases__name')) | ||||
|
r728 | return tags | ||
def add_fav_tag(self, tag): | ||||
tags = self.get_setting(SETTING_FAVORITE_TAGS) | ||||
if not tags: | ||||
|
r1874 | tags = [tag.get_name()] | ||
|
r728 | else: | ||
|
r1874 | if not tag.get_name() in tags: | ||
tags.append(tag.get_name()) | ||||
|
r789 | |||
tags.sort() | ||||
|
r728 | self.set_setting(SETTING_FAVORITE_TAGS, tags) | ||
def del_fav_tag(self, tag): | ||||
tags = self.get_setting(SETTING_FAVORITE_TAGS) | ||||
|
r1874 | if tag.get_name() in tags: | ||
tags.remove(tag.get_name()) | ||||
|
r728 | self.set_setting(SETTING_FAVORITE_TAGS, tags) | ||
|
r1064 | def get_hidden_tags(self) -> list: | ||
|
r728 | tag_names = self.get_setting(SETTING_HIDDEN_TAGS) | ||
tags = [] | ||||
if tag_names: | ||||
|
r1891 | tags = list(Tag.objects.filter(aliases__in=TagAlias.objects | ||
.filter_localized(parent__aliases__name__in=tag_names)) | ||||
.order_by('aliases__name')) | ||||
|
r728 | |||
return tags | ||||
def add_hidden_tag(self, tag): | ||||
tags = self.get_setting(SETTING_HIDDEN_TAGS) | ||||
if not tags: | ||||
|
r1874 | tags = [tag.get_name()] | ||
|
r728 | else: | ||
|
r1874 | if not tag.get_name() in tags: | ||
tags.append(tag.get_name()) | ||||
|
r789 | |||
tags.sort() | ||||
|
r728 | self.set_setting(SETTING_HIDDEN_TAGS, tags) | ||
def del_hidden_tag(self, tag): | ||||
tags = self.get_setting(SETTING_HIDDEN_TAGS) | ||||
|
r1874 | if tag.get_name() in tags: | ||
tags.remove(tag.get_name()) | ||||
|
r728 | self.set_setting(SETTING_HIDDEN_TAGS, tags) | ||
|
r730 | |||
|
r1323 | def get_fav_threads(self) -> dict: | ||
return self.get_setting(SETTING_FAVORITE_THREADS, default=dict()) | ||||
def add_or_read_fav_thread(self, opening_post): | ||||
threads = self.get_fav_threads() | ||||
|
r1626 | |||
max_fav_threads = settings.get_int('View', 'MaxFavoriteThreads') | ||||
if (str(opening_post.id) in threads) or (len(threads) < max_fav_threads): | ||||
thread = opening_post.get_thread() | ||||
# Don't check for new posts if the thread is archived already | ||||
if thread.is_archived(): | ||||
last_id = FAV_THREAD_NO_UPDATES | ||||
else: | ||||
last_id = thread.get_replies().last().id | ||||
threads[str(opening_post.id)] = last_id | ||||
self.set_setting(SETTING_FAVORITE_THREADS, threads) | ||||
|
r1323 | |||
def del_fav_thread(self, opening_post): | ||||
threads = self.get_fav_threads() | ||||
if self.thread_is_fav(opening_post): | ||||
del threads[str(opening_post.id)] | ||||
self.set_setting(SETTING_FAVORITE_THREADS, threads) | ||||
def thread_is_fav(self, opening_post): | ||||
return str(opening_post.id) in self.get_fav_threads() | ||||
|
r730 | |||
|
r1429 | def get_notification_usernames(self): | ||
|
r1452 | names = set() | ||
|
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) | ||||
|
r1452 | return names | ||
|
r1429 | |||
|
r1500 | def get_image_by_alias(self, alias): | ||
images = self.get_setting(SETTING_IMAGES) | ||||
if images is not None and len(images) > 0: | ||||
return images.get(alias) | ||||
def add_image_alias(self, alias, image): | ||||
images = self.get_setting(SETTING_IMAGES) | ||||
if images is None: | ||||
images = dict() | ||||
images.put(alias, image) | ||||
|
r1429 | |||
|
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 | ||||
|
r1122 | def get_setting(self, setting, default=None): | ||
|
r730 | if setting in self.session: | ||
return self.session[setting] | ||||
else: | ||||
|
r1296 | self.set_setting(setting, default) | ||
|
r1122 | return default | ||
|
r730 | |||
def set_setting(self, setting, value): | ||||
self.session[setting] = value | ||||
|
r990 | |||
def get_settings_manager(request) -> SettingsManager: | ||||
""" | ||||
Get settings manager based on the request object. Currently only | ||||
session-based manager is supported. In the future, cookie-based or | ||||
database-based managers could be implemented. | ||||
""" | ||||
return SessionSettingsManager(request.session) | ||||