<commit_before># -*- coding: utf-8 -*-
"""
    pyClanSphere.plugins.bulletin_board.models
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Models for the board.

    :copyright: (c) 2009 by the pyClanSphere Team, see AUTHORS for more details.
    :license: BSD, see LICENSE for more details.
"""

from datetime import datetime
from operator import attrgetter

from pyClanSphere.api import db, get_request
from pyClanSphere.models import User, AnonymousUser
from pyClanSphere.utils.pagination import Pagination

from pyClanSphere.plugins.bulletin_board.privileges import *
from pyClanSphere.plugins.bulletin_board.database import *


class TopicEmpty(Exception):
    """Class used to tell that the given topic has no posts"""
    pass

class CategoryQuery(db.Query):
    """Addon methods for querying categories"""

    def get_list(self, endpoint=None, page=1, per_page=None,
                 url_args=None, raise_if_empty=True, paginator=Pagination):
        """Return a dict with pagination and categories."""

        if per_page is None:
            per_page = 20

        # send the query
        offset = per_page * (page - 1)
        categorylist = self.order_by(db.asc(Category.ordering)) \
                           .offset(offset).limit(per_page).all()

        # if raising exceptions is wanted, raise i
        if raise_if_empty and (page != 1 and not categorylist):
            raise NotFound()

        pagination = paginator(endpoint, page, per_page,
                                self.count(), url_args=url_args)

        return {
            'categories':       categorylist,
            'pagination':       pagination,
        }


class Category(object):
    """Representation of a category"""

    query = db.query_property(CategoryQuery)

    def __init__(self, name=None, ordering=0):
        self.name = name
        self.ordering = ordering

    def can_edit(self, user=None):
        return True


class ForumQuery(db.Query):
    """Addon methods for querying forums"""

    def get_list(self, endpoint=None, page=1, per_page=None,
                 url_args=None, raise_if_empty=True, paginator=Pagination):
        """Return a dict with pagination and categories."""

        if per_page is None:
            per_page = 20

        # XXX: this should all be in a one-go in the database as it is much faster
        #      there but works for now

        # retrieve (pre-filtered) forumlist
        offset = per_page * (page - 1)
        forumlist = self.offset(offset).limit(per_page).all()

        # fetch an unique list of categories from selected forums
        categories_temp = [forum.category for forum in forumlist]
        categories = sorted(set(categories_temp), key=attrgetter('ordering'))

        # order by category ordering, then forum ordering
        retlist = []
        for category in categories:
            for forum in self.filter(Forum.category_id==category.id).order_by(Forum.ordering).all():
                retlist.append(forum)

        # if raising exceptions is wanted, raise it
        if raise_if_empty and (page != 1 and not forumlist):
            raise NotFound()<commit_msg>Use right list for check<commit_after># -*- coding: utf-8 -*-
"""
    pyClanSphere.plugins.bulletin_board.models
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Models for the board.

    :copyright: (c) 2009 by the pyClanSphere Team, see AUTHORS for more details.
    :license: BSD, see LICENSE for more details.
"""

from datetime import datetime
from operator import attrgetter

from pyClanSphere.api import db, get_request
from pyClanSphere.models import User, AnonymousUser
from pyClanSphere.utils.pagination import Pagination

from pyClanSphere.plugins.bulletin_board.privileges import *
from pyClanSphere.plugins.bulletin_board.database import *


class TopicEmpty(Exception):
    """Class used to tell that the given topic has no posts"""
    pass

class CategoryQuery(db.Query):
    """Addon methods for querying categories"""

    def get_list(self, endpoint=None, page=1, per_page=None,
                 url_args=None, raise_if_empty=True, paginator=Pagination):
        """Return a dict with pagination and categories."""

        if per_page is None:
            per_page = 20

        # send the query
        offset = per_page * (page - 1)
        categorylist = self.order_by(db.asc(Category.ordering)) \
                           .offset(offset).limit(per_page).all()

        # if raising exceptions is wanted, raise i
        if raise_if_empty and (page != 1 and not categorylist):
            raise NotFound()

        pagination = paginator(endpoint, page, per_page,
                                self.count(), url_args=url_args)

        return {
            'categories':       categorylist,
            'pagination':       pagination,
        }


class Category(object):
    """Representation of a category"""

    query = db.query_property(CategoryQuery)

    def __init__(self, name=None, ordering=0):
        self.name = name
        self.ordering = ordering

    def can_edit(self, user=None):
        return True


class ForumQuery(db.Query):
    """Addon methods for querying forums"""

    def get_list(self, endpoint=None, page=1, per_page=None,
                 url_args=None, raise_if_empty=True, paginator=Pagination):
        """Return a dict with pagination and categories."""

        if per_page is None:
            per_page = 20

        # XXX: this should all be in a one-go in the database as it is much faster
        #      there but works for now

        # retrieve (pre-filtered) forumlist
        offset = per_page * (page - 1)
        forumlist = self.offset(offset).limit(per_page).all()

        # fetch an unique list of categories from selected forums
        categories_temp = [forum.category for forum in forumlist]
        categories = sorted(set(categories_temp), key=attrgetter('ordering'))

        # order by category ordering, then forum ordering
        retlist = []
        for category in categories:
            for forum in self.filter(Forum.category_id==category.id).order_by(Forum.ordering).all():
                retlist.append(forum)

        # if raising exceptions is wanted, raise it
        if raise_if_empty and (page != 1 and not retlist):
            raise NotFound()
{'fraction_non_alphanumeric': 0.10166799046862589, 'fraction_numerical': 0.0050833995234312946, 'mean_word_length': 2.5833807626636314, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5098748', 'n_tokens_mistral': 1789, 'n_tokens_neox': 1694, 'n_words': 535}