Changeset 0792ddd in flexograder for gui/gui.py


Ignore:
Timestamp:
11/20/25 13:15:40 (5 months ago)
Author:
Enrico Schwass <ennoausberlin@…>
Branches:
fake-data, main, master
Children:
e75910d
Parents:
aaaa4b8
Message:

new entity creation rules enforced

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gui/gui.py

    raaaa4b8 r0792ddd  
    55from pathlib import Path
    66from datetime import datetime
    7 from flexoentity import FlexOID, logger
     7from flexoentity import FlexOID, DomainManager, logger, Domain
    88from builder.exam import Exam
    99from builder.question_catalog import QuestionCatalog
     10from builder.question_catalog import question_factory
    1011from builder.media_items import NullMediaItem
    1112from builder.catalog_manager import CatalogManager
     
    1314from builder.exam_elements import SingleChoiceQuestion, MultipleChoiceQuestion, InstructionBlock
    1415from .menu import AppMenu
     16from gui.domain_management_dialog import DomainManagementDialog
    1517from .actions_panel import ActionsPanel
    1618from .select_panel import SelectPanel
     
    2628    def __init__(self):
    2729        super().__init__()
     30        self.domain_manager = DomainManager()
    2831        self.catalog_manager = CatalogManager()
    2932        self.exam_manager = ExamManager()
     
    4750
    4851        self._recent_actions = []
    49         self.domain_set = set()
    5052        self.clipboard = []  # holds copied Question objects
    5153        self.clipboard_action = None  # "copy"
     
    147149            simpledialog.askstring("Author", "Enter author:", parent=self) or "unknown"
    148150        )
    149         catalog = QuestionCatalog.with_domain(did)
     151        catalog = QuestionCatalog.with_domain_id(did)
    150152        catalog.title = title
    151153        catalog.author =author
     
    242244            return []
    243245        # Always provide deterministic order (e.g. by question ID)
    244         return sorted(self.active_catalog.questions, key=lambda q: q.id)
     246        return sorted(self.active_catalog.questions, key=lambda q: q.flexo_id)
    245247
    246248    def create_exam_dialog(self):
     
    265267            self.target_exam_var.set(active_exam.title)
    266268
    267     def get_question_editor_for(parent, question, available_domains):
     269    def get_question_editor_for(self, parent, question, available_domains):
    268270        """
    269271        Return the appropriate editor dialog instance for a given question.
     
    271273        """
    272274        if question.qtype in ("single_choice", "multiple_choice"):
    273             return OptionQuestionEditorDialog(parent, question, available_domains)
     275            return OptionQuestionEditorDialog(parent, question.to_dict(), available_domains)
    274276        if question.qtype in ("instruction", "text"):
    275277            # even though InstructionBlock may subclass Question,
    276278            # it doesn’t need options
    277             return DefaultQuestionEditorDialog(parent, question, available_domains)
    278         else:
    279             # Fallback for any unknown or simple Question subclass
    280             return DefaultQuestionEditorDialog(parent, question, available_domains)
     279            return DefaultQuestionEditorDialog(parent, question.to_dict(), available_domains)
     280       
     281        # Fallback for any unknown or simple Question subclass
     282        return DefaultQuestionEditorDialog(parent, question.to_dict(), available_domains)
    281283
    282284    def add_question(self):
     
    290292        media = []
    291293
    292         if qtype == "single_choice":
    293             q = SingleChoiceQuestion(text="Neue Frage",
    294                                      options=[], media=media)
    295         elif qtype == "multiple_choice":
    296             q = MultipleChoiceQuestion(text="Neue Mehrfachfrage",
    297                                        options=[], media=media)
     294        if qtype in ["single_choice", "multiple_choice"]:
     295            question_dict = {"qtype": qtype,
     296                             "text": "Neue Frage",
     297                             "options": [],
     298                             "media": media}
    298299        elif qtype == "instruction":
    299             q = InstructionBlock(text="Neue Instruktion", media=media)
     300            question_dict = InstructionBlock(text="Neue Instruktion", media=media)
    300301        else:
    301302            messagebox.showerror("Error", f"Unknown question type: {qtype}")
    302303            return
    303304
    304         dlg = OptionQuestionEditorDialog(self, q, self.domain_set)
     305        print(self.domain_manager.all_domain_ids())
     306        dlg = OptionQuestionEditorDialog(self, question_dict, self.domain_manager.all_domain_ids())
    305307        self.wait_window(dlg)
    306         current_question = dlg.result
    307 
    308         if current_question:
    309             current_question.flexo_id = FlexOID.generate(
    310                 current_question.domain,
    311                 current_question.entity_type,
    312                 current_question.state,
    313                 current_question.text_seed
    314             )
     308        question_dict = dlg.result
     309
     310        if question_dict:
     311            question_dict["qtype"] = "single_choice"
     312            current_question = question_factory(question_dict)
    315313            active_catalog.add_questions([current_question])
    316314            self.refresh_all()
    317             self.log_action(f"Updated - Question {q.id} updated.")
    318             self.log_action(f"New Question ID - Assigned ID: {current_question.id}")
     315            self.log_action(f"Updated - Question {current_question.flexo_id} updated.")
     316            self.log_action(f"New Question ID - Assigned ID: {current_question.flexo_id}")
    319317
    320318    def add_selected_question_to_exam(self):
     
    332330            return
    333331
    334         if not messagebox.askyesno("Delete", f"Delete question {q.id}?"):
     332        if not messagebox.askyesno("Delete", f"Delete question {q.flexo_id}?"):
    335333            return
    336334
    337335        try:
    338             if self.active_catalog.remove(q.id):
     336            if self.active_catalog.remove(q.flexo_id):
    339337                self.refresh_all()
    340                 self.log_action(f"Deleted - Question {q.id} removed.")
     338                self.log_action(f"Deleted - Question {q.flexo_id} removed.")
    341339        except ValueError as e:
    342340            messagebox.showerror("Forbidden", str(e))
     
    358356    def edit_selected_question(self):
    359357        q = self.require_selected_question()
    360         dlg = self.get_question_editor_for(q, self.domain_set)
     358        dlg = self.get_question_editor_for(self, q, self.domain_manager.all_domain_ids())
    361359        self.wait_window(dlg)
    362360        current_question = dlg.result
     
    364362            self.active_catalog.add_questions([current_question])
    365363            self.refresh_all()
    366             self.log_action(f"Updated - Question {q.id} updated.")
     364            self.log_action(f"Updated - Question {q.flexo_id} updated.")
    367365
    368366    def import_exam_as_temp_catalog(self, exam_path: str):
     
    484482            return
    485483
    486         catalogs = self.catalog_manager.catalogs()
     484        catalogs = self.catalog_manager.catalogs
    487485        if len(catalogs) < 2:
    488486            messagebox.showinfo(action.title(), "You need at least two catalogs.")
     
    606604            return
    607605
    608         entry = simpledialog.askstring("Add Domain",
    609                                        "Enter domain (e.g. ELEK_Elektrotechnik):",
    610                                        parent=self)
    611         if not entry:
     606        dlg = DomainManagementDialog(self, self.domain_manager)
     607        self.wait_window()
     608        if not dlg:
    612609            return
    613610
    614611        try:
    615             active.add_domain(entry)
    616             self.domain_set.update([entry])
    617             self.log_action(f"Added - Domain '{entry}' added to catalog '{active.catalog_id}'.")
     612            self.domain_manager.register(Domain.with_domain_id(dlg.result))
     613            self.log_action(f"Added - Domain '{dlg.result}' added to catalog '{active.catalog_id}'.")
    618614        except ValueError as e:
    619615            messagebox.showerror("Invalid Input", str(e))
Note: See TracChangeset for help on using the changeset viewer.