Exemple #1
0
 async def prepare(self):
     self.session = await self.update_session()
     if 'domain_id' in self.request.match_info:
         self.domain_id = self.request.match_info.pop('domain_id')
     if 'uid' in self.session:
         uid = self.session['uid']
         self.user, self.domain, self.domain_user, bdoc = await asyncio.gather(
             user.get_by_uid(uid), domain.get(self.domain_id),
             domain.get_user(self.domain_id, uid),
             blacklist.get(self.remote_ip))
         if not self.user:
             raise error.UserNotFoundError(uid)
         if not self.domain_user:
             self.domain_user = {}
     else:
         self.domain, bdoc = await asyncio.gather(
             domain.get(self.domain_id), blacklist.get(self.remote_ip))
     self.view_lang = self.get_setting('view_lang')
     try:
         self.timezone = pytz.timezone(self.get_setting('timezone'))
     except pytz.UnknownTimeZoneError:
         pass
     self.locale = locale.get(self.view_lang)
     self.datetime_stamp = _datetime_stamp
     if bdoc:
         raise error.BlacklistedError(self.remote_ip)
     if not self.GLOBAL and not self.has_priv(builtin.PRIV_VIEW_ALL_DOMAIN):
         self.check_perm(builtin.PERM_VIEW)
Exemple #2
0
    async def post_copy_to_domain(self,
                                  *,
                                  pid: document.convert_doc_id,
                                  dest_domain_id: str,
                                  numeric_pid: bool = False,
                                  hidden: bool = False):
        uid = self.user['_id']
        pdoc = await problem.get(self.domain_id, pid, uid)
        if pdoc.get('hidden', False):
            self.check_perm(builtin.PERM_VIEW_PROBLEM_HIDDEN)
        ddoc, dudoc = await asyncio.gather(
            domain.get(dest_domain_id), domain.get_user(dest_domain_id, uid))
        if not dudoc:
            dudoc = {}
        if not self.dudoc_has_perm(dudoc=dudoc,
                                   perm=builtin.PERM_CREATE_PROBLEM,
                                   ddoc=ddoc,
                                   udoc=self.user):
            # TODO: This is the destination domain's PermissionError.
            raise error.PermissionError(builtin.PERM_CREATE_PROBLEM)

        pid = None
        if numeric_pid:
            pid = await domain.inc_pid_counter(dest_domain_id)
        pid = await problem.copy(pdoc, dest_domain_id, uid, pid, hidden)

        new_url = self.reverse_url('problem_settings',
                                   pid=pid,
                                   domain_id=dest_domain_id)
        self.json_or_redirect(new_url, new_problem_url=new_url)
Exemple #3
0
 async def prepare(self):
   self.session = await self.update_session()
   if 'domain_id' in self.request.match_info:
     self.domain_id = self.request.match_info.pop('domain_id')
   if 'uid' in self.session:
     uid = self.session['uid']
     self.user, self.domain, self.domain_user, bdoc = await asyncio.gather(
         user.get_by_uid(uid),
         domain.get(self.domain_id),
         domain.get_user(self.domain_id, uid),
         blacklist.get(self.remote_ip))
     if not self.user:
       raise error.UserNotFoundError(uid)
     if not self.domain_user:
       self.domain_user = {}
   else:
     self.domain, bdoc = await asyncio.gather(
         domain.get(self.domain_id), blacklist.get(self.remote_ip))
   self.view_lang = self.get_setting('view_lang')
   try:
     self.timezone = pytz.timezone(self.get_setting('timezone'))
   except pytz.UnknownTimeZoneError:
     pass
   self.locale = locale.get(self.view_lang)
   self.datetime_stamp = _datetime_stamp
   if bdoc:
     raise error.BlacklistedError(self.remote_ip)
   if not self.GLOBAL and not self.has_priv(builtin.PRIV_VIEW_ALL_DOMAIN):
     self.check_perm(builtin.PERM_VIEW)
Exemple #4
0
 async def get(self, *, tid: objectid.ObjectId,
               pid: document.convert_doc_id):
     uid = self.user['_id'] if self.has_priv(
         builtin.PRIV_USER_PROFILE) else None
     tdoc, pdoc = await asyncio.gather(
         contest.get(self.domain_id, document.TYPE_HOMEWORK, tid),
         problem.get(self.domain_id, pid, uid))
     tsdoc, udoc, dudoc = await asyncio.gather(
         contest.get_status(self.domain_id, document.TYPE_HOMEWORK,
                            tdoc['doc_id'], self.user['_id']),
         user.get_by_uid(tdoc['owner_uid']),
         domain.get_user(domain_id=self.domain_id, uid=tdoc['owner_uid']))
     attended = tsdoc and tsdoc.get('attend') == 1
     if not self.is_done(tdoc):
         if not attended:
             raise error.HomeworkNotAttendedError(tdoc['doc_id'])
         if not self.is_ongoing(tdoc):
             raise error.HomeworkNotLiveError(tdoc['doc_id'])
     if pid not in tdoc['pids']:
         raise error.ProblemNotFoundError(self.domain_id, pid,
                                          tdoc['doc_id'])
     path_components = self.build_path(
         (self.translate('homework_main'),
          self.reverse_url('homework_main')),
         (tdoc['title'], self.reverse_url('homework_detail', tid=tid)),
         (pdoc['title'], None))
     self.render('problem_detail.html',
                 tdoc=tdoc,
                 pdoc=pdoc,
                 tsdoc=tsdoc,
                 udoc=udoc,
                 attended=attended,
                 dudoc=dudoc,
                 page_title=pdoc['title'],
                 path_components=path_components)
Exemple #5
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)
Exemple #6
0
 async def get(self, *, pid: document.convert_doc_id):
   # TODO(twd2): check status, eg. test, hidden problem, ...
   uid = self.user['_id'] if self.has_priv(builtin.PRIV_USER_PROFILE) else None
   pdoc = await problem.get(self.domain_id, pid, uid)
   if pdoc.get('hidden', False):
     self.check_perm(builtin.PERM_VIEW_PROBLEM_HIDDEN)
   udoc, dudoc = await asyncio.gather(user.get_by_uid(pdoc['owner_uid']),
                                      domain.get_user(self.domain_id, pdoc['owner_uid']))
   if uid == None:
     rdocs = []
   else:
     # TODO(iceboy): needs to be in sync with contest_detail_problem_submit
     rdocs = await record \
         .get_user_in_problem_multi(uid, self.domain_id, pdoc['doc_id']) \
         .sort([('_id', -1)]) \
         .limit(10) \
         .to_list()
   if not self.prefer_json:
     path_components = self.build_path(
         (self.translate('problem_main'), self.reverse_url('problem_main')),
         (pdoc['title'], self.reverse_url('problem_detail', pid=pdoc['doc_id'])),
         (self.translate('problem_submit'), None))
     self.render('problem_submit.html', pdoc=pdoc, udoc=udoc, rdocs=rdocs, dudoc=dudoc,
                 page_title=pdoc['title'], path_components=path_components)
   else:
     self.json({'rdocs': rdocs})
Exemple #7
0
    async def get(self, *, uid: int):
        is_self_profile = self.has_priv(
            builtin.PRIV_USER_PROFILE) and self.user['_id'] == uid
        udoc = await user.get_by_uid(uid)
        if not udoc:
            raise error.UserNotFoundError(uid)
        dudoc, sdoc = await asyncio.gather(
            domain.get_user(self.domain_id, udoc['_id']),
            token.get_most_recent_session_by_uid(udoc['_id']))

        rdocs = record.get_multi(get_hidden=self.has_priv(
            builtin.PRIV_VIEW_HIDDEN_RECORD),
                                 uid=uid).sort([('_id', -1)])
        rdocs = await rdocs.limit(10).to_list()
        pdict = await problem.get_dict_multi_domain(
            (rdoc['domain_id'], rdoc['pid']) for rdoc in rdocs)

        # check hidden problem
        if not self.has_perm(builtin.PERM_VIEW_PROBLEM_HIDDEN):
            f = {'hidden': False}
        else:
            f = {}
        pdocs = problem.get_multi(domain_id=self.domain_id, owner_uid=uid,
                                  **f).sort([('_id', -1)])
        pcount = await pdocs.count()
        pdocs = await pdocs.limit(10).to_list()

        psdocs = problem.get_multi_solution_by_uid(self.domain_id, uid)
        psdocs_hot = problem.get_multi_solution_by_uid(self.domain_id, uid)
        pscount = await psdocs.count()
        psdocs = await psdocs.limit(10).to_list()
        psdocs_hot = await psdocs_hot.sort([('vote', -1), ('doc_id', -1)]
                                           ).limit(10).to_list()

        if self.has_perm(builtin.PERM_VIEW_DISCUSSION):
            ddocs = discussion.get_multi(self.domain_id, owner_uid=uid)
            dcount = await ddocs.count()
            ddocs = await ddocs.limit(10).to_list()
            vndict = await discussion.get_dict_vnodes(
                self.domain_id, map(discussion.node_id, ddocs))
        else:
            ddocs = []
            vndict = {}
            dcount = 0

        self.render('user_detail.html',
                    is_self_profile=is_self_profile,
                    udoc=udoc,
                    dudoc=dudoc,
                    sdoc=sdoc,
                    rdocs=rdocs,
                    pdict=pdict,
                    pdocs=pdocs,
                    pcount=pcount,
                    psdocs=psdocs,
                    pscount=pscount,
                    psdocs_hot=psdocs_hot,
                    ddocs=ddocs,
                    dcount=dcount,
                    vndict=vndict)
Exemple #8
0
 async def get(self, *, rid: objectid.ObjectId):
     rdoc = await record.get(rid)
     if not rdoc:
         raise error.RecordNotFoundError(rid)
     # TODO(iceboy): Check domain permission, permission for visibility in place.
     if rdoc['domain_id'] != self.domain_id:
         self.redirect(
             self.reverse_url('record_detail',
                              rid=rid,
                              domain_id=rdoc['domain_id']))
         return
     # check permission for visibility: contest
     show_status = True
     if rdoc['tid']:
         now = datetime.datetime.utcnow()
         try:
             tdoc = await contest.get(rdoc['domain_id'], rdoc['tid'])
             show_status = contest.RULES[tdoc['rule']].show_func(tdoc, now) \
                           or self.has_perm(builtin.PERM_VIEW_CONTEST_HIDDEN_STATUS)
         except error.DocumentNotFoundError:
             tdoc = None
     else:
         tdoc = None
     # TODO(twd2): futher check permission for visibility.
     if (not self.own(rdoc, field='uid')
             and not self.has_perm(builtin.PERM_READ_RECORD_CODE)
             and not self.has_priv(builtin.PRIV_READ_RECORD_CODE)):
         del rdoc['code']
     if not show_status and 'code' not in rdoc:
         raise error.PermissionError(
             builtin.PERM_VIEW_CONTEST_HIDDEN_STATUS)
     udoc, dudoc = await asyncio.gather(
         user.get_by_uid(rdoc['uid']),
         domain.get_user(self.domain_id, rdoc['uid']))
     try:
         pdoc = await problem.get(rdoc['domain_id'], rdoc['pid'])
     except error.ProblemNotFoundError:
         pdoc = {}
     if show_status and 'judge_uid' in rdoc:
         judge_udoc = await user.get_by_uid(rdoc['judge_uid'])
     else:
         judge_udoc = None
     # check permission for visibility: hidden problem
     if pdoc.get(
             'hidden',
             False) and not self.has_perm(builtin.PERM_VIEW_PROBLEM_HIDDEN):
         pdoc = None
     self.render('record_detail.html',
                 rdoc=rdoc,
                 udoc=udoc,
                 dudoc=dudoc,
                 pdoc=pdoc,
                 tdoc=tdoc,
                 judge_udoc=judge_udoc,
                 show_status=show_status)
Exemple #9
0
 async def get(self, *, rid: objectid.ObjectId):
     rdoc = await record.get(rid)
     if not rdoc:
         raise error.RecordNotFoundError(rid)
     # TODO(iceboy): Check domain permission, permission for visibility in place.
     if rdoc['domain_id'] != self.domain_id:
         self.redirect(
             self.reverse_url('record_detail',
                              rid=rid,
                              domain_id=rdoc['domain_id']))
         return
     if rdoc['tid']:
         show_status, tdoc = await self.rdoc_contest_visible(rdoc)
     else:
         show_status, tdoc = True, None
     # TODO(twd2): futher check permission for visibility.
     if (not self.own(rdoc, field='uid')
             and not self.has_perm(builtin.PERM_READ_RECORD_CODE)
             and not self.has_priv(builtin.PRIV_READ_RECORD_CODE)):
         del rdoc['code']
     if not show_status and 'code' not in rdoc:
         if tdoc['doc_type'] == document.TYPE_CONTEST:
             raise error.PermissionError(
                 builtin.PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD)
         else:  # TYPE_HOMEWORK
             raise error.PermissionError(
                 builtin.PERM_VIEW_HOMEWORK_HIDDEN_SCOREBOARD)
     udoc, dudoc = await asyncio.gather(
         user.get_by_uid(rdoc['uid']),
         domain.get_user(self.domain_id, rdoc['uid']))
     try:
         pdoc = await problem.get(rdoc['domain_id'], rdoc['pid'])
     except error.ProblemNotFoundError:
         pdoc = {}
     if show_status and 'judge_uid' in rdoc:
         judge_udoc = await user.get_by_uid(rdoc['judge_uid'])
     else:
         judge_udoc = None
     # check permission for visibility: hidden problem
     if pdoc.get(
             'hidden',
             False) and not self.has_perm(builtin.PERM_VIEW_PROBLEM_HIDDEN):
         pdoc = None
     url_prefix = '/d/{}'.format(urllib.parse.quote(self.domain_id))
     self.render('record_detail.html',
                 rdoc=rdoc,
                 udoc=udoc,
                 dudoc=dudoc,
                 pdoc=pdoc,
                 tdoc=tdoc,
                 judge_udoc=judge_udoc,
                 show_status=show_status,
                 socket_url=url_prefix +
                 '/records/{}/conn'.format(rid))  # FIXME(twd2): magic
Exemple #10
0
    async def post(self,
                   *,
                   src_domain_id: str,
                   src_pids: str,
                   numeric_pid: bool = False,
                   hidden: bool = False):
        src_ddoc, src_dudoc = await asyncio.gather(
            domain.get(src_domain_id),
            domain.get_user(src_domain_id, self.user['_id']))
        if not src_dudoc:
            src_dudoc = {}
        if not self.dudoc_has_perm(ddoc=src_ddoc,
                                   dudoc=src_dudoc,
                                   udoc=self.user,
                                   perm=builtin.PERM_VIEW_PROBLEM):
            # TODO: This is the source domain's PermissionError.
            raise error.PermissionError(builtin.PERM_VIEW_PROBLEM)

        src_pids = misc.dedupe(
            map(document.convert_doc_id,
                src_pids.replace('\r\n', '\n').split('\n')))
        if len(src_pids) > self.MAX_PROBLEMS_PER_REQUEST:
            raise error.BatchCopyLimitExceededError(
                self.MAX_PROBLEMS_PER_REQUEST, len(src_pids))
        pdocs = await problem.get_multi(domain_id=src_domain_id, doc_id={'$in': src_pids}) \
          .sort('doc_id', 1) \
          .to_list()

        exist_pids = [pdoc['doc_id'] for pdoc in pdocs]
        if len(src_pids) != len(exist_pids):
            for pid in src_pids:
                if pid not in exist_pids:
                    raise error.ProblemNotFoundError(src_domain_id, pid)

        for pdoc in pdocs:
            if pdoc.get('hidden', False):
                if not self.dudoc_has_perm(
                        ddoc=src_ddoc,
                        dudoc=src_dudoc,
                        udoc=self.user,
                        perm=builtin.PERM_VIEW_PROBLEM_HIDDEN):
                    # TODO: This is the source domain's PermissionError.
                    raise error.PermissionError(
                        builtin.PERM_VIEW_PROBLEM_HIDDEN)

        for pdoc in pdocs:
            pid = None
            if numeric_pid:
                pid = await domain.inc_pid_counter(self.domain_id)
            await problem.copy(pdoc, self.domain_id, self.user['_id'], pid,
                               hidden)

        self.redirect(self.reverse_url('problem_main'))
Exemple #11
0
 async def get(self, *, pid: document.convert_doc_id):
   uid = self.user['_id'] if self.has_priv(builtin.PRIV_USER_PROFILE) else None
   pdoc = await problem.get(self.domain_id, pid, uid)
   if not self.own(pdoc, builtin.PERM_EDIT_PROBLEM_SELF):
     self.check_perm(builtin.PERM_EDIT_PROBLEM)
   udoc, dudoc = await asyncio.gather(user.get_by_uid(pdoc['owner_uid']),
                                      domain.get_user(self.domain_id, pdoc['owner_uid']))
   path_components = self.build_path(
       (self.translate('problem_main'), self.reverse_url('problem_main')),
       (pdoc['title'], self.reverse_url('problem_detail', pid=pdoc['doc_id'])),
       (self.translate('problem_edit'), None))
   self.render('problem_edit.html', pdoc=pdoc, udoc=udoc, dudoc=dudoc,
               page_title=pdoc['title'], path_components=path_components)
Exemple #12
0
 async def get(self, *, pid: document.convert_doc_id):
   # TODO(twd2)
   uid = self.user['_id'] if self.has_priv(builtin.PRIV_USER_PROFILE) else None
   pdoc = await problem.get(self.domain_id, pid, uid)
   if pdoc.get('hidden', False):
     self.check_perm(builtin.PERM_VIEW_PROBLEM_HIDDEN)
   udoc, dudoc = await asyncio.gather(user.get_by_uid(pdoc['owner_uid']),
                                      domain.get_user(self.domain_id, pdoc['owner_uid']))
   path_components = self.build_path(
       (self.translate('problem_main'), self.reverse_url('problem_main')),
       (pdoc['title'], self.reverse_url('problem_detail', pid=pdoc['doc_id'])),
       (self.translate('problem_statistics'), None))
   self.render('problem_statistics.html', pdoc=pdoc, udoc=udoc, dudoc=dudoc,
               page_title=pdoc['title'], path_components=path_components)
Exemple #13
0
 async def get(self, *, pid: document.convert_doc_id):
   uid = self.user['_id'] if self.has_priv(builtin.PRIV_USER_PROFILE) else None
   pdoc = await problem.get(self.domain_id, pid, uid)
   if pdoc.get('hidden', False):
     self.check_perm(builtin.PERM_VIEW_PROBLEM_HIDDEN)
   udoc, dudoc = await asyncio.gather(user.get_by_uid(pdoc['owner_uid']),
                                      domain.get_user(self.domain_id, pdoc['owner_uid']))
   tdocs, ctdocs, htdocs = await asyncio.gather(self._get_related_trainings(pid),
                                                self._get_related_contests(pid),
                                                self._get_related_homework(pid))
   path_components = self.build_path(
       (self.translate('problem_main'), self.reverse_url('problem_main')),
       (pdoc['title'], None))
   self.render('problem_detail.html', pdoc=pdoc, udoc=udoc, dudoc=dudoc,
               tdocs=tdocs, ctdocs=ctdocs, htdocs=htdocs,
               page_title=pdoc['title'], path_components=path_components)
Exemple #14
0
 async def get(self, *, pid: document.convert_doc_id):
   uid = self.user['_id'] if self.has_priv(builtin.PRIV_USER_PROFILE) else None
   pdoc = await problem.get(self.domain_id, pid, uid)
   if pdoc.get('hidden', False):
     self.check_perm(builtin.PERM_VIEW_PROBLEM_HIDDEN)
   udoc, dudoc = await asyncio.gather(user.get_by_uid(pdoc['owner_uid']),
                                      domain.get_user(self.domain_id, pdoc['owner_uid']))
   tdocs = await training.get_multi(self.domain_id, **{'dag.pids': pid}).to_list() \
           if self.has_perm(builtin.PERM_VIEW_TRAINING) else None
   ctdocs = await contest.get_multi(self.domain_id, pids=pid).to_list() \
            if self.has_perm(builtin.PERM_VIEW_CONTEST) else None
   path_components = self.build_path(
       (self.translate('problem_main'), self.reverse_url('problem_main')),
       (pdoc['title'], None))
   self.render('problem_detail.html', pdoc=pdoc, udoc=udoc, tdocs=tdocs, ctdocs=ctdocs,
               dudoc=dudoc, page_title=pdoc['title'], path_components=path_components)
Exemple #15
0
 async def get(self, tid: objectid.ObjectId):
   tdoc = await training.get(self.domain_id, tid)
   pids = self.get_pids(tdoc)
   # TODO(twd2): check status, eg. test, hidden problem, ...
   if not self.has_perm(builtin.PERM_VIEW_PROBLEM_HIDDEN):
     f = {'hidden': False}
   else:
     f = {}
   owner_udoc, owner_dudoc, pdict = await asyncio.gather(
       user.get_by_uid(tdoc['owner_uid']),
       domain.get_user(domain_id=self.domain_id, uid=tdoc['owner_uid']),
       problem.get_dict(self.domain_id, pids, **f))
   psdict = await problem.get_dict_status(self.domain_id,
                                          self.user['_id'], pdict.keys())
   done_pids = set()
   prog_pids = set()
   for pid, psdoc in psdict.items():
     if 'status' in psdoc:
       if psdoc['status'] == constant.record.STATUS_ACCEPTED:
         done_pids.add(pid)
       else:
         prog_pids.add(pid)
   nsdict = {}
   ndict = {}
   done_nids = set()
   for node in tdoc['dag']:
     ndict[node['_id']] = node
     total_count = len(node['pids'])
     done_count = len(set(node['pids']) & set(done_pids))
     nsdoc = {'progress': int(100 * done_count / total_count) if total_count else 100,
              'is_done': self.is_done(node, done_nids, done_pids),
              'is_progress': self.is_progress(node, done_nids, done_pids, prog_pids),
              'is_open': self.is_open(node, done_nids, done_pids, prog_pids),
              'is_invalid': self.is_invalid(node, done_nids)}
     if nsdoc['is_done']:
       done_nids.add(node['_id'])
     nsdict[node['_id']] = nsdoc
   tsdoc = await training.set_status(self.domain_id, tdoc['doc_id'], self.user['_id'],
                                     done_nids=list(done_nids), done_pids=list(done_pids),
                                     done=len(done_nids) == len(tdoc['dag']))
   path_components = self.build_path(
       (self.translate('training_main'), self.reverse_url('training_main')),
       (tdoc['title'], None))
   self.render('training_detail.html', tdoc=tdoc, tsdoc=tsdoc, pids=pids,
               pdict=pdict, psdict=psdict, ndict=ndict, nsdict=nsdict,
               owner_udoc=owner_udoc, owner_dudoc=owner_dudoc,
               page_title=tdoc['title'], path_components=path_components)
Exemple #16
0
 async def on_record_change(self, e):
   rdoc = e['value']
   for key, value in self.query.items():
     if rdoc[key] != value:
       return
   if rdoc['tid']:
     show_status, tdoc = await self.rdoc_contest_visible(rdoc)
     if not show_status:
       return
   # TODO(iceboy): projection.
   udoc, dudoc, pdoc = await asyncio.gather(user.get_by_uid(rdoc['uid']),
                                            domain.get_user(self.domain_id, rdoc['uid']),
                                            problem.get(rdoc['domain_id'], rdoc['pid']))
   # check permission for visibility: hidden problem
   if pdoc.get('hidden', False) and (pdoc['domain_id'] != self.domain_id
                                     or not self.has_perm(builtin.PERM_VIEW_PROBLEM_HIDDEN)):
     pdoc = None
   self.send(html=self.render_html('record_main_tr.html', rdoc=rdoc, udoc=udoc, dudoc=dudoc, pdoc=pdoc))
Exemple #17
0
 async def on_record_change(self, e):
   rdoc = e['value']
   for key, value in self.query.items():
     if rdoc[key] != value:
       return
   if rdoc['tid']:
     show_status, tdoc = await self.rdoc_contest_visible(rdoc)
     if not show_status:
       return
   # TODO(iceboy): projection.
   udoc, dudoc, pdoc = await asyncio.gather(user.get_by_uid(rdoc['uid']),
                                            domain.get_user(self.domain_id, rdoc['uid']),
                                            problem.get(rdoc['domain_id'], rdoc['pid']))
   # check permission for visibility: hidden problem
   if pdoc.get('hidden', False) and (pdoc['domain_id'] != self.domain_id
                                     or not self.has_perm(builtin.PERM_VIEW_PROBLEM_HIDDEN)):
     pdoc = None
   self.send(html=self.render_html('record_main_tr.html', rdoc=rdoc, udoc=udoc, dudoc=dudoc, pdoc=pdoc))
Exemple #18
0
  async def get(self, *, uid: int):
    is_self_profile = self.has_priv(builtin.PRIV_USER_PROFILE) and self.user['_id'] == uid
    udoc = await user.get_by_uid(uid)
    if not udoc:
      raise error.UserNotFoundError(uid)
    dudoc, sdoc = await asyncio.gather(domain.get_user(self.domain_id, udoc['_id']),
                                       token.get_most_recent_session_by_uid(udoc['_id']))

    rdocs = record.get_multi(get_hidden=self.has_priv(builtin.PRIV_VIEW_HIDDEN_RECORD),
                             uid=uid).sort([('_id', -1)])
    rdocs = await rdocs.limit(10).to_list()
    pdict = await problem.get_dict_multi_domain((rdoc['domain_id'], rdoc['pid']) for rdoc in rdocs)

    # check hidden problem
    if not self.has_perm(builtin.PERM_VIEW_PROBLEM_HIDDEN):
      f = {'hidden': False}
    else:
      f = {}
    pdocs = problem.get_multi(domain_id=self.domain_id, owner_uid=uid, **f).sort([('_id', -1)])
    pcount = await pdocs.count()
    pdocs = await pdocs.limit(10).to_list()

    psdocs = problem.get_multi_solution_by_uid(self.domain_id, uid)
    psdocs_hot = problem.get_multi_solution_by_uid(self.domain_id, uid)
    pscount = await psdocs.count()
    psdocs = await psdocs.limit(10).to_list()
    psdocs_hot = await psdocs_hot.sort([('vote', -1), ('doc_id', -1)]).limit(10).to_list()

    if self.has_perm(builtin.PERM_VIEW_DISCUSSION):
      ddocs = discussion.get_multi(self.domain_id, owner_uid=uid)
      dcount = await ddocs.count()
      ddocs = await ddocs.limit(10).to_list()
      vndict = await discussion.get_dict_vnodes(self.domain_id, map(discussion.node_id, ddocs))
    else:
      ddocs = []
      vndict = {}
      dcount = 0

    self.render('user_detail.html', is_self_profile=is_self_profile,
                udoc=udoc, dudoc=dudoc, sdoc=sdoc,
                rdocs=rdocs, pdict=pdict, pdocs=pdocs, pcount=pcount,
                psdocs=psdocs, pscount=pscount, psdocs_hot=psdocs_hot,
                ddocs=ddocs, dcount=dcount, vndict=vndict)
Exemple #19
0
 async def get(self, *, uid: int):
     is_self_profile = self.has_priv(
         builtin.PRIV_USER_PROFILE) and self.user['_id'] == uid
     udoc = await user.get_by_uid(uid)
     if not udoc:
         raise error.UserNotFoundError(uid)
     dudoc, sdoc = await asyncio.gather(
         domain.get_user(self.domain_id, udoc['_id']),
         token.get_most_recent_session_by_uid(udoc['_id']))
     email = self.get_udoc_setting(udoc, 'mail')
     if email:
         email = email.replace('@', random.choice([' [at] ', '#']))
     bg = random.randint(1, 21)
     rdocs = record.get_multi(get_hidden=self.has_priv(
         builtin.PRIV_VIEW_HIDDEN_RECORD),
                              uid=uid).sort([('_id', -1)])
     rdocs = await rdocs.limit(10).to_list(None)
     # TODO(twd2): check status, eg. test, hidden problem, ...
     pdocs = problem.get_multi(domain_id=self.domain_id,
                               owner_uid=uid).sort([('_id', -1)])
     pcount = await pdocs.count()
     pdocs = await pdocs.limit(10).to_list(None)
     psdocs = problem.get_multi_solution_by_uid(self.domain_id, uid)
     pscount = await psdocs.count()
     psdocs = await psdocs.limit(10).to_list(None)
     ddocs = discussion.get_multi(self.domain_id, owner_uid=uid)
     dcount = await ddocs.count()
     ddocs = await ddocs.limit(10).to_list(None)
     self.render('user_detail.html',
                 is_self_profile=is_self_profile,
                 udoc=udoc,
                 dudoc=dudoc,
                 sdoc=sdoc,
                 email=email,
                 bg=bg,
                 rdocs=rdocs,
                 pdocs=pdocs,
                 pcount=pcount,
                 psdocs=psdocs,
                 pscount=pscount,
                 ddocs=ddocs,
                 dcount=dcount)
Exemple #20
0
 async def get(self, *, rid: objectid.ObjectId):
   rdoc = await record.get(rid)
   if not rdoc:
     raise error.RecordNotFoundError(rid)
   # TODO(iceboy): Check domain permission, permission for visibility in place.
   if rdoc['domain_id'] != self.domain_id:
     self.redirect(self.reverse_url('record_detail', rid=rid, domain_id=rdoc['domain_id']))
     return
   if rdoc['tid']:
     show_status, tdoc = await self.rdoc_contest_visible(rdoc)
   else:
     show_status, tdoc = True, None
   # TODO(twd2): futher check permission for visibility.
   if (not self.own(rdoc, field='uid')
       and not self.has_perm(builtin.PERM_READ_RECORD_CODE)
       and not self.has_priv(builtin.PRIV_READ_RECORD_CODE)):
     del rdoc['code']
   if not show_status and 'code' not in rdoc:
     if tdoc['doc_type'] == document.TYPE_CONTEST:
       raise error.PermissionError(builtin.PERM_VIEW_CONTEST_HIDDEN_SCOREBOARD)
     else: # TYPE_HOMEWORK
       raise error.PermissionError(builtin.PERM_VIEW_HOMEWORK_HIDDEN_SCOREBOARD)
   udoc, dudoc = await asyncio.gather(
       user.get_by_uid(rdoc['uid']),
       domain.get_user(self.domain_id, rdoc['uid']))
   try:
     pdoc = await problem.get(rdoc['domain_id'], rdoc['pid'])
   except error.ProblemNotFoundError:
     pdoc = {}
   if show_status and 'judge_uid' in rdoc:
     judge_udoc = await user.get_by_uid(rdoc['judge_uid'])
   else:
     judge_udoc = None
   # check permission for visibility: hidden problem
   if pdoc.get('hidden', False) and not self.has_perm(builtin.PERM_VIEW_PROBLEM_HIDDEN):
     pdoc = None
   url_prefix = '/d/{}'.format(urllib.parse.quote(self.domain_id))
   self.render('record_detail.html', rdoc=rdoc, udoc=udoc, dudoc=dudoc, pdoc=pdoc, tdoc=tdoc,
               judge_udoc=judge_udoc, show_status=show_status,
               socket_url=url_prefix + '/records/{}/conn'.format(rid)) # FIXME(twd2): magic
Exemple #21
0
 async def get(self, *, tid: objectid.ObjectId, pid: document.convert_doc_id):
   uid = self.user['_id'] if self.has_priv(builtin.PRIV_USER_PROFILE) else None
   tdoc, pdoc = await asyncio.gather(contest.get(self.domain_id, document.TYPE_CONTEST, tid),
                                     problem.get(self.domain_id, pid, uid))
   tsdoc, udoc, dudoc = await asyncio.gather(
       contest.get_status(self.domain_id, document.TYPE_CONTEST, tdoc['doc_id'], self.user['_id']),
       user.get_by_uid(tdoc['owner_uid']),
       domain.get_user(domain_id=self.domain_id, uid=tdoc['owner_uid']))
   attended = tsdoc and tsdoc.get('attend') == 1
   if not self.is_done(tdoc):
     if not attended:
       raise error.ContestNotAttendedError(tdoc['doc_id'])
     if not self.is_ongoing(tdoc):
       raise error.ContestNotLiveError(tdoc['doc_id'])
   if pid not in tdoc['pids']:
     raise error.ProblemNotFoundError(self.domain_id, pid, tdoc['doc_id'])
   path_components = self.build_path(
       (self.translate('contest_main'), self.reverse_url('contest_main')),
       (tdoc['title'], self.reverse_url('contest_detail', tid=tid)),
       (pdoc['title'], None))
   self.render('problem_detail.html', tdoc=tdoc, pdoc=pdoc, tsdoc=tsdoc, udoc=udoc,
               attended=attended, dudoc=dudoc,
               page_title=pdoc['title'], path_components=path_components)
Exemple #22
0
 async def get(self, tid: objectid.ObjectId):
     tdoc = await training.get(self.domain_id, tid)
     pids = self.get_pids(tdoc)
     # TODO(twd2): check status, eg. test, hidden problem, ...
     if not self.has_perm(builtin.PERM_VIEW_PROBLEM_HIDDEN):
         f = {'hidden': False}
     else:
         f = {}
     owner_udoc, owner_dudoc, pdict = await asyncio.gather(
         user.get_by_uid(tdoc['owner_uid']),
         domain.get_user(domain_id=self.domain_id, uid=tdoc['owner_uid']),
         problem.get_dict(self.domain_id, pids, **f))
     psdict = await problem.get_dict_status(self.domain_id,
                                            self.user['_id'], pdict.keys())
     done_pids = set()
     prog_pids = set()
     for pid, psdoc in psdict.items():
         if 'status' in psdoc:
             if psdoc['status'] == constant.record.STATUS_ACCEPTED:
                 done_pids.add(pid)
             else:
                 prog_pids.add(pid)
     nsdict = {}
     ndict = {}
     done_nids = set()
     for node in tdoc['dag']:
         ndict[node['_id']] = node
         total_count = len(node['pids'])
         done_count = len(set(node['pids']) & set(done_pids))
         nsdoc = {
             'progress':
             int(100 * done_count / total_count) if total_count else 100,
             'is_done':
             self.is_done(node, done_nids, done_pids),
             'is_progress':
             self.is_progress(node, done_nids, done_pids, prog_pids),
             'is_open':
             self.is_open(node, done_nids, done_pids, prog_pids),
             'is_invalid':
             self.is_invalid(node, done_nids)
         }
         if nsdoc['is_done']:
             done_nids.add(node['_id'])
         nsdict[node['_id']] = nsdoc
     tsdoc = await training.set_status(self.domain_id,
                                       tdoc['doc_id'],
                                       self.user['_id'],
                                       done_nids=list(done_nids),
                                       done_pids=list(done_pids),
                                       done=len(done_nids) == len(
                                           tdoc['dag']))
     path_components = self.build_path((self.translate('training_main'),
                                        self.reverse_url('training_main')),
                                       (tdoc['title'], None))
     self.render('training_detail.html',
                 tdoc=tdoc,
                 tsdoc=tsdoc,
                 pids=pids,
                 pdict=pdict,
                 psdict=psdict,
                 ndict=ndict,
                 nsdict=nsdict,
                 owner_udoc=owner_udoc,
                 owner_dudoc=owner_dudoc,
                 page_title=tdoc['title'],
                 path_components=path_components)