forked from trco/django-bootstrap-modal-forms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix waring from BSModalDeleteView / DeleteMessageMixin with Django 4 * fix when form has field named method * add new mixin for FormValidation, remove SuccessMessageMixin dependecy * Updated project to current Django LTS version Updated deprecated "is_safe_url" function to "url_has_allowed_host_and_scheme". Added automatically database population, if DB is empty. Updated requirements to current version. Updated settings. Removed outdated version support. Updated gitignore. * Minor refactoring * Updated Project Added type hints. Updated test cases. Removed last remaining snippets for outdated Django versions. * Removed unused constant * Updated required version * Minor Bugfix Removed __slots__ from dataclass, to match Python 3.8 support. * refactor save method in CreateUpdateAjaxMixin * remove compatibility.py * remove utils.py * remove types * add is_ajax method and remove imports * remove unneeded class inheritence * remove obsolete class name parameter * revert examples to version in master branch * remove static folder * remove types from tests * remove unneeded comments * update get and set for form action and method attributes * update bootstrap5.modal.forms.min.js * update assert string to pass the test * update DeleteMessageMixin comment * cleanup .gitignore --------- Co-authored-by: Christian Wiegand <christianwgd@users.noreply.github.com> Co-authored-by: Mark Monaghan <markmono@gmail.com> Co-authored-by: aDramaQueen <richard.saeuberlich@o2online.de>
- Loading branch information
1 parent
36e1597
commit 024e41a
Showing
22 changed files
with
215 additions
and
313 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ database/db.sqlite3 | |
geckodriver.log | ||
__pycache__ | ||
*.pyc | ||
.env/ | ||
.env/ |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,96 @@ | ||
from django.contrib import messages | ||
from django.contrib.auth import login as auth_login | ||
from django.http import HttpResponseRedirect | ||
from django.http import HttpResponseRedirect, HttpResponse | ||
|
||
from .utils import is_ajax | ||
|
||
|
||
class PassRequestMixin(object): | ||
class PassRequestMixin: | ||
""" | ||
Mixin which puts the request into the form's kwargs. | ||
Form Mixin which puts the request into the form's kwargs. | ||
Note: Using this mixin requires you to pop the `request` kwarg | ||
out of the dict in the super of your form's `__init__`. | ||
""" | ||
|
||
def get_form_kwargs(self): | ||
kwargs = super(PassRequestMixin, self).get_form_kwargs() | ||
kwargs.update({'request': self.request}) | ||
kwargs = super().get_form_kwargs() | ||
kwargs['request'] = self.request | ||
return kwargs | ||
|
||
|
||
class PopRequestMixin(object): | ||
class PopRequestMixin: | ||
""" | ||
Mixin which pops request out of the kwargs and attaches it to the form's | ||
Form Mixin which pops request out of the kwargs and attaches it to the form's | ||
instance. | ||
Note: This mixin must precede forms.ModelForm/forms.Form. The form is not | ||
expecting these kwargs to be passed in, so they must be popped off before | ||
anything else is done. | ||
""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
def __init__(self, *args, **kwargs) -> None: | ||
self.request = kwargs.pop('request', None) | ||
super(PopRequestMixin, self).__init__(*args, **kwargs) | ||
super().__init__(*args, **kwargs) | ||
|
||
|
||
class CreateUpdateAjaxMixin(object): | ||
class CreateUpdateAjaxMixin: | ||
""" | ||
Mixin which passes or saves object based on request type. | ||
ModelForm Mixin which passes or saves object based on request type. | ||
""" | ||
|
||
def save(self, commit=True): | ||
if not is_ajax(self.request.META) or self.request.POST.get('asyncUpdate') == 'True': | ||
instance = super(CreateUpdateAjaxMixin, self).save(commit=commit) | ||
else: | ||
instance = super(CreateUpdateAjaxMixin, self).save(commit=False) | ||
return instance | ||
isAjaxRequest = is_ajax(self.request.META) | ||
asyncUpdate = self.request.POST.get('asyncUpdate') == 'True' | ||
|
||
if not isAjaxRequest or asyncUpdate: | ||
return super().save(commit=commit) | ||
if isAjaxRequest: | ||
return super().save(commit=False) | ||
|
||
|
||
class DeleteMessageMixin(object): | ||
class DeleteMessageMixin: | ||
""" | ||
Mixin which adds message to BSModalDeleteView and only calls the delete method if request | ||
is not ajax request. | ||
Generic View Mixin which adds message to BSModalDeleteView and only calls the post method if request | ||
is not ajax request. In case request is ajax post method calls delete method, which redirects to success url. | ||
""" | ||
def delete(self, request, *args, **kwargs): | ||
|
||
def post(self, request, *args, **kwargs): | ||
if not is_ajax(request.META): | ||
messages.success(request, self.success_message) | ||
return super(DeleteMessageMixin, self).delete(request, *args, **kwargs) | ||
return super().post(request, *args, **kwargs) | ||
else: | ||
self.object = self.get_object() | ||
return HttpResponseRedirect(self.get_success_url()) | ||
|
||
class LoginAjaxMixin(object): | ||
|
||
class LoginAjaxMixin: | ||
""" | ||
Mixin which authenticates user if request is not ajax request. | ||
Generic View Mixin which authenticates user if request is not ajax request. | ||
""" | ||
|
||
def form_valid(self, form): | ||
if not is_ajax(self.request.META): | ||
auth_login(self.request, form.get_user()) | ||
messages.success(self.request, self.success_message) | ||
return HttpResponseRedirect(self.get_success_url()) | ||
return HttpResponseRedirect(self.get_success_url()) | ||
|
||
|
||
class FormValidationMixin: | ||
""" | ||
Generic View Mixin which saves object and redirects to success_url if request is not ajax request. Otherwise response 204 No content is returned. | ||
""" | ||
|
||
def form_valid(self, form): | ||
isAjaxRequest = is_ajax(self.request.META) | ||
asyncUpdate = self.request.POST.get('asyncUpdate') == 'True' | ||
|
||
if isAjaxRequest: | ||
if asyncUpdate: | ||
form.save() | ||
return HttpResponse(status=204) | ||
|
||
form.save() | ||
messages.success(self.request, self.success_message) | ||
return HttpResponseRedirect(self.success_url) | ||
|
||
|
||
def is_ajax(meta): | ||
return 'HTTP_X_REQUESTED_WITH' in meta and meta['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' |
Oops, something went wrong.