# 假设已经有了User模型和相应的权限管理
from django.contrib.auth.models import User
from django.db import models
from django.utils.translation import gettext_lazy as _
class Article(models.Model):
title = models.CharField(max_length=255)
content = models.TextField()
author = models.ForeignKey(User, on_delete=models.CASCADE)
class UEditorFieldFile(models.FileField):
def __init__(self, *args, **kwargs):
kwargs['storage'] = ueditor_storage()
super().__init__(*args, **kwargs)
class UEditorField(models.Field):
def __init__(self, **kwargs):
self.max_length = 255
self.upload_to = kwargs.get('upload_to', '')
self.image_only = kwargs.get('image_only', False)
super().__init__(**kwargs)
def deconstruct(self):
name, path, args, kwargs = super().deconstruct()
kwargs['upload_to'] = self.upload_to
kwargs['image_only'] = self.image_only
return name, path, args, kwargs
def get_internal_type(self):
return "UEditorField"
def formfield(self, **kwargs):
defaults = {
'form_class': UEditorFormField,
'widget': UEditorWidget(
image_only=self.image_only,
upload_to=self.upload_to,
),
}
defaults.update(kwargs)
return super().formfield(**defaults)
# 权限管理相关的函数和类
class UEditorWidget(widgets.Textarea):
def __init__(self, image_only=False, upload_to='ueditor/', **kwargs):
self.image_only = image_only
self.upload_to = upload_to
super().__init__(**kwargs)
class Media:
js = (
'ueditor/ueditor.config.js',
'ueditor/ueditor.all.js',
)
class UEditorFormField(forms.CharField):
def __init__(self, image_only=False, upload_to='ueditor/', **kwargs):
self.image_only = image_only
self.upload_to = upload_to
super().__init__(**kwargs)
def widget_attrs(self, widget):
attrs = super().widget_attrs(widget)
attrs.update({
'image_only': self.image_only,
'upload_to': self.upload_to,
})
return attrs
# 存储接口,需要实现ueditor_storage函数
def ueditor_storage():
# 返回定制的存储系统
# 权限管理装饰器
def require_permission(permission):
def decorator(view_func):
def _wrapped_view(request, *a
评论已关闭