<commit_before>"""Helper functions

Consists of functions to typically be used within templates, but also
available to Controllers. This module is available to templates as 'h'.
"""
import cgi
from datetime import datetime
import hashlib
import json
import urllib

from paste.deploy.converters import asbool, asint
from pylons import tmpl_context as c, request
from pylons.i18n import _
from webhelpers.html import literal
from webhelpers.html.tags import file
from webhelpers.pylonslib import Flash as _Flash
from webhelpers.text import truncate

from adhocracy import config
from adhocracy.lib import cache
from adhocracy.lib import democracy
from adhocracy.lib import sorting
from adhocracy.lib import version
from adhocracy.lib.auth.authentication import allowed_login_types
from adhocracy.lib.auth.authorization import has as has_permission
from adhocracy.lib.auth.csrf import url_token, field_token
from adhocracy.lib.helpers import site_helper as site
from adhocracy.lib.helpers import user_helper as user
from adhocracy.lib.helpers import proposal_helper as proposal
from adhocracy.lib.helpers import text_helper as text
from adhocracy.lib.helpers import page_helper as page
from adhocracy.lib.helpers import delegateable_helper as delegateable
from adhocracy.lib.helpers import tag_helper as tag
from adhocracy.lib.helpers import poll_helper as poll
from adhocracy.lib.helpers import comment_helper as comment
from adhocracy.lib.helpers import selection_helper as selection
from adhocracy.lib.helpers import delegation_helper as delegation
from adhocracy.lib.helpers import instance_helper as instance
from adhocracy.lib.helpers import abuse_helper as abuse, tutorial
from adhocracy.lib.helpers import milestone_helper as milestone
from adhocracy.lib.helpers import recaptcha_helper as recaptcha
from adhocracy.lib.helpers import staticpage_helper as staticpage
from adhocracy.lib.helpers import badge_helper as badge
from adhocracy.lib.helpers import treatment_helper as treatment

from adhocracy.lib.helpers.fanstatic_helper import (FanstaticNeedHelper,
                                                    get_socialshareprivacy_url)
from adhocracy.lib.helpers import feedback_helper as feedback
from adhocracy.lib.helpers.url import build
from adhocracy.lib.helpers.site_helper import base_url
from adhocracy.lib.watchlist import make_watch, find_watch
from adhocracy import model, static
from adhocracy.i18n import countdown_time, format_date
from adhocracy.i18n import date_tag
from adhocracy.i18n import datetime_tag


flash = _Flash()
recaptcha = recaptcha.Recaptcha()
need = FanstaticNeedHelper(static)


def allow_user_registration():
    return asbool(config.get('adhocracy.allow_registration', 'True'))


def sorted_flash_messages():
    '''
    Return the flash messages sorted by priority, keeping
    the order.
    '''
    order = ['error', 'warning', 'success', 'notice']
    sorted_ = []
    unsorted = flash.pop_messages()
    for category in order:
        for message in unsorted:
            if message.category == category:
                sorted_.append(message)
    return sorted_


def immutable_proposal_message():
    return _("This proposal is currently being voted on and cannot "
             "be modified.")


def comments_sorted(comments, root=None, variant=None, key=None):
    from adhocracy.lib.tiles.comment_tiles import CommentTile
    comments = [c for c in comments if
                (c.variant == variant and c.reply == root)]
    _comments = []
    if key is None:
        comments = sorting.comment_order(comments)
    else:
        comments = sorted(comments, key=key)<commit_msg>use Text.HEAD when variant is None in comments_sorted<commit_after>"""Helper functions

Consists of functions to typically be used within templates, but also
available to Controllers. This module is available to templates as 'h'.
"""
import cgi
from datetime import datetime
import hashlib
import json
import urllib

from paste.deploy.converters import asbool, asint
from pylons import tmpl_context as c, request
from pylons.i18n import _
from webhelpers.html import literal
from webhelpers.html.tags import file
from webhelpers.pylonslib import Flash as _Flash
from webhelpers.text import truncate

from adhocracy import config
from adhocracy.lib import cache
from adhocracy.lib import democracy
from adhocracy.lib import sorting
from adhocracy.lib import version
from adhocracy.lib.auth.authentication import allowed_login_types
from adhocracy.lib.auth.authorization import has as has_permission
from adhocracy.lib.auth.csrf import url_token, field_token
from adhocracy.lib.helpers import site_helper as site
from adhocracy.lib.helpers import user_helper as user
from adhocracy.lib.helpers import proposal_helper as proposal
from adhocracy.lib.helpers import text_helper as text
from adhocracy.lib.helpers import page_helper as page
from adhocracy.lib.helpers import delegateable_helper as delegateable
from adhocracy.lib.helpers import tag_helper as tag
from adhocracy.lib.helpers import poll_helper as poll
from adhocracy.lib.helpers import comment_helper as comment
from adhocracy.lib.helpers import selection_helper as selection
from adhocracy.lib.helpers import delegation_helper as delegation
from adhocracy.lib.helpers import instance_helper as instance
from adhocracy.lib.helpers import abuse_helper as abuse, tutorial
from adhocracy.lib.helpers import milestone_helper as milestone
from adhocracy.lib.helpers import recaptcha_helper as recaptcha
from adhocracy.lib.helpers import staticpage_helper as staticpage
from adhocracy.lib.helpers import badge_helper as badge
from adhocracy.lib.helpers import treatment_helper as treatment

from adhocracy.lib.helpers.fanstatic_helper import (FanstaticNeedHelper,
                                                    get_socialshareprivacy_url)
from adhocracy.lib.helpers import feedback_helper as feedback
from adhocracy.lib.helpers.url import build
from adhocracy.lib.helpers.site_helper import base_url
from adhocracy.lib.watchlist import make_watch, find_watch
from adhocracy import model, static
from adhocracy.i18n import countdown_time, format_date
from adhocracy.i18n import date_tag
from adhocracy.i18n import datetime_tag
from adhocracy.model.text import Text


flash = _Flash()
recaptcha = recaptcha.Recaptcha()
need = FanstaticNeedHelper(static)


def allow_user_registration():
    return asbool(config.get('adhocracy.allow_registration', 'True'))


def sorted_flash_messages():
    '''
    Return the flash messages sorted by priority, keeping
    the order.
    '''
    order = ['error', 'warning', 'success', 'notice']
    sorted_ = []
    unsorted = flash.pop_messages()
    for category in order:
        for message in unsorted:
            if message.category == category:
                sorted_.append(message)
    return sorted_


def immutable_proposal_message():
    return _("This proposal is currently being voted on and cannot "
             "be modified.")


def comments_sorted(comments, root=None, variant=None, key=None):
    from adhocracy.lib.tiles.comment_tiles import CommentTile
    comments = [c for c in comments if c.reply == root and
                ((variant is None and c.variant == Text.HEAD)
                or c.variant == variant)]
    _comments = []
    if key is None:
        comments = sorting.comment_order(comments)
    else:
        comments = sorted(comments, key=key)
{'fraction_non_alphanumeric': 0.05764038876889849, 'fraction_numerical': 0.0021598272138228943, 'mean_word_length': 4.4040846097738875, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4723662', 'n_tokens_mistral': 2123, 'n_tokens_neox': 2035, 'n_words': 711}