Example #1
0
    def is_accessible(self):
        if not current_user_is_logged():
            return False

        if not current_user_is_superuser():
            return False

        return True
Example #2
0
def article(article_id, slug=None):
    a = Article.get_or_404(article_id)
    if not current_user_is_logged() and not a.visible:
        return redirect(url_for("security.login", next=request.path))

    response = make_response(render_template("university/_article.html", article=a, discipline=discipline))

    return response
Example #3
0
    def get(self, article_id, slug=None):
        a = Article.get_or_404(article_id)
        if not current_user_is_logged() and not a.visible:
            return redirect(url_for('security.login', next=request.path))

        response = make_response(
            render_template(
                "university/_article.html",
                article=a,
                discipline=discipline,
            ))

        return response
Example #4
0
def inject_admin():
    data = {}
    message = Message.query.order_by(desc(Message.created_at)).first()
    if current_user_is_logged():
        groups = Group.query.order_by(Group.title).order_by(Group.year, Group.title).all()
        admin_groups = OrderedDict()
        for year in Group.active_years():
            admin_groups[year] = [group for group in groups if group.year == year]

        data = {
            'current_year': Group.current_year(),
            'admin_groups': admin_groups,
            'admin_disciplines': Discipline.query.all(),
        }
    data.update({
        'message': message,
    })
    return data
Example #5
0
def inject_admin():
    data = {}
    message = Message.query.order_by(desc(Message.created_at)).first()
    if current_user_is_logged():
        groups = Group.query.order_by(Group.title).order_by(Group.year, Group.title).all()
        admin_groups = OrderedDict()
        for year in Group.active_years():
            admin_groups[year] = [group for group in groups if group.year == year]

        settings = Setting.instance()

        data = {
            'COMPLEX_CHOICES': Task.COMPLEX_CHOICES,
            'current_year': settings.active_year,
            'site_disabled': settings.site_disabled,
            'admin_groups': admin_groups,
            'admin_disciplines': Discipline.query.all(),
        }
    data.update({
        'message': message,
    })
    return data
Example #6
0
def group_marks(group_id, slug=None, discipline_id=None):
    group = Group.query.get_or_404(group_id)

    if current_user_is_logged():
        disciplines = Discipline.query
    else:
        disciplines = group.disciplines

    if not discipline_id:
        discipline_id = request.cookies.get("discipline_id", None)
        if not discipline_id:
            discipline = disciplines.first()
            if discipline:
                discipline_id = discipline.id
            else:
                return redirect(url_for("university.index"))

    discipline = (
        disciplines.filter(Discipline.id == discipline_id)
        .options(joinedload("labs"), joinedload("files"), joinedload("articles"))
        .first()
    )
    if discipline is None:
        discipline = disciplines.first()

    # if not discipline and user is not logged then redirect
    if not discipline and not current_user_is_logged():
        return redirect(url_for("security.login", next=request.path))

    if group.year != Group.current_year() and not current_user_is_logged():
        return redirect(url_for("security.login", next=request.path))

    def make_cache_key():
        return cache_key_for_students_marks(group.id, discipline.id)

    def get_all_data(group, discipline):
        lessons = (
            Lesson.query.filter(Lesson.group_id == group.id)
            .filter(Lesson.discipline_id == discipline.id)
            .order_by(Lesson.date, Lesson.id)
            .all()
        )

        students = (
            group.students.outerjoin(Student.marks)
            .options(contains_eager(Student.marks))
            .filter(or_(Mark.lesson_id == None, Mark.lesson_id.in_([i.id for i in lessons])))
            .all()
        )

        labs = discipline.labs

        students_info = {}
        for student in students:
            student_info = {"marks": {}, "tasks": {}, "points": 0, "percents": 0}
            students_info[student.id] = student_info
            for mark in student.marks:
                student_info["marks"][mark.lesson_id] = mark

            student_info["points"], student_info["percents"] = student.points(
                student_info["marks"],
                lessons,
                sum([len(lab.tasks) for lab in labs if lab.regular and lab.visible]),
                len(student_info["tasks"]),
            )

        return {"students": students, "lessons": lessons, "labs": labs, "students_info": students_info}

    data = get_all_data(group, discipline)

    template = "university/group.html"
    if request.headers.get("X-Pjax", None):
        template = "university/_marks.html"

    response = make_response(
        render_template(
            template,
            group=group,
            discipline=discipline,
            students=data["students"],
            lessons=data["lessons"],
            labs=data["labs"],
            articles=[a for a in discipline.articles if a.visible or current_user_is_logged()],
            disciplines=disciplines.order_by(Discipline.title).all(),
            has_visible_labs=len([lab for lab in data["labs"] if lab.visible]) > 0,
            students_info=data["students_info"],
            lesson_types=Lesson.LESSON_TYPES,
            marks_types=Mark.MARKS,
        )
    )

    response.set_cookie("discipline_id", str(discipline_id))

    return response
Example #7
0
def inject_user():
    return {
        "user": current_user,
        "is_logged": current_user_is_logged()
    }
Example #8
0
def inject_login_form():
    if not current_user_is_logged():
        return {
            "login_user_form": LoginForm()
        }
    return {}
Example #9
0
 def dispatch_request(self, *args, **kwargs):
     settings = Setting.instance()
     if settings.site_disabled and not current_user_is_logged():
         return render_template("disabled.html")
     return super(SiteDisabledMixin, self).dispatch_request(*args, **kwargs)
Example #10
0
    def get(self, group_id, slug=None, discipline_id=None):
        group = Group.query.get_or_404(group_id)

        if current_user_is_logged():
            disciplines = Discipline.query
        else:
            disciplines = group.disciplines

        if not discipline_id:
            discipline_id = request.cookies.get('discipline_id', None)
            if not discipline_id:
                discipline = disciplines.first()
                if discipline:
                    discipline_id = discipline.id
                else:
                    return redirect(url_for("university.index"))

        discipline = disciplines.filter(Discipline.id == discipline_id) \
            .options(joinedload('labs'), joinedload('files'), joinedload('articles')).first()
        if discipline is None:
            discipline = disciplines.first()

        # if not discipline and user is not logged then redirect
        if not discipline and not current_user_is_logged():
            return redirect(url_for('security.login', next=request.path))

        if group.year not in Group.current_year(
        ) and not current_user_is_logged():
            return redirect(url_for('security.login', next=request.path))

        def make_cache_key():
            return cache_key_for_students_marks(group.id, discipline.id)

        def get_all_data(group, discipline):
            lessons = Lesson.query.filter(Lesson.group_id == group.id) \
                .filter(Lesson.discipline_id == discipline.id) \
                .order_by(Lesson.date, Lesson.id).all()

            students = group.students \
                .outerjoin(Student.marks) \
                .options(contains_eager(Student.marks)) \
                .filter(or_(Mark.lesson_id == None, Mark.lesson_id.in_([i.id for i in lessons]))).all()

            labs = discipline.labs

            students_info = {}
            for student in students:
                student_info = {
                    'marks': {},
                    'tasks': {},
                    'points': 0,
                    'percents': 0,
                }
                students_info[student.id] = student_info
                for mark in student.marks:
                    student_info['marks'][mark.lesson_id] = mark

                for task in student.tasks:
                    student_info['tasks'][task.task_id] = task

                student_info['points'], student_info['percents'] \
                    = student.points(student_info['marks'],
                                     lessons,
                                     sum([len([t for t in lab.tasks if not t.ignore]) for lab in labs if lab.regular and lab.visible]),
                                     len(student_info['tasks']))

            return {
                'students': students,
                'lessons': lessons,
                'labs': labs,
                'students_info': students_info
            }

        data = get_all_data(group, discipline)

        template = "university/group.html"
        if request.headers.get('X-Pjax', None):
            template = "university/_marks.html"

        response = make_response(
            render_template(
                template,
                group=group,
                discipline=discipline,
                students=data['students'],
                lessons=data['lessons'],
                labs=data['labs'],
                articles=[
                    a for a in discipline.articles
                    if a.visible or current_user_is_logged()
                ],
                disciplines=disciplines.order_by(Discipline.title).all(),
                has_visible_labs=len(
                    [lab for lab in data['labs'] if lab.visible]) > 0,
                students_info=data['students_info'],
                lesson_types=Lesson.LESSON_TYPES,
                marks_types=Mark.MARKS,
            ))

        response.set_cookie('discipline_id', str(discipline_id))

        return response
Example #11
0
def inject_user():
    return {
        "user": current_user,
        "is_logged": current_user_is_logged()
    }
Example #12
0
def inject_login_form():
    if not current_user_is_logged():
        return {
            "login_user_form": LoginForm()
        }
    return {}