Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazily load the attachment_upload_to configuration setting #331

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ SETUP

USAGE
-----
## Django models
In `models.py`,

```python
from django.db import models

from django_summernote.fields import SummernoteTextField

class Post(models.Model):
content = SummernoteTextField()
```

`SummernoteTextField` inherits all of the properties of a regular `TextField`. Using this method, the `SummernoteWidget` will automatically be used in the django admin.


## Django admin site
### Apply summernote to all TextField in model
In `admin.py`,
Expand Down
4 changes: 2 additions & 2 deletions django_summernote/apps.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.apps import AppConfig
from django.conf import settings as django_settings
from django_summernote.utils import (
LANG_TO_LOCALE, uploaded_filepath, get_theme_files
LANG_TO_LOCALE, get_theme_files
)


Expand Down Expand Up @@ -29,7 +29,7 @@ def get_default_config(self):

# Attachment settings
'disable_attachment': False,
'attachment_upload_to': uploaded_filepath,
'attachment_upload_to': 'django_summernote.utils.uploaded_filepath',
'attachment_storage_class': None,
'attachment_filesize_limit': 1024 * 1024,
'attachment_require_authentication': False,
Expand Down
21 changes: 20 additions & 1 deletion django_summernote/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,25 @@
}


def get_by_qname(path, desc):
try:
dot = path.rindex('.')
except ValueError:
raise ImproperlyConfigured("%s isn't a %s module." % (path, desc))
module, objname = path[:dot], path[dot + 1:]
try:
mod = import_module(module)
except ImportError as e:
raise ImproperlyConfigured('Error importing %s module %s: "%s"' %
(desc, module, e))
try:
obj = getattr(mod, objname)
return obj
except AttributeError:
raise ImproperlyConfigured('%s module "%s" does not define "%s"'
% (desc[0].upper() + desc[1:], module, objname))


def using_config(_func=None):
"""
This allows a function to use Summernote configuration
Expand Down Expand Up @@ -204,7 +223,7 @@ def get_attachment_upload_to():
"""
Return 'attachment_upload_to' from configuration
"""
return config['attachment_upload_to']
return get_by_qname(config['attachment_upload_to'], 'attachment_upload_to')


@using_config
Expand Down