Heng2004 commited on
Commit
86725d8
·
verified ·
1 Parent(s): fe6264c

Create teacher_panel.py

Browse files
Files changed (1) hide show
  1. teacher_panel.py +150 -0
teacher_panel.py ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # teacher_panel.py
2
+ from typing import List, Tuple, Any
3
+
4
+ import qa_store
5
+ from qa_store import normalize_question
6
+ from loader import (
7
+ generate_new_manual_id,
8
+ save_manual_qa_file,
9
+ rebuild_combined_qa,
10
+ manual_qa_table_data,
11
+ )
12
+
13
+
14
+ def _table() -> List[List[str]]:
15
+ return manual_qa_table_data()
16
+
17
+
18
+ def teacher_save_add_overwrite(
19
+ question: str, answer: str, current_id: str
20
+ ) -> Tuple[str, str, str, List[List[str]], str]:
21
+ """
22
+ Add a new manual Q&A or overwrite existing one
23
+ with the same normalized question.
24
+ """
25
+ question = (question or "").strip()
26
+ answer = (answer or "").strip()
27
+ current_id = current_id or ""
28
+
29
+ if not question or not answer:
30
+ msg = "❌ ກະລຸນາໃສ່ທັງຄໍາຖາມແລະຄໍາຕອບ."
31
+ return question, answer, current_id, _table(), msg
32
+
33
+ norm_q = normalize_question(question)
34
+ entry = qa_store.MANUAL_QA_INDEX.get(norm_q)
35
+
36
+ if entry:
37
+ # overwrite existing manual Q&A
38
+ entry["q"] = question
39
+ entry["a"] = answer
40
+ entry["norm_q"] = norm_q
41
+ current_id = entry["id"]
42
+ msg = "✅ ອັບເດດ Q&A ທີ່ມີຢູ່ແລ້ວ."
43
+ else:
44
+ # create new entry
45
+ new_id = generate_new_manual_id()
46
+ entry = {
47
+ "id": new_id,
48
+ "q": question,
49
+ "a": answer,
50
+ "norm_q": norm_q,
51
+ }
52
+ qa_store.MANUAL_QA_LIST.append(entry)
53
+ qa_store.MANUAL_QA_INDEX[norm_q] = entry
54
+ current_id = new_id
55
+ msg = "✅ ບັນທຶກ Q&A ໃໝ່ສໍາເລັດ."
56
+
57
+ save_manual_qa_file()
58
+ rebuild_combined_qa()
59
+
60
+ return question, answer, current_id, _table(), msg
61
+
62
+
63
+ def teacher_update_current(
64
+ question: str, answer: str, current_id: str
65
+ ) -> Tuple[str, str, str, List[List[str]], str]:
66
+ """
67
+ Update the currently selected manual Q&A by id.
68
+ """
69
+ question = (question or "").strip()
70
+ answer = (answer or "").strip()
71
+ current_id = current_id or ""
72
+
73
+ if not current_id:
74
+ msg = "❌ ກະລຸນາເລືອກ Q&A ຈາກຕາຕະລາງກ່ອນ."
75
+ return question, answer, current_id, _table(), msg
76
+
77
+ if not question or not answer:
78
+ msg = "❌ ກະລຸນາໃສ່ທັງຄໍາຖາມແລະຄໍາຕອບ."
79
+ return question, answer, current_id, _table(), msg
80
+
81
+ entry = next((e for e in qa_store.MANUAL_QA_LIST if e["id"] == current_id), None)
82
+ if not entry:
83
+ msg = "⚠️ ບໍ່ພົບ Q&A ທີ່ເລືອກ."
84
+ current_id = ""
85
+ return question, answer, current_id, _table(), msg
86
+
87
+ # remove old index
88
+ qa_store.MANUAL_QA_INDEX.pop(entry["norm_q"], None)
89
+
90
+ new_norm = normalize_question(question)
91
+ entry["q"] = question
92
+ entry["a"] = answer
93
+ entry["norm_q"] = new_norm
94
+ qa_store.MANUAL_QA_INDEX[new_norm] = entry
95
+
96
+ save_manual_qa_file()
97
+ rebuild_combined_qa()
98
+
99
+ msg = "✅ ແກ້ໄຂ Q&A ສໍາເລັດ."
100
+ return question, answer, current_id, _table(), msg
101
+
102
+
103
+ def teacher_delete_current(
104
+ current_id: str,
105
+ ) -> Tuple[str, str, str, List[List[str]], str]:
106
+ """
107
+ Delete the currently selected manual Q&A.
108
+ """
109
+ current_id = current_id or ""
110
+
111
+ if not current_id:
112
+ msg = "❌ ກະລຸນາເລືອກ Q&A ກ່ອນຈະລຶບ."
113
+ return "", "", current_id, _table(), msg
114
+
115
+ entry = next((e for e in qa_store.MANUAL_QA_LIST if e["id"] == current_id), None)
116
+ if not entry:
117
+ msg = "⚠️ ບໍ່ພົບ Q&A ທີ່ຈະລຶບ."
118
+ return "", "", "", _table(), msg
119
+
120
+ qa_store.MANUAL_QA_LIST = [e for e in qa_store.MANUAL_QA_LIST if e["id"] != current_id]
121
+ qa_store.MANUAL_QA_INDEX.pop(entry["norm_q"], None)
122
+
123
+ save_manual_qa_file()
124
+ rebuild_combined_qa()
125
+
126
+ msg = "🗑️ ລຶບ Q&A ສໍາເລັດ."
127
+ return "", "", "", _table(), msg
128
+
129
+
130
+ def teacher_on_table_select(
131
+ evt: Any, table: List[List[str]]
132
+ ) -> Tuple[str, str, str, str]:
133
+ """
134
+ When teacher clicks a row in the Q&A table, load Q/A into textboxes.
135
+ """
136
+ try:
137
+ # evt.index can be int or [row, col]
138
+ idx = evt.index[0] if isinstance(evt.index, (list, tuple)) else evt.index
139
+ row = table[idx]
140
+ current_id = str(row[0])
141
+ question = str(row[1])
142
+ answer = str(row[2])
143
+ msg = f"✏️ ເລືອກ Q&A ID {current_id} ສໍາລັບແກ້ໄຂ."
144
+ except Exception: # noqa: BLE001
145
+ current_id = ""
146
+ question = ""
147
+ answer = ""
148
+ msg = "⚠️ ບໍ່ສາມາດເລືອກແຖວໄດ້."
149
+
150
+ return question, answer, current_id, msg