Defining Actions

Defining actions in Djing

Djing actions allow you to perform custom tasks on one or more Django models. For example, you might write an action that sends an email to a user containing account data they have requested. Or, you might write an action to transfer a group of records to another user.

Once an action has been attached to a resource definition, you may initiate it from the resource’s index or detail pages:

Overview

Djing actions may be generated using the djing:action CLI command. By default, all actions are placed in the djing_admin/app/Djing/Actions directory:

commander djing:action EmailAccountProfile

You may generate a destructive action by passing the --destructive option:

commander djing:action DeleteUserData --destructive

To learn how to define Djing actions, let’s look at an example. In this example, we’ll define an action that sends an email message to a user or group of users:

djing_admin/app/Djing/Actions/EmailAccountProfile.py
from typing import List
from django.db.models import base
from djing.core.Actions.Action import Action
from djing.core.Fields.ActionFields import ActionFields
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.emails.WelcomeEmail import WelcomeEmail

class EmailAccountProfile(Action):
    def handle(self, fields: ActionFields, models: List[base.Model]):
        for model in models:
            WelcomeEmail(model).send()

    def fields(self, request: DjingRequest):
        return []

The most important method of an action is the handle method. The handle method receives the values for any fields attached to the action, as well as a List of selected models. The handle method always receives a List of models, even if the action is only being performed against a single model.

Within the handle method, you may perform whatever tasks are necessary to complete the action. You are free to update database records, send emails, call other services, etc. The sky is the limit!

Action Titles

Typically, Djing utilizes the action’s class name to determine the displayable name of the action that should be shown in the action selection menu. If you would like to change the displayable name of the action, you may define a name property on the action class:

djing_admin/app/Djing/Actions/EmailAccountProfile.py
from typing import List
from django.db.models import base
from djing.core.Actions.Action import Action

class EmailAccountProfile(Action):
    name = "Send Account Profile via E-mail"
    
    # ...

Destructive Actions

You may designate an action as destructive or dangerous by defining an action class that extends Djing.Actions.DestructiveAction. This will change the color of the action’s confirm button to red:

Action Fields

Sometimes you may wish to gather additional information from the user before dispatching an action. For this reason, Djing allows you to attach most of Djing’s supported fields directly to an action. When the action is initiated, Djing will prompt the user to provide input for the fields:

To add a field to an action, add the field to the array of fields returned by the action’s fields method:

djing_admin/app/Djing/Actions/EmailAccountProfile.py
from djing.core.Actions.Action import Action
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing.core.Fields.Text import Text


class EmailAccountProfile(Action):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Text.make("Subject"),
        ]

Action Fields Default Values

You may use the default method to set the default value for an action field:

djing_admin/app/Djing/Actions/EmailAccountProfile.py
from djing.core.Actions.Action import Action
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing.core.Fields.Text import Text


class EmailAccountProfile(Action):
    # ...

    def fields(self, request: DjingRequest):
        return [
            Text.make("Subject").default(lambda request: "Test Subject"),
        ]

Action Responses

Typically, when an action is executed, a generic “success” messages is displayed in the Djing UI. However, you are free to customize this response using a variety of methods available via the ActionResponse class. To display a custom “success” message, you may invoke the ActionResponse.success method from your handle method:

djing_admin/app/Djing/Actions/EmailAccountProfile.py
from typing import List
from django.db.models import base
from djing.core.Actions.Action import Action
from djing.core.Fields.ActionFields import ActionFields
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.emails.WelcomeEmail import WelcomeEmail
from djing.core.Actions.ActionResponse import ActionResponse

class EmailAccountProfile(Action):
    def handle(self, fields: ActionFields, models: List[base.Model]):
        for model in models:
            WelcomeEmail(model).send()
        
        return ActionResponse.success("Email has been sent to the users.")

    def fields(self, request: DjingRequest):
        return []

To return a red, “danger” message, you may invoke the ActionResponse.danger method:

djing_admin/app/Djing/Actions/EmailAccountProfile.py
from typing import List
from django.db.models import base
from djing.core.Actions.Action import Action
from djing.core.Fields.ActionFields import ActionFields
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.emails.WelcomeEmail import WelcomeEmail
from djing.core.Actions.ActionResponse import ActionResponse

class EmailAccountProfile(Action):
    def handle(self, fields: ActionFields, models: List[base.Model]):
        for model in models:
            WelcomeEmail(model).send()
        
        return ActionResponse.danger("There was some issue sending email.")

    def fields(self, request: DjingRequest):
        return []

Redirect Responses

To redirect the user to an entirely new location after the action is executed, you may use the ActionResponse.redirect method:

djing_admin/app/Djing/Actions/EmailAccountProfile.py
from typing import List
from django.db.models import base
from djing.core.Actions.Action import Action
from djing.core.Fields.ActionFields import ActionFields
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.emails.WelcomeEmail import WelcomeEmail
from djing.core.Actions.ActionResponse import ActionResponse

class EmailAccountProfile(Action):
    def handle(self, fields: ActionFields, models: List[base.Model]):
        for model in models:
            WelcomeEmail(model).send()
        
        return ActionResponse.redirect("https://example.com")

    def fields(self, request: DjingRequest):
        return []

To redirect the user to another location within Djing, you may use the ActionResponse.visit method:

djing_admin/app/Djing/Actions/EmailAccountProfile.py
from typing import List
from django.db.models import base
from djing.core.Actions.Action import Action
from djing.core.Fields.ActionFields import ActionFields
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.emails.WelcomeEmail import WelcomeEmail
from djing.core.Actions.ActionResponse import ActionResponse

class EmailAccountProfile(Action):
    def handle(self, fields: ActionFields, models: List[base.Model]):
        for model in models:
            WelcomeEmail(model).send()
        
        return ActionResponse.visit("/resources/posts/new", {
            "resource": "users",
        })

    def fields(self, request: DjingRequest):
        return []

To redirect the user to a new location in a new browser tab, you may use the ActionResponse.open_in_new_tab method:

djing_admin/app/Djing/Actions/EmailAccountProfile.py
from typing import List
from django.db.models import base
from djing.core.Actions.Action import Action
from djing.core.Fields.ActionFields import ActionFields
from djing.core.Http.Requests.DjingRequest import DjingRequest
from djing_admin.emails.WelcomeEmail import WelcomeEmail
from djing.core.Actions.ActionResponse import ActionResponse

class EmailAccountProfile(Action):
    def handle(self, fields: ActionFields, models: List[base.Model]):
        for model in models:
            WelcomeEmail(model).send()
        
        return ActionResponse.open_in_new_tab("https://example.com")

    def fields(self, request: DjingRequest):
        return []

Last updated