Ejemplo n.º 1
0
 async def prepare(self):
     self.translate = locale.get_translate(
         options.default_locale)  # Default translate for errors.
     self.session = await self.update_session()
     self.domain_id = self.request.match_info.pop('domain_id',
                                                  builtin.DOMAIN_ID_SYSTEM)
     if 'uid' in self.session:
         uid = self.session['uid']
         self.user, self.domain, self.domain_user = await asyncio.gather(
             user.get_by_uid(uid), domain.get(self.domain_id),
             domain.get_user(self.domain_id, uid))
         if not self.user:
             raise error.UserNotFoundError(uid)
         if not self.domain_user:
             self.domain_user = {}
     else:
         self.user = builtin.USER_GUEST
         self.domain = await domain.get(self.domain_id)
         self.domain_user = builtin.DOMAIN_USER_GUEST
     if not self.domain:
         raise error.DomainNotFoundError(self.domain_id)
     self.view_lang = self.get_setting('view_lang')
     # TODO(iceboy): UnknownTimeZoneError
     self.timezone = pytz.timezone(self.get_setting('timezone'))
     self.translate = locale.get_translate(self.view_lang)
     self.datetime_span = functools.partial(_datetime_span,
                                            timezone=self.timezone)
     self.datetime_stamp = _datetime_stamp
     self.reverse_url = functools.partial(_reverse_url,
                                          domain_id=self.domain_id)
     self.build_path = functools.partial(_build_path,
                                         domain_id=self.domain_id,
                                         domain_name=self.domain['name'])
     if not self.has_priv(builtin.PRIV_VIEW_ALL_DOMAIN):
         self.check_perm(builtin.PERM_VIEW)
Ejemplo n.º 2
0
async def _send_ac_mail(handler, rdoc):
    udoc = await user.get_by_uid(rdoc['uid'])
    if not udoc:
        return

    class User(setting.SettingMixin):
        def __init__(self, udoc):
            self.session = None
            self.user = udoc

        def has_priv(self, p):
            return True

    u = User(udoc)
    if not u.get_setting('send_code'):
        return
    translate = locale.get_translate(u.get_setting('view_lang'))
    pdoc = await problem.get(rdoc['domain_id'], rdoc['pid'])
    await handler.send_mail(udoc['mail'],
                            translate('P{0} - {1} Accepted!').format(
                                pdoc['doc_id'], pdoc['title']),
                            'ac_mail.html',
                            rdoc=rdoc,
                            pdoc=pdoc,
                            _=translate)
Ejemplo n.º 3
0
Archivo: base.py Proyecto: JoshOY/vj4
 async def prepare(self):
   self.session = await self.update_session()
   if self.session and 'uid' in self.session:
     self.user = await user.get_by_uid(self.session['uid']) or builtin.USER_GUEST
   else:
     self.user = builtin.USER_GUEST
   self.translate = locale.get_translate(self.get_setting('view_lang'))
   # TODO(iceboy): use user timezone.
   self.datetime_span = _get_datetime_span('Asia/Shanghai')
   self.domain_id = self.request.match_info.pop('domain_id', builtin.DOMAIN_ID_SYSTEM)
   self.reverse_url = functools.partial(_reverse_url, domain_id=self.domain_id)
   self.build_path = functools.partial(_build_path, domain_id=self.domain_id)
   self.domain = await domain.get(self.domain_id)
   if not self.domain:
     raise error.DomainNotFoundError(self.domain_id)
Ejemplo n.º 4
0
def _get_locale_context(locale_name, tzname):
    translate = locale.get_translate(locale_name)
    tz = pytz.timezone('Asia/Shanghai')
    datetime_full = translate('%Y-%m-%d %H:%M:%S')

    @functools.lru_cache()
    def _datetime_span(dt):
        if not dt.tzinfo:
            dt = dt.replace(tzinfo=pytz.utc)
        # TODO(iceboy): add a class for javascript selection.
        return markupsafe.Markup(
            '<span data-timestamp="{0}">{1}</span>'.format(
                int(dt.astimezone(pytz.utc).timestamp()),
                dt.astimezone(tz).strftime(datetime_full)))

    return {'_': translate, 'datetime_span': _datetime_span}
Ejemplo n.º 5
0
async def _send_ac_mail(handler, rdoc):
    udoc = await user.get_by_uid(rdoc['uid'])
    if not udoc:
        return
    u = setting.UserSetting(udoc)
    if not u.get_setting('send_code'):
        return
    if rdoc.get('hidden') and not handler.udoc_has_priv(
            udoc, builtin.PRIV_VIEW_HIDDEN_RECORD):
        return
    translate = locale.get_translate(u.get_setting('view_lang'))
    pdoc = await problem.get(rdoc['domain_id'], rdoc['pid'])
    await handler.send_mail(udoc['mail'],
                            translate('P{0} - {1} Accepted!').format(
                                pdoc['doc_id'], pdoc['title']),
                            'ac_mail.html',
                            rdoc=rdoc,
                            pdoc=pdoc,
                            _=translate)