##// END OF EJS Templates
Tripcode collision preventer
Tripcode collision preventer

File last commit:

r1297:dd1093e1 default
r1297:dd1093e1 default
Show More
settingsmanager.py
174 lines | 5.0 KiB | text/x-python | PythonLexer
neko259
Added ability to reset tripcode
r1296 import uuid
neko259
Tripcode collision preventer
r1297
import boards
from boards.abstracts.tripcode import Tripcode
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 from boards.models import Tag
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'
neko259
Cleaned up some code
r905 # Remove this, it is not used any more cause there is a user's permission
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 PERMISSION_MODERATE = 'moderator'
SETTING_THEME = 'theme'
SETTING_FAVORITE_TAGS = 'favorite_tags'
SETTING_HIDDEN_TAGS = 'hidden_tags'
SETTING_PERMISSIONS = 'permissions'
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 ability to reset tripcode
r1296 SETTING_TRIPCODE = 'tripcode'
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)
def has_permission(self, permission):
permissions = self.get_setting(SETTING_PERMISSIONS)
if permissions:
return permission in permissions
else:
return False
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
def add_permission(self, permission):
permissions = self.get_setting(SETTING_PERMISSIONS)
if not permissions:
permissions = [permission]
else:
neko259
Added login and logout for moderators
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)
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_PERMISSIONS, permissions)
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
Fixed loading tag threads when hidden tags exist
r1195 tags = list(Tag.objects.filter(name__in=tag_names))
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:
tags = [tag.name]
else:
if not tag.name in tags:
tags.append(tag.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)
if tag.name in tags:
tags.remove(tag.name)
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
Fixed loading tag threads when hidden tags exist
r1195 tags = list(Tag.objects.filter(name__in=tag_names))
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:
tags = [tag.name]
else:
if not tag.name in tags:
tags.append(tag.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)
if tag.name in tags:
tags.remove(tag.name)
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
Added ability to reset tripcode
r1296 def get_tripcode(self):
neko259
Tripcode collision preventer
r1297 tripcode = self.get_setting(SETTING_TRIPCODE)
if tripcode is None:
self.reset_tripcode()
tripcode = self.get_setting(SETTING_TRIPCODE)
return tripcode
neko259
Added ability to reset tripcode
r1296
def reset_tripcode(self):
neko259
Tripcode collision preventer
r1297 tripcode = Tripcode(str(uuid.uuid4()))
# If we cannot find a collision-free tripcode, then let the collision
# be destiny
collision_counter = 0
while boards.models.Post.objects.filter(
tripcode__startswith=tripcode.get_short_text()).exists()\
and collision_counter < MAX_TRIPCODE_COLLISIONS:
tripcode = Tripcode(str(uuid.uuid4()))
collision_counter += 1
print('Tripcode collision detected') # FIXME Use proper logging
self.set_setting(SETTING_TRIPCODE, tripcode.get_full_text())
neko259
Added ability to reset tripcode
r1296
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
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)