Пример #1
0
 def create(self, token, school: SchoolModel) -> SchoolModel:
     """
     Create a new school
     """
     headers = self.create_default_header(token)
     data = school.json()
     url = self.url_for("/schools/")
     response = self.requests.post(url, data=data, headers=headers)
     self.check_response_status(response, expected_status=201)
     return SchoolModel.parse_obj(response.json())
Пример #2
0
    def update(self, token, school: SchoolModel) -> SchoolModel:
        """
        Update an existing school.
        If successfull, a new school model is returned with the latest version of the
        school data.
        """

        headers = self.create_default_header(token)
        url = self.url_for(f"/school/{school.id}")
        response = self.requests.put(url, data=school.json(), headers=headers)
        self.check_response_status(response, expected_status=200)

        return SchoolModel.parse_obj(response.json())
Пример #3
0
def update(school_id: int):
    """
    Update the data of an existing school.
    """
    service: srv.SchoolService = digicubes.school
    token = digicubes.token
    form = SchoolForm()
    db_school: SchoolModel = service.get(token, school_id)

    # What about the creation date and the modified date?
    if form.is_submitted():
        if form.validate({"name": [SchoolNameAvailable(school_id=school_id)]}):
            upschool = SchoolModel.parse_obj(form.data)
            upschool.id = school_id
            digicubes.school.update(token, upschool)

            return redirect(url_for("school.get", school_id=school_id))
    else:
        # This is the request to display the form with
        # the current data ofthe school
        form = SchoolForm(obj=db_school)

    form.submit.label.text = "Update"

    return render_template(
        "school/update_school.jinja",
        form=form,
        school=db_school,
        action=url_for("school.update", school_id=db_school.id),
    )
Пример #4
0
 def _get_space_schools(self, token, user: UserModel,
                        space: str) -> List[SchoolModel]:
     headers = self.create_default_header(token)
     url = self.url_for(f"/user/{user.id}/{space}/schools/")
     response = self.requests.get(url, headers=headers)
     self.check_response_status(response, expected_status=200)
     return [SchoolModel.parse_obj(school) for school in response.json()]
Пример #5
0
def school_add_teacher(school_id: int):

    form = SimpleTextForm()

    if form.is_submitted():
        if form.validate():
            login = form.login.data
            # Check if a user with the given login exists
            user = server.user.get_by_login_or_none(server.token, login)
            if user is None:
                flash("No such user")
            elif server.school.add_teacher(server.token,
                                           SchoolModel(id=school_id), user):
                flash("Teacher added successfully")
                return redirect(url_for("school.get", school_id=school_id))
            else:
                # Now lets see, if the user has not the right role.
                if server.user.has_role(server.token, user, "teacher"):
                    flash("Teacher not added. No reason.")
                else:
                    flash("User is not a teacher.")

    return render_template(
        "admin/school_add_teacher.jinja",
        form=form,
        action=url_for("admin.school_add_teacher", school_id=school_id),
        teacher=server.school.get_school_teacher(server.token, school_id),
    )
Пример #6
0
def update(school_id: int, course_id: int):
    """
    Update an existing course
    """
    service: srv.SchoolService = digicubes.school
    token = digicubes.token
    form = CourseForm()

    # First get the course to be updated
    service.get_courses(token, SchoolModel(id=school_id))

    # if method is post and all fields are valid,
    # create the new course.
    if form.validate_on_submit():

        course = CourseModel.parse_obj(form.data)
        course.school_id = int(course.school_id)
        course.id = int(course_id)
        service.update_course(token, course)
        return redirect(url_for("school.get", school_id=school_id))

    school_proxy: SchoolModel = service.get(token, school_id)
    course: CourseModel = service.get_course(token, course_id)
    action_url = url_for("course.update", school_id=school_proxy.id, course_id=course.id)
    form = CourseForm(obj=course)
    form.submit.label.text = "Update"
    return render_template(
        "course/update_course.jinja",
        school=school_proxy,
        course=course,
        form=form,
        action=action_url,
    )
Пример #7
0
 def delete(self, token, school_id: int) -> Optional[SchoolModel]:
     """
     Deletes a school from the database
     """
     headers = self.create_default_header(token)
     url = self.url_for(f"/school/{school_id}")
     response = self.requests.delete(url, headers=headers)
     self.check_response_status(response, expected_status=200)
     return SchoolModel.parse_obj(response.json())
Пример #8
0
def get_all():
    """
    Display all schools
    """
    school_list = digicubes.school.all(digicubes.token)

    if requested_html():
        return render_template("school/schools.jinja", schools=school_list)

    return SchoolModel.list_model(school_list).json()
Пример #9
0
    def rfc_school_get_course_info(data: DataType) -> RfcResponse:
        school_id = data.get("school_id", None)
        assert school_id is not None, "No school id provided"
        courses = server.school.get_courses(server.token, SchoolModel(id=school_id))
        # TODO: An dieser stelle brauche ich nicht alle Felder der
        # Kurse. Aber die Methode get_courses unterstützt das

        private_courses = list([c.id for c in courses if c.is_private])
        return RfcResponse(
            data={"count_courses": len(courses), "count_private_courses": len(private_courses)}
        )
Пример #10
0
    def get_by_name(self, token: str, name: str) -> SchoolModel:
        """
        Get a single school by the name.

        Endpoint: /school/byname/{name}
        Method: GET
        """
        response = self.requests.get(self.url_for(f"/school/byname/{name}"),
                                     headers=self.create_default_header(token))
        self.check_response_status(response, expected_status=200)
        return SchoolModel.parse_obj(response.json())
Пример #11
0
    def all(self, token) -> SchoolList:
        """
        Returns all schools.
        The result is a list of ``SchoolModel`` objects.
        """
        headers = self.create_default_header(token)
        url = self.url_for("/schools/")
        response = self.requests.get(url, headers=headers)

        self.check_response_status(response, expected_status=200)

        return [SchoolModel.parse_obj(school) for school in response.json()]
Пример #12
0
def school_remove_teacher(school_id: int, teacher_id: int):
    try:
        success = server.school.remove_teacher(server.token,
                                               SchoolModel(id=school_id),
                                               UserModel(id=teacher_id))
        if success:
            flash("Teacher removed")
        else:
            flash("Could not remove teacher.")

        return redirect(url_for("school.get", school_id=school_id))
    except Exception:
        logger.exception("Could not remove teacher from school")
        return redirect(url_for("school.get", school_id=school_id))
Пример #13
0
    def get(self,
            token,
            school_id: int,
            fields: XFieldList = None) -> Optional[SchoolModel]:
        """
        Get a single school specified by its id

        Requested Endpoint: `/school/{school_id}`
        """
        headers = self.create_default_header(token)
        if fields is not None:
            headers[self.X_FILTER_FIELDS] = ",".join(fields)

        url = self.url_for(f"/school/{school_id}")
        response = self.requests.get(url, headers=headers)
        self.check_response_status(response, expected_status=200)

        return SchoolModel.parse_obj(response.json())
Пример #14
0
def create():
    """Create a new school"""
    # token = digicubes.token
    form = SchoolForm()
    if form.is_submitted():
        if form.validate({"name": [SchoolNameAvailable()]}):
            new_school = SchoolModel(
                name=form.name.data,
                description=form.description.data,
            )
            digicubes.school.create(digicubes.token, new_school)

            # Now lets see, if the current user has the "headmaster" role.
            # if so, make him a headmaster for the newly created school.
            return redirect(url_for("school.get_all"))

    form.submit.label.ttext = "Create"
    return render_template("school/create_school.jinja",
                           form=form,
                           action=url_for("school.create"))
Пример #15
0
def create(school_id: int):
    """
    Create a new course for the school

    The method GET will render the creation form, while
    the method POST will validate the form and create the
    course, if possible.

    If any errors occure during the validation of the form,
    the form will be rerendered.
    """

    # Create the form instance
    form: CourseForm = CourseForm()

    # if method is post and all fields are valid,
    # create the new course.
    if form.validate_on_submit():

        new_course = CourseModel.parse_obj(form.data)
        new_course.created_by_id = current_user.id

        # Create the course.
        # TODO: Errors may occoure and have to be handled in a proper way.
        server.school.create_course(server.token, SchoolModel(id=school_id), new_course)

        # After succesfully creating the course go back to
        # the administration page of the school.
        return redirect(url_for("school.get", school_id=school_id))

    # Get the school from the server and render out the form.
    token: str = server.token
    school_model: SchoolModel = server.school.get(token, school_id)
    form.submit.label.text = "Create"
    action_url = url_for("course.create", school_id=school_model.id)
    return render_template(
        "course/create_course.jinja",
        school=school_model,
        form=form,
        action=action_url,
    )