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 @@ -1,3 +1,5 @@ +import re + from django.core.paginator import EmptyPage from django.http import Http404 from django.shortcuts import render, redirect @@ -38,6 +40,7 @@ PARAMETER_RSS_URL = 'rss_url' TEMPLATE = 'boards/all_threads.html' DEFAULT_PAGE = 1 +PATTERN_THREAD_NUMBER = re.compile(r'^(\D*)(\d+)(\D*)$') class AllThreadsView(BaseBoardView, PaginatedMixin, DispatcherMixin): @@ -59,18 +62,8 @@ class AllThreadsView(BaseBoardView, Pagi if not form: t_from_id = request.GET.get('t_from_id') if t_from_id: - source_op = Post.objects.get(id=int(t_from_id)) - tags_str = ' '.join([tag.get_name() for tag in source_op.get_thread().get_tags()]) - post_link = '[post]{}[/post]'.format(source_op.id) #FIXME To constants - new_title = source_op.get_title() + ' NEW' # TODO More intelligent name change - - form = ThreadForm(error_class=PlainErrorList, - initial={ - FORM_TAGS: tags_str, - 'subscribe': subscribe_by_default, - FORM_TEXT: post_link, - FORM_TITLE: new_title, - }) + form = self.get_rollover_form(request, t_from_id, + subscribe_by_default) else: form = ThreadForm(error_class=PlainErrorList, initial={FORM_TAGS: self.tag_name, @@ -149,3 +142,29 @@ class AllThreadsView(BaseBoardView, Pagi settings_manager = get_settings_manager(request) settings_manager.set_setting(SETTING_ONLY_FAVORITES, not settings_manager.get_setting(SETTING_ONLY_FAVORITES, False)) + + def get_rollover_form(self, request, t_from_id, subscribe_by_default): + """ + Create a new form template, passing on threads, link to the old thread, + and incremeting the thread number in the title if there is any + (or adding 2 if there isn't, normally meaning this was the first + thread in a series). + """ + source_op = Post.objects.get(id=int(t_from_id)) + tags_str = ' '.join([tag.get_name() for tag in source_op.get_thread().get_tags()]) + post_link = '[post]{}[/post]'.format(source_op.id) #FIXME To constants + + old_title = source_op.get_title() + m = PATTERN_THREAD_NUMBER.match(old_title) + if m: + new_title = m.group(1) + str(int(m.group(2)) + 1) + m.group(3) + else: + new_title = old_title + ' 2' + + return ThreadForm(error_class=PlainErrorList, + initial={ + FORM_TAGS: tags_str, + 'subscribe': subscribe_by_default, + FORM_TEXT: post_link, + FORM_TITLE: new_title, + })