| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | from __future__ import annotations |
| 15 | |
| 16 | import config as C |
| 17 | import timeline as T |
| 18 | from emit import sq_string |
| 19 | from ids import rid |
| 20 | |
| 21 | USER_COUNTER_FIELDS = [ |
| 22 | "chatbot_thread_count", "board_count", |
| 23 | "homework_submitted_total", "homework_on_time_total", "exam_sat_total", |
| 24 | "pomodoro_finished_total", "pomodoro_focus_ms_total", "marks_given_total", |
| 25 | "lessons_held_total", "pool_approved_total", "pool_published_total", |
| 26 | "lessons_attended_total", "high_mark_total", |
| 27 | "study_streak_longest", "study_streak_current", "study_streak_last_day", |
| 28 | "pomodoro_counted_day", "pomodoro_counted_today", |
| 29 | ] |
| 30 | |
| 31 | |
| 32 | def emit_users(ctx) -> None: |
| 33 | em = ctx.emitter |
| 34 | for u in ctx.users: |
| 35 | row = {"id": u.rid, "username": u.username, |
| 36 | "password_hash": u.profile["password_hash"], "role": u.role, |
| 37 | "name": u.name, "surname": u.surname} |
| 38 | for key in ("email", "phone", "birth_date", "theme", "language", |
| 39 | "palette_color", "display_name", "bio", "avatar_file", |
| 40 | "avatar_content_type", "avatar_size"): |
| 41 | row[key] = u.profile.get(key) |
| 42 | for field in USER_COUNTER_FIELDS: |
| 43 | if field in C.UNWRITTEN_COUNTERS: |
| 44 | row[field] = None |
| 45 | else: |
| 46 | row[field] = u.counters.get(field) |
| 47 | em.add("user", row) |
| 48 | |
| 49 | |
| 50 | def emit_structure(ctx) -> None: |
| 51 | |
| 52 | em = ctx.emitter |
| 53 | |
| 54 | course_count = len(ctx.courses) |
| 55 | class_count = len(ctx.classes) |
| 56 | for term in ctx.terms: |
| 57 | row = dict(term) |
| 58 | if row["id"] is ctx.term_bahar or str(row["id"]) == str(ctx.term_bahar): |
| 59 | row["course_count"] = course_count |
| 60 | row["class_count"] = class_count |
| 61 | else: |
| 62 | row["course_count"] = 0 |
| 63 | row["class_count"] = 0 |
| 64 | em.add("term", row) |
| 65 | |
| 66 | for cg in ctx.classes: |
| 67 | grade = (C.INCONSISTENT_GRADE_VALUE |
| 68 | if cg.name == C.INCONSISTENT_GRADE_BRANCH else cg.grade) |
| 69 | em.add("class_group", { |
| 70 | "id": cg.rid, "name": cg.name, "grade": grade, |
| 71 | "term": ctx.term_bahar, "creator": ctx.admin.rid, |
| 72 | "teacher": cg.teacher.rid, |
| 73 | "class_member_count": cg.member_count, |
| 74 | "class_course_count": cg.course_count, |
| 75 | }) |
| 76 | |
| 77 | for grade in C.GRADES: |
| 78 | courses = [co.rid for co in ctx.core_courses if co.grade == grade] |
| 79 | em.add("class_blueprint", { |
| 80 | "id": ctx.blueprints[grade], "grade": grade, |
| 81 | "courses": courses, "creator": ctx.admin.rid, |
| 82 | }) |
| 83 | |
| 84 | for co in ctx.courses: |
| 85 | em.add("course", { |
| 86 | "id": co.rid, "creator": co.creator.rid, |
| 87 | "teachers": [t.rid for t in co.teachers], |
| 88 | "title": co.title, |
| 89 | "description": "%s dersi." % co.title, |
| 90 | "kind": co.kind, |
| 91 | "term": ctx.term_bahar, |
| 92 | "capacity": co.capacity, |
| 93 | "enrollment_count": co.enrollment_count, |
| 94 | }) |
| 95 | |
| 96 | for sb in ctx.subjects: |
| 97 | em.add("subject", { |
| 98 | "id": sb.rid, "course": sb.course.rid, "name": sb.name, |
| 99 | "description": sb.description, |
| 100 | "exam_question_count": sb.exam_question_count, |
| 101 | "homework_count": sb.homework_count, |
| 102 | }) |
| 103 | |
| 104 | |
| 105 | def emit_reference_tables(ctx) -> None: |
| 106 | |
| 107 | em = ctx.emitter |
| 108 | em.add("settings", dict({"id": rid("settings", "school")}, **C.SETTINGS)) |
| 109 | |
| 110 | for kind in C.EXAM_KINDS: |
| 111 | em.add("kind_ref", { |
| 112 | "id": rid("kind_ref", kind), |
| 113 | "count": ctx.kind_ref_counts.get(kind, 0), |
| 114 | "retired": False, |
| 115 | }) |
| 116 | for slot in C.MEAL_SLOTS: |
| 117 | em.add("slot_ref", { |
| 118 | "id": rid("slot_ref", slot), |
| 119 | "count": ctx.slot_ref_counts.get(slot, 0), |
| 120 | "retired": False, |
| 121 | }) |
| 122 | |
| 123 | |
| 124 | for mark in ("board_roster", "profile_counters"): |
| 125 | em.add("migration_mark", { |
| 126 | "id": rid("migration_mark", mark), |
| 127 | "done_at": T.T_NOW, "fingerprint": None, |
| 128 | }) |
| 129 | |
| 130 | |
| 131 | def write_control_file(path: str, created_ms: int) -> None: |
| 132 | |
| 133 | |
| 134 | modules = ", ".join(sq_string(m) for m in C.MODULES_ALL) |
| 135 | with open(path, "w", encoding="utf-8", newline="\n") as fh: |
| 136 | fh.write("-- ---------------------------------------------------------------\n") |
| 137 | fh.write("-- Hezarfen tohum verisi — 00_control.surql\n") |
| 138 | fh.write("-- Tablo: school (CONTROL veritabanı) — 1 satır\n") |
| 139 | fh.write("-- Bu dosya CONTROL veritabanında çalıştırılır (USE NS hezarfen DB control).\n") |
| 140 | fh.write("-- Okul zaten builder ile oluşturulmuşsa satır UPSERT ile güncellenir;\n") |
| 141 | fh.write("-- 21 modülün tamamı açılır (Module::ALL, sıralı saklanır — E23).\n") |
| 142 | fh.write("-- Diğer dosyalar OKUL veritabanında, numara sırasıyla yüklenir.\n") |
| 143 | fh.write("-- ---------------------------------------------------------------\n\n") |
| 144 | fh.write("UPSERT school:⟨%s⟩ CONTENT {\n" % C.SCHOOL_SLUG) |
| 145 | fh.write(" slug: %s,\n" % sq_string(C.SCHOOL_SLUG)) |
| 146 | fh.write(" name: %s,\n" % sq_string(C.SCHOOL_NAME)) |
| 147 | fh.write(" status: %s,\n" % sq_string("active")) |
| 148 | fh.write(" created_at: %d,\n" % created_ms) |
| 149 | fh.write(" modules: [%s]\n" % modules) |
| 150 | fh.write("};\n") |