Skip to content

refactor: added print_question method - #7

Open
JanaDrazkova wants to merge 5 commits into
mainfrom
jd-print_method
Open

refactor: added print_question method#7
JanaDrazkova wants to merge 5 commits into
mainfrom
jd-print_method

Conversation

@JanaDrazkova

Copy link
Copy Markdown

No description provided.

@JanaDrazkova JanaDrazkova linked an issue Apr 24, 2026 that may be closed by this pull request
@JanaDrazkova JanaDrazkova changed the title Added print_question method refactor: added print_question method Apr 25, 2026
Comment thread src/quiz_common/models.py
"type": "question",
"text": self.text,
"options": [opt.answer for opt in self.options],
"options": [opt.ask() for opt in self.options],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put this back to not expose the correct value to the admin and client.

Comment thread src/quiz_common/models.py Outdated
"""Return a dictionary representation of the option, used when sending json."""
return {
"answer": self.answer,
"correct": self.correct,

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about changing this line to the default False value, so the actual correctness is still hidden?

Suggested change
"correct": self.correct,
"correct": False,

I look forward to the discussion on Thursday.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can do it like that.

Optionally we can check some method how to adjust the serialization (model_dump) - we can rewrite the correct field there. There should be a possibility of serialization with and without parameters.

@ninabel

ninabel commented May 28, 2026

Copy link
Copy Markdown

Maybe we should use Pydantic serialization approach.
Here https://pydantic.dev/docs/validation/latest/concepts/serialization/#serialization-context is described how to use context for model_dump.
So my suggestion is to add field_serializer method to Option model, something like:

    @field_serializer('correct', mode='plain')
    @classmethod
    def hide_right_answers(cls, v: bool, info: FieldSerializationInfo):
           if info.context == "for players":
               return "false"
           else return str(v)

And then call self.model_dump(context="for players") in the method ask for Question.

@ZelenyMartin

Copy link
Copy Markdown
Contributor

We can try something like that:

@field_serializer('correct', mode='plain')
@classmethod
def hide_right_answers(cls, v: bool, info: FieldSerializationInfo):
    return False if info.context == "for players" else v

Usage: self.model_dump(context="for players")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Move print_question to common module as Question class method

3 participants