diff --git a/boards/config/default_settings.ini b/boards/config/default_settings.ini new file mode 100644 --- /dev/null +++ b/boards/config/default_settings.ini @@ -0,0 +1,33 @@ +[Version] +Version = 2.7.0 Chani +SiteName = Neboard + +[Cache] +# Timeout for caching, if cache is used +CacheTimeout = 600 + +[Forms] +# Max post length in characters +MaxTextLength = 30000 +MaxImageSize = 8000000 +LimitPostingSpeed = false + +[Messages] +# Thread bumplimit +MaxPostsPerThread = 10 +# Old posts will be archived or deleted if this value is reached +MaxThreadCount = 5 + +[View] +DefaultTheme = md +DefaultImageViewer = simple +LastRepliesCount = 3 +ThreadsPerPage = 3 + +[Storage] +# Enable archiving threads instead of deletion when the thread limit is reached +ArchiveThreads = true + +[External] +# Thread update +WebsocketsEnabled = false diff --git a/boards/context_processors.py b/boards/context_processors.py --- a/boards/context_processors.py +++ b/boards/context_processors.py @@ -51,11 +51,12 @@ def user_and_ui_processor(request): # This shows the moderator panel context[CONTEXT_MODERATOR] = utils.is_moderator(request) - context[CONTEXT_VERSION] = settings.VERSION - context[CONTEXT_SITE_NAME] = settings.SITE_NAME + context[CONTEXT_VERSION] = settings.get('Version', 'Version') + context[CONTEXT_SITE_NAME] = settings.get('Version', 'SiteName') context[CONTEXT_IMAGE_VIEWER] = settings_manager.get_setting( - SETTING_IMAGE_VIEWER, default=settings.DEFAULT_IMAGE_VIEWER) + SETTING_IMAGE_VIEWER, + default=settings.get('View', 'DefaultImageViewer')) get_notifications(context, request) diff --git a/boards/default_settings.py b/boards/default_settings.py deleted file mode 100644 --- a/boards/default_settings.py +++ /dev/null @@ -1,23 +0,0 @@ -VERSION = '2.7.0 Chani' -SITE_NAME = 'Neboard' - -CACHE_TIMEOUT = 600 # Timeout for caching, if cache is used -LOGIN_TIMEOUT = 3600 # Timeout between login tries -MAX_TEXT_LENGTH = 30000 # Max post length in characters -MAX_IMAGE_SIZE = 8 * 1024 * 1024 # Max image size - -# Thread bumplimit -MAX_POSTS_PER_THREAD = 10 -# Old posts will be archived or deleted if this value is reached -MAX_THREAD_COUNT = 5 -THREADS_PER_PAGE = 3 -DEFAULT_THEME = 'md' -DEFAULT_IMAGE_VIEWER = 'simple' -LAST_REPLIES_COUNT = 3 - -# Enable archiving threads instead of deletion when the thread limit is reached -ARCHIVE_THREADS = True -# Limit posting speed -LIMIT_POSTING_SPEED = False -# Thread update -WEBSOCKETS_ENABLED = False diff --git a/boards/forms.py b/boards/forms.py --- a/boards/forms.py +++ b/boards/forms.py @@ -172,11 +172,10 @@ class PostForm(NeboardForm): def clean_text(self): text = self.cleaned_data['text'].strip() if text: - if len(text) > board_settings.MAX_TEXT_LENGTH: + max_length = board_settings.get_int('Forms', 'MaxTextLength') + if len(text) > max_length: raise forms.ValidationError(_('Text must have less than %s ' - 'characters') % - str(board_settings - .MAX_TEXT_LENGTH)) + 'characters') % str(max_length)) return text def clean_image(self): @@ -256,7 +255,7 @@ class PostForm(NeboardForm): posting_delay = settings.POSTING_DELAY - if board_settings.LIMIT_POSTING_SPEED: + if board_settings.get_bool('Forms', 'LimitPostingSpeed'): now = time.time() current_delay = 0 @@ -283,10 +282,11 @@ class PostForm(NeboardForm): self.session[LAST_POST_TIME] = now def validate_image_size(self, size: int): - if size > board_settings.MAX_IMAGE_SIZE: + max_size = board_settings.get_int('Forms', 'MaxImageSize') + if size > max_size: raise forms.ValidationError( _('Image must be less than %s bytes') - % str(board_settings.MAX_IMAGE_SIZE)) + % str(max_size)) def _get_image_from_url(self, url: str) -> SimpleUploadedFile: """ diff --git a/boards/models/post.py b/boards/models/post.py --- a/boards/models/post.py +++ b/boards/models/post.py @@ -330,7 +330,7 @@ class Post(models.Model, Viewable): Sends post HTML data to the thread web socket. """ - if not settings.WEBSOCKETS_ENABLED: + if not settings.get_bool('External', 'WebsocketsEnabled'): return thread_ids = list() diff --git a/boards/models/thread.py b/boards/models/thread.py --- a/boards/models/thread.py +++ b/boards/models/thread.py @@ -34,12 +34,13 @@ class ThreadManager(models.Manager): threads = Thread.objects.filter(archived=False).order_by('-bump_time') thread_count = threads.count() - if thread_count > settings.MAX_THREAD_COUNT: - num_threads_to_delete = thread_count - settings.MAX_THREAD_COUNT + max_thread_count = settings.get_int('Messages', 'MaxThreadCount') + if thread_count > max_thread_count: + num_threads_to_delete = thread_count - max_thread_count old_threads = threads[thread_count - num_threads_to_delete:] for thread in old_threads: - if settings.ARCHIVE_THREADS: + if settings.get_bool('Storage', 'ArchiveThreads'): self._archive_thread(thread) else: thread.delete() @@ -55,7 +56,7 @@ class ThreadManager(models.Manager): def get_thread_max_posts(): - return settings.MAX_POSTS_PER_THREAD + return settings.get_int('Messages', 'MaxPostsPerThread') class Thread(models.Model): @@ -122,11 +123,13 @@ class Thread(models.Model): Gets several last replies, not including opening post """ - if settings.LAST_REPLIES_COUNT > 0: + last_replies_count = settings.get_int('View', 'LastRepliesCount') + + if last_replies_count > 0: reply_count = self.get_reply_count() if reply_count > 0: - reply_count_to_show = min(settings.LAST_REPLIES_COUNT, + reply_count_to_show = min(last_replies_count, reply_count - 1) replies = self.get_replies() last_replies = replies[reply_count - reply_count_to_show:] @@ -138,7 +141,7 @@ class Thread(models.Model): Gets number of posts between opening post and last replies. """ reply_count = self.get_reply_count() - last_replies_count = min(settings.LAST_REPLIES_COUNT, + last_replies_count = min(settings.get_int('View', 'LastRepliesCount'), reply_count - 1) return reply_count - last_replies_count - 1 @@ -222,7 +225,7 @@ class Thread(models.Model): post.threads.update(last_edit_time=self.last_edit_time) def notify_clients(self): - if not settings.WEBSOCKETS_ENABLED: + if not settings.get_bool('External', 'WebsocketsEnabled'): return client = Client() diff --git a/boards/rss.py b/boards/rss.py --- a/boards/rss.py +++ b/boards/rss.py @@ -10,7 +10,7 @@ from boards import settings # TODO Make tests for all of these class AllThreadsFeed(Feed): - title = settings.SITE_NAME + ' - All threads' + title = settings.get('Version', 'SiteName') + ' - All threads' link = '/' description_template = 'boards/rss/post.html' diff --git a/boards/settings.py b/boards/settings.py --- a/boards/settings.py +++ b/boards/settings.py @@ -1,2 +1,18 @@ -from boards.default_settings import * +import configparser + + +config = configparser.ConfigParser() +config.read('boards/config/default_settings.ini') +config.read('boards/config/settings.ini') + +def get(section, name): + return config[section][name] + + +def get_int(section, name): + return int(get(section, name)) + + +def get_bool(section, name): + return get(section, name) == 'true' diff --git a/boards/views/all_threads.py b/boards/views/all_threads.py --- a/boards/views/all_threads.py +++ b/boards/views/all_threads.py @@ -51,7 +51,7 @@ class AllThreadsView(PostMixin, BaseBoar self.settings_manager = get_settings_manager(request) paginator = get_paginator(self.get_threads(), - settings.THREADS_PER_PAGE) + settings.get_int('View', 'ThreadsPerPage')) paginator.current_page = int(page) try: diff --git a/boards/views/settings.py b/boards/views/settings.py --- a/boards/views/settings.py +++ b/boards/views/settings.py @@ -32,7 +32,8 @@ class SettingsView(BaseBoardView): initial={ FORM_THEME: selected_theme, FORM_IMAGE_VIEWER: settings_manager.get_setting( - SETTING_IMAGE_VIEWER, default=settings.DEFAULT_IMAGE_VIEWER), + SETTING_IMAGE_VIEWER, + default=settings.get('View', 'DefaultImageViewer')), FORM_USERNAME: settings_manager.get_setting(SETTING_USERNAME), FORM_TIMEZONE: request.session.get( SESSION_TIMEZONE, timezone.get_current_timezone()), diff --git a/boards/views/thread/thread.py b/boards/views/thread/thread.py --- a/boards/views/thread/thread.py +++ b/boards/views/thread/thread.py @@ -51,7 +51,7 @@ class ThreadView(BaseBoardView, PostMixi params[CONTEXT_LASTUPDATE] = str(thread_to_show.last_edit_time) params[CONTEXT_THREAD] = thread_to_show - if settings.WEBSOCKETS_ENABLED: + if settings.get_bool('External', 'WebsocketsEnabled'): token_time = format(timezone.now(), u'U') params[CONTEXT_WS_TIME] = token_time