settings.py
SUMMERNOTE_CONFIG = {
'attachment_upload_to': my_custom_upload_to_func(),
'attachment_storage_class': 'my.custom.storage.class.name',
'attachment_model': 'my.custom.attachment.model',
}
as you know django-summernote is a rich text editor field used in django.
but when you implement that plugin to your app you get only minimum features and default options. when you add SUMMERNOTE_CONFIG you can customize your rich text field and add more features in it.
for example:
In your question you add some keys and values in 'SUMMERNOTE_CONFIG' it is about adding attachments. attachment_upload_to is path to the folder which needs to save attachments. attachment_model is to create a model for your attachments
eg:
# my/custom/attachment.py
from django_summernote import AbstractAttachment
class Attachment(django_summernote.AbstractAttachment):
pass
# django_project/settings.py
SUMMERNOTE_CONFIG = {
'attachment_upload_to': '/path/to/attachment_folder',
'attachment_model': 'my.custom.attachment.Attachment',
}
Related
I'm using drf_spectacular to be able to use swagger and it's working fine.
I define the required parameters for my API views (whether in the path or in the header) like this:
#extend_schema(
OpenApiParameter(
name='accept-language',
type=str,
location=OpenApiParameter.HEADER,
description="fa or en. The default value is en"
),
)
but I don't want to add these lines of code to all of my API views. Is there an easy way to do this? (Something like defining these lines of code in SPECTACULAR_SETTINGS)
I already found an APPEND_COMPONENTS option in drf_spectacular's documentation but I'm not familiar with it.
You can create a custom schema class from drf_spectacular.openapi.AutoSchema and override the get_override_parameters(...) method as
from drf_spectacular.openapi import AutoSchema
from drf_spectacular.utils import OpenApiParameter
class CustomAutoSchema(AutoSchema):
global_params = [
OpenApiParameter(
name="accept-language",
type=str,
location=OpenApiParameter.HEADER,
description="`fa` or `en`. The default value is en",
)
]
def get_override_parameters(self):
params = super().get_override_parameters()
return params + self.global_params
and then attach this CustomAutoSchema class in your DEFAULT_SCHEMA_CLASS setting
REST_FRAMEWORK = {
# YOUR SETTINGS
"DEFAULT_SCHEMA_CLASS": "path.to.your.custom.class.CustomAutoSchema",
}
I've been using Wagtail to create a website with additional text annotations. The user flow is that there is some highlighted text in a paragraph, which when clicked shows an annotation off to one side. The expected HTML result is:
A sentence with <span class='link'>A link<span class='hidden-text'>Hidden text</span></span>
I would like to achieve this with a single item on the draftail menu, with a UI similar to the URL creator- the user selects the text, and adds the annotation text.
I have followed the instructions on https://docs.wagtail.io/en/stable/advanced_topics/customisation/extending_draftail.html to create a new inline style which produces the link, however I can't then add the hidden-text:
# 1. Use the register_rich_text_features hook.
#hooks.register('register_rich_text_features')
def register_mark_feature(features):
"""
Registering the `mark` feature, which uses the `MARK` Draft.js inline style type,
and is stored as HTML with a `<mark>` tag.
"""
feature_name = 'mark'
type_ = 'SAMPLE'
tag = 'sample'
# 2. Configure how Draftail handles the feature in its toolbar.
control = {
'type': type_,
'label': '?',
'description': 'Hint link',
}
# 3. Call register_editor_plugin to register the configuration for Draftail.
features.register_editor_plugin(
'draftail', feature_name, draftail_features.InlineStyleFeature(control)
)
# 4.configure the content transform from the DB to the editor and back.
db_conversion = {
'from_database_format': {tag: InlineStyleElementHandler(type_)},
'to_database_format': {'style_map': {type_: tag}},
}
# 5. Call register_converter_rule to register the content transformation conversion.
features.register_converter_rule('contentstate', feature_name, db_conversion)
# 6. (optional) Add the feature to the default features list to make it available
# on rich text fields that do not specify an explicit 'features' list
features.default_features.append('mark')
to get What you want, easiest way is to create your own Template tags filters, create your own markdown replacements, let's say, in the rich text you assigned the link to part of your paragraph like this "Click here this is a hidden text" and then you put [ht] right before the "this is a hidden text" and a[th] right after that targeted hidden text,then in the template use self.field_name|replace:"[ht]"|replace:"[th]"
in template tag file (for example myapp/templatetags/my_richtext.py):
from django import template
from wagtail.images.models import Image
from django.utils.safestring import mark_safe
register = template.Library()
#register.filter(needs_autoescape=True)
def replace(value, arg, autoescape=True):
if arg == "[ht]": result = value.replace(arg, "<span class='hidden'>")
elif arg == "[th]": result = value.replace(arg, "</span>")
return mark_safe(result)
How can we add custom classes to wagtail dashboard buttons like 'View Live', 'Edit' Buttons for contents created. When I browse through the core files I noticed core wagtail hooks for admin page like below (I know we are not supposed to edit the core files)
if page.live and page.url:
yield PageListingButton(
_('View live'),
page.url,
attrs={'target': "_blank", 'rel': 'noopener noreferrer', 'title': _("View live version of '{title}'").format(title=page.get_admin_display_title())},
priority=30
)
If I add 'class':'custom-class' to attrs value then the default class disappears and custom-class appears
What is the right way to do this
Edit (On further investigation)
Using wagtail hooks
I created a new wagtail app to register the hook so that I was able to register the button without altering the core files, but now I have two buttons(Duplicate), but I was expecting to edit the existing button's class attrs values
from wagtail.core import hooks
from wagtail.admin import widgets as wagtailadmin_widgets
from wagtail.admin.widgets import Button, ButtonWithDropdownFromHook, PageListingButton
#hooks.register('register_page_listing_buttons')
def page_listing_buttons(page, page_perms, is_parent=False):
if page.live and page.url:
yield PageListingButton(
('View live'),
page.url,
attrs={'target': "_blank", 'rel': 'noopener noreferrer', 'title': ("View live version of '{title}'").format(title=page.get_admin_display_title())},
priority=30
)
This portion of the documentation should address your need.
I have the following classes:
class Plugin(models.Model):
pass
class Instance(models.Model):
plugins = models.ManyToManyField(Plugin, blank=True, null=True)
My goal is to write a cron job which gets a list of plugins from this instance to reflect the instance_plugin relationship. This means that if a plugin is removed from the instance in real life then it must also be reflected here.
for instance in Instance.objects.all():
url = 'http://{}/wordpress_plugins'.format(instance.ip_address)
...
plugins = []
for path, dic in response.json().items():
plugin, created = Plugin.objects.update_or_create(
...
)
plugins.add(plugin)
instance.plugin_set = plugins
I am getting TypeError: Direct assignment to the forward side of a many-to-many set is prohibited. Use plugins.set() instead.
What is the best approach to what I am doing?
Maybe this can do what you want to do.
If you edit you codes with the fields that all your models have,
I can provide you a better one.
for instance in Instance.objects.all():
url = 'http://{}/wordpress_plugins'.format(instance.ip_address)
...
plugins = []
for path, dic in response.json().items():
plugin, created = Plugin.objects.update_or_create(
...
)
plugins.add(plugin)
# First Option
# We add each plugin from the Plugins List
[instance.plugins.add(item) for item in plugins]
# We loop through the instance to see which plugin is not in the New List Plugin, we delete it
for plug in instance.plugins.all():
if plug not in plugins:
plug.delete()
# 2nd Option
# We clear the intance ManyToMany Field
instance.plugins.clear()
# We add each plugin from the Plugins List
[instance.plugins.add(item) for item in plugins]
# We loop The all The Plugin, to see which plugin does not have a reverse relation to instance
for plug in Plugin.objects.all():
if not plug.instance_set.all():
plug.delete()
I am working on a custom markup formatter for Django and I wanted to interface it with MarkItUp.
It is successfully being called when I save the field, which is of type MarkupField, but when I try to preview this field from the Admin interface using the default settings, all I get back in the preview box is plaintext.
I am using the MarkItUp version from here: http://bitbucket.org/carljm/django-markitup/src
Here are my settings:
MARKITUP_FILTER = ('util.wookie.formatter.wookie', {})
JQUERY_URL = 'js/lib/jquery.js'
In urls, I have the following line:
(r'^markitup/', include('markitup.urls')),
Any ideas?
Is something serving the assets from both the MARKITUP_MEDIA_URL the JQUERY_URL locations?