<commit_before>import asyncio
import collections
import datetime
import functools

from vj4 import app
from vj4 import constant
from vj4 import error
from vj4.model import builtin
from vj4.model import domain
from vj4.model import user
from vj4.model.adaptor import discussion
from vj4.model.adaptor import contest
from vj4.model.adaptor import training
from vj4.handler import base
import vj4.handler.training
from vj4.util import validator


@app.route('/', 'domain_main')
class DomainMainHandler(base.Handler, vj4.handler.training.TrainingMixin):
  CONTESTS_ON_MAIN = 5
  TRAININGS_ON_MAIN = 5
  DISCUSSIONS_ON_MAIN = 20

  async def prepare_contest(self):
    if self.has_perm(builtin.PERM_VIEW_CONTEST):
      tdocs = await contest.get_multi(self.domain_id) \
                           .limit(self.CONTESTS_ON_MAIN) \
                           .to_list()
      tsdict = await contest.get_dict_status(self.domain_id, self.user['_id'],
                                             (tdoc['doc_id'] for tdoc in tdocs))
    else:
      tdocs = []
      tsdict = {}
    return tdocs, tsdict

  async def prepare_training(self):
    if self.has_perm(builtin.PERM_VIEW_TRAINING):
      tdocs = await training.get_multi(self.domain_id) \
                            .sort('doc_id', 1) \
                            .limit(self.TRAININGS_ON_MAIN) \
                            .to_list()
      tsdict = await training.get_dict_status(self.domain_id, self.user['_id'],
                                              (tdoc['doc_id'] for tdoc in tdocs))
    else:
      tdocs = []
      tsdict = {}
    return tdocs, tsdict

  async def prepare_discussion(self):
    if self.has_perm(builtin.PERM_VIEW_DISCUSSION):
      ddocs = await discussion.get_multi(self.domain_id) \
                              .limit(self.DISCUSSIONS_ON_MAIN) \
                              .to_list()
      vndict = await discussion.get_dict_vnodes(self.domain_id, map(discussion.node_id, ddocs))
    else:
      ddocs = []
      vndict = {}
    return ddocs, vndict

  async def get(self):
    (tdocs, tsdict), (trdocs, trsdict), (ddocs, vndict) = await asyncio.gather(
        self.prepare_contest(), self.prepare_training(), self.prepare_discussion())
    udict = await user.get_dict(ddoc['owner_uid'] for ddoc in ddocs)
    self.render('domain_main.html', discussion_nodes=await discussion.get_nodes(self.domain_id),
                tdocs=tdocs, tsdict=tsdict, trdocs=trdocs, trsdict=trsdict,
                ddocs=ddocs, vndict=vndict,
                udict=udict, datetime_stamp=self.datetime_stamp)


@app.route('/domain', 'domain_manage')
class DomainManageHandler(base.Handler):
  async def get(self):
    self.redirect(self.reverse_url('domain_manage_dashboard'))


@app.route('/domain/dashboard', 'domain_manage_dashboard')
class DomainDashboardHandler(base.Handler):
  async def get(self):
    if not self.has_perm(builtin.PERM_EDIT_PERM):
       self.check_perm(builtin.PERM_EDIT_DESCRIPTION)
    self.render('domain_manage_dashboard.html')


@app.route('/domain/edit', 'domain_manage_edit')
class DomainEditHandler(base.Handler):
  @base.require_perm(builtin.PERM_EDIT_DESCRIPTION)
  async def get(self):
    self.render('domain_manage_edit.html')

  @base.require_perm(builtin.PERM_EDIT_DESCRIPTION)
  @base.post_argument
  @base.require_csrf_token
  @base.sanitize
  async def post(self, *, name: str, gravatar: str, bulletin: str):
    await domain.edit(self.domain_id, name=name, gravatar=gravatar, bulletin=bulletin)
    self.json_or_redirect(self.url)


@app.route('/domain/join_applications', 'domain_manage_join_applications')
class DomainJoinApplicationsHandler(base.Handler):
  @property
  @functools.lru_cache()
  def now(self):
    # TODO(twd2): This does not work on multi-machine environment.
    return datetime.datetime.utcnow()

  @base.require_perm(builtin.PERM_EDIT_PERM)
  async def get(self):
    roles = sorted(list(self.domain['roles'].keys()))
    roles_with_text = [(role, role) for role in roles]
    join_settings = domain.get_join_settings(self.domain, self.now)
    expirations = vj4.constant.domain.JOIN_EXPIRATION_RANGE.copy()
    if not join_settings:
      del expirations[vj4.constant.domain.JOIN_EXPIRATION_KEEP_CURRENT]
    self.render('domain_manage_join_applications.html', roles_with_text=roles_with_text,
                join_settings=join_settings, expirations=expirations)
<commit_msg>fix the wrong usage of vj4.constant module (#413)<commit_after>import asyncio
import collections
import datetime
import functools

from vj4 import app
from vj4 import constant
from vj4 import error
from vj4.model import builtin
from vj4.model import domain
from vj4.model import user
from vj4.model.adaptor import discussion
from vj4.model.adaptor import contest
from vj4.model.adaptor import training
from vj4.handler import base
import vj4.handler.training
from vj4.util import validator


@app.route('/', 'domain_main')
class DomainMainHandler(base.Handler, vj4.handler.training.TrainingMixin):
  CONTESTS_ON_MAIN = 5
  TRAININGS_ON_MAIN = 5
  DISCUSSIONS_ON_MAIN = 20

  async def prepare_contest(self):
    if self.has_perm(builtin.PERM_VIEW_CONTEST):
      tdocs = await contest.get_multi(self.domain_id) \
                           .limit(self.CONTESTS_ON_MAIN) \
                           .to_list()
      tsdict = await contest.get_dict_status(self.domain_id, self.user['_id'],
                                             (tdoc['doc_id'] for tdoc in tdocs))
    else:
      tdocs = []
      tsdict = {}
    return tdocs, tsdict

  async def prepare_training(self):
    if self.has_perm(builtin.PERM_VIEW_TRAINING):
      tdocs = await training.get_multi(self.domain_id) \
                            .sort('doc_id', 1) \
                            .limit(self.TRAININGS_ON_MAIN) \
                            .to_list()
      tsdict = await training.get_dict_status(self.domain_id, self.user['_id'],
                                              (tdoc['doc_id'] for tdoc in tdocs))
    else:
      tdocs = []
      tsdict = {}
    return tdocs, tsdict

  async def prepare_discussion(self):
    if self.has_perm(builtin.PERM_VIEW_DISCUSSION):
      ddocs = await discussion.get_multi(self.domain_id) \
                              .limit(self.DISCUSSIONS_ON_MAIN) \
                              .to_list()
      vndict = await discussion.get_dict_vnodes(self.domain_id, map(discussion.node_id, ddocs))
    else:
      ddocs = []
      vndict = {}
    return ddocs, vndict

  async def get(self):
    (tdocs, tsdict), (trdocs, trsdict), (ddocs, vndict) = await asyncio.gather(
        self.prepare_contest(), self.prepare_training(), self.prepare_discussion())
    udict = await user.get_dict(ddoc['owner_uid'] for ddoc in ddocs)
    self.render('domain_main.html', discussion_nodes=await discussion.get_nodes(self.domain_id),
                tdocs=tdocs, tsdict=tsdict, trdocs=trdocs, trsdict=trsdict,
                ddocs=ddocs, vndict=vndict,
                udict=udict, datetime_stamp=self.datetime_stamp)


@app.route('/domain', 'domain_manage')
class DomainManageHandler(base.Handler):
  async def get(self):
    self.redirect(self.reverse_url('domain_manage_dashboard'))


@app.route('/domain/dashboard', 'domain_manage_dashboard')
class DomainDashboardHandler(base.Handler):
  async def get(self):
    if not self.has_perm(builtin.PERM_EDIT_PERM):
       self.check_perm(builtin.PERM_EDIT_DESCRIPTION)
    self.render('domain_manage_dashboard.html')


@app.route('/domain/edit', 'domain_manage_edit')
class DomainEditHandler(base.Handler):
  @base.require_perm(builtin.PERM_EDIT_DESCRIPTION)
  async def get(self):
    self.render('domain_manage_edit.html')

  @base.require_perm(builtin.PERM_EDIT_DESCRIPTION)
  @base.post_argument
  @base.require_csrf_token
  @base.sanitize
  async def post(self, *, name: str, gravatar: str, bulletin: str):
    await domain.edit(self.domain_id, name=name, gravatar=gravatar, bulletin=bulletin)
    self.json_or_redirect(self.url)


@app.route('/domain/join_applications', 'domain_manage_join_applications')
class DomainJoinApplicationsHandler(base.Handler):
  @property
  @functools.lru_cache()
  def now(self):
    # TODO(twd2): This does not work on multi-machine environment.
    return datetime.datetime.utcnow()

  @base.require_perm(builtin.PERM_EDIT_PERM)
  async def get(self):
    roles = sorted(list(self.domain['roles'].keys()))
    roles_with_text = [(role, role) for role in roles]
    join_settings = domain.get_join_settings(self.domain, self.now)
    expirations = constant.domain.JOIN_EXPIRATION_RANGE.copy()
    if not join_settings:
      del expirations[constant.domain.JOIN_EXPIRATION_KEEP_CURRENT]
    self.render('domain_manage_join_applications.html', roles_with_text=roles_with_text,
                join_settings=join_settings, expirations=expirations)

{'fraction_non_alphanumeric': 0.10435961147503953, 'fraction_numerical': 0.004969505308335216, 'mean_word_length': 3.2757122163206183, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1165851', 'n_tokens_mistral': 2937, 'n_tokens_neox': 2860, 'n_words': 533}