<commit_before># coding=utf-8
"""
"""
from __future__ import absolute_import

import imghdr
from string import strip

import PIL
import sqlalchemy as sa
from flask import request, render_template
from flask_babel import lazy_gettext as _l, gettext as _
from wtforms.fields import (
  BooleanField, TextAreaField, StringField, IntegerField,
)
from wtforms.validators import ValidationError, required, optional

from abilian.core.models.subjects import Group
from abilian.web.forms import Form, FormPermissions
from abilian.web.forms.fields import Select2Field, FileField
from abilian.web.forms.widgets import TextArea, ImageInput, BooleanWidget
from abilian.web.forms.validators import length

from .models import Community


def _group_choices():
  m_prop = Group.members.property
  membership = m_prop.secondary
  query = Group.query.session.query(
    Group.id,
    Group.name,
    Community.name.label('community'),
    sa.sql.func.count(membership.c.user_id).label('members_count'),)
  query = query.outerjoin(m_prop.secondary, m_prop.primaryjoin)\
               .outerjoin(Community, Community.group.property.primaryjoin)\
               .group_by(Group.id, Group.name, Community.name)\
               .order_by(sa.sql.func.lower(Group.name))
  choices = [(u'', u'')]
  for g in query:
    label = u'{} ({:d} membres)'.format(g.name, g.members_count)
    if g.community:
      label += u' — Communauté: {}'.format(g.community)
    choices.append((unicode(g.id), label))

  return choices


class CommunityForm(Form):
  name = StringField(label=_l(u"Name"), validators=[required()])<commit_msg>ensure no autoflush in group query<commit_after># coding=utf-8
"""
"""
from __future__ import absolute_import

import imghdr
from string import strip

import PIL
import sqlalchemy as sa
from flask import request
from flask_babel import lazy_gettext as _l, gettext as _
from wtforms.fields import (
  BooleanField, TextAreaField, StringField,
)
from wtforms.validators import ValidationError, required, optional

from abilian.core.models.subjects import Group
from abilian.web.forms import Form
from abilian.web.forms.fields import Select2Field, FileField
from abilian.web.forms.widgets import TextArea, ImageInput, BooleanWidget
from abilian.web.forms.validators import length

from .models import Community


def _group_choices():
  m_prop = Group.members.property
  membership = m_prop.secondary
  query = Group.query.session.query(
    Group.id,
    Group.name,
    Community.name.label('community'),
    sa.sql.func.count(membership.c.user_id).label('members_count'),)
  query = query.outerjoin(m_prop.secondary, m_prop.primaryjoin)\
               .outerjoin(Community, Community.group.property.primaryjoin)\
               .group_by(Group.id, Group.name, Community.name)\
               .order_by(sa.sql.func.lower(Group.name))\
               .autoflush(False)
  choices = [(u'', u'')]

  for g in query:
    label = u'{} ({:d} membres)'.format(g.name, g.members_count)
    if g.community:
      label += u' — Communauté: {}'.format(g.community)
    choices.append((unicode(g.id), label))

  return choices


class CommunityForm(Form):
  name = StringField(label=_l(u"Name"), validators=[required()])
{'fraction_non_alphanumeric': 0.11384903306300687, 'fraction_numerical': 0.0012476606363069245, 'mean_word_length': 4.155948553054662, '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': '1440996', 'n_tokens_mistral': 1019, 'n_tokens_neox': 983, 'n_words': 244}