Beispiel #1
0
    def view_course(course_name, course_year):
        user = User(session['user'])
        course = Courses().find_one(name=course_name,
                                    year=course_year,
                                    only_active=False)
        problems: List[Problem] = sorted(list(
            course.problem_db.find(disabled=(None, False))),
                                         key=problem_cat_getter)

        languages = Languages.db().find(disabled=(None, False))

        if not user.is_admin():
            problems = [p for p in problems if p.is_visible()]

        cat_problems = OrderedDict()
        for cat, items in groupby(problems, key=problem_cat_getter):
            cat_problems[cat] = list(items)

        return render_template_ext(
            'view_course.njk',
            user=user,
            notifications=Mongo().load_notifications(user.id),
            course=course,
            languages=languages,
            has_categories=len(cat_problems) > 1,
            problems=problems,
            cat_problems=cat_problems,
            title=course.name,
            subtitle=course.year,
            breadcrumbs=Breadcrumbs.new(Link.CoursesBtn(), ),
            js_no_cache=['solution.js'])
Beispiel #2
0
    def decorated_function(*args, **kwargs):
        from database.objects import User
        try:
            user = User(session['user'])
            if not user.is_admin():
                raise Exception('Access denied')
        except:
            return redirect(Env.url_login)

        return f(*args, **kwargs)
Beispiel #3
0
    def view_courses():
        user = User(session['user'])

        courses = list(Courses().find(only_active=not user.is_admin()))

        return render_template_ext('view_courses.njk',
                                   title='Course list',
                                   user=user,
                                   notifications=Mongo().load_notifications(
                                       user.id),
                                   courses=courses)
Beispiel #4
0
    def view_result(course_name, course_year, problem_id, _id=None):
        user = User(session['user'])

        if user.is_admin():
            return redirect(
                url_for('admin_problem',
                        course_name=course_name,
                        course_year=course_year,
                        problem_id=problem_id))

        course = Courses().find_one(name=course_name,
                                    year=course_year,
                                    only_active=False)
        problem = course.problem_db[problem_id]
        results = list()
        result = None
        breadcrumbs = [Link.CoursesBtn(), Link.CourseBtn(course)]

        # TODO check access
        if _id:
            document = Mongo().result_by_id(_id)
            if document:
                # add to previous solution if already executed
                if document.result:
                    results.append(document.peek())
                else:
                    result = document.peek()
                    breadcrumbs.append(Link.ProblemBtn(course, problem))

        if Env.use_database:
            for prev in Mongo().peek_last_n_results(20, user.id, course.id,
                                                    problem.id):
                # push only valid result
                if prev.get('result') and str(prev['_id']) != str(_id):
                    results.append(prev)

        if _id:
            for r in results:
                if str(r['_id']) == str(_id):
                    r['active'] = 'active'

        results = sorted(results, reverse=True, key=lambda x: x.get('attempt'))

        return render_template_ext(
            'results.njk',
            user=user,
            notifications=Mongo().read_notifications(user.id),
            results=results,
            result=result,
            requestReview=True,
            title='Problem %s' % problem.name,
            breadcrumbs=Breadcrumbs.new(*breadcrumbs),
        )
Beispiel #5
0
def student_submit_solution(data):
    print(data)
    user = User(session['user'])

    try:
        type = str(data['type'])
        action = ProcessRequestType(type)
        skip_docker = not data.get('docker', True)
    except:
        Emittor.error('Unsupported action', [
            'Given action is not supported:',
            '    %s' % data['type'], '', 'Please contact [email protected]',
            'if you think this is a mistake.'
        ])
        return

    if not user.is_admin() and (skip_docker or action
                                in (ProcessRequestType.GENERATE_INPUT,
                                    ProcessRequestType.GENERATE_OUTPUT)):
        Emittor.error('Operation not permitted', [
            'You do not have sufficient privileges to perform action:',
            '    %s (skip docker: %s)' %
            (action, skip_docker), '', 'Please contact [email protected]',
            'if you want to gain the privileges.'
        ])
        return

    request = processing.request.ProcessRequest(
        user=user,
        lang=data['lang'],
        problem=data['prob'],
        course=data['course'],
        solution=data['src'],
        type=action,
        docker=False if (skip_docker and user.is_admin()) else True,
    )

    mongo.save_log(request.get_log_dict())

    # ignore problems which are past due
    if request.problem.time_left < 0:
        return

    Emittor.register_events(request)
    Emittor.queue_status(queue_status())

    time.sleep(0.1)
    queue.append(request)
    Emittor.queue_push(request)

    time.sleep(0.1)

    # put a barrier here so only certain amount fo users can process code at once
    # while other will see queue list
    with thread_lock:
        try:
            request.process()
        except ConfigurationException as e:
            if user.is_admin():
                logger.exception('[visible to admin only] invalid yaml config')
                Emittor.exception(e)
        except Exception as e:
            logger.exception('process error:')
            Emittor.exception(e)
        finally:
            mongo.save_result(request.get_result_dict())
            request.save_result()
            request.destroy()

    queue.remove(request)
    Emittor.queue_pop(request)
Beispiel #6
0
    def view_result(course_name, course_year, problem_id, _id=None):
        user = User(session['user'])

        if user.is_admin():
            return redirect(
                url_for('admin_problem',
                        course_name=course_name,
                        course_year=course_year,
                        problem_id=problem_id))

        course = Courses().find_one(name=course_name,
                                    year=course_year,
                                    only_active=False)
        problem = course.problem_db[problem_id]
        results = list()
        result = None
        breadcrumbs = [Link.CoursesBtn(), Link.CourseBtn(course)]

        # TODO check access
        if _id:
            document = Mongo().result_by_id(_id)
            if document:
                # add to previous solution if already executed
                if document.result:
                    results.append(document)
                else:
                    result = document
                    breadcrumbs.append(Link.ProblemBtn(course, problem))

        if Env.use_database:
            for prev in Mongo().peek_last_n_results(10, user.id, course.id,
                                                    problem.id):
                # push only valid result
                if prev.result and str(prev._id) != str(_id):
                    results.append(prev)

        if _id:
            for r in results:
                if str(r._id) == str(_id):
                    r.active = 'active'

        def get_attempt(obj):
            try:
                return int(obj.attempt)
            except:
                return 0

        results = sorted(results, reverse=True, key=get_attempt)

        return render_template_ext(
            'view_result.njk',
            user=user,
            notifications=Mongo().load_notifications(user.id),
            results=results,
            result=result,
            requestReview=True,
            title='Problem %s' % problem.name,
            breadcrumbs=Breadcrumbs.new(*breadcrumbs),
            js=[
                '//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.6/highlight.min.js',
                '/static/js/lib/highlightjs-line-numbers.js'
            ],
            js_no_cache=['sockets.js', 'process.js'])