Files
blogsite/studio/forms.py
T
xiaoan 128dc20cd5 加入「拾编」写作台:文章 / 分类管理 + Markdown 上传
- 新增 studio 应用与 /manage/ 写作台
- 文章增删改查、状态/分类/标签、搜索筛选分页
- 分类增删改查,支持父分类
- 直接上传 .md(front matter)并创建文章,编辑页支持 Markdown 导入
- Post 增加 content_format,公开文章页支持 Markdown 渲染
- 复用拾匣密码解锁;首页页脚新增写作台入口
2026-11-21 12:52:28 +08:00

172 lines
5.1 KiB
Python

from django import forms
from blog.models import Category, Post, Tag
from .utils import descendant_ids
class PostForm(forms.Form):
title = forms.CharField(
max_length=200,
label="标题",
widget=forms.TextInput(attrs={
"class": "st-input st-title-input",
"placeholder": "文章标题",
}),
)
slug = forms.CharField(
required=False,
max_length=200,
label="URL 别名",
widget=forms.TextInput(attrs={
"class": "st-input",
"placeholder": "留空自动生成,建议英文",
}),
)
content = forms.CharField(
label="正文",
widget=forms.Textarea(attrs={
"class": "st-input st-content",
"rows": 24,
"spellcheck": "false",
"placeholder": "写点什么,或直接粘贴 / 导入 Markdown",
}),
)
content_format = forms.ChoiceField(
choices=Post.CONTENT_FORMAT_CHOICES,
label="内容格式",
widget=forms.Select(attrs={"class": "st-input"}),
)
excerpt = forms.CharField(
required=False,
label="摘要",
widget=forms.Textarea(attrs={
"class": "st-input",
"rows": 3,
"placeholder": "留空会从正文自动提取",
}),
)
published_at = forms.DateTimeField(
label="发布时间",
input_formats=["%Y-%m-%dT%H:%M", "%Y-%m-%dT%H:%M:%S"],
widget=forms.DateTimeInput(
format="%Y-%m-%dT%H:%M",
attrs={"class": "st-input", "type": "datetime-local"},
),
)
status = forms.ChoiceField(
choices=Post.STATUS_CHOICES,
label="状态",
widget=forms.Select(attrs={"class": "st-input"}),
)
categories = forms.ModelMultipleChoiceField(
queryset=Category.objects.none(),
required=False,
label="分类",
widget=forms.CheckboxSelectMultiple,
)
tags = forms.ModelMultipleChoiceField(
queryset=Tag.objects.none(),
required=False,
label="标签",
widget=forms.CheckboxSelectMultiple,
)
new_tags = forms.CharField(
required=False,
max_length=300,
label="新标签",
widget=forms.TextInput(attrs={
"class": "st-input",
"placeholder": "多个用逗号分隔",
}),
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["categories"].queryset = Category.objects.order_by("name")
self.fields["tags"].queryset = Tag.objects.order_by("name")
class CategoryForm(forms.Form):
id = forms.IntegerField(required=False, widget=forms.HiddenInput)
name = forms.CharField(
max_length=100,
label="名称",
widget=forms.TextInput(attrs={"class": "st-input", "placeholder": "分类名称"}),
)
slug = forms.CharField(
required=False,
max_length=100,
label="URL 别名",
widget=forms.TextInput(attrs={
"class": "st-input",
"placeholder": "留空自动生成,可中文",
}),
)
description = forms.CharField(
required=False,
label="描述",
widget=forms.Textarea(attrs={
"class": "st-input",
"rows": 2,
"placeholder": "可选",
}),
)
parent = forms.ModelChoiceField(
queryset=Category.objects.none(),
required=False,
label="上级分类",
empty_label="无上级分类",
widget=forms.Select(attrs={"class": "st-input"}),
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
edit_id = None
if self.is_bound:
try:
edit_id = int(self.data.get("id") or 0)
except (TypeError, ValueError):
edit_id = None
elif self.initial.get("id"):
try:
edit_id = int(self.initial["id"])
except (TypeError, ValueError):
edit_id = None
queryset = Category.objects.order_by("name")
if edit_id:
category = Category.objects.filter(pk=edit_id).first()
if category:
queryset = queryset.exclude(
pk__in={category.pk} | descendant_ids(category)
)
self.fields["parent"].queryset = queryset
class MarkdownUploadForm(forms.Form):
file = forms.FileField(
label="Markdown 文件",
widget=forms.ClearableFileInput(attrs={
"accept": ".md,.markdown,text/markdown",
}),
)
category = forms.ModelChoiceField(
queryset=Category.objects.none(),
required=False,
label="归入分类",
empty_label="不归入分类",
widget=forms.Select(attrs={"class": "st-input"}),
)
status = forms.ChoiceField(
choices=Post.STATUS_CHOICES,
initial="draft",
label="默认状态",
widget=forms.Select(attrs={"class": "st-input"}),
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["category"].queryset = Category.objects.order_by("name")