CRUD mixins for Django class-based views.
Go to file
Mark Veidemanis eba4142960
Vary objects cache on user ID and object list
2023-02-11 14:29:15 +00:00
mixins Vary objects cache on user ID and object list 2023-02-11 14:29:15 +00:00
.gitignore Add templates 2023-02-10 07:20:30 +00:00
.pre-commit-config.yaml Add templates 2023-02-10 07:20:30 +00:00
LICENSE Initial commit 2023-02-10 07:20:30 +00:00
MANIFEST.in Add files to manifest properly 2023-02-10 20:18:49 +00:00
README.md Initial commit 2023-02-10 07:20:30 +00:00
pyproject.toml Remove commented code in config files 2023-02-10 20:31:14 +00:00
setup.cfg Remove commented code in config files 2023-02-10 20:31:14 +00:00

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 view
  • model: the model you want to view with this helper
  • page_title: the page title to render
  • page_subtitle: the page subtitle to render

List URLs:

  • list_url_name: the name of the list URL to include in the context, passed as list_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 as submit_url -- used in the form
  • submit_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 as delete_all_url -- used in the form's "Delete all" button
  • widget_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 key pk 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, set self.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.