存储与Django(-cms)页面链接的简单笔记。支持apphooks(但不支持自定义应用)!
项目描述
django_simple_notes
通过通用外键(使用内容类型)将私有笔记(仅对登录用户可用)添加到您的Django-CMS站点的不同页面。
需求
django-filer
:使用文件夹/权限和专用界面管理媒体文件django-ckeditor
:如果您仍在使用django<3,请安装版本<6django-ckeditor-filebrowser-filer
:允许使用django-filer文件系统进行图片/文件上传django
:与django 2.2+配合使用python
:与python >= 3.10(可能与较旧的python 3版本兼容)
安装
- 运行
python3 -m pip install django_simple_notes
- 可选:如果您仍在使用django < 3,请添加
django-ckeditor<6
- 可选:如果您仍在使用django < 3,请添加
- 将这些应用添加到您的
INSTALLED_APPS
"filer", "ckeditor", "ckeditor_uploader", # for hosting images in your ckeditor view, see below for a ready-to-use config "ckeditor_filebrowser_filer", "django_simple_notes",
- 配置此(在此处查看文档)
CKEDITOR_UPLOAD_PATH = ""
- 将这些URL添加到您的
urls.py
# Upload images using ckeditor & django-filer path('ckeditor/', include('ckeditor_uploader.urls')), path('filer/', include('filer.urls')), path('filebrowser_filer/', include('ckeditor_filebrowser_filer.urls')),
- 运行
python manage.py migrate django_simple_notes
- 就是这样!
特性
- 在您的网站几乎每个页面上添加私有笔记
- 原生支持 djangocms 页面和 djangocms-blog 文章!
- 您可以为 apphooks 添加自己的函数以支持更多类型的页面!
- 私人笔记包含 1 个 ckeditor 字段(您可以在其中添加图片),以及许多文件字段(例如,如果您需要存储特定页面上的 PDF 文件)
- 支持在不同语言中对同一页面的不同笔记
- 支持单站点和多站点!
模板标签,太棒了!
这里有一个模板标签,您可以使用它来显示文本,如果当前页面存在私人笔记并且不为空。如果当前用户已登录并且有查看私人笔记的权限,则模板标签将仅返回 True
。
以下是使用方法
{% load django_simple_notes %}
{% have_non_empty_note as check_have_non_empty_note %}
{% if check_have_non_empty_note %}
<p>
<i>This page have a private note.</i>
</p>
{% endif %}
它是如何工作的?
主要逻辑在 cms_toolbars.py
中的 get_content_type_and_id
函数中。
此函数将尝试检索对象的内容类型和 ID(CMS 页面、DjangoCMS 博文等)。如果找到对象,则另一个函数将为该对象创建一个空的 SimpleNote,并将其保存。这样,我们就不需要在工具栏中区分笔记的创建和编辑链接(我不知道是否可以只刷新工具栏以返回正确的链接(创建/编辑))。
另一个酷炫的函数是 admin.py
中的 get_queryset
。如果我们在 change
页面(在管理员中编辑对象),它将返回完整的查询集。然而,在 list
视图中,它将仅返回“非空”的对象(为了不过度拥挤列表视图)。
自定义它!
您可以在设置中定义自己的函数来查找 object_ct
和 object_id
!
Django simple notes 将尝试从您的站点设置中获取一个名为 SIMPLE_NOTES_CUSTOM_CONTENT_TYPE_AND_ID
的函数。然后执行它,如果它返回 None, None
,则启动其自己的 get_content_type_and_id
函数(以检索 cms/djangocms-blog ct/id)。
以下是一个入门示例
只需将此文件复制/粘贴到您的站点设置中,并根据您的需求更新它。
def SIMPLE_NOTES_CUSTOM_CONTENT_TYPE_AND_ID(request):
# init parameters
object_ct = None
object_id = None
# condition from request (use this to find the object/page type)
if request.resolver_match.view_name in (
"my_app_view",
):
# necessary imports, be sure to include them in your function or you might stumble accross an import loop
from my_app.models import MyModel
slug = (
request.resolver_match.kwargs["slug"]
if "slug" in request.resolver_match.kwargs
else ""
)
# a condition (if we have a slug, then retrieve the object using its slug)
if slug:
my_object = MyModel.objects.get(slug=slug)
object_ct = my_object
object_id = my_object.id
else:
# another condition (for if we have its id)
id = (
request.resolver_match.kwargs["pk"]
if "pk" in request.resolver_match.kwargs
else ""
)
if id:
my_object = MyModel.objects.get(id=id)
object_ct = MyModel
object_id = my_object.id
# return found ct/id or None/None
# None/None will allow the regular `get_content_type_and_id` to proceed, searching for cms or blog pages
return object_ct, object_id
为了获取对象 URL,您需要添加另一个函数
此函数名为 SIMPLE_NOTES_ADMIN_URLS
,如果它存在,则在 admin.py
中调用。
其目的是返回当前对象的 URL,以便在管理员列表视图中添加直接链接到页面。
如果返回的 URL 是 None
,则 simple notes 将尝试获取 CMS 页面或 djangocms 博文的文章 URL。
如果没有找到链接,则将显示对象名称,但不带链接。
以下是一个入门示例
def SIMPLE_NOTES_ADMIN_URLS(obj):
if obj.content_type.model == "mymodel": # an object from a model with get_absolute_url()
from my_app.models import MyModel
return MyModel.objects.get(id=obj.object_id).get_absolute_url()
if obj.content_type.model == "myothermodel": # another model, which don't have get_absolute_url()
from django.urls import reverse
return reverse("view-name", kwargs={"pk": obj.object_id})
return None
哦,您还可以自定义此功能
SIMPLE_NOTES_TOOLBAR_MENU_TEXT
(默认为_("Simple notes")
):工具栏菜单文本。SIMPLE_NOTES_TOOLBAR_EDIT_TEXT
(默认为_("Edit note for current page")
):工具栏编辑按钮文本。SIMPLE_NOTES_TOOLBAR_LIST_TEXT
(默认为_("Notes list")
):工具栏“显示笔记列表”文本。SIMPLE_NOTES_APP_NAME
(默认为SIMPLE_NOTES_TOOLBAR_MENU_TEXT
内容):Django 管理员中的应用程序名称。SIMPLE_NOTES_NOTE_ADMIN_NAME
(默认为_("note")
):Django 管理员中单个对象的名称(用于标题:“编辑 笔记”)。
带有默认值的工具栏截图。
Ckeditor 配置
您需要配置 django-ckeditor
以在笔记中使其工作。以下是一个可直接粘贴到项目设置中的现成配置片段。
CKEDITOR_UPLOAD_PATH = "uploads/"
CKEDITOR_IMAGE_BACKEND = "pillow"
CKEDITOR_THUMBNAIL_SIZE = (150, 150)
CKEDITOR_ALLOW_NONIMAGE_FILES = False
CKEDITOR_CONFIGS = {
"default": {
"language": "{{ language }}",
"toolbar": "Simple",
"toolbar_Simple": [
["Undo", "Redo"],
["Styles", "Format"],
["TextColor", "BGColor"],
["Subscript", "Superscript", "-", "RemoveFormat", "PasteText", "PasteFromWord", "FilerImage"],
["Link", "Unlink"],
["Source"],
],
"autoParagraph": False,
"colorButton_colors": "01b6ad,00b6ef,a0cd49,ffc01c,9d1a75,fff,000",
"skin": "moono-lisa",
"height": "600px",
"extraPlugins": "filerimage",
"removePlugins": "image" # do not use the classic image plugin, use the one from django-ckeditor-filebrowser-filer
}
}
您可以通过查看 django-ckeditor 文档 来了解有关这些配置值以及如何自定义它们的更多信息。
待办事项?
以下是(可能)未来功能的预览
- 支持简单的 Django 应用程序!
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。