|
|
from django.shortcuts import get_object_or_404
|
|
|
from boards.models import Tag
|
|
|
|
|
|
__author__ = 'neko259'
|
|
|
|
|
|
SESSION_SETTING = 'setting'
|
|
|
|
|
|
PERMISSION_MODERATE = 'moderator'
|
|
|
|
|
|
SETTING_THEME = 'theme'
|
|
|
SETTING_FAVORITE_TAGS = 'favorite_tags'
|
|
|
SETTING_HIDDEN_TAGS = 'hidden_tags'
|
|
|
SETTING_PERMISSIONS = 'permissions'
|
|
|
|
|
|
DEFAULT_THEME = 'md'
|
|
|
|
|
|
|
|
|
def get_settings_manager(request):
|
|
|
"""
|
|
|
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)
|
|
|
|
|
|
|
|
|
class SettingsManager:
|
|
|
"""
|
|
|
Base settings manager class. get_setting and set_setting methods should
|
|
|
be overriden.
|
|
|
"""
|
|
|
def __init__(self):
|
|
|
pass
|
|
|
|
|
|
def get_theme(self):
|
|
|
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
|
|
|
|
|
|
def get_setting(self, setting):
|
|
|
pass
|
|
|
|
|
|
def set_setting(self, setting, value):
|
|
|
pass
|
|
|
|
|
|
def add_permission(self, permission):
|
|
|
permissions = self.get_setting(SETTING_PERMISSIONS)
|
|
|
if not permissions:
|
|
|
permissions = [permission]
|
|
|
else:
|
|
|
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)
|
|
|
self.set_setting(SETTING_PERMISSIONS, permissions)
|
|
|
|
|
|
def get_fav_tags(self):
|
|
|
tag_names = self.get_setting(SETTING_FAVORITE_TAGS)
|
|
|
tags = []
|
|
|
if tag_names:
|
|
|
for tag_name in tag_names:
|
|
|
tag = get_object_or_404(Tag, name=tag_name)
|
|
|
tags.append(tag)
|
|
|
|
|
|
return tags
|
|
|
|
|
|
def add_fav_tag(self, tag):
|
|
|
tags = self.get_setting(SETTING_FAVORITE_TAGS)
|
|
|
if not tags:
|
|
|
tags = [tag.name]
|
|
|
else:
|
|
|
if not tag.name in tags:
|
|
|
tags.append(tag.name)
|
|
|
self.set_setting(SETTING_FAVORITE_TAGS, tags)
|
|
|
|
|
|
def del_fav_tag(self, tag):
|
|
|
tags = self.get_setting(SETTING_FAVORITE_TAGS)
|
|
|
if tag.name in tags:
|
|
|
tags.remove(tag.name)
|
|
|
self.set_setting(SETTING_FAVORITE_TAGS, tags)
|
|
|
|
|
|
def get_hidden_tags(self):
|
|
|
tag_names = self.get_setting(SETTING_HIDDEN_TAGS)
|
|
|
tags = []
|
|
|
if tag_names:
|
|
|
for tag_name in tag_names:
|
|
|
tag = get_object_or_404(Tag, name=tag_name)
|
|
|
tags.append(tag)
|
|
|
|
|
|
return tags
|
|
|
|
|
|
def add_hidden_tag(self, tag):
|
|
|
tags = self.get_setting(SETTING_HIDDEN_TAGS)
|
|
|
if not tags:
|
|
|
tags = [tag.name]
|
|
|
else:
|
|
|
if not tag.name in tags:
|
|
|
tags.append(tag.name)
|
|
|
self.set_setting(SETTING_HIDDEN_TAGS, tags)
|
|
|
|
|
|
def del_hidden_tag(self, tag):
|
|
|
tags = self.get_setting(SETTING_HIDDEN_TAGS)
|
|
|
if tag.name in tags:
|
|
|
tags.remove(tag.name)
|
|
|
self.set_setting(SETTING_HIDDEN_TAGS, tags)
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
def get_setting(self, setting):
|
|
|
if setting in self.session:
|
|
|
return self.session[setting]
|
|
|
else:
|
|
|
return None
|
|
|
|
|
|
def set_setting(self, setting, value):
|
|
|
self.session[setting] = value
|
|
|
|
|
|
|