Skip to content

Commit

Permalink
Merge f65b177 into e785a7f
Browse files Browse the repository at this point in the history
  • Loading branch information
PauMAVA committed Nov 26, 2019
2 parents e785a7f + f65b177 commit b963d1d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 18 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ sweetify.error(self.request, 'Some error happened here - reload the site', persi
sweetify.warning(self.request, 'This is a warning... I guess')
```

Additionally, you can issue multiple alerts without reloading the page **ONLY** if you are using SweetAlerts 2. To do so, you must define your options in a dictionary:
```python
import sweetify

# Call two consecutive alerts (args1 is the options dict for the first alert and args2 the one for the second alert):
sweetify.multiple(self.request, args1, args2)

# Call five consecutive alerts:
sweetify.multiple(self.request, args1, args2, args3, args4, args5)
```
## Example Usage
```python
import sweetify
Expand All @@ -64,7 +74,16 @@ def test_view(request):
sweetify.success(request, 'You did it', text='Good job! You successfully showed a SweetAlert message', persistent='Hell yeah')
return redirect('/')
```
Example usage for multiple alerts:
```python
import sweetify

def test_view(request):
args1 = dict(title='Test1', icon='info', text="Text placeholder1", button="Next")
args2 = dict(title='Test2', icon='success', text="Text placeholder2", timer=5000, timerProgressBar='true', persistent="Close")
sweetify.multiple(request, args1, args2)
return redirect('/')
```
## Replacement for SuccessMessageMixin
Sweetify includes a drop-in replacement for `SuccessMessageMixin`.
Just replace the Django mixin with Sweetify's `SweetifySuccessMixin` and you are good to go.
Expand Down
24 changes: 18 additions & 6 deletions sweetify/sweetify.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
def _flash_config(request, opts):
request.session['sweetify'] = json.dumps(opts, cls=LazyEncoder)

def _flash_multiple_configs(request, jsonData):
request.session['sweetify'] = jsonData

def _is_string(s):
# if we use Python 3
Expand All @@ -24,12 +26,7 @@ def _is_string(s):
# we use Python 2
return isinstance(s, basestring)


def sweetalert(request, title, **kwargs):
opts = DEFAULT_OPTS.copy()
opts.update(kwargs)

opts['title'] = title
def _treat_data(opts):

button = opts.pop('button', None)
if button:
Expand All @@ -56,7 +53,13 @@ def sweetalert(request, title, **kwargs):
opts['button'] = opts['confirmButtonText']
else:
opts['button'] = False
return opts

def sweetalert(request, title, **kwargs):
opts = DEFAULT_OPTS.copy()
opts.update(kwargs)
opts['title'] = title
opts = _treat_data(opts)
_flash_config(request, opts)


Expand All @@ -78,3 +81,12 @@ def error(request, title, **kwargs):
def warning(request, title, **kwargs):
kwargs['type'] = 'warning'
return sweetalert(request, title, **kwargs)

def multiple(request, *args):
optsls = []
for dictionary in args:
opts = DEFAULT_OPTS.copy()
opts.update(dictionary)
opts = _treat_data(opts)
optsls.append(json.dumps(opts, cls=LazyEncoder))
_flash_multiple_configs(request, optsls)
40 changes: 28 additions & 12 deletions sweetify/templatetags/sweetify.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,33 @@

@register.simple_tag(takes_context=True)
def sweetify(context):
opts = context.request.session.pop('sweetify', None)
if not opts:
return ''
opts = context.request.session.pop('sweetify', None)

if getattr(settings, 'SWEETIFY_SWEETALERT_LIBRARY', 'sweetalert2') == 'sweetalert2':
script = 'Swal.fire({})'.format(opts)
else:
script = 'swal({})'.format(opts)
if not opts:
return ''
if isinstance(opts, list):
if getattr(settings, 'SWEETIFY_SWEETALERT_LIBRARY', 'sweetalert2') == 'sweetalert':
#TODO Add sweetalert 1 compatibility with multiple alerts.
return''
script = concatenate(opts)
else:
if getattr(settings, 'SWEETIFY_SWEETALERT_LIBRARY', 'sweetalert2') == 'sweetalert2':
script = 'Swal.fire({})'.format(opts)
else:
script = 'swal({})'.format(opts)
return mark_safe("""<script>{}</script>""".format(script))

return mark_safe("""
<script>
{}
</script>
""".format(script))
def concatenate(list):
i = 0
length = len(list)
script = 'Swal.fire({})'
for opts in list:
if i == 0:
script = script.format(opts)
i = i + 1
elif i < length:
script += '.then((result) => {{if (result.value) {{Swal.fire({}'.format(opts)
script += ')'
for k in range(length - 1):
script += '}})'
return script

0 comments on commit b963d1d

Please sign in to comment.