# Path: forum/models.py
# class Topic(models.Model):
#     node = models.ForeignKey(Node, on_delete=models.PROTECT, related_name='topics')
#     author = models.ForeignKey(User, on_delete=models.PROTECT, related_name='topics')
#     title = models.CharField(max_length=128, blank=False, null=False)
#     markdown = models.TextField(blank=True, null=True)
#     content = models.TextField(blank=True, null=True)
#     abstract = models.TextField(blank=True, null=True)
#     date_created = models.DateTimeField(auto_now_add=True)
#     last_modified = models.DateTimeField(auto_now=True, blank=True, null=True)
#     liker = models.ManyToManyField(User, blank=True, related_name='like_topics')
#     viewed = models.IntegerField(default=0)
#     reply_count = models.IntegerField(default=0)
#     last_replied = models.DateTimeField(blank=True, null=True)
#     rank = models.IntegerField(default=10)
#     reply_reward = models.BooleanField(default=False)
#     like_reward = models.BooleanField(default=False)
#     admin_star = models.BooleanField(default=False)
#     deleted = models.BooleanField(default=False)
#     editable = models.BooleanField(default=True)
#     
#     class Meta():
#         ordering = ['-rank', '-date_created']


import re
from PIL import Image, ImageOps
from io import BytesIO
from django.contrib.auth.models import User
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.http import HttpResponseForbidden
from django.shortcuts import get_object_or_404
from django.core.files.uploadedfile import SimpleUploadedFile
from forum.models import Topic

def get_pagination(current_page, num_pages, count):
    page_list = []
    
    show_pages = 2*count+1
    if show_pages >= num_pages:
        page_list.extend(range(1, num_pages+1))
    elif current_page - count < 1:
        page_list.extend(range(1, show_pages+1))
    elif current_page + count > num_pages:
        page_list.extend(range(num_pages+1-show_pages, num_pages+1))
    else:
        page_list.extend(range(current_page-count, current_page+count+1))
    
    return page_list

def topic_pagination(page, topics):
    paginator = Paginator(topics, NUM_PER_PAGE)
    try:
        topic_list = paginator.page(page)
    except PageNotAnInteger:
        topic_list = paginator.page(1)
    except EmptyPage:
        topic_list = paginator.page(paginator.num_pages)
    
    page_list = get_pagination(topic_list.number, paginator.num_pages, 2)
    
    return topic_list, page_list

def author_required(view_func):

# Path: forum/models.py
# class Topic(models.Model):
#     node = models.ForeignKey(Node, on_delete=models.PROTECT, related_name='topics')
#     author = models.ForeignKey(User, on_delete=models.PROTECT, related_name='topics')
#     title = models.CharField(max_length=128, blank=False, null=False)
#     markdown = models.TextField(blank=True, null=True)
#     content = models.TextField(blank=True, null=True)
#     abstract = models.TextField(blank=True, null=True)
#     date_created = models.DateTimeField(auto_now_add=True)
#     last_modified = models.DateTimeField(auto_now=True, blank=True, null=True)
#     liker = models.ManyToManyField(User, blank=True, related_name='like_topics')
#     viewed = models.IntegerField(default=0)
#     reply_count = models.IntegerField(default=0)
#     last_replied = models.DateTimeField(blank=True, null=True)
#     rank = models.IntegerField(default=10)
#     reply_reward = models.BooleanField(default=False)
#     like_reward = models.BooleanField(default=False)
#     admin_star = models.BooleanField(default=False)
#     deleted = models.BooleanField(default=False)
#     editable = models.BooleanField(default=True)
#     
#     class Meta():
#         ordering = ['-rank', '-date_created']


# Path: forum/utils.py
import re
from PIL import Image, ImageOps
from io import BytesIO
from django.contrib.auth.models import User
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.http import HttpResponseForbidden
from django.shortcuts import get_object_or_404
from django.core.files.uploadedfile import SimpleUploadedFile
from forum.models import Topic


def get_pagination(current_page, num_pages, count):
    page_list = []
    
    show_pages = 2*count+1
    if show_pages >= num_pages:
        page_list.extend(range(1, num_pages+1))
    elif current_page - count < 1:
        page_list.extend(range(1, show_pages+1))
    elif current_page + count > num_pages:
        page_list.extend(range(num_pages+1-show_pages, num_pages+1))
    else:
        page_list.extend(range(current_page-count, current_page+count+1))
    
    return page_list

def topic_pagination(page, topics):
    paginator = Paginator(topics, NUM_PER_PAGE)
    try:
        topic_list = paginator.page(page)
    except PageNotAnInteger:
        topic_list = paginator.page(1)
    except EmptyPage:
        topic_list = paginator.page(paginator.num_pages)
    
    page_list = get_pagination(topic_list.number, paginator.num_pages, 2)
    
    return topic_list, page_list

def author_required(view_func):

    def _wrapped_view_func(request, *args, **kwargs):