summaryrefslogtreecommitdiffstats
path: root/ui/new_article_dialog.py
blob: 0e0b4f5a30b897aee8a1ad2c4f3c121abb3d24d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from __future__ import annotations
from datetime import date
from pathlib import Path
from PyQt6.QtWidgets import (
    QDialog, QVBoxLayout, QFormLayout, QHBoxLayout,
    QLineEdit, QComboBox, QLabel, QPushButton, QMessageBox,
)
from core.models import ARTICLE_TYPES

STUB_TEMPLATE = """\
+++
title = "{title}"
date = {date}
type = "{type}"
draft = true
excerpt = ""
tags = []
categories = []
+++

TODO: scrivi il contenuto qui.
"""

class NewArticleDialog(QDialog):
    def __init__(self, blog_root: Path, parent=None):
        super().__init__(parent)
        self.setWindowTitle("Nuovo articolo")
        self.setMinimumWidth(400)
        self._blog_root = blog_root
        self._created_path: Path | None = None
        self._build_ui()

    @property
    def created_path(self) -> Path | None:
        return self._created_path

    def _build_ui(self):
        layout = QVBoxLayout(self)
        form = QFormLayout()

        self._slug = QLineEdit()
        self._slug.setPlaceholderText("mio-articolo-2026")
        form.addRow("Slug:", self._slug)

        self._title = QLineEdit()
        self._title.setPlaceholderText("Il mio articolo")
        form.addRow("Titolo:", self._title)

        self._lang = QComboBox()
        self._lang.addItems(["it", "en"])
        form.addRow("Lingua:", self._lang)

        self._type = QComboBox()
        self._type.addItems(ARTICLE_TYPES)
        form.addRow("Tipo:", self._type)

        layout.addLayout(form)

        btns = QHBoxLayout()
        create_btn = QPushButton("Crea")
        create_btn.clicked.connect(self._create)
        cancel_btn = QPushButton("Annulla")
        cancel_btn.clicked.connect(self.reject)
        btns.addStretch()
        btns.addWidget(cancel_btn)
        btns.addWidget(create_btn)
        layout.addLayout(btns)

    def _create(self):
        slug = self._slug.text().strip()
        title = self._title.text().strip()
        lang = self._lang.currentText()
        article_type = self._type.currentText()

        if not slug or not title:
            QMessageBox.warning(self, "Errore", "Slug e titolo sono obbligatori.")
            return

        target = self._blog_root / "content" / lang / "articles" / slug
        if target.exists():
            QMessageBox.warning(self, "Errore", f"Esiste già: {target}")
            return

        target.mkdir(parents=True)
        index_md = target / "index.md"
        index_md.write_text(STUB_TEMPLATE.format(
            title=title,
            date=date.today().isoformat(),
            type=article_type,
        ), encoding="utf-8")

        self._created_path = index_md
        self.accept()