Skip to content

Commit

Permalink
Merged a number of pull requests and fixed issues (#63)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
timmyomahony authored Jan 3, 2018
1 parent e2b99f6 commit ea37b78
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
4 changes: 2 additions & 2 deletions example/app/music/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ 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):
'''A music song.'''
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)
2 changes: 1 addition & 1 deletion example/requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions pagedown/static/admin/css/pagedown.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

.wmd-preview-title {
margin: 10px 0 5px 0 !important;
padding-left: 0px !important;
}

.wmd-preview{
Expand Down Expand Up @@ -78,6 +79,8 @@

.wmd-preview p{
margin: 0 0 15px;
margin-left: 0 !important;
padding-left: 0 !important;
}

.wmd-preview img{
Expand Down
7 changes: 6 additions & 1 deletion pagedown/utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
31 changes: 18 additions & 13 deletions pagedown/widgets.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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 = {
Expand All @@ -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)

0 comments on commit ea37b78

Please sign in to comment.