Skip to content

Commit

Permalink
refactor: split example package code into multiple modules
Browse files Browse the repository at this point in the history
  • Loading branch information
MHajoha committed Nov 27, 2023
1 parent 364f1de commit f02a6f1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 52 deletions.
53 changes: 1 addition & 52 deletions example/python/local/example/__init__.py
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
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:
Expand Down
33 changes: 33 additions & 0 deletions example/python/local/example/form.py
Original file line number Diff line number Diff line change
@@ -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")])
22 changes: 22 additions & 0 deletions example/python/local/example/question_type.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f02a6f1

Please sign in to comment.