sweetify


Namesweetify JSON
Version 2.3.1 PyPI version JSON
download
home_pagehttps://github.com/Atrox/sweetify-django
SummarySweetAlert integration for Django
upload_time2022-05-07 16:15:30
maintainer
docs_urlNone
authorAtrox
requires_python>=3.6.2,<4.0.0
licenseBSD-3-Clause
keywords sweetalert django messages
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            # Sweetify - SweetAlert for Django

[![Build Status](https://img.shields.io/travis/Atrox/sweetify-django.svg?style=flat-square)](https://travis-ci.org/Atrox/sweetify-django)
[![Latest Version](https://img.shields.io/pypi/v/sweetify.svg?style=flat-square)](https://pypi.python.org/pypi/sweetify)
[![Coverage Status](https://img.shields.io/coveralls/Atrox/sweetify-django.svg?style=flat-square)](https://coveralls.io/r/Atrox/sweetify-django)

**Sweetify** allows you to use [SweetAlert](http://t4t5.github.io/sweetalert/) or [SweetAlert2](https://github.com/limonte/sweetalert2) for your temporary messages.
_See the examples below, to see how to use this library_

## Installation

**Note: This package does not provide the client-side files of SweetAlert. You have to provide them yourself.**

Install the latest version with `pip`:

```bash
pip install --upgrade sweetify
```

Then you have to add `sweetify` to your django apps:

```python
INSTALLED_APPS = [
    ...
    'sweetify'
]
```

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:

```jinja
...

{% load sweetify %}
{% sweetify %}

</body>
</html>
```

## Usage

You can now easily create alerts in your views with any of the following methods provided by **Sweetify**:

```python
import sweetify

# Base method with no type specified
sweetify.sweetalert(self.request, 'Westworld is awesome', text='Really... if you have the chance - watch it!', persistent='I agree!')

# Additional methods with the type already defined
sweetify.info(self.request, 'Message sent', button='Ok', timer=3000)
sweetify.success(self.request, 'You successfully changed your password')
sweetify.error(self.request, 'Some error happened here - reload the site', persistent=':(')
sweetify.warning(self.request, 'This is a warning... I guess')
```

We also support toast messages _(SweetAlert2 only)_

```python
import sweetify

# Base method, default icon is set to success
sweetify.toast(self.request, 'Cheers to new toast')

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:

```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

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.

```python
from sweetify.views import SweetifySuccessMixin

class TestUpdateView(SweetifySuccessMixin, UpdateView):
    model = TestModel
    fields = ['text']
    success_message = 'TestModel successfully updated!'
```

## Options

**By default, all alerts will dismiss after a sensible default number of seconds.**

Default options set by **Sweetify**:

```python
sweetify.DEFAULT_OPTS = {
    'showConfirmButton': False,
    'timer': 2500,
    'allowOutsideClick': True,
    'confirmButtonText': 'OK',
}
```

The following special options provided by **Sweetify** are available:

```python
# Shows the alert with a button, but will still close automatically
sweetify.sweetalert(self.request, 'Title', button=True)
sweetify.sweetalert(self.request, 'Title', button='Awesome!') # Custom text for the button

# Shows the alert with a button and only closes if the button is pressed
sweetify.sweetalert(self.request, 'Title', persistent=True)
sweetify.sweetalert(self.request, 'Title', persistent='Awesome!') # Custom text for the button
```

You also can use any other available option that [SweetAlert accepts](http://t4t5.github.io/sweetalert/):

```python
sweetify.info(self.request, 'Sweet!', text='Here is a custom image', imageUrl='images/thumbs-up.jpg', timer=5000)
```

If you use [CSP](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) you can use the optional `nonce` parameter on the `sweetify` tag:

```jinja
{% load sweetify %}

<!-- static nonce -->
{% sweetify nonce="XYZ" %}

<!-- from request -->
{% sweetify nonce=request.csp_nonce %}
```

## Development

Use the `Makefile`to execute common tasks:

- Install dependencies

```shell
$ make install
```

- Run all tests

```shell
$ make test
```

## Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

- [Report bugs](https://github.com/atrox/sweetify-django/issues)
- Fix bugs and [submit pull requests](https://github.com/atrox/sweetify-django/pulls)
- Write, clarify, or fix documentation
- Suggest or add new features

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Atrox/sweetify-django",
    "name": "sweetify",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6.2,<4.0.0",
    "maintainer_email": "",
    "keywords": "sweetalert,django,messages",
    "author": "Atrox",
    "author_email": "hello@atrox.dev",
    "download_url": "https://files.pythonhosted.org/packages/aa/61/58e5088c21f01d5bb4386fc056db0e0fb2f33179e92022cb7a1f6afb0fd1/sweetify-2.3.1.tar.gz",
    "platform": null,
    "description": "# Sweetify - SweetAlert for Django\n\n[![Build Status](https://img.shields.io/travis/Atrox/sweetify-django.svg?style=flat-square)](https://travis-ci.org/Atrox/sweetify-django)\n[![Latest Version](https://img.shields.io/pypi/v/sweetify.svg?style=flat-square)](https://pypi.python.org/pypi/sweetify)\n[![Coverage Status](https://img.shields.io/coveralls/Atrox/sweetify-django.svg?style=flat-square)](https://coveralls.io/r/Atrox/sweetify-django)\n\n**Sweetify** allows you to use [SweetAlert](http://t4t5.github.io/sweetalert/) or [SweetAlert2](https://github.com/limonte/sweetalert2) for your temporary messages.\n_See the examples below, to see how to use this library_\n\n## Installation\n\n**Note: This package does not provide the client-side files of SweetAlert. You have to provide them yourself.**\n\nInstall the latest version with `pip`:\n\n```bash\npip install --upgrade sweetify\n```\n\nThen you have to add `sweetify` to your django apps:\n\n```python\nINSTALLED_APPS = [\n    ...\n    'sweetify'\n]\n```\n\nNext up you have to specify, in your settings, which library you are using (SweetAlert or SweetAlert2):\n\n```python\n# possible options: 'sweetalert', 'sweetalert2' - default is 'sweetalert2'\nSWEETIFY_SWEETALERT_LIBRARY = 'sweetalert2'\n```\n\nNext add the following lines to the bottom of your layout/base template:\n\n```jinja\n...\n\n{% load sweetify %}\n{% sweetify %}\n\n</body>\n</html>\n```\n\n## Usage\n\nYou can now easily create alerts in your views with any of the following methods provided by **Sweetify**:\n\n```python\nimport sweetify\n\n# Base method with no type specified\nsweetify.sweetalert(self.request, 'Westworld is awesome', text='Really... if you have the chance - watch it!', persistent='I agree!')\n\n# Additional methods with the type already defined\nsweetify.info(self.request, 'Message sent', button='Ok', timer=3000)\nsweetify.success(self.request, 'You successfully changed your password')\nsweetify.error(self.request, 'Some error happened here - reload the site', persistent=':(')\nsweetify.warning(self.request, 'This is a warning... I guess')\n```\n\nWe also support toast messages _(SweetAlert2 only)_\n\n```python\nimport sweetify\n\n# Base method, default icon is set to success\nsweetify.toast(self.request, 'Cheers to new toast')\n\nsweetify.toast(self.request, 'Oops, something went wrong !', icon=\"error\", timer=3000)\nsweetify.toast(self.request, 'Persistent toast that only goes away once clicked', icon='warning', persistent=\"Bye toast!\")\n```\n\nAdditionally, 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:\n\n```python\nimport sweetify\n\n# Call two consecutive alerts (args1 is the options dict for the first alert and args2 the one for the second alert):\nsweetify.multiple(self.request, args1, args2)\n\n# Call five consecutive alerts:\nsweetify.multiple(self.request, args1, args2, args3, args4, args5)\n```\n\n## Example Usage\n\n```python\nimport sweetify\n\ndef test_view(request):\n    sweetify.success(request, 'You did it', text='Good job! You successfully showed a SweetAlert message', persistent='Hell yeah')\n    return redirect('/')\n```\n\nExample usage for multiple alerts:\n\n```python\nimport sweetify\n\ndef test_view(request):\n    args1 = dict(title='Test1', icon='info', text=\"Text placeholder1\", button=\"Next\")\n    args2 = dict(title='Test2', icon='success', text=\"Text placeholder2\", timer=5000, timerProgressBar='true', persistent=\"Close\")\n    sweetify.multiple(request, args1, args2)\n    return redirect('/')\n```\n\n## Replacement for SuccessMessageMixin\n\nSweetify includes a drop-in replacement for `SuccessMessageMixin`.\nJust replace the Django mixin with Sweetify's `SweetifySuccessMixin` and you are good to go.\n\n```python\nfrom sweetify.views import SweetifySuccessMixin\n\nclass TestUpdateView(SweetifySuccessMixin, UpdateView):\n    model = TestModel\n    fields = ['text']\n    success_message = 'TestModel successfully updated!'\n```\n\n## Options\n\n**By default, all alerts will dismiss after a sensible default number of seconds.**\n\nDefault options set by **Sweetify**:\n\n```python\nsweetify.DEFAULT_OPTS = {\n    'showConfirmButton': False,\n    'timer': 2500,\n    'allowOutsideClick': True,\n    'confirmButtonText': 'OK',\n}\n```\n\nThe following special options provided by **Sweetify** are available:\n\n```python\n# Shows the alert with a button, but will still close automatically\nsweetify.sweetalert(self.request, 'Title', button=True)\nsweetify.sweetalert(self.request, 'Title', button='Awesome!') # Custom text for the button\n\n# Shows the alert with a button and only closes if the button is pressed\nsweetify.sweetalert(self.request, 'Title', persistent=True)\nsweetify.sweetalert(self.request, 'Title', persistent='Awesome!') # Custom text for the button\n```\n\nYou also can use any other available option that [SweetAlert accepts](http://t4t5.github.io/sweetalert/):\n\n```python\nsweetify.info(self.request, 'Sweet!', text='Here is a custom image', imageUrl='images/thumbs-up.jpg', timer=5000)\n```\n\nIf you use [CSP](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) you can use the optional `nonce` parameter on the `sweetify` tag:\n\n```jinja\n{% load sweetify %}\n\n<!-- static nonce -->\n{% sweetify nonce=\"XYZ\" %}\n\n<!-- from request -->\n{% sweetify nonce=request.csp_nonce %}\n```\n\n## Development\n\nUse the `Makefile`to execute common tasks:\n\n- Install dependencies\n\n```shell\n$ make install\n```\n\n- Run all tests\n\n```shell\n$ make test\n```\n\n## Contributing\n\nEveryone is encouraged to help improve this project. Here are a few ways you can help:\n\n- [Report bugs](https://github.com/atrox/sweetify-django/issues)\n- Fix bugs and [submit pull requests](https://github.com/atrox/sweetify-django/pulls)\n- Write, clarify, or fix documentation\n- Suggest or add new features\n",
    "bugtrack_url": null,
    "license": "BSD-3-Clause",
    "summary": "SweetAlert integration for Django",
    "version": "2.3.1",
    "split_keywords": [
        "sweetalert",
        "django",
        "messages"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "475a6c2fb29ff57806a578f10f922481",
                "sha256": "941ed8914467d5a29eb263079fd4f39661df81bfded6476ed4ef2ad57a4c25ba"
            },
            "downloads": -1,
            "filename": "sweetify-2.3.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "475a6c2fb29ff57806a578f10f922481",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6.2,<4.0.0",
            "size": 7433,
            "upload_time": "2022-05-07T16:15:29",
            "upload_time_iso_8601": "2022-05-07T16:15:29.145263Z",
            "url": "https://files.pythonhosted.org/packages/dd/20/83496b38a1b995b41c69c12b9287d4b6e4fbf83882261a615d9a5ddf7420/sweetify-2.3.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "12e5bd5cc6d668b4108c1d28f5f143bb",
                "sha256": "37100d8f02929076cd060b1c1aae7575448af32e61ca8c03bc003548e31ffc70"
            },
            "downloads": -1,
            "filename": "sweetify-2.3.1.tar.gz",
            "has_sig": false,
            "md5_digest": "12e5bd5cc6d668b4108c1d28f5f143bb",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6.2,<4.0.0",
            "size": 7608,
            "upload_time": "2022-05-07T16:15:30",
            "upload_time_iso_8601": "2022-05-07T16:15:30.965148Z",
            "url": "https://files.pythonhosted.org/packages/aa/61/58e5088c21f01d5bb4386fc056db0e0fb2f33179e92022cb7a1f6afb0fd1/sweetify-2.3.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-05-07 16:15:30",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "Atrox",
    "github_project": "sweetify-django",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "lcname": "sweetify"
}
        
Elapsed time: 0.02046s