Exemple #1
0
class UserGroup(db.Model):
    __table_args__ = (
        db.UniqueConstraint('user_id', 'group_id', name='group_id'),
        {
            'schema': 'moodle'
        },
    )
    __tablename__ = 'mdl_ejudge_group_users'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('moodle.mdl_user.id'))
    group_id = db.Column(db.Integer,
                         db.ForeignKey('moodle.mdl_ejudge_group.id'))

    user = db.relationship('SimpleUser',
                           backref=db.backref('user_groups', lazy='select'))
    group = db.relationship('Group',
                            backref=db.backref('user_groups', lazy='select'))

    @staticmethod
    def create_if_not_exists(user_id, group_id):
        user_group = db.session.query(UserGroup).filter_by(
            user_id=user_id, group_id=group_id).first()
        if user_group:
            return None

        return UserGroup(user_id=user_id, group_id=group_id)
Exemple #2
0
class StatementProblem(db.Model):
    __table_args__ = {'schema': 'moodle'}
    __tablename__ = 'mdl_statements_problems_correlation'

    id = db.Column(db.Integer, primary_key=True)
    statement_id = db.Column(db.Integer,
                             db.ForeignKey('moodle.mdl_statements.id'))
    problem_id = db.Column(db.Integer, db.ForeignKey('moodle.mdl_problems.id'))
    rank = db.Column('rank', db.Integer)
    hidden = db.Column('hidden', db.Integer)

    statement = db.relationship(
        'Statement',
        backref=db.backref(
            'StatementProblems',
            collection_class=attribute_mapped_collection("rank")))

    # reference to the "Keyword" object
    problem = db.relationship('Problem',
                              backref=db.backref('StatementProblems'))

    def __init__(self, statement_id, problem_id, rank):
        self.statement_id = statement_id
        self.problem_id = problem_id
        self.rank = rank
        self.hidden = 0
class CourseModule(db.Model):
    __table_args__ = {'schema': 'moodle'}
    __tablename__ = 'mdl_course_modules'

    id = db.Column(db.Integer, primary_key=True)
    course_id = db.Column('course', db.Integer, db.ForeignKey('moodle.mdl_course.id'))
    module = db.Column(db.Integer)
    instance_id = db.Column('instance', db.Integer)
    section_id = db.Column('section', db.Integer, db.ForeignKey('moodle.mdl_course_sections.id'))
    visible = db.Column(db.Boolean)

    course = db.relationship('Course', backref=db.backref('course_modules', lazy='dynamic'))
    section = db.relationship('CourseSection', backref=db.backref('modules', lazy='dynamic'))

    @property
    def instance(self) -> Type['CourseModuleInstance']:
        if not hasattr(self, '_instance'):
            instance_class = next(
                (
                    subclass
                    for subclass in CourseModuleInstance.__subclasses__()
                    if subclass.MODULE == self.module
                ),
                None
            )
            if not instance_class:
                self._instance = None
            else:
                self._instance = db.session.query(instance_class) \
                    .filter_by(id=self.instance_id) \
                    .first()
        return self._instance

    @deprecated(' `Do not use serialize inside model!` ')
    def serialize(self):
        serialized = attrs_to_dict(
            self,
            'id',
            'course_id',
            'module',
            'section_id',
            'visible',
        )
        if self.instance:
            serialized['type'] = self.instance.MODULE_TYPE
            if self.instance.MODULE_TYPE == 'STATEMENT':
                serialized['instance'] = attrs_to_dict(
                    self.instance,
                    'id',
                    'name',
                )
            elif self.instance.MODULE_TYPE in ['BOOK', 'MONITOR']:
                serialized['instance'] = self.instance.serialize(course_module_id=self.id)
            else:
                serialized['instance'] = self.instance.serialize()

        return serialized
Exemple #4
0
class Group(db.Model):
    __table_args__ = (db.ForeignKeyConstraint(['owner_id'],
                                              ['moodle.mdl_user.id']), {
                                                  'schema': 'moodle'
                                              })
    __tablename__ = 'mdl_ejudge_group'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Unicode(100))
    description = db.Column(db.Text)
    owner_id = db.Column(db.Integer)
    visible = db.Column(db.Integer)

    owner = db.relationship('SimpleUser',
                            backref=db.backref('groups', lazy='select'),
                            lazy='joined')

    def serialize(self, attributes=None):
        if not attributes:
            attributes = (
                'name',
                'description',
                'owner_id',
                'visible',
            )
        serialized = attrs_to_dict(self, *attributes)
        return serialized
class CourseSection(db.Model):
    __table_args__ = {'schema': 'moodle'}
    __tablename__ = 'mdl_course_sections'

    id = db.Column(db.Integer, primary_key=True)
    course_id = db.Column('course', db.Integer,
                          db.ForeignKey('moodle.mdl_course.id'))
    section = db.Column(db.Integer)
    summary = db.Column(db.Text)
    sequence_text = db.Column('sequence', db.Text)
    visible = db.Column(db.Boolean)

    course = db.relationship('Course',
                             backref=db.backref(
                                 'sections',
                                 lazy='dynamic',
                                 order_by='CourseSection.section'))

    def __init__(self):
        self._sequence = None

    @property
    def sequence(self):
        if not getattr(self, '_sequence'):
            try:
                self._sequence = list(map(int, self.sequence_text.split(',')))
            except Exception:
                self._sequence = []
        return self._sequence

    @sequence.setter
    def sequence(self, value):
        self._sequence = value
        self.sequence_text = ','.join(list(map(str, value)))

    def serialize(self):
        serialized = attrs_to_dict(
            self,
            'id',
            'course_id',
            'section',
            'summary',
            'sequence',
            'visible',
        )
        serialized['modules'] = [
            module.serialize()
            for module in self.modules.filter_by(visible=True).all()
        ]

        return serialized
class ProblemStandings(StandingsMixin, db.Model):
    __tablename__ = 'problem_standings'
    __table_args__ = {'schema': 'pynformatics'}

    problem_id = db.Column(db.Integer,
                           db.ForeignKey('moodle.mdl_problems.id'),
                           primary_key=True)
    problem = db.relationship('EjudgeProblem',
                              backref=db.backref('standings',
                                                 uselist=False,
                                                 lazy='joined'))

    @classmethod
    def create(cls, **kwargs):
        instance = cls(**kwargs)
        db.session.add(instance)

        # Flush, чтобы получить из базы problem
        db.session.flush([instance])

        # Expire, чтобы у задачи стал доступен standings
        db.session.expire(instance.problem)

        log.info('ProblemStandings(problem_id=%s) Created. Starting updates' %
                 instance.problem_id)

        users = db.session.query(SimpleUser) \
            .join(EjudgeRun) \
            .filter(
                and_(
                    EjudgeRun.contest_id == instance.problem.ejudge_contest_id,
                    EjudgeRun.prob_id == instance.problem.problem_id
                )
            ) \
            .distinct() \
            .all()

        with db.session.no_autoflush:
            for i, user in enumerate(users):
                instance.update(user)

        log.info('ProblemStandings(problem_id=%s) Updates finished.' %
                 instance.problem_id)

        return instance

    def update(self, user):
        super(ProblemStandings, self).update(user)

        user_runs = self.problem.ejudge_runs \
            .filter_by(user_id=user.ejudge_id) \
            .order_by(EjudgeRun.create_time) \
            .all()

        processed = {
            'attempts': 0,
            'score': 0,
            'status': None,
        }
        for run in user_runs:
            processed['attempts'] += 1
            if run.score > processed['score']:
                processed['score'] = run.score
                processed['status'] = run.status

            if run.score == 100:
                break

        self.json[user.id].update(processed)

    def serialize(self):
        return self.json
class StatementStandings(StandingsMixin, db.Model):
    __tablename__ = 'statement_standings'

    statement_id = db.Column(db.Integer,
                             db.ForeignKey('moodle.mdl_statements.id'),
                             primary_key=True)
    statement = db.relationship('Statement',
                                backref=db.backref('standings',
                                                   uselist=False,
                                                   lazy='joined'))

    @classmethod
    def create(cls, **kwargs):
        instance = cls(**kwargs)
        db.session.add(instance)

        # Flush, чтобы получить из базы statement
        db.session.flush([instance])

        # Expire, чтобы у statement стал доступен standings
        db.session.expire(instance.statement)

        log.info(
            'StatementStandings(statement_id=%s) Created. Starting updates' %
            instance.statement_id)

        pynformatics_runs = db.session.query(PynformaticsRun) \
            .filter_by(statement_id=instance.statement_id) \
            .all()

        with db.session.no_autoflush:
            for pynformatics_run in pynformatics_runs:
                instance.update(pynformatics_run.run)

        log.info('StatementStandings(statement_id=%s) Updates finished.' %
                 instance.statement_id)

        return instance

    def update(self, run):
        user = run.user
        super(StatementStandings, self).update(user)

        runs = self.json[str(user.id)].get('runs', [])

        replace_index = index_of(
            runs, lambda run_json: run_json['run_id'] == run.run_id and
            run_json['contest_id'] == run.contest_id)

        if replace_index is not None:
            runs[replace_index] = StatementStandings.serialize_run(run)
        else:
            insert_index = index_of(
                runs, lambda run_json: run_json['create_time'] > run.
                create_time.isoformat(), len(runs))
            runs.insert(insert_index, StatementStandings.serialize_run(run))

        self.json[str(user.id)]['runs'] = runs
        self.json.changed()

    # TODO: добавить обработку настроек контеста
    def serialize(self, group_id=None):
        result = self.json
        if group_id:
            try:
                group = db.session.query(Group).filter_by(id=group_id).one()
            except Exception:
                raise ValueError('Group not found')

            result = {
                str(user_group.user_id): result[str(user_group.user_id)]
                for user_group in group.user_groups
                if str(user_group.user_id) in result
            }

        return result
Exemple #8
0
class Run(db.Model):
    __table_args__ = ({'schema': 'pynformatics'}, )
    __tablename__ = 'runs'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('moodle.mdl_user.id'))
    problem_id = db.Column(db.Integer, db.ForeignKey('moodle.mdl_problems.id'))
    statement_id = db.Column(db.Integer,
                             db.ForeignKey('moodle.mdl_statements.id'))
    score = db.Column(db.Integer)

    user = db.relationship('SimpleUser', backref='runs', lazy='select')
    problem = db.relationship('EjudgeProblem',
                              backref=db.backref('runs', lazy='dynamic'))
    statement = db.relationship('Statement', backref='runs')

    create_time = db.Column(db.DateTime(), default=datetime.datetime.utcnow)

    # Поля скопированные из ejudge.runs
    ejudge_run_id = db.Column('ej_run_id', db.Integer)
    ejudge_contest_id = db.Column('ej_contest_id', db.Integer)
    ejudge_run_uuid = db.Column('ej_run_uuid', db.String(40))

    ejudge_score = db.Column('ej_score', db.Integer)
    ejudge_status = db.Column('ej_status', db.Integer)
    ejudge_language_id = db.Column('ej_lang_id', db.Integer)
    ejudge_test_num = db.Column('ej_test_num', db.Integer)

    ejudge_create_time = db.Column('ej_create_time', db.DateTime)
    ejudge_last_change_time = db.Column('ej_last_change_time', db.DateTime)
    ejudge_url = db.Column(db.String(50))

    source_hash = db.Column(db.String(32))  # We are using md5 hex digest

    def update_source(self, blob: bytes):
        mongo.db.source.insert_one({
            'run_id': self.id,
            'blob': blob,
        })
        return blob

    def remove_source(self):
        mongo.db.source.find_one_and_delete({'run_id': self.id})

    def move_protocol_to_rejudge_collection(self, rejudge_id: int):
        protocol = mongo.db.protocol.find_one({'run_id': self.id})
        if protocol:
            del protocol['_id']
            protocol['rejudge_id'] = rejudge_id
            mongo.db.rejudge.insert_one(protocol)
            mongo.db.protocol.find_one_and_delete({'run_id': self.id})

    @property
    def source(self) -> Optional[bytes]:
        data = mongo.db.source.find_one({'run_id': self.id})
        if data is None:
            logging.error(f'Cannot find source for run #{self.id}')
            return None
        blob = data.get('blob')
        return blob

    @property
    def protocol(self) -> Optional[dict]:
        return mongo.db.protocol.find_one({'run_id': self.id}, {'_id': False})

    @protocol.setter
    def protocol(self, protocol_source: dict):
        mongo.db.protocol.update({'run_id': self.id},
                                 protocol_source,
                                 upsert=True)

    @staticmethod
    def generate_source_hash(blob: bytes) -> str:
        m = hashlib.md5()
        m.update(blob)
        return m.hexdigest()

    @property
    def status(self):
        return self.ejudge_status

    @property
    def language_id(self):
        return self.ejudge_language_id

    @staticmethod
    @deprecated
    def pick_ejudge_columns(ejudge_run):
        return {
            'ejudge_run_id': ejudge_run.run_id,
            'ejudge_contest_id': ejudge_run.contest_id,
            'ejudge_run_uuid': ejudge_run.run_uuid,
            'ejudge_score': ejudge_run.score,
            'ejudge_status': ejudge_run.status,
            'ejudge_language_id': ejudge_run.lang_id,
            'ejudge_test_num': ejudge_run.test_num,
            'ejudge_create_time': ejudge_run.create_time,
            'ejudge_last_change_time': ejudge_run.last_change_time,
        }

    def serialize(self, attributes=None):
        if attributes is None:
            attributes = (
                'id',
                'user',
                'problem_id',
                'statement_id',
                'score',
                'status',
                'language_id',
                'create_time',
                'ejudge_run_id',
                'ejudge_contest_id',
            )
        if hasattr(g, 'user') and g.user.id == self.user_id:
            attributes = (
                *attributes,
                'source',
            )
        serialized = attrs_to_dict(self, *attributes)

        if 'create_time' in attributes:
            serialized['create_time'] = str(self.create_time)

        if 'user' in attributes:
            serialized['user'] = self.user.serialize()

        return serialized
Exemple #9
0
class EjudgeRun(db.Model):
    __table_args__ = (db.ForeignKeyConstraint(['contest_id', 'prob_id'], [
        'moodle.mdl_ejudge_problem.ejudge_contest_id',
        'moodle.mdl_ejudge_problem.problem_id'
    ]), db.ForeignKeyConstraint(['user_id'], ['moodle.mdl_user.ej_id']), {
        'schema': 'ejudge'
    })
    __tablename__ = 'runs'

    run_id = db.Column(db.Integer, primary_key=True)
    contest_id = db.Column(db.Integer, primary_key=True)
    size = db.Column(db.Integer)
    create_time = db.Column(db.DateTime)
    create_nsec = db.Column(db.Integer)
    user_id = db.Column(db.Integer)
    prob_id = db.Column(db.Integer)  # TODO: rename to problem_id
    lang_id = db.Column(db.Integer)
    status = db.Column(db.Integer)
    ssl_flag = db.Column(db.Integer)
    ip_version = db.Column(db.Integer)
    ip = db.Column(db.String(64))
    hash = db.Column(db.String(128))
    run_uuid = db.Column(db.String(40))
    score = db.Column(db.Integer)
    test_num = db.Column(db.Integer)
    score_adj = db.Column(db.Integer)
    locale_id = db.Column(db.Integer)
    judge_id = db.Column(db.Integer)
    variant = db.Column(db.Integer)
    pages = db.Column(db.Integer)
    is_imported = db.Column(db.Integer)
    is_hidden = db.Column(db.Integer)
    is_readonly = db.Column(db.Integer)
    is_examinable = db.Column(db.Integer)
    mime_type = db.Column(db.String(64))
    examiners0 = db.Column(db.Integer)
    examiners1 = db.Column(db.Integer)
    examiners2 = db.Column(db.Integer)
    exam_score0 = db.Column(db.Integer)
    exam_score1 = db.Column(db.Integer)
    exam_score2 = db.Column(db.Integer)
    last_change_time = db.Column(db.DateTime)
    last_change_nsec = db.Column(db.Integer)
    is_marked = db.Column(db.Integer)
    is_saved = db.Column(db.Integer)
    saved_status = db.Column(db.Integer)
    saved_score = db.Column(db.Integer)
    saved_test = db.Column(db.Integer)
    passed_mode = db.Column(db.Integer)
    eoln_type = db.Column(db.Integer)
    store_flags = db.Column(db.Integer)
    token_flags = db.Column(db.Integer)
    token_count = db.Column(db.Integer)

    comments = db.relationship('Comment', backref=db.backref('comments'))
    user = db.relationship('SimpleUser',
                           backref=db.backref('simpleuser'),
                           uselist=False)
    problem = db.relationship('EjudgeProblem',
                              backref=db.backref('ejudge_runs',
                                                 lazy='dynamic'),
                              uselist=False)

    SIGNAL_DESCRIPTION = {
        1:
        "Hangup detected on controlling terminal or death of controlling process",
        2: "Interrupt from keyboard",
        3: "Quit from keyboard",
        4: "Illegal Instruction",
        6: "Abort signal",
        7: "Bus error (bad memory access)",
        8: "Floating point exception",
        9: "Kill signal",
        11: "Invalid memory reference",
        13: "Broken pipe: write to pipe with no readers",
        14: "Timer signal",
        15: "Termination signal"
    }

    @db.reconstructor
    def init_on_load(self):
        self.out_path = "/home/judges/{0:06d}/var/archive/output/{1}/{2}/{3}/{4:06d}.zip".format(
            self.contest_id, to32(self.run_id // (32**3) % 32),
            to32(self.run_id // (32**2) % 32), to32(self.run_id // 32 % 32),
            self.run_id)
        self._out_arch = None
        self._out_arch_file_names = set()

    @lazy
    def get_audit(self):
        data = safe_open(submit_path(audit_path, self.contest_id, self.run_id),
                         'r').read()
        if type(data) == bytes:
            data = data.decode('ascii')
        return data

    @lazy
    def get_sources(self):
        data = safe_open(
            submit_path(sources_path, self.contest_id, self.run_id),
            'rb').read()
        for encoding in ['utf-8', 'ascii', 'windows-1251']:
            try:
                data = data.decode(encoding)
            except:
                print('decoded:', encoding)
                pass
            else:
                break
        else:
            return 'Ошибка кодировки'
        return data

    def get_output_file(self,
                        test_num,
                        tp="o",
                        size=None):  #tp: o - output, e - stderr, c - checker
        data = self.get_output_archive().getfile("{0:06}.{1}".format(
            test_num, tp)).decode('ascii')
        if size is not None:
            data = data[:size]
        return data

    def get_output_file_size(self,
                             test_num,
                             tp="o"):  #tp: o - output, e - stderr, c - checker
        data = self.get_output_archive().getfile("{0:06}.{1}".format(
            test_num, tp)).decode('ascii')
        return len(data)

    def get_output_archive(self):
        if "output_archive" not in self.__dict__:
            self.output_archive = EjudgeArchiveReader(
                submit_path(output_path, self.contest_id, self.run_id))
        return self.output_archive

    def get_test_full_protocol(self, test_num):
        """
        Возвращает полный протокол по номеру теста
        :param test_num: - str
        """
        judge_info = self.judge_tests_info[test_num]
        test_protocol = self.problem.get_test_full(test_num)

        test_protocol.update(self.tests[test_num])

        test_protocol['big_output'] = False
        try:
            if self.get_output_file_size(int(test_num), tp='o') <= 255:
                test_protocol['output'] = self.get_output_file(int(test_num),
                                                               tp='o')
            else:
                test_protocol['output'] = self.get_output_file(
                    int(test_num), tp='o', size=255) + '...\n'
                test_protocol['big_output'] = True
        except OSError as e:
            test_protocol['output'] = judge_info.get('output', '')

        try:
            if self.get_output_file_size(int(test_num), tp='c') <= 255:
                test_protocol['checker_output'] = self.get_output_file(
                    int(test_num), tp='c')
            else:
                test_protocol['checker_output'] = self.get_output_file(
                    int(test_num), tp='c', size=255) + '...\n'
        except OSError as e:
            test_protocol['checker_output'] = judge_info.get('checker', '')

        try:
            if self.get_output_file_size(int(test_num), tp='e') <= 255:
                test_protocol['error_output'] = self.get_output_file(
                    int(test_num), tp='e')
            else:
                test_protocol['error_output'] = self.get_output_file(
                    int(test_num), tp='e', size=255) + '...\n'
        except OSError as e:
            test_protocol['error_output'] = judge_info.get('stderr', '')

        if 'term-signal' in judge_info:
            test_protocol['extra'] = 'Signal %(signal)s. %(description)s' % {
                'signal': judge_info['term-signal'],
                'description':
                self.SIGNAL_DESCRIPTION[judge_info['term-signal']],
            }
        if 'exit-code' in judge_info:
            test_protocol['extra'] = test_protocol.get(
                'extra', '') + '\n Exit code %(exit_code)s. ' % {
                    'exit_code': judge_info['exit-code']
                }

        for type_ in [('o', 'output'), ('c', 'checker_output'),
                      ('e', 'error_output')]:
            file_name = '{0:06d}.{1}'.format(int(test_num), type_[0])
            if self._out_arch is None:
                try:
                    self._out_arch = zipfile.ZipFile(self.out_path, 'r')
                    self._out_arch_file_names = set(self._out_arch.namelist())
                except:
                    pass
            if file_name not in self._out_arch_file_names or type_[
                    1] in test_protocol:
                continue
            with self._out_arch.open(file_name, 'r') as f:
                test_protocol[
                    type_[1]] = f.read(1024).decode("utf-8") + "...\n"

        return test_protocol

    def parsetests(self):
        """
        Parse tests data from xml archive
        """
        self.test_count = 0
        self.tests = {}
        self.judge_tests_info = {}
        self.status_string = None
        self.compiler_output = None
        self.host = None
        self.maxtime = None

        if self.xml:
            rep = self.xml.getElementsByTagName('testing-report')[0]
            self.tests_count = int(rep.getAttribute('run-tests'))
            self.status_string = rep.getAttribute('status')

            compiler_output_elements = self.xml.getElementsByTagName(
                'compiler_output')
            if compiler_output_elements:
                self.compiler_output = getattr(
                    compiler_output_elements[0].firstChild, 'nodeValue', '')

            host_elements = self.xml.getElementsByTagName('host')
            if host_elements:
                self.host = host_elements[0].firstChild.nodeValue

            for node in self.xml.getElementsByTagName('test'):
                number = node.getAttribute('num')
                status = node.getAttribute('status')
                time = node.getAttribute('time')
                real_time = node.getAttribute('real-time')
                max_memory_used = node.getAttribute('max-memory-used')
                self.test_count += 1

                try:
                    time = int(time)
                except ValueError:
                    time = 0

                try:
                    real_time = int(real_time)
                except ValueError:
                    real_time = 0

                test = {
                    'status': status,
                    'string_status': get_string_status(status),
                    'real_time': real_time,
                    'time': time,
                    'max_memory_used': max_memory_used,
                }
                judge_info = {}

                for _type in ('input', 'output', 'correct', 'stderr',
                              'checker'):
                    lst = node.getElementsByTagName(_type)
                    if lst and lst[0].firstChild:
                        judge_info[_type] = lst[0].firstChild.nodeValue
                    else:
                        judge_info[_type] = ''

                if node.hasAttribute('term-signal'):
                    judge_info['term-signal'] = int(
                        node.getAttribute('term-signal'))
                if node.hasAttribute('exit-code'):
                    judge_info['exit-code'] = int(
                        node.getAttribute('exit-code'))

                self.judge_tests_info[number] = judge_info
                self.tests[number] = test
            try:
                #print([test['time'] for test in self.tests.values()] + [test['real_time'] for test in self.tests.values()])
                self.maxtime = max(
                    [test['time'] for test in self.tests.values()] +
                    [test['real_time'] for test in self.tests.values()])
            except ValueError:
                pass

    @staticmethod
    def get_by(run_id, contest_id):
        try:
            return db.session.query(EjudgeRun).filter(
                EjudgeRun.run_id == int(run_id)).filter(
                    EjudgeRun.contest_id == int(contest_id)).first()
        except:
            return None

    @lazy
    def _get_compilation_protocol(self):
        filename = submit_path(protocols_path, self.contest_id, self.run_id)
        if filename:
            if os.path.isfile(filename):
                myopen = lambda x, y: open(x, y, encoding='utf-8')
            else:
                filename += '.gz'
                myopen = gzip.open
            try:
                xml_file = myopen(filename, 'r')
                try:
                    res = xml_file.read()
                    try:
                        res = res.decode('cp1251').encode('utf8')
                    except:
                        pass

                    try:
                        return str(res, encoding='UTF-8')
                    except TypeError:
                        try:
                            res = res.decode('cp1251').encode('utf8')
                        except Exception:
                            pass
                        return res
                except Exception as e:
                    return e
            except IOError as e:
                return e
        else:
            return ''

    @lazy
    def _get_protocol(self):
        filename = submit_path(protocols_path, self.contest_id, self.run_id)
        if filename != '':
            return get_protocol_from_file(filename)
        else:
            return '<a></a>'

    protocol = property(_get_protocol)
    compilation_protocol = property(_get_compilation_protocol)

    @lazy
    def fetch_tested_protocol_data(self):
        self.xml = xml.dom.minidom.parseString(str(self.protocol))
        self.parsetests()

    def _set_output_archive(self, val):
        self.output_archive = val

    def get_pynformatics_run(self):
        if self.pynformatics_run:
            return self.pynformatics_run

        pynformatics_run = PynformaticsRun(
            run=self,
            source=self.get_sources(),
        )
        db.session.add(pynformatics_run)
        return pynformatics_run

    def serialize(self, attributes=None):
        if not attributes:
            attributes = (
                'run_id',
                'contest_id',
                'create_time',
                'lang_id',
                'prob_id',
                'score',
                'size',
                'status',
                'problem_id',
            )

        serialized = attrs_to_dict(self, *attributes)

        if 'create_time' in attributes:
            serialized['create_time'] = str(serialized['create_time'])

        if 'problem_id' in attributes:
            serialized['problem_id'] = self.problem.id

        serialized.update(self.get_pynformatics_run().serialize())

        user = getattr(g, 'user', None)
        if not user or user.ejudge_id != self.user.ejudge_id:
            serialized['user'] = self.user.serialize()

        return serialized
Exemple #10
0
class Statement(CourseModuleInstance, db.Model):
    __table_args__ = {'schema': 'moodle'}
    __tablename__ = 'mdl_statements'
    __mapper_args__ = {
        'polymorphic_identity': 'statement',
        'concrete': True,
    }

    MODULE = 19

    id = db.Column(db.Integer, primary_key=True)
    course_id = db.Column('course', db.Integer,
                          db.ForeignKey('moodle.mdl_course.id'))
    name = db.Column(db.Unicode(255))
    summary = db.Column(MEDIUMTEXT)
    numbering = db.Column(db.Integer)
    disable_printing = db.Column('disableprinting', db.Boolean)
    custom_titles = db.Column('customtitles', db.Boolean)
    time_created = db.Column('timecreated', db.Integer)
    time_modified = db.Column('timemodified', db.Integer)
    contest_id = db.Column(db.Integer)
    time_start = db.Column('timestart', db.Integer)
    time_stop = db.Column('timestop', db.Integer)
    olympiad = db.Column(db.Boolean)
    virtual_olympiad = db.Column(db.Boolean)
    virtual_duration = db.Column(db.Integer)
    settings = db.Column(JsonType)

    course = db.relationship('Course',
                             backref=db.backref('statements', lazy='dynamic'))

    problems = association_proxy('StatementProblems', 'problem')
    user = association_proxy('StatementUsers1', 'user')

    SETTINGS_SCHEMA = {
        'type': 'object',
        'properties': {
            'allowed_languages': {
                'type': 'array',
                'uniqueItems': True,
                'items': {
                    'type': 'integer',
                    'enum': list(LANG_NAME_BY_ID.keys()),
                }
            },
            'type': {
                'oneOf': [{
                    'type': 'null',
                }, {
                    'type': 'string',
                    'enum': [
                        'olympiad',
                        'virtual',
                    ],
                }],
            },
            'group': {
                'type': 'integer',
            },
            'team': {
                'type': 'boolean',
            },
            'time_start': {
                'type': 'integer',
            },
            'time_stop': {
                'type': 'integer',
            },
            'freeze_time': {
                'type': 'integer',
            },
            'standings': {
                'type': 'boolean',
            },
            'test_only_samples': {
                'type': 'boolean',
            },
            'reset_submits_on_start': {
                'type': 'boolean',
            },
            'test_until_fail': {
                'type': 'boolean',
            },
            'start_from_scratch': {
                'type': 'boolean',
            },
            'restrict_view': {
                'type': 'boolean',
            }
        },
        'additionalProperties': False,
    }
    SETTINGS_SCHEMA_VALIDATOR = Draft4Validator(SETTINGS_SCHEMA)

    def get_allowed_languages(self):
        if not (self.settings and 'allowed_languages' in self.settings):
            return None
        return self.settings['allowed_languages']

    def set_settings(self, settings):
        validation_error = next(
            self.SETTINGS_SCHEMA_VALIDATOR.iter_errors(settings), None)
        if validation_error:
            raise ValueError(validation_error.message)
        self.settings = settings

        if settings.get('time_start'):
            self.time_start = settings['time_start']

        if settings.get('time_stop'):
            self.time_stop = settings['time_stop']

        if 'type' in settings:
            type_ = settings['type']
            if type_ == None:
                self.olympiad = False
                self.virtual_olympiad = False
            elif type_ == 'olympiad':
                self.olympiad = True
                self.virtual_olympiad = False
            else:
                self.olympiad = False
                self.virtual_olympiad = True

        self.time_modified = int(time.time())

    def start_participant(
        self,
        user,
        duration,
        password=None,
    ):
        if self.course \
                and self.course.require_password() \
                and password != self.course.password:
            raise ValueError()

        if self.participants.filter(Participant.user_id == user.id).count():
            raise ValueError()

        if user.get_active_participant():
            raise ValueError()

        new_participant = Participant(
            user_id=user.id,
            statement_id=self.id,
            start=int(time.time()),
            duration=duration,
        )
        db.session.add(new_participant)

        return new_participant

    def finish_participant(self, user):
        active_participant = user.get_active_participant()
        if not active_participant or active_participant.statement_id != self.id:
            raise ValueError()

        active_participant.duration = int(time.time() -
                                          active_participant.start)
        return active_participant

    def start(
        self,
        user,
        password=None,
    ):
        if not self.olympiad:
            raise ValueError()

        now = time.time()
        if now < self.time_start:
            raise ValueError()
        if now >= self.time_stop:
            raise ValueError()

        return self.start_participant(
            user=user,
            duration=self.time_stop - int(time.time()),
            password=password,
        )

    def finish(self, user):
        if not self.olympiad:
            raise ValueError()

        return self.finish_participant(user)

    def serialize(self, attributes=None):
        if not attributes:
            attributes = (
                'id',
                'name',
                'olympiad',
                'settings',
                'time_start',
                'time_stop',
                'virtual_olympiad',
                'virtual_duration',
                'course_module_id',
                'course',
                'require_password',
            )
        serialized = attrs_to_dict(self, *attributes)
        if 'course' in attributes and self.course:
            serialized['course'] = self.course.serialize()
        serialized['course_module_id'] = getattr(self.course_module, 'id',
                                                 None)

        if 'require_password' in attributes:
            if self.course:
                serialized['require_password'] = self.course.require_password()
            else:
                serialized['require_password'] = False

        user = getattr(g, 'user', None)
        if self.olympiad or self.virtual_olympiad:
            if not user:
                return serialized

            try:
                participant = self.participants.filter_by(
                    user_id=user.id).one()
            except NoResultFound:
                return serialized

            serialized['participant'] = participant.serialize()

        serialized['problems'] = {
            rank: {
                'id': statement_problem.problem.id,
                'name': statement_problem.problem.name,
            }
            for rank, statement_problem in self.StatementProblems.items()
            if statement_problem.problem and not statement_problem.hidden
        }
        return serialized