Пример #1
0
 def test_segment_creation(self):
     c = self.make_standard_course()
     self.session.add(c)
     lesson = self.make_standard_course_lesson(course=c)
     self.session.add(lesson)
     segment = datamodels.Segment(
         title="Some example segment",
         duration_seconds=120,
         url="https://super-cool.com/kitty.jpg",
         language="EN",
         order=1,
         _thumbnail="https://super-cool.com/kitty.jpg",
         lesson=lesson,
         type="video",
         video_type=datamodels.enums.VideoTypeEnum.interview,
         external_id="123asd123",
         slug="some-example-segment",
         barrier=datamodels.enums.SegmentBarrierEnum.normal
     )
     self.session.add(segment)
     segment = datamodels.get_segment(1)
     self.assertEqual(segment.title, "Some example segment")
     self.assertEqual(segment.duration_seconds, 120)
     self.assertEqual(segment.order, 1)
     self.assertEqual(segment.lesson.title, lesson.title)
     self.assertEqual(segment.type, "video")
Пример #2
0
def course_add_edit_intro_lesson(user, course, course_slug, institute=""):
    form = AjaxCSRFTokenForm(request.form)
    intro_lesson = course.intro_lesson

    if form.validate() and "intro_lesson" in request.form:
        db = datamodels.get_session()

        slug = "intro-lesson"
        if (datamodels.get_lesson_by_slugs(course.slug, "intro-lesson")
                is not None and not intro_lesson):
            slug = slug + "-" + str(uuid4())[:3]
        if intro_lesson:
            segment = intro_lesson.intro_segment
            set_segment_video_url(segment, request.form["segment_url"])
            html = ""
        else:
            intro_lesson = datamodels.Lesson(
                title="Intro lesson",
                slug=slug,
                description="Intro lesson video",
                order=0,
                course=course,
            )

            db.add(intro_lesson)

            segment = datamodels.Segment(
                lesson=intro_lesson,
                order=0,
                type="video",
                title="Introduction",
                barrier=SegmentBarrierEnum.normal,
                video_type=VideoTypeEnum.standard,
                url=request.form["segment_url"],
                duration_seconds=0,
                slug="intro-segment",
            )
            set_segment_video_url(segment, request.form["segment_url"])
            html = render_intro(segment)

        db.add(segment)
        db.commit()
        return jsonify({
            "message":
            "Intro lesson {}".format("updated" if not html else "added"),
            "html":
            html,
        })
    else:
        return jsonify({"message": "Couldn't create intro lesson"}), 400
Пример #3
0
 def make_segment(
     lesson,
     thumbnail="thumbnail_1",
     title="Segment",
     duration_seconds=200,
     url="fitzroyacademy.com",
 ):
     segment = datamodels.Segment(
         title=title,
         duration_seconds=duration_seconds,
         url=url,
         language="EN",
         order=1,
         _thumbnail=thumbnail,
         lesson=lesson,
     )
     return segment
Пример #4
0
 def make_segment(
         lesson,
         thumbnail="https://some_fancy_url_without_thumbnail_1.com/thumbnail.jpg",
         title="Segment",
         duration_seconds=200,
         url="fitzroyacademy.com",
 ):
     segment = datamodels.Segment(
         title=title,
         duration_seconds=duration_seconds,
         url=url,
         language="EN",
         order=1,
         _thumbnail=thumbnail,
         lesson=lesson,
     )
     return segment
Пример #5
0
def add_edit_intro_segment(user, course, course_slug, lesson_id, institute=""):
    lesson = datamodels.Course.find_lesson_by_course_slug_and_id(
        course.slug, lesson_id)
    if not lesson:
        return jsonify({"message": "Lesson do not match course"}), 400

    if not AjaxCSRFTokenForm(request.form).validate():
        return jsonify({"message": "Invalid CSRF token"}), 400

    slug = "intro-video"

    instance = lesson.intro_segment
    editing = instance is not None
    if not instance:
        instance = datamodels.Segment(
            url=request.form["segment_url"],
            type=SegmentType.video,
            video_type=VideoTypeEnum.standard,
            barrier=SegmentBarrierEnum.normal,
            order=0,
            lesson=lesson,
            slug=slug,
            title="Intro segment",
            text=request.form.get("text_segment_content",
                                  "Intro segment video"),
        )

    segment = datamodels.find_segment_by_slugs(course_slug, lesson.slug, slug)
    if segment and segment.order != 0:
        instance.slug = slug + "-" + uuid4()[:3]

    try:
        set_segment_video_url(instance, request.form["segment_url"])
    except ValueError as e:
        return jsonify({"message": str(e)}), 400

    instance.save()
    data = {"message": "Intro video updated"}

    if not editing:
        data = {"message": "Intro video added.", "html": render_intro(segment)}

    return jsonify(data)
Пример #6
0
    def make_segment(lesson,
                     thumbnail="thumbnail_1",
                     title="Segment",
                     duration_seconds=200,
                     url="fitzroyacademy.com/some_external_video_id",
                     order=1,
                     slug=None,
                     seg_type=SegmentType.text,
                     barrier=None,
                     video_type=None,
                     survey_type=None):
        if slug is None:
            slug = slugify(title)
        if barrier is None:
            barrier = SegmentBarrierEnum.normal
        if seg_type == "text":
            video_type = None
        elif seg_type == "survey":
            survey_type = survey_type or "emoji"
        else:
            if video_type is None:
                video_type = VideoTypeEnum.standard

        segment = datamodels.Segment(title=title,
                                     duration_seconds=duration_seconds,
                                     url=url,
                                     language="EN",
                                     order=order,
                                     _thumbnail=thumbnail,
                                     lesson=lesson,
                                     slug=slug,
                                     type=seg_type,
                                     barrier=barrier,
                                     video_type=video_type,
                                     survey_type=survey_type)
        return segment
Пример #7
0
def reseed():
    session = datamodels.get_session()

    for student in stubs.students:
        user = copy.deepcopy(student)
        password = user.pop("password")
        u = datamodels.User(**user)
        u.password = password
        session.add(u)
    session.commit()

    c = datamodels.Course(
        title="Into to Social Enterprise",
        slug="fitzroy-academy",
        course_code="abc123",
        target_audience="Super early stage social enterprise founders, starting your first business or a new project from scratch.",
        skill_level="Introduction.",
        visibility="public",
        info="Start here! Basic business models and customer discovery, to pitching for investment. тЭдя╕П ЁЯЪА",
        workload_summary="This course will take 20-30 hours on average, and is best done in teams.",
        summary_html="""
        <p><strong>Go from zero to one:</strong> From a basic idea to early customers, business models and getting the numbers right.</p>
        <p>We don't need any previous business experience, but by the end you'll cover quite complex topics like financial modelling, </p>
        <p><strong>On the social impact</strong> side of things, you'll define your impact model, look into creating behaviour
        change that lasts, and maybe even think about partnering with another organisation to create impact.</p>
        """,
        cover_image="/static/assets/images/lessons/customer-interviews.jpg",
        guest_access=True,
        draft=False,
        workload_title="",
        workload_subtitle="",
    )
    session.add(c)
    session.commit()

    c.add_instructor(datamodels.get_user(1))  # Homer
    c.add_instructor(datamodels.get_user(2))  # Marge

    c.enroll(datamodels.get_user(3))  # Bart
    c.enroll(datamodels.get_user(4))  # Lisa
    c.enroll(datamodels.get_user(5))  # Maggie
    session.add(c)
    session.commit()

    for i, lesson in enumerate(stubs.lessons):
        lesson = copy.deepcopy(lesson)
        resources = lesson.pop("resources")
        segments = lesson.pop("segments")
        lesson.pop("id")
        lesson.pop("course_id")
        lesson["language"] = "en"
        new_lesson = datamodels.Lesson(**lesson)
        new_lesson.course = c
        c.lessons.append(new_lesson)
        session.add(new_lesson)
        session.commit()
        for j, segment in enumerate(segments):
            segment = copy.deepcopy(segment)
            segment["duration_seconds"] = get_seconds(segment.pop("duration"))
            segment.pop("lesson_id")
            segment.pop("course_id")
            segment.pop("template")  # ultimately derived from external URL
            segment["slug"] = segment.pop("id")
            segment["language"] = "en"
            segment["type"] = SegmentType.video
            s = datamodels.Segment(**segment)
            new_lesson.segments.append(s)
            s.lesson = new_lesson
            session.add(s)
        for j, resource in enumerate(resources):
            resource = copy.deepcopy(resource)
            resource["language"] = resource.pop("lang")
            resource["slug"] = resource.pop("id")
            resource["order"] = j
            r = datamodels.Resource(**resource)
            r.lesson = new_lesson
            new_lesson.resources.append(r)
            session.add(r)
        session.commit()

    for user_progress in stubs.user_segment_progress:
        user = datamodels.User.find_by_email(user_progress["email"])
        for lesson in user_progress["lessons"]:
            for slug, progress in lesson["progress"].items():
                # we can search segments by slug because in stubs segments slugs are unique
                segment = (
                    datamodels.Segment.objects()
                    .filter(datamodels.Segment.slug == slug)
                    .first()
                )
                datamodels.SegmentUserProgress.save_user_progress(
                    segment.id, user.id, progress
                )

    # default institute, whitout subdomain
    fitz_institute = datamodels.Institute(name="Fitzroyacademy", logo="", slug="")

    session.add(fitz_institute)
    session.commit()

    fitz_institute.add_admin(datamodels.get_user(1))  # Homer

    institute1 = datamodels.Institute(name="Jedi Temple", logo="", slug="jeditemple")

    session.add(institute1)
    session.commit()

    institute1.add_admin(datamodels.get_user(3))  # Bart
    session.commit()
Пример #8
0
def add_edit_segment(
    user,
    course,
    course_slug,
    lesson_id,
    content_type=None,
    segment_id=None,
    institute="",
):
    if (content_type not in ["text", "video", "intro_video", "survey"]
            and not segment_id):
        return jsonify({"message": "Wrong action"}), 400

    form = AddSegmentForm(request.form)
    if not form.validate():
        return jsonify({"message": form.errors}), 400

    lesson = datamodels.Course.find_lesson_by_course_slug_and_id(
        course.slug, lesson_id)
    if not lesson:
        return jsonify({"message": "Lesson do not match course"}), 400

    if segment_id:  # Case of an existing segment
        instance = datamodels.Segment.find_by_id(segment_id)
        if not instance or instance.lesson_id != lesson.id:
            return jsonify({"message": "No such segment for this lesson"}), 400
        content_type = instance.type.name
    else:  # Case of a new segment
        instance = datamodels.Segment()
        instance.lesson_id = lesson.id
        instance.duration_seconds = 0

        if content_type == "intro_video":
            if datamodels.Segment.first(lesson_id=lesson_id, order=0):
                return jsonify({"message":
                                "Intro segment already exists"}), 400
            else:
                instance.order = 0
        else:
            last_segment = lesson.last_child(instance)
            instance.order = last_segment.order + 1 if last_segment else 1

    segment_name = form.segment_name.data
    slug = slugify(segment_name)
    if not (slug and (datamodels.find_segment_by_slugs(
            course.slug, lesson.slug, slug) is None or slug == instance.slug)):
        return jsonify({"message":
                        "Can't create segment with such name."}), 400

    barrier = getattr(SegmentBarrierEnum,
                      request.form.get("permissions", "normal"), "normal")
    instance.barrier = barrier
    instance.title = request.form["segment_name"]
    instance.slug = slug

    if content_type == "text":
        if ("text_segment_content" not in request.form
                or not request.form["text_segment_content"]):
            return jsonify({"message": "Segment description is required"}), 400

        instance.text = request.form["text_segment_content"]
        instance.type = SegmentType.text
    elif content_type in ["video", "video_intro"]:
        if "segment_url" not in request.form or not request.form["segment_url"]:
            return jsonify({"message": "Segment URL is requied"}), 400

        video_type = getattr(VideoTypeEnum,
                             request.form.get("video_types", "standard"),
                             "standard")

        try:
            set_segment_video_url(instance, request.form["segment_url"])
        except ValueError as e:
            return jsonify({"message": str(e)}), 400
        instance.type = SegmentType.video
        instance.video_type = video_type
    elif content_type == "survey":
        survey_id = request.form.get("survey_types", None)
        instance.type = SegmentType.survey
        if not survey_id:
            return jsonify({"message": "Survey type not provided."}), 400

        reader = SurveyViewInterface(survey_id=survey_id)
        template = reader.read_questions_template_from_view(data=request.form)
        try:
            instance.save_questions_template(template)
        except (AssertionError, ValueError) as e:
            msg = str(e)
            return jsonify({"message":
                            msg if msg else "Some error occurred"}), 400
    else:
        return jsonify({"message": "Content not supported"}), 400

    try:
        instance.save()
    except ValidationError:
        return jsonify({"message":
                        "Chose different name for this segment."}), 400

    response = {
        "message":
        "Segment {} {}".format(instance.title,
                               "edited" if segment_id else "added"),
        "html":
        render_segment_list_element(course=course,
                                    lesson=lesson,
                                    segment=instance),
        "id":
        instance.id,
    }

    return jsonify(response), 200