Mark Veidemanis 2c66ade95e | ||
---|---|---|
mixins | ||
.gitignore | ||
.pre-commit-config.yaml | ||
LICENSE | ||
MANIFEST.in | ||
README.md | ||
pyproject.toml | ||
setup.cfg |
README.md
django-crud-mixins
CRUD and form mixins for Django. Useful for single-page-applications using Gridstack.
Usage
Add to your requirements.txt
file:
git+https://git.zm.is/XF/django-crud-mixins
View helpers
The view helpers help create simple CRUD views for your application. They are geared towards a single-page application using Gridstack, but can be used on full pages as well by specifying a type
of page
always.
The view permission helpers add a user=request.user
argument to all queryset filters, ensuring your views can only access the data of the requesting user. Additional filters can be set by overriding the set_extra_args
method (detailed below).
Import the helpers from your view:
from mixins.views import ObjectCreate, ObjectDelete, ObjectList, ObjectRead, ObjectUpdate
Then, add the views:
class AccountList(LoginRequiredMixin, OTPRequiredMixin, ObjectList):
list_template = "partials/account-list.html"
model = Account
page_title = "List of accounts"
list_url_name = "accounts"
list_url_args = ["type"]
submit_url_name = "account_create"
class AccountCreate(LoginRequiredMixin, OTPRequiredMixin, ObjectCreate):
model = Account
form_class = AccountForm
submit_url_name = "account_create"
class AccountUpdate(LoginRequiredMixin, OTPRequiredMixin, ObjectUpdate):
model = Account
form_class = AccountForm
submit_url_name = "account_update"
class AccountDelete(LoginRequiredMixin, OTPRequiredMixin, ObjectDelete):
model = Account
Variables
These variables can be added to the classes to adjust functionality:
Basic:
list_template
: the name of the template for the list viewmodel
: the model you want to view with this helperpage_title
: the page title to renderpage_subtitle
: the page subtitle to render
List URLs:
list_url_name
: the name of the list URL to include in the context, passed aslist_url
list_url_args
: arguments for the above, taken from view kwargs, or locals
Submit URLs:
submit_url_name
: the name of the submit URL to include in the context, passed assubmit_url
-- used in the formsubmit_url_args
: arguments for the above, taken from view kwargs, or locals
For ObjectList
only:
delete_all_url_name
: the name of the delete-all URL to include in the context, passed asdelete_all_url
-- used in the form's "Delete all" buttonwidget_options
: options for the Gristack widget
For ObjectCreate
and ObjectUpdate
only:
hide_cancel
: whether to hide the cancel button in the form
For ObjectUpdate
only:
pk_required
: whether the primary keypk
is required in the URL kwargs
Methods
These methods can be added to the classes to adjust functionality:
For ObjectCreate
and ObjectUpdate
only:
post_save(self, obj)
: called after the object has been saved
For ObjectCreate
only:
pre_save_mutate(self, user, obj)
: called before the object is saved, AbortSave can be raised with an error message to abort the save
These methods can be used on all classes, as they are inherited from the RestrictedViewMixin
:
set_extra_args(self, user)
: adjusts the queryset filter with extra parameters, setself.extra_permission_args
from this method to a dictionary of arguments
Form permission helper
The form permission helper RestrictedFormMixin
can be used as a mixin in your forms:
class YourModelForm(RestrictedFormMixin, ModelForm):
class Meta:
model = YourModel
fields = (
"name",
)
It's that simple!
The form will automatically add a user=request.user
argument to all queryset filters, ensuring your forms can only access the data of the requesting user.