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:
143
webapp/app.js
143
webapp/app.js
@@ -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();
|
||||
|
||||
@@ -6,6 +6,22 @@
|
||||
--accent: #7aa2f7;
|
||||
--duty: #9ece6a;
|
||||
--today: #bb9af7;
|
||||
--unavailable: #e0af68;
|
||||
--vacation: #7dcfff;
|
||||
--error: #f7768e;
|
||||
}
|
||||
|
||||
[data-theme="light"] {
|
||||
--bg: #d5d6db;
|
||||
--surface: #b8b9c4;
|
||||
--text: #343b58;
|
||||
--muted: #6b7089;
|
||||
--accent: #2e7de0;
|
||||
--duty: #587d0a;
|
||||
--today: #7847b3;
|
||||
--unavailable: #b8860b;
|
||||
--vacation: #0d6b9e;
|
||||
--error: #c43b3b;
|
||||
}
|
||||
|
||||
html {
|
||||
@@ -134,8 +150,8 @@ body {
|
||||
}
|
||||
|
||||
.day.holiday {
|
||||
background: linear-gradient(135deg, var(--surface) 0%, rgba(187, 154, 247, 0.15) 100%);
|
||||
border: 1px solid rgba(187, 154, 247, 0.35);
|
||||
background: linear-gradient(135deg, var(--surface) 0%, color-mix(in srgb, var(--today) 15%, transparent) 100%);
|
||||
border: 1px solid color-mix(in srgb, var(--today) 35%, transparent);
|
||||
}
|
||||
|
||||
.day {
|
||||
@@ -221,13 +237,13 @@ body {
|
||||
}
|
||||
|
||||
.unavailable-marker {
|
||||
color: #e0af68;
|
||||
background: rgba(224, 175, 104, 0.25);
|
||||
color: var(--unavailable);
|
||||
background: color-mix(in srgb, var(--unavailable) 25%, transparent);
|
||||
}
|
||||
|
||||
.vacation-marker {
|
||||
color: #7dcfff;
|
||||
background: rgba(125, 207, 255, 0.25);
|
||||
color: var(--vacation);
|
||||
background: color-mix(in srgb, var(--vacation) 25%, transparent);
|
||||
}
|
||||
|
||||
.duty-list {
|
||||
@@ -260,24 +276,65 @@ body {
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
.duty-item {
|
||||
/* Timeline: dates left, cards right */
|
||||
.duty-list.duty-timeline {
|
||||
border-left: 2px solid var(--muted);
|
||||
padding-left: 0;
|
||||
margin-left: 2px;
|
||||
}
|
||||
|
||||
.duty-timeline-day {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.duty-timeline-day--today {
|
||||
scroll-margin-top: 200px;
|
||||
}
|
||||
|
||||
.duty-timeline-row {
|
||||
display: grid;
|
||||
grid-template-columns: 5.5em 1fr;
|
||||
gap: 0 8px;
|
||||
grid-template-columns: 4.2em 1fr;
|
||||
gap: 0 10px;
|
||||
align-items: start;
|
||||
margin-bottom: 8px;
|
||||
min-height: 1px;
|
||||
}
|
||||
|
||||
.duty-timeline-date {
|
||||
font-size: 0.8rem;
|
||||
color: var(--muted);
|
||||
padding-top: 10px;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.duty-timeline-day--today .duty-timeline-date {
|
||||
color: var(--today);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.duty-timeline-card-wrap {
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.duty-timeline-card.duty-item,
|
||||
.duty-list .duty-item {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr;
|
||||
gap: 2px 0;
|
||||
align-items: baseline;
|
||||
padding: 8px 10px;
|
||||
margin-bottom: 6px;
|
||||
margin-bottom: 0;
|
||||
border-radius: 8px;
|
||||
background: var(--surface);
|
||||
border-left: 3px solid var(--duty);
|
||||
}
|
||||
|
||||
.duty-item--unavailable {
|
||||
border-left-color: #e0af68;
|
||||
border-left-color: var(--unavailable);
|
||||
}
|
||||
|
||||
.duty-item--vacation {
|
||||
border-left-color: #7dcfff;
|
||||
border-left-color: var(--vacation);
|
||||
}
|
||||
|
||||
.duty-item .duty-item-type {
|
||||
@@ -302,6 +359,15 @@ body {
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.duty-timeline-card .duty-item-type { grid-column: 1; grid-row: 1; }
|
||||
.duty-timeline-card .name { grid-column: 1; grid-row: 2; min-width: 0; }
|
||||
.duty-timeline-card .time { grid-column: 1; grid-row: 3; }
|
||||
|
||||
.duty-item--current {
|
||||
border-left-color: var(--today);
|
||||
background: color-mix(in srgb, var(--today) 12%, var(--surface));
|
||||
}
|
||||
|
||||
.loading, .error {
|
||||
text-align: center;
|
||||
padding: 12px;
|
||||
@@ -309,7 +375,7 @@ body {
|
||||
}
|
||||
|
||||
.error {
|
||||
color: #f7768e;
|
||||
color: var(--error);
|
||||
}
|
||||
|
||||
.error[hidden], .loading.hidden {
|
||||
@@ -327,7 +393,7 @@ body {
|
||||
}
|
||||
|
||||
.access-denied p:first-child {
|
||||
color: #f7768e;
|
||||
color: var(--error);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user