From ea37b78e4d8a2a66be8a326c2d8508e338bc1e6a Mon Sep 17 00:00:00 2001 From: Timmy O'Mahony Date: Wed, 3 Jan 2018 20:23:38 +0000 Subject: [PATCH] Merged a number of pull requests and fixed issues (#63) * Make media on AdminPagedownWidget lazy This avoids resolving static files on import which may fail if static files are not in manifest when using ManifestStaticFilesStorage. * Include super media in AdminPagedownWidget media property * Lazily handle media for Django 1.10+ * Cleaned up comments * Cleaned up comments * Address #60 * Fix HTML Preview Label * Update the example code to support Django2.0 --- example/app/music/models.py | 4 ++-- example/requirements.txt | 2 +- pagedown/static/admin/css/pagedown.css | 3 +++ pagedown/utils.py | 7 +++++- pagedown/widgets.py | 31 +++++++++++++++----------- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/example/app/music/models.py b/example/app/music/models.py index 9f1a6ce..cf3cc06 100644 --- a/example/app/music/models.py +++ b/example/app/music/models.py @@ -11,7 +11,7 @@ class Album(models.Model): '''A music album.''' name = models.CharField(max_length=128) description = models.TextField(blank=True) - artist = models.ForeignKey(Artist) + artist = models.ForeignKey(Artist, on_delete=models.CASCADE) class Song(models.Model): @@ -19,4 +19,4 @@ class Song(models.Model): name = models.CharField(max_length=128) description = models.TextField(blank=True) lyrics = models.TextField(blank=True) - album = models.ForeignKey(Album) + album = models.ForeignKey(Album, on_delete=models.CASCADE) diff --git a/example/requirements.txt b/example/requirements.txt index 7663bba..16d2bbd 100644 --- a/example/requirements.txt +++ b/example/requirements.txt @@ -1,5 +1,5 @@ appdirs==1.4.3 -Django==1.11 +Django==2.0 django-pagedown==0.1.3 packaging==16.8 pyparsing==2.2.0 diff --git a/pagedown/static/admin/css/pagedown.css b/pagedown/static/admin/css/pagedown.css index 8d84359..deb3afb 100644 --- a/pagedown/static/admin/css/pagedown.css +++ b/pagedown/static/admin/css/pagedown.css @@ -24,6 +24,7 @@ .wmd-preview-title { margin: 10px 0 5px 0 !important; + padding-left: 0px !important; } .wmd-preview{ @@ -78,6 +79,8 @@ .wmd-preview p{ margin: 0 0 15px; + margin-left: 0 !important; + padding-left: 0 !important; } .wmd-preview img{ diff --git a/pagedown/utils.py b/pagedown/utils.py index 6e731e4..eface46 100644 --- a/pagedown/utils.py +++ b/pagedown/utils.py @@ -1,12 +1,17 @@ +from django import VERSION from django.conf import settings def compatible_staticpath(path): ''' Try to return a path to static the static files compatible all - the way back to Django 1.2. If anyone has a cleaner or better + the way back to Django 1.2. If anyone has a cleaner or better way to do this let me know! ''' + if VERSION >= (1, 10): + # Since Django 1.10, forms.Media automatically invoke static + # lazily on the path if it is relative. + return path try: # >= 1.4 from django.templatetags.static import static diff --git a/pagedown/widgets.py b/pagedown/widgets.py index ad932f6..6313133 100644 --- a/pagedown/widgets.py +++ b/pagedown/widgets.py @@ -1,20 +1,23 @@ from django import VERSION, forms from django.contrib.admin import widgets as admin_widgets +from django.utils.html import conditional_escape +from django.template import Context, loader + +# Django 1.7 compatibility try: from django.forms.utils import flatatt except ImportError: - from django.forms.util import flatatt # <1.7 -from django.utils.html import conditional_escape -from django.template import Context, loader + from django.forms.util import flatatt from pagedown import settings as pagedown_settings from pagedown.utils import compatible_staticpath +# Python 3 compatibility +# https://docs.djangoproject.com/en/1.5/topics/python3/#string-handling try: from django.utils.encoding import force_unicode -except ImportError: # python3 - # https://docs.djangoproject.com/en/1.5/topics/python3/#string-handling +except ImportError: from django.utils.encoding import force_text as force_unicode @@ -53,7 +56,7 @@ def render(self, name, value, attrs=None): final_attrs["class"] = "" final_attrs["class"] += " wmd-input" template = loader.get_template(self.template) - + # Compatibility fix: # see https://github.com/timmyomahony/django-pagedown/issues/42 context = { @@ -67,10 +70,12 @@ def render(self, name, value, attrs=None): class AdminPagedownWidget(PagedownWidget, admin_widgets.AdminTextareaWidget): - class Media: - css = { - "all": (compatible_staticpath("admin/css/pagedown.css"),) - } - js = ( - compatible_staticpath("admin/js/pagedown.js"), - ) + def _media(self): + return super(AdminPagedownWidget, self).media + forms.Media( + css={ + "all": (compatible_staticpath("admin/css/pagedown.css"),) + }, + js=( + compatible_staticpath("admin/js/pagedown.js"), + )) + media = property(_media)