Skip to content

Commit

Permalink
cleanup code and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Atrox committed Dec 3, 2021
1 parent acb8f27 commit 0bc5c8f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 33 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2017, Atrox <mail@atrox.me> (https://atrox.me)
Copyright (c) 2021, Atrox <hello@atrox.dev> (https://atrox.dev)
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
20 changes: 5 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,15 @@ sweetify.error(self.request, 'Some error happened here - reload the site', persi
sweetify.warning(self.request, 'This is a warning... I guess')
```

basic toast alert
We also support toast messages *(SweetAlert2 only)*
```python
import sweetify

# By default success toast with self distrust will be displayed
# Base method, default icon is set to success
sweetify.toast(self.request, 'Cheers to new toast')
# Error toast with self distrust in 3 second will be displayed
sweetify.toast(self.request, 'Oops, something went wrong !', timer=3000)
# Warning toast with "Awesome sauce" button (user interaction required to close)
sweetify.toast(self.request, 'Cheers to new toast', 'warning', persistent_toast="Awesome sauce")

sweetify.toast(self.request, 'Oops, something went wrong !', icon="error", timer=3000)
sweetify.toast(self.request, 'Persistent toast that only goes away once clicked', icon='warning', persistent="Bye toast!")
```

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:
Expand Down Expand Up @@ -97,15 +96,6 @@ def test_view(request):
return redirect('/')
```

Example for toast alerts

```python
import sweetify

# persistent toast alert with warning icon
sweetify.toast(self.request, 'Cheers to new toast', 'warning', persistent_toast="Awesome sauce")
```

## 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
23 changes: 7 additions & 16 deletions sweetify/sweetify.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,18 @@ def _flash_config(request, opts):
request.session["sweetify"] = json.dumps(opts, cls=LazyEncoder)


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


def _treat_data(opts):

button = opts.pop("button", None)
if button:
opts["showConfirmButton"] = True

if isinstance(button, str):
opts["confirmButtonText"] = button

persistent_toast = opts.pop("persistent_toast", None)
if persistent_toast:
opts["showConfirmButton"] = True
opts["timer"] = None
if isinstance(persistent_toast, str):
opts["confirmButtonText"] = persistent_toast

persistent = opts.pop("persistent", None)
if persistent:
opts["showConfirmButton"] = True
Expand All @@ -47,7 +39,6 @@ def _treat_data(opts):

# 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):
Expand All @@ -61,18 +52,18 @@ def sweetalert(request, title, **kwargs):
opts = DEFAULT_OPTS.copy()
opts.update(kwargs)
opts["title"] = title
if opts.pop("is_toast", None):
opts.pop("allowOutsideClick", None)
opts = _treat_data(opts)
_flash_config(request, opts)


def toast(request, title, icon="success", **kwargs):
if getattr(settings, "SWEETIFY_SWEETALERT_LIBRARY", "sweetalert2") == "sweetalert":
raise RuntimeError("toasts are currently not supported in sweetalert")

kwargs["icon"] = icon
kwargs["toast"] = True
kwargs["position"] = "top-end"
kwargs["timerProgressBar"] = True
kwargs["is_toast"] = True
kwargs.setdefault("position", "top-end")
kwargs.setdefault("timerProgressBar", True)
return sweetalert(request, title, **kwargs)


Expand Down
2 changes: 1 addition & 1 deletion sweetify/templatetags/sweetify.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def sweetify(context):

if isinstance(opts, list):
if library == "sweetalert":
raise RuntimeError("multiple alerts are currently not supported for sweetalert 1")
raise RuntimeError("multiple alerts are currently not supported in sweetalert")

script = concatenate(opts)
else:
Expand Down

0 comments on commit 0bc5c8f

Please sign in to comment.