Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4bd9933a87 | ||
|
|
5c5e97faae | ||
|
|
dd7f0b5df7 | ||
|
|
5032906d91 | ||
|
|
4300e901ab | ||
|
|
6a0e162596 | ||
|
|
603bd9f108 |
@@ -5,9 +5,6 @@ import tortoise.validators
|
||||
|
||||
from app.models.base import AbstractModel
|
||||
|
||||
# https://stackoverflow.com/a/57639657
|
||||
CRONTAB_REGEX = r"^(@(annually|yearly|monthly|weekly|daily|hourly|reboot))|(@every (\d+(ns|us|µs|ms|s|m|h))+)|((((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*) ?){5,7})$"
|
||||
|
||||
|
||||
class Task(AbstractModel):
|
||||
list_name = tortoise.fields.CharField(max_length=128, db_index=True, null=False)
|
||||
@@ -15,7 +12,6 @@ class Task(AbstractModel):
|
||||
reset_cron = tortoise.fields.CharField(
|
||||
max_length=64,
|
||||
null=True,
|
||||
validators=[tortoise.validators.RegexValidator(CRONTAB_REGEX, 0)],
|
||||
)
|
||||
check_date = tortoise.fields.DatetimeField(null=True)
|
||||
previous_check_date = tortoise.fields.DatetimeField(null=True)
|
||||
|
||||
@@ -6,6 +6,7 @@ import flask_limiter
|
||||
import flask_limiter.util
|
||||
import gunicorn.app.base
|
||||
import tortoise
|
||||
from tortoise.functions import Count
|
||||
|
||||
from app import PKG_NAME
|
||||
from app.lang import get_lang
|
||||
@@ -56,6 +57,18 @@ class Server(gunicorn.app.base.BaseApplication):
|
||||
lang=lambda key: get_lang(locale, key),
|
||||
)
|
||||
|
||||
@self.app.route("/api/meta", methods=["GET"])
|
||||
async def metadata() -> dict:
|
||||
return {
|
||||
"tasks": await Task.all().count(),
|
||||
"lists": (
|
||||
await Task.all()
|
||||
.annotate(count=Count("list_name", distinct=True))
|
||||
.first()
|
||||
.values("count")
|
||||
)["count"],
|
||||
}
|
||||
|
||||
@self.app.route("/api/lists/<list_name>/tasks", methods=["GET"])
|
||||
async def get_tasks(list_name: str) -> list:
|
||||
return await Task.filter(list_name=list_name[:128]).values()
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "tout-doux",
|
||||
"version": "1.2.0",
|
||||
"version": "1.4.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"engines": {
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[project]
|
||||
name = "tout-doux"
|
||||
version = "1.2.0"
|
||||
version = "1.4.0"
|
||||
description = ""
|
||||
requires-python = ">=3.14"
|
||||
dependencies = [
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
{
|
||||
"cron": {
|
||||
"type": {
|
||||
"one_time": "One time",
|
||||
"manual": "Manual",
|
||||
"every_day": "Every day",
|
||||
"every_week": "Every week",
|
||||
"every_month": "Every month",
|
||||
"every_year": "Every year",
|
||||
"specific": "Specific"
|
||||
"custom": "Custom (crontab)",
|
||||
"one_time": "One time (then delete)"
|
||||
},
|
||||
"week": [
|
||||
"Monday",
|
||||
@@ -84,5 +85,6 @@
|
||||
"save_button": "Save task",
|
||||
"clone_button": "Clone task",
|
||||
"delete_button": "Delete task",
|
||||
"recent": "Recent lists"
|
||||
"recent": "Recent lists",
|
||||
"metadata": "{lists} lists / {tasks} tasks"
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
{
|
||||
"cron": {
|
||||
"type": {
|
||||
"one_time": "Une seule fois",
|
||||
"manual": "Manuel",
|
||||
"every_day": "Tous les jours",
|
||||
"every_week": "Toutes les semaines",
|
||||
"every_month": "Tous les mois",
|
||||
"every_year": "Tous les ans",
|
||||
"specific": "Spécifique"
|
||||
"custom": "Personnalisé (crontab)",
|
||||
"one_time": "Une seule fois (puis suppression)"
|
||||
},
|
||||
"week": [
|
||||
"Lundi",
|
||||
@@ -84,5 +85,6 @@
|
||||
"save_button": "Sauvegarder la tâche",
|
||||
"clone_button": "Cloner la tâche",
|
||||
"delete_button": "Supprimer la tâche",
|
||||
"recent": "Listes récentes"
|
||||
"recent": "Listes récentes",
|
||||
"metadata": "{lists} listes / {tasks} tâches"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
import type { Metadata } from "@/types";
|
||||
|
||||
export async function getMetadata(): Promise<Metadata> {
|
||||
const response = await fetch(`/api/meta`);
|
||||
return await response.json();
|
||||
}
|
||||
@@ -10,7 +10,7 @@ const rawCron = ref<string>(DEFAULT_CRON);
|
||||
const valid = ref<boolean>(true);
|
||||
const props = defineProps<{ editMode: boolean }>();
|
||||
|
||||
const cronType = ref<CronType>(CronType.ONE_TIME);
|
||||
const cronType = ref<CronType>(CronType.MANUAL);
|
||||
const cronValue = ref<number>(0);
|
||||
|
||||
function onCronChanged() {
|
||||
@@ -34,7 +34,7 @@ function onChangeCronType() {
|
||||
case CronType.EVERY_YEAR:
|
||||
cronValue.value = 1;
|
||||
break;
|
||||
case CronType.SPECIFIC:
|
||||
case CronType.CUSTOM:
|
||||
cron.value ??= DEFAULT_CRON;
|
||||
break;
|
||||
}
|
||||
@@ -64,8 +64,8 @@ watch(cron, () => {
|
||||
<template>
|
||||
<div class="flex gap-2">
|
||||
<select v-model="cronType" class="select flex-1" @change="onChangeCronType">
|
||||
<option :value="CronType.ONE_TIME">
|
||||
{{ $t("cron.type.one_time") }}
|
||||
<option :value="CronType.MANUAL">
|
||||
{{ $t("cron.type.manual") }}
|
||||
</option>
|
||||
<option :value="CronType.EVERY_DAY">
|
||||
{{ $t("cron.type.every_day") }}
|
||||
@@ -79,8 +79,11 @@ watch(cron, () => {
|
||||
<option :value="CronType.EVERY_YEAR">
|
||||
{{ $t("cron.type.every_year") }}
|
||||
</option>
|
||||
<option :value="CronType.SPECIFIC">
|
||||
{{ $t("cron.type.specific") }}
|
||||
<option :value="CronType.CUSTOM">
|
||||
{{ $t("cron.type.custom") }}
|
||||
</option>
|
||||
<option :value="CronType.ONE_TIME">
|
||||
{{ $t("cron.type.one_time") }}
|
||||
</option>
|
||||
</select>
|
||||
<select
|
||||
@@ -149,7 +152,7 @@ watch(cron, () => {
|
||||
</option>
|
||||
</select>
|
||||
<input
|
||||
v-if="cronType === CronType.SPECIFIC"
|
||||
v-if="cronType === CronType.CUSTOM"
|
||||
v-model="rawCron"
|
||||
type="text"
|
||||
class="input font-mono flex-1"
|
||||
|
||||
@@ -1,9 +1,23 @@
|
||||
<script setup lang="ts">
|
||||
import { defineAsyncComponent } from "vue";
|
||||
import { getMetadata } from "@/api/meta";
|
||||
import type { Metadata } from "@/types";
|
||||
import { defineAsyncComponent, onBeforeMount, ref } from "vue";
|
||||
|
||||
const metadata = ref<Metadata|null>(null);
|
||||
|
||||
const LangInput = defineAsyncComponent(
|
||||
() => import("@/components/LangInput.vue"),
|
||||
);
|
||||
|
||||
function fetchMetadata() {
|
||||
void getMetadata()
|
||||
.then((data) => {
|
||||
metadata.value = data;
|
||||
setTimeout(fetchMetadata, 600000);
|
||||
});
|
||||
}
|
||||
|
||||
onBeforeMount(fetchMetadata);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -19,6 +33,7 @@ const LangInput = defineAsyncComponent(
|
||||
>
|
||||
klemek
|
||||
</a>
|
||||
<span v-if="metadata">- {{ $t("metadata", metadata) }}</span>
|
||||
</p>
|
||||
</aside>
|
||||
</footer>
|
||||
|
||||
@@ -13,10 +13,11 @@ import { updateTask } from "@/api/tasks";
|
||||
const CronInput = defineAsyncComponent(
|
||||
() => import("@/components/CronInput.vue"),
|
||||
);
|
||||
import { taskCompleted, taskNextReset } from "@/lib/tasks";
|
||||
import { isTemporary, taskCompleted, taskNextReset } from "@/lib/tasks";
|
||||
import { relativeTime } from "@/lib/dates";
|
||||
import { useAlertStore } from "@/stores/alerts";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import { isOneTime } from "@/lib/cron";
|
||||
|
||||
const emit =
|
||||
defineEmits<(e: "clone" | "delete" | "updated", task: Task) => void>();
|
||||
@@ -38,10 +39,12 @@ const i18n = useI18n();
|
||||
const { alertSuccess, alertError } = useAlertStore();
|
||||
|
||||
async function onOpen() {
|
||||
if (!isTemporary(task.value)) {
|
||||
editMode.value = true;
|
||||
await nextTick();
|
||||
editNameInput.value?.focus();
|
||||
}
|
||||
}
|
||||
|
||||
function onClose() {
|
||||
editName.value = task.value.name;
|
||||
@@ -51,10 +54,14 @@ function onClose() {
|
||||
|
||||
function onSave() {
|
||||
if (editName.value.trim()) {
|
||||
updateTask(task.value.id, {
|
||||
const data = {
|
||||
name: editName.value.trim(),
|
||||
reset_cron: editCron.value,
|
||||
})
|
||||
};
|
||||
task.value.name = editName.value.trim();
|
||||
task.value.reset_cron = editCron.value;
|
||||
editMode.value = false;
|
||||
updateTask(task.value.id, data)
|
||||
.then((newTask) => {
|
||||
task.value = newTask;
|
||||
emit("updated", newTask);
|
||||
@@ -63,47 +70,60 @@ function onSave() {
|
||||
.catch(() => {
|
||||
alertError(i18n.t("alerts.task_update_error"));
|
||||
});
|
||||
task.value.name = editName.value;
|
||||
task.value.reset_cron = editCron.value;
|
||||
editMode.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function onCheck() {
|
||||
if (!isTemporary(task.value)) {
|
||||
if (!checked.value) {
|
||||
updateTask(task.value.id, {
|
||||
if (isOneTime(task.value.reset_cron)) {
|
||||
emit("delete", task.value);
|
||||
} else {
|
||||
const data = {
|
||||
check_date: new Date().toISOString(),
|
||||
previous_check_date: task.value.check_date?.toISOString(),
|
||||
})
|
||||
.then((newTask) => {
|
||||
task.value = newTask;
|
||||
emit("updated", newTask);
|
||||
};
|
||||
task.value.previous_check_date = task.value.check_date;
|
||||
task.value.check_date = new Date();
|
||||
updateTask(task.value.id, data)
|
||||
.then((data) => {
|
||||
task.value = data;
|
||||
emit("updated", data);
|
||||
})
|
||||
.catch(() => {
|
||||
alertError(i18n.t("alerts.task_update_error"));
|
||||
});
|
||||
}
|
||||
} else {
|
||||
updateTask(task.value.id, {
|
||||
const data = {
|
||||
check_date: null,
|
||||
previous_check_date: task.value.check_date?.toISOString(),
|
||||
})
|
||||
.then((newTask) => {
|
||||
task.value = newTask;
|
||||
emit("updated", newTask);
|
||||
};
|
||||
task.value.previous_check_date = task.value.check_date;
|
||||
task.value.check_date = null;
|
||||
updateTask(task.value.id, data)
|
||||
.then((data) => {
|
||||
task.value = data;
|
||||
emit("updated", data);
|
||||
})
|
||||
.catch(() => {
|
||||
alertError(i18n.t("alerts.task_update_error"));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function onClone() {
|
||||
if (!isTemporary(task.value)) {
|
||||
emit("clone", task.value);
|
||||
}
|
||||
}
|
||||
|
||||
function onDelete() {
|
||||
if (!isTemporary(task.value)) {
|
||||
emit("delete", task.value);
|
||||
}
|
||||
}
|
||||
|
||||
onBeforeMount(() => {
|
||||
editName.value = task.value.name;
|
||||
@@ -112,6 +132,7 @@ onBeforeMount(() => {
|
||||
refreshKey.value++;
|
||||
}, 60000);
|
||||
});
|
||||
|
||||
watch(task, () => {
|
||||
if (!editMode.value && task.value.name !== editName.value) {
|
||||
editName.value = task.value.name;
|
||||
@@ -131,7 +152,7 @@ watch(task, () => {
|
||||
type="checkbox"
|
||||
class="checkbox"
|
||||
:checked="checked"
|
||||
@click.prevent="onCheck"
|
||||
@click="onCheck"
|
||||
/>
|
||||
</div>
|
||||
<div
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
export enum CronType {
|
||||
ONE_TIME,
|
||||
MANUAL,
|
||||
EVERY_DAY,
|
||||
EVERY_WEEK,
|
||||
EVERY_MONTH,
|
||||
EVERY_YEAR,
|
||||
SPECIFIC,
|
||||
CUSTOM,
|
||||
ONE_TIME,
|
||||
}
|
||||
|
||||
export enum SortType {
|
||||
|
||||
@@ -1,12 +1,19 @@
|
||||
import { CronType } from "@/enums";
|
||||
import { CronExpressionParser } from "cron-parser";
|
||||
|
||||
const ONE_TIME_VALUE = "@one-time";
|
||||
|
||||
export function parseCron(cron: string | null): {
|
||||
type: CronType;
|
||||
value: number;
|
||||
} {
|
||||
let value: RegExpMatchArray | null;
|
||||
if (cron === null) {
|
||||
return {
|
||||
type: CronType.MANUAL,
|
||||
value: 0,
|
||||
};
|
||||
} else if (cron === ONE_TIME_VALUE) {
|
||||
return {
|
||||
type: CronType.ONE_TIME,
|
||||
value: 0,
|
||||
@@ -33,7 +40,7 @@ export function parseCron(cron: string | null): {
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
type: CronType.SPECIFIC,
|
||||
type: CronType.CUSTOM,
|
||||
value: 0,
|
||||
};
|
||||
}
|
||||
@@ -45,6 +52,8 @@ export function getCron(
|
||||
fallback: string,
|
||||
): string | null {
|
||||
switch (type) {
|
||||
case CronType.ONE_TIME:
|
||||
return ONE_TIME_VALUE;
|
||||
case CronType.EVERY_DAY:
|
||||
return `0 ${value.toFixed(0)} * * *`;
|
||||
case CronType.EVERY_WEEK:
|
||||
@@ -53,7 +62,7 @@ export function getCron(
|
||||
return `0 0 ${value.toFixed(0)} * *`;
|
||||
case CronType.EVERY_YEAR:
|
||||
return `0 0 1 ${value.toFixed(0)} *`;
|
||||
case CronType.SPECIFIC:
|
||||
case CronType.CUSTOM:
|
||||
if (validCron(fallback)) {
|
||||
return fallback;
|
||||
}
|
||||
@@ -62,6 +71,10 @@ export function getCron(
|
||||
return null;
|
||||
}
|
||||
|
||||
export function isOneTime(cron: string | null): boolean {
|
||||
return cron === ONE_TIME_VALUE;
|
||||
}
|
||||
|
||||
export function validCron(cron: string): boolean {
|
||||
try {
|
||||
CronExpressionParser.parse(cron);
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { SortType } from "@/enums";
|
||||
import type { RawTask, Task } from "@/types";
|
||||
import type { RawTask, Task, TaskCreateData } from "@/types";
|
||||
import { CronExpressionParser } from "cron-parser";
|
||||
import { isOneTime } from "@/lib/cron";
|
||||
|
||||
const TEMPORARY_ID = "tmp";
|
||||
|
||||
export function parseTask(task: RawTask): Task {
|
||||
return {
|
||||
@@ -16,8 +19,28 @@ export function parseTask(task: RawTask): Task {
|
||||
};
|
||||
}
|
||||
|
||||
export function newTemporaryTask(data: TaskCreateData): Task {
|
||||
return {
|
||||
id: TEMPORARY_ID,
|
||||
name: data.name,
|
||||
reset_cron: data.reset_cron,
|
||||
created_at: new Date(),
|
||||
updated_at: new Date(),
|
||||
check_date: null,
|
||||
previous_check_date: null,
|
||||
};
|
||||
}
|
||||
|
||||
export function isTemporary(task: Task): boolean {
|
||||
return task.id === TEMPORARY_ID;
|
||||
}
|
||||
|
||||
export function taskNextReset(task: Task): Date | null {
|
||||
if (task.check_date === null || task.reset_cron === null) {
|
||||
if (
|
||||
task.check_date === null ||
|
||||
task.reset_cron === null ||
|
||||
isOneTime(task.reset_cron)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
|
||||
@@ -46,3 +46,8 @@ export interface Alert {
|
||||
created: Date;
|
||||
seconds: number;
|
||||
}
|
||||
|
||||
export interface Metadata {
|
||||
tasks: number;
|
||||
lists: number;
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
const TaskItem = defineAsyncComponent(
|
||||
() => import("@/components/TaskItem.vue"),
|
||||
);
|
||||
import { taskCompleted, sortTasks } from "@/lib/tasks";
|
||||
import { taskCompleted, sortTasks, newTemporaryTask } from "@/lib/tasks";
|
||||
import { SortType } from "@/enums";
|
||||
import { booleanCookieRef, enumCookieRef } from "@/lib/cookies";
|
||||
import { useAlertStore } from "@/stores/alerts";
|
||||
@@ -88,13 +88,16 @@ function reSortTasks(update = false) {
|
||||
|
||||
function onNewTask() {
|
||||
if (taskInput.value.trim() && list.value && !loading.value) {
|
||||
createTask(list.value, {
|
||||
const data = {
|
||||
name: taskInput.value.trim(),
|
||||
reset_cron: null,
|
||||
})
|
||||
.then((data: Task) => {
|
||||
tasks.value.push(data);
|
||||
};
|
||||
const task = newTemporaryTask(data);
|
||||
tasks.value.push(task);
|
||||
reSortTasks(true);
|
||||
createTask(list.value, data)
|
||||
.then((data: Task) => {
|
||||
task.id = data.id;
|
||||
alertSuccess(i18n.t("alerts.task_created"));
|
||||
})
|
||||
.catch(() => {
|
||||
@@ -112,12 +115,16 @@ function onUpdateTask() {
|
||||
|
||||
function onCloneTask(task: Task) {
|
||||
if (list.value) {
|
||||
createTask(list.value, {
|
||||
const data = {
|
||||
name: task.name,
|
||||
reset_cron: task.reset_cron,
|
||||
})
|
||||
};
|
||||
const newTask = newTemporaryTask(data);
|
||||
tasks.value.push(newTask);
|
||||
reSortTasks(true);
|
||||
createTask(list.value, data)
|
||||
.then((data: Task) => {
|
||||
tasks.value.push(data);
|
||||
task.id = data.id;
|
||||
reSortTasks(true);
|
||||
alertSuccess(i18n.t("alerts.task_cloned"));
|
||||
})
|
||||
@@ -128,16 +135,15 @@ function onCloneTask(task: Task) {
|
||||
}
|
||||
|
||||
function onDeleteTask(task: Task) {
|
||||
tasks.value.splice(tasks.value.indexOf(task), 1);
|
||||
reSortTasks(true);
|
||||
deleteTask(task.id)
|
||||
.then(() => {
|
||||
reSortTasks(true);
|
||||
alertSuccess(i18n.t("alerts.task_deleted"));
|
||||
})
|
||||
.catch(() => {
|
||||
alertError(i18n.t("alerts.task_delete_error"));
|
||||
});
|
||||
|
||||
tasks.value.splice(tasks.value.indexOf(task), 1);
|
||||
}
|
||||
|
||||
function onChangeSortType() {
|
||||
|
||||
Reference in New Issue
Block a user