| import json |
| import time |
| import uuid |
| import gradio as gr |
| from config import MATERIALS_JSON, CODES_JSON |
|
|
| def read_json(file_path): |
| with open(file_path, 'r', encoding='utf-8') as f: |
| return json.load(f) |
|
|
| def write_json(file_path, data): |
| with open(file_path, 'w', encoding='utf-8') as f: |
| json.dump(data, f, ensure_ascii=False, indent=2) |
|
|
| def get_all_subjects(section): |
| json_file = MATERIALS_JSON if section == "学习资料" else CODES_JSON |
| data = read_json(json_file) |
| subjects = sorted(set(item["subject"] for item in data if item["subject"])) |
| return subjects if subjects else ["无可用学科"] |
|
|
| def add_row(inputs): |
| if len(inputs) < 10: |
| inputs.append({"subject": "", "link": "", "contributor": ""}) |
| updates = [inputs] |
| for i in range(10): |
| updates.append(gr.update(visible=i < len(inputs))) |
| return updates |
|
|
|
|
| def update_input_value(index, subject, link, contributor, inputs): |
| if index < len(inputs): |
| inputs[index] = {"subject": subject, "link": link, "contributor": contributor} |
| return inputs |
|
|
|
|
| def submit_material(section, inputs): |
| from config import MATERIALS_JSON, CODES_JSON |
| json_file = MATERIALS_JSON if section == "学习资料" else CODES_JSON |
| valid_inputs = [item for item in inputs if item["subject"].strip() and item["link"].strip() and item["contributor"].strip()] |
| if not valid_inputs: |
| return ( |
| f"**{section}** 投稿失败:请至少添加一个科目、链接和贡献人!", |
| "", |
| inputs, |
| *[gr.update(visible=i < len(inputs)) for i in range(10)] |
| ) |
| selected_subjects = [item["subject"].strip() for item in valid_inputs] |
| if len(selected_subjects) != len(set(selected_subjects)): |
| return ( |
| f"**{section}** 投稿失败:每个科目只能关联一个链接,请勿重复输入科目!", |
| "", |
| inputs, |
| *[gr.update(visible=i < len(inputs)) for i in range(10)] |
| ) |
| data = read_json(json_file) |
| submission_ids = [] |
| for item in valid_inputs: |
| submission_id = str(uuid.uuid4()) |
| file_name = f"{item['subject']}_{int(time.time())}" |
| submission = { |
| "id": submission_id, |
| "name": file_name, |
| "subject": item["subject"].strip(), |
| "link": item["link"].strip(), |
| "contributor": item["contributor"].strip(), |
| "timestamp": time.strftime("%Y-%m-%d %H:%M:%S"), |
| "is_reviewed": False, |
| "is_approved": False, |
| "reason": "" |
| } |
| data.append(submission) |
| submission_ids.append(submission_id) |
| write_json(json_file, data) |
| inputs[:] = [{"subject": "", "link": "", "contributor": ""}] |
| return ( |
| f"**{section}** 投稿成功!\n唯一编码:\n" + "\n".join([f"- `{sid}`" for sid in submission_ids]) + |
| "\n请保存这些编码以查询审核状态!", |
| "", |
| inputs, |
| gr.update(visible=True), |
| *[gr.update(visible=False) for _ in range(9)] |
| ) |
|
|
| def check_submission_status(submission_id, section): |
| json_file = MATERIALS_JSON if section == "学习资料" else CODES_JSON |
| data = read_json(json_file) |
| for item in data: |
| if item["id"] == submission_id: |
| if not item["is_reviewed"]: |
| return f"**{section}** 投稿状态:待审核,感谢你的贡献!" |
| elif item["is_approved"]: |
| return (f"**{section}** 投稿状态:审核成功,感谢你的贡献!\n" |
| f"您上传的科目版本:{item['name']}") |
| else: |
| return f"**{section}** 投稿状态:审核失败\n原因:{item['reason']}" |
| return f"未找到 **{section}** 的投稿编码 `{submission_id}`!" |
|
|
| def query_available_materials(section): |
| json_file = MATERIALS_JSON if section == "学习资料" else CODES_JSON |
| data = read_json(json_file) |
| approved = [item for item in data if item["is_approved"]] |
| if not approved: |
| return f"暂无已审核的 **{section}**!" |
| subject_groups = {} |
| for item in approved: |
| subject = item["subject"] |
| if subject not in subject_groups: |
| subject_groups[subject] = [] |
| subject_groups[subject].append(item["name"]) |
| result = f"## 当前已审核的 {section}\n" |
| for subject in sorted(subject_groups.keys()): |
| names = subject_groups[subject] |
| result += f"{subject} {len(names)} 个:\n" |
| for name in sorted(names): |
| result += f"- {name}\n" |
| result += "\n" |
| return result.strip() |
|
|
| def search_materials(subject, section): |
| json_file = MATERIALS_JSON if section == "学习资料" else CODES_JSON |
| data = read_json(json_file) |
| approved = [item for item in data if item["is_approved"] and item["subject"] == subject] |
| if not approved: |
| return f"暂无 **{subject}** 的 **{section}**!" |
| result = f"## {subject} 的 {section}\n" |
| for item in approved: |
| result += f"- **{item['name']}**:{item['link']}\n" |
| return result |
|
|