diff --git a/LICENSE b/LICENSE index 67ae86f..688d5ae 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2017, Atrox (https://atrox.me) +Copyright (c) 2021, Atrox (https://atrox.dev) All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/README.md b/README.md index aa5ebac..59cc007 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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. diff --git a/sweetify/sweetify.py b/sweetify/sweetify.py index 325e609..f94c4fd 100644 --- a/sweetify/sweetify.py +++ b/sweetify/sweetify.py @@ -16,12 +16,11 @@ 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 @@ -29,13 +28,6 @@ def _treat_data(opts): 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 @@ -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): @@ -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) diff --git a/sweetify/templatetags/sweetify.py b/sweetify/templatetags/sweetify.py index 1b0a28e..6ef3559 100644 --- a/sweetify/templatetags/sweetify.py +++ b/sweetify/templatetags/sweetify.py @@ -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: