Пример #1
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)
Пример #2
0
 async def post(self, *, tid: objectid.ObjectId,
                pid: document.convert_doc_id, lang: str, code: str):
     tdoc, pdoc = await asyncio.gather(
         contest.get(self.domain_id, document.TYPE_HOMEWORK, tid),
         problem.get(self.domain_id, pid))
     tsdoc = await contest.get_status(self.domain_id,
                                      document.TYPE_HOMEWORK,
                                      tdoc['doc_id'], self.user['_id'])
     if not tsdoc or tsdoc.get('attend') != 1:
         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'])
     rid = await record.add(self.domain_id,
                            pdoc['doc_id'],
                            constant.record.TYPE_SUBMISSION,
                            self.user['_id'],
                            lang,
                            code,
                            ttype=document.TYPE_HOMEWORK,
                            tid=tdoc['doc_id'],
                            hidden=True)
     await contest.update_status(self.domain_id, document.TYPE_HOMEWORK,
                                 tdoc['doc_id'], self.user['_id'], rid,
                                 pdoc['doc_id'], False, 0)
     if not self.can_show_record(tdoc):
         self.json_or_redirect(
             self.reverse_url('homework_detail', tid=tdoc['doc_id']))
     else:
         self.json_or_redirect(self.reverse_url('record_detail', rid=rid))
Пример #3
0
async def update_status(domain_id: str, doc_type: int, tid: objectid.ObjectId,
                        uid: int, rid: objectid.ObjectId,
                        pid: document.convert_doc_id, accept: bool,
                        score: int):
    """This method returns None when the modification has been superseded by a parallel operation."""
    if doc_type not in [document.TYPE_CONTEST, document.TYPE_HOMEWORK]:
        raise error.InvalidArgumentError('doc_type')
    tdoc = await document.get(domain_id, doc_type, tid)
    tsdoc = await document.rev_push_status(domain_id, tdoc['doc_type'],
                                           tdoc['doc_id'], uid, 'journal', {
                                               'rid': rid,
                                               'pid': pid,
                                               'accept': accept,
                                               'score': score
                                           })
    if 'attend' not in tsdoc or not tsdoc['attend']:
        if tdoc['doc_type'] == document.TYPE_CONTEST:
            raise error.ContestNotAttendedError(domain_id, tid, uid)
        elif tdoc['doc_type'] == document.TYPE_HOMEWORK:
            raise error.HomeworkNotAttendedError(domain_id, tid, uid)
        else:
            raise error.InvalidArgumentError('doc_type')

    journal = _get_status_journal(tsdoc)
    stats = RULES[tdoc['rule']].stat_func(tdoc, journal)
    tsdoc = await document.rev_set_status(domain_id,
                                          tdoc['doc_type'],
                                          tid,
                                          uid,
                                          tsdoc['rev'],
                                          journal=journal,
                                          **stats)
    return tsdoc
Пример #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 = 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']))
   attended = tsdoc and tsdoc.get('attend') == 1
   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'])
   if self.can_show_record(tdoc):
     rdocs = await record.get_user_in_problem_multi(uid, self.domain_id, pdoc['doc_id'], get_hidden=True) \
                         .sort([('_id', -1)]) \
                         .limit(10) \
                         .to_list()
   else:
     rdocs = []
   if not self.prefer_json:
     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'], self.reverse_url('homework_detail_problem', tid=tid, pid=pid)),
         (self.translate('homework_detail_problem_submit'), None))
     self.render('problem_submit.html', tdoc=tdoc, pdoc=pdoc, rdocs=rdocs,
                 tsdoc=tsdoc, udoc=udoc, attended=attended,
                 page_title=pdoc['title'], path_components=path_components)
   else:
     self.json({'rdocs': rdocs})
Пример #5
0
 async def post(self, *, ctype: str, tid: objectid.ObjectId,
                pid: document.convert_doc_id, lang: str,
                code: objectid.ObjectId):
     doc_type = constant.contest.CTYPE_TO_DOCTYPE[ctype]
     # TODO(iceboy): rate limit base on ip.
     tdoc, pdoc = await asyncio.gather(
         contest.get(self.domain_id, doc_type, tid),
         problem.get(self.domain_id, pid))
     tsdoc = await contest.get_status(self.domain_id, doc_type,
                                      tdoc['doc_id'], self.user['_id'])
     if not tsdoc or tsdoc.get('attend') != 1:
         if ctype == 'contest':
             raise error.ContestNotAttendedError(tdoc['doc_id'])
         elif ctype == 'homework':
             raise error.HomeworkNotAttendedError(tdoc['doc_id'])
         else:
             raise error.InvalidArgumentError('ctype')
     if not self.is_ongoing(tdoc):
         if ctype == 'contest':
             raise error.ContestNotLiveError(tdoc['doc_id'])
         elif ctype == 'homework':
             raise error.HomeworkNotLiveError(tdoc['doc_id'])
         else:
             raise error.InvalidArgumentError('ctype')
     if pid not in tdoc['pids']:
         raise error.ProblemNotFoundError(self.domain_id, pid,
                                          tdoc['doc_id'])
     # TODO(tc-imba): only constant.record.CODE_TYPE_TAR is supported now
     rid = await record.add(self.domain_id,
                            pdoc['doc_id'],
                            constant.record.TYPE_SUBMISSION,
                            self.user['_id'],
                            lang,
                            code,
                            tid=tdoc['doc_id'],
                            hidden=False,
                            code_type=constant.record.CODE_TYPE_TAR)
     await contest.update_status(self.domain_id, tdoc['doc_id'],
                                 self.user['_id'], rid, pdoc['doc_id'],
                                 False, 0)
     if not self.can_show_record(tdoc):
         self.json_or_redirect(
             self.reverse_url('contest_detail',
                              ctype=ctype,
                              tid=tdoc['doc_id']))
     else:
         self.json_or_redirect(self.reverse_url('record_detail', rid=rid))
Пример #6
0
 async def get(self, *, ctype: str, tid: objectid.ObjectId,
               pid: document.convert_doc_id):
     doc_type = constant.contest.CTYPE_TO_DOCTYPE[ctype]
     uid = self.user['_id'] if self.has_priv(
         builtin.PRIV_USER_PROFILE) else None
     tdoc, pdoc = await asyncio.gather(
         contest.get(self.domain_id, doc_type, tid),
         problem.get(self.domain_id, pid, uid))
     tsdoc, udoc = await asyncio.gather(
         contest.get_status(self.domain_id, doc_type, tdoc['doc_id'],
                            self.user['_id']),
         user.get_by_uid(tdoc['owner_uid']))
     attended = tsdoc and tsdoc.get('attend') == 1
     if not self.is_finished(tdoc):
         if not attended:
             if ctype == 'contest':
                 raise error.ContestNotAttendedError(tdoc['doc_id'])
             elif ctype == 'homework':
                 raise error.HomeworkNotAttendedError(tdoc['doc_id'])
             else:
                 raise error.InvalidArgumentError('ctype')
         if not self.is_ongoing(tdoc):
             if ctype == 'contest':
                 raise error.ContestNotLiveError(tdoc['doc_id'])
             elif ctype == 'homework':
                 raise error.HomeworkNotLiveError(tdoc['doc_id'])
             else:
                 raise error.InvalidArgumentError('ctype')
     if pid not in tdoc['pids']:
         raise error.ProblemNotFoundError(self.domain_id, pid,
                                          tdoc['doc_id'])
     path_components = self.build_path(
         (self.translate('page.contest_main.{0}.title'.format(ctype)),
          self.reverse_url('contest_main', ctype=ctype)),
         (tdoc['title'],
          self.reverse_url('contest_detail', ctype=ctype, tid=tid)),
         (pdoc['title'], None))
     self.render('problem_detail.html',
                 tdoc=tdoc,
                 pdoc=pdoc,
                 tsdoc=tsdoc,
                 udoc=udoc,
                 attended=attended,
                 page_title=pdoc['title'],
                 path_components=path_components)
Пример #7
0
 async def get(self, *, ctype: str, tid: objectid.ObjectId,
               pid: document.convert_doc_id):
     doc_type = constant.contest.CTYPE_TO_DOCTYPE[ctype]
     uid = self.user['_id'] if self.has_priv(
         builtin.PRIV_USER_PROFILE) else None
     tdoc, pdoc = await asyncio.gather(
         contest.get(self.domain_id, doc_type, tid),
         problem.get(self.domain_id, pid, uid))
     tsdoc, udoc = await asyncio.gather(
         contest.get_status(self.domain_id, doc_type, tdoc['doc_id'],
                            self.user['_id']),
         user.get_by_uid(tdoc['owner_uid']))
     attended = tsdoc and tsdoc.get('attend') == 1
     if not attended:
         if ctype == 'contest':
             raise error.ContestNotAttendedError(tdoc['doc_id'])
         elif ctype == 'homework':
             raise error.HomeworkNotAttendedError(tdoc['doc_id'])
         else:
             raise error.InvalidArgumentError('ctype')
     if not self.is_ongoing(tdoc):
         if ctype == 'contest':
             raise error.ContestNotLiveError(tdoc['doc_id'])
         elif ctype == 'homework':
             raise error.HomeworkNotLiveError(tdoc['doc_id'])
         else:
             raise error.InvalidArgumentError('ctype')
     if pid not in tdoc['pids']:
         raise error.ProblemNotFoundError(self.domain_id, pid,
                                          tdoc['doc_id'])
     if self.can_show_record(tdoc):
         rdocs = await record.get_user_in_problem_multi(uid, self.domain_id, pdoc['doc_id'], get_hidden=True) \
           .sort([('_id', -1)]) \
           .limit(10) \
           .to_list()
     else:
         rdocs = []
     if not self.prefer_json:
         path_components = self.build_path(
             (self.translate('page.contest_main.{0}.title'.format(ctype)),
              self.reverse_url('contest_main', ctype=ctype)),
             (tdoc['title'],
              self.reverse_url('contest_detail', ctype=ctype, tid=tid)),
             (pdoc['title'],
              self.reverse_url(
                  'contest_detail_problem', ctype=ctype, tid=tid, pid=pid)),
             (self.translate('page.contest_detail_problem_submit.{0}.title'.
                             format(ctype)), None))
         languages = filter_language(pdoc.get('languages') or [])
         default_lang = len(languages) and list(languages.keys())[0] or ''
         self.render('problem_submit.html',
                     tdoc=tdoc,
                     pdoc=pdoc,
                     rdocs=rdocs,
                     tsdoc=tsdoc,
                     udoc=udoc,
                     attended=attended,
                     page_title=pdoc['title'],
                     path_components=path_components,
                     languages=languages,
                     default_lang=default_lang)
     else:
         self.json({'rdocs': rdocs})