From f02a6f183efb05f8000d07a3ddfae233e4d8cd21 Mon Sep 17 00:00:00 2001 From: Maximilian Haye Date: Mon, 27 Nov 2023 13:31:53 +0100 Subject: [PATCH] refactor: split example package code into multiple modules --- example/python/local/example/__init__.py | 53 +------------------ example/python/local/example/form.py | 33 ++++++++++++ example/python/local/example/question_type.py | 22 ++++++++ 3 files changed, 56 insertions(+), 52 deletions(-) create mode 100644 example/python/local/example/form.py create mode 100644 example/python/local/example/question_type.py diff --git a/example/python/local/example/__init__.py b/example/python/local/example/__init__.py index 8432bb82..b8b89a1a 100644 --- a/example/python/local/example/__init__.py +++ b/example/python/local/example/__init__.py @@ -1,59 +1,8 @@ # This file is part of the QuestionPy SDK. (https://questionpy.org) # The QuestionPy SDK is free software released under terms of the MIT license. See LICENSE.md. # (c) Technische Universität Berlin, innoCampus -from importlib.resources import files -from questionpy_common.models import QuestionModel, ScoringMethod, AttemptModel, AttemptUi - -from questionpy import QuestionType, Attempt, Question, BaseQuestionState, BaseAttemptState -from questionpy.form import * - - -class NameGroup(FormModel): - first_name = text_input("First Name") - last_name = text_input("Last Name") - - -class MyOptions(OptionEnum): - OPT_1 = option("Option 1", selected=True) - OPT_2 = option("Option 2") - - -class Participants(FormModel): - role = select("Role:", MyOptions) - name = group("Name", NameGroup) - - -class MyModel(FormModel): - static = static_text("Some static text", """Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam - nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam - et justo duo dolores et ea rebum.""") - input = text_input("My second Input", required=True, hide_if=[is_checked("chk")]) - chk = checkbox("Left label", None, help="""Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam - nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.""") - radio = radio_group("My Radio Group", MyOptions) - my_select = select("My select box", MyOptions, multiple=True) - my_hidden = hidden("foo") - my_repetition = repeat(Participants) - - has_name = checkbox(None, "I have a name.") - name_group = group("Name", NameGroup, disable_if=[is_not_checked("has_name")]) - - -class ExampleAttempt(Attempt["ExampleQuestion", BaseAttemptState]): - def export(self) -> AttemptModel: - return AttemptModel(variant=1, ui=AttemptUi( - content=(files(__package__) / "multiple-choice.xhtml").read_text() - )) - - -class ExampleQuestion(Question[BaseQuestionState[MyModel], ExampleAttempt]): - def export(self) -> QuestionModel: - return QuestionModel(scoring_method=ScoringMethod.AUTOMATICALLY_SCORABLE) - - -class ExampleQuestionType(QuestionType[MyModel, ExampleQuestion]): - pass +from .question_type import ExampleQuestionType def init() -> ExampleQuestionType: diff --git a/example/python/local/example/form.py b/example/python/local/example/form.py new file mode 100644 index 00000000..5e993665 --- /dev/null +++ b/example/python/local/example/form.py @@ -0,0 +1,33 @@ +from questionpy.form import FormModel, text_input, OptionEnum, option, select, group, static_text, is_checked,\ + checkbox, radio_group, hidden, repeat, is_not_checked + + +class NameGroup(FormModel): + first_name = text_input("First Name") + last_name = text_input("Last Name") + + +class MyOptions(OptionEnum): + OPT_1 = option("Option 1", selected=True) + OPT_2 = option("Option 2") + + +class Participants(FormModel): + role = select("Role:", MyOptions) + name = group("Name", NameGroup) + + +class MyModel(FormModel): + static = static_text("Some static text", """Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam + nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam + et justo duo dolores et ea rebum.""") + input = text_input("My second Input", required=True, hide_if=[is_checked("chk")]) + chk = checkbox("Left label", None, help="""Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam + nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.""") + radio = radio_group("My Radio Group", MyOptions) + my_select = select("My select box", MyOptions, multiple=True) + my_hidden = hidden("foo") + my_repetition = repeat(Participants) + + has_name = checkbox(None, "I have a name.") + name_group = group("Name", NameGroup, disable_if=[is_not_checked("has_name")]) diff --git a/example/python/local/example/question_type.py b/example/python/local/example/question_type.py new file mode 100644 index 00000000..ad51042b --- /dev/null +++ b/example/python/local/example/question_type.py @@ -0,0 +1,22 @@ +from importlib.resources import files + +from questionpy_common.models import AttemptModel, AttemptUi, QuestionModel, ScoringMethod + +from questionpy import Attempt, BaseAttemptState, Question, BaseQuestionState, QuestionType +from .form import MyModel + + +class ExampleAttempt(Attempt["ExampleQuestion", BaseAttemptState]): + def export(self) -> AttemptModel: + return AttemptModel(variant=1, ui=AttemptUi( + content=(files(__package__) / "multiple-choice.xhtml").read_text() + )) + + +class ExampleQuestion(Question[BaseQuestionState[MyModel], ExampleAttempt]): + def export(self) -> QuestionModel: + return QuestionModel(scoring_method=ScoringMethod.AUTOMATICALLY_SCORABLE) + + +class ExampleQuestionType(QuestionType[MyModel, ExampleQuestion]): + pass