Add configuration rules, refactor settings management, and enhance import functionality

- Introduced a new configuration file `.cursorrules` to define coding standards, error handling, testing requirements, and project-specific guidelines.
- Refactored `config.py` to implement a `Settings` dataclass for better management of environment variables, improving testability and maintainability.
- Updated the import duty schedule handler to utilize session management with `session_scope`, ensuring proper database session handling.
- Enhanced the import service to streamline the duty schedule import process, improving code organization and readability.
- Added new service layer functions to encapsulate business logic related to group duty pinning and duty schedule imports.
- Updated README documentation to reflect the new configuration structure and improved import functionality.
This commit is contained in:
2026-02-18 12:35:11 +03:00
parent 8697b9e30b
commit 5331fac334
25 changed files with 1032 additions and 397 deletions

View File

@@ -3,6 +3,47 @@
const RETRY_DELAY_MS = 800;
const RETRY_AFTER_ACCESS_DENIED_MS = 1200;
const THEME_BG = { dark: "#1a1b26", light: "#d5d6db" };
function getTheme() {
if (typeof window === "undefined") return "dark";
var twa = window.Telegram?.WebApp;
if (twa?.colorScheme) {
return twa.colorScheme;
}
var cssScheme = "";
try {
cssScheme = getComputedStyle(document.documentElement).getPropertyValue("--tg-color-scheme").trim();
} catch (e) {}
if (cssScheme === "light" || cssScheme === "dark") {
return cssScheme;
}
if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
return "dark";
}
return "light";
}
function applyTheme() {
var scheme = getTheme();
document.documentElement.dataset.theme = scheme;
var bg = THEME_BG[scheme] || THEME_BG.dark;
if (window.Telegram?.WebApp?.setBackgroundColor) {
window.Telegram.WebApp.setBackgroundColor(bg);
}
if (window.Telegram?.WebApp?.setHeaderColor) {
window.Telegram.WebApp.setHeaderColor(bg);
}
}
applyTheme();
if (typeof window !== "undefined" && window.Telegram?.WebApp) {
setTimeout(applyTheme, 0);
setTimeout(applyTheme, 100);
} else if (window.matchMedia) {
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", applyTheme);
}
const MONTHS = [
"Январь", "Февраль", "Март", "Апрель", "Май", "Июнь",
"Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь", "Декабрь"
@@ -382,6 +423,31 @@
return key.slice(8, 10) + "." + key.slice(5, 7);
}
/** Format ISO date as HH:MM in local time. */
function formatTimeLocal(isoStr) {
const d = new Date(isoStr);
return String(d.getHours()).padStart(2, "0") + ":" + String(d.getMinutes()).padStart(2, "0");
}
/** Build HTML for one timeline duty card: one-day "DD.MM, HH:MM HH:MM" or multi-day "DD.MM HH:MM DD.MM HH:MM". */
function dutyTimelineCardHtml(d, isCurrent) {
const startLocal = localDateString(new Date(d.start_at));
const endLocal = localDateString(new Date(d.end_at));
const startDDMM = dateKeyToDDMM(startLocal);
const endDDMM = dateKeyToDDMM(endLocal);
const startTime = formatTimeLocal(d.start_at);
const endTime = formatTimeLocal(d.end_at);
let timeStr;
if (startLocal === endLocal) {
timeStr = startDDMM + ", " + startTime + " " + endTime;
} else {
timeStr = startDDMM + " " + startTime + " " + endDDMM + " " + endTime;
}
const typeLabel = isCurrent ? "Сейчас дежурит" : (EVENT_TYPE_LABELS[d.event_type] || "Дежурство");
const extraClass = isCurrent ? " duty-item--current" : "";
return "<div class=\"duty-item duty-item--duty duty-timeline-card" + extraClass + "\"><span class=\"duty-item-type\">" + escapeHtml(typeLabel) + "</span> <span class=\"name\">" + escapeHtml(d.full_name) + "</span><div class=\"time\">" + escapeHtml(timeStr) + "</div></div>";
}
/** Build HTML for one duty card. @param {object} d - duty. @param {string} [typeLabelOverride] - e.g. "Сейчас дежурит". @param {boolean} [showUntilEnd] - show "до HH:MM" instead of range. @param {string} [extraClass] - e.g. "duty-item--current". */
function dutyItemHtml(d, typeLabelOverride, showUntilEnd, extraClass) {
const startDate = new Date(d.start_at);
@@ -406,57 +472,46 @@
}
function renderDutyList(duties) {
duties = duties.filter(function (d) { return d.event_type === "duty"; });
if (duties.length === 0) {
dutyListEl.innerHTML = "<p class=\"muted\">В этом месяце событий нет.</p>";
dutyListEl.classList.remove("duty-timeline");
dutyListEl.innerHTML = "<p class=\"muted\">В этом месяце дежурств нет.</p>";
return;
}
const grouped = {};
duties.forEach(function (d) {
const date = localDateString(new Date(d.start_at));
if (!grouped[date]) grouped[date] = [];
grouped[date].push(d);
});
let dates = Object.keys(grouped).sort();
dutyListEl.classList.add("duty-timeline");
const todayKey = localDateString(new Date());
const firstKey = localDateString(firstDayOfMonth(current));
const lastKey = localDateString(lastDayOfMonth(current));
const showTodayInMonth = todayKey >= firstKey && todayKey <= lastKey;
if (showTodayInMonth && dates.indexOf(todayKey) === -1) {
dates = [todayKey].concat(dates).sort();
}
let html = "";
const dateSet = new Set();
duties.forEach(function (d) {
dateSet.add(localDateString(new Date(d.start_at)));
});
if (showTodayInMonth) dateSet.add(todayKey);
let dates = Array.from(dateSet).sort();
const now = new Date();
let fullHtml = "";
dates.forEach(function (date) {
const isToday = date === todayKey;
const dayBlockClass = "duty-list-day" + (isToday ? " duty-list-day--today" : "");
const titleText = isToday ? "Сегодня, " + dateKeyToDDMM(date) : dateKeyToDDMM(date);
html += "<div class=\"" + dayBlockClass + "\"><h2 class=\"duty-list-day-title\">" + escapeHtml(titleText) + "</h2>";
if (isToday) {
const now = new Date();
const todayDuties = duties.filter(function (d) { return dutyOverlapsLocalDay(d, todayKey); }).sort(function (a, b) { return new Date(a.start_at) - new Date(b.start_at); });
todayDuties.forEach(function (d) {
const start = new Date(d.start_at);
const end = new Date(d.end_at);
const isCurrent = start <= now && now < end;
if (isCurrent && d.event_type === "duty") {
html += dutyItemHtml(d, "Сейчас дежурит", true, "duty-item--current");
} else {
html += dutyItemHtml(d);
}
});
} else {
const list = grouped[date] || [];
list.forEach(function (d) { html += dutyItemHtml(d); });
const dayClass = "duty-timeline-day" + (isToday ? " duty-timeline-day--today" : "");
const dateLabel = isToday ? "Сегодня, " + dateKeyToDDMM(date) : dateKeyToDDMM(date);
const dayDuties = duties.filter(function (d) { return localDateString(new Date(d.start_at)) === date; }).sort(function (a, b) { return new Date(a.start_at) - new Date(b.start_at); });
let dayHtml = "";
dayDuties.forEach(function (d) {
const start = new Date(d.start_at);
const end = new Date(d.end_at);
const isCurrent = isToday && start <= now && now < end;
dayHtml += "<div class=\"duty-timeline-row\"><span class=\"duty-timeline-date\">" + escapeHtml(dateLabel) + "</span><div class=\"duty-timeline-card-wrap\">" + dutyTimelineCardHtml(d, isCurrent) + "</div></div>";
});
if (dayDuties.length === 0 && isToday) {
dayHtml += "<div class=\"duty-timeline-row duty-timeline-row--empty\"><span class=\"duty-timeline-date\">" + escapeHtml(dateLabel) + "</span><div class=\"duty-timeline-card-wrap\"></div></div>";
}
html += "</div>";
fullHtml += "<div class=\"" + dayClass + "\" data-date=\"" + escapeHtml(date) + "\">" + dayHtml + "</div>";
});
dutyListEl.innerHTML = html;
var scrollTarget = dutyListEl.querySelector(".duty-list-day--today");
dutyListEl.innerHTML = fullHtml;
var scrollTarget = dutyListEl.querySelector(".duty-timeline-day--today");
if (scrollTarget) {
var stickyEl = document.getElementById("calendarSticky");
var stickyHeight = stickyEl ? stickyEl.offsetHeight : 0;
var targetTop = scrollTarget.getBoundingClientRect().top + window.scrollY;
window.scrollTo(0, Math.max(0, targetTop - stickyHeight));
scrollTarget.scrollIntoView({ behavior: "auto", block: "start" });
}
}
@@ -499,13 +554,11 @@
if (window.Telegram.WebApp.expand) {
window.Telegram.WebApp.expand();
}
var bg = "#1a1b26";
if (window.Telegram.WebApp.setBackgroundColor) {
window.Telegram.WebApp.setBackgroundColor(bg);
}
if (window.Telegram.WebApp.setHeaderColor) {
window.Telegram.WebApp.setHeaderColor(bg);
applyTheme();
if (window.Telegram.WebApp.onEvent) {
window.Telegram.WebApp.onEvent("theme_changed", applyTheme);
}
requestAnimationFrame(function () { applyTheme(); });
setTimeout(cb, 0);
} else {
cb();