Skip to content

Commit

Permalink
Merge e55bb10 into bf909d2
Browse files Browse the repository at this point in the history
  • Loading branch information
Atrox committed Apr 22, 2019
2 parents bf909d2 + e55bb10 commit ca13808
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ INSTALLED_APPS = [
]
```

Next up you have to specify, in your settings, which library you are using (SweetAlert or SweetAlert2):
```python
# possible options: 'sweetalert', 'sweetalert2' - default is 'sweetalert2'
SWEETIFY_SWEETALERT_LIBRARY = 'sweetalert2'
```

Next add the following lines to the bottom of your layout/base template:
```html
...
Expand Down
12 changes: 12 additions & 0 deletions sweetify/sweetify.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
import sys

from django.conf import settings

from .encoder import LazyEncoder

DEFAULT_OPTS = {
Expand Down Expand Up @@ -45,6 +47,16 @@ def sweetalert(request, title, **kwargs):
if _is_string(persistent):
opts['confirmButtonText'] = persistent

# sweetalert changes
if getattr(settings, 'SWEETIFY_SWEETALERT_LIBRARY', 'sweetalert2') == 'sweetalert':
opts['icon'] = opts.pop('type', None)
opts['closeOnClickOutside'] = opts.pop('allowOutsideClick', None)

if opts.pop('showConfirmButton', False):
opts['button'] = opts['confirmButtonText']
else:
opts['button'] = False

_flash_config(request, opts)


Expand Down
10 changes: 8 additions & 2 deletions sweetify/templatetags/sweetify.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django import template
from django.conf import settings
from django.utils.safestring import mark_safe

register = template.Library()
Expand All @@ -10,8 +11,13 @@ def sweetify(context):
if not opts:
return ''

if getattr(settings, 'SWEETIFY_SWEETALERT_LIBRARY', 'sweetalert2') == 'sweetalert2':
script = 'Swal.fire({})'.format(opts)
else:
script = 'swal({})'.format(opts)

return mark_safe("""
<script>
swal({})
{}
</script>
""".format(opts))
""".format(script))
3 changes: 3 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@
ROOT_URLCONF = 'crispy_forms.tests.urls'
SECRET_KEY = 'secretkey'
SITE_ROOT = os.path.dirname(os.path.abspath(__file__))

# sweetify
SWEETIFY_SWEETALERT_LIBRARY = 'sweetalert2'

0 comments on commit ca13808

Please sign in to comment.