-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from smotornyuk/master
feat: switch dashboard to collections
- Loading branch information
Showing
4 changed files
with
165 additions
and
88 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 |
---|---|---|
@@ -0,0 +1,142 @@ | ||
from __future__ import annotations | ||
|
||
import sqlalchemy as sa | ||
from dominate import tags | ||
from typing import Any | ||
import ckan.plugins.toolkit as tk | ||
from ckanext.collection.types import ( | ||
InputFilter, | ||
LinkFilter, | ||
) | ||
from ckanext.collection.utils import Filters, ModelData | ||
|
||
from ckanext.collection.types import InputFilter, LinkFilter, SelectFilter | ||
from ckanext.ap_main.collection.base import ( | ||
ApCollection, | ||
BulkAction, | ||
RowAction, | ||
GlobalAction, | ||
) | ||
|
||
from ckanext.mailcraft.model import Email | ||
|
||
|
||
class MailCollection(ApCollection[Any]): | ||
ColumnsFactory = ApCollection.ColumnsFactory.with_attributes( | ||
names=[ | ||
"bulk-action", | ||
"id", | ||
"subject", | ||
"sender", | ||
"recipient", | ||
"state", | ||
"timestamp", | ||
"row_actions", | ||
], | ||
sortable={ | ||
"subject", | ||
"sender", | ||
"recipient", | ||
"timestamp", | ||
}, | ||
searchable={"subject"}, | ||
labels={ | ||
"bulk-action": tk.literal( | ||
tags.input_( | ||
type="checkbox", | ||
name="bulk_check", | ||
id="bulk_check", | ||
data_module="ap-bulk-check", | ||
data_module_selector='input[name="entity_id"]', | ||
) | ||
), | ||
"id": "Id", | ||
"subject": "Subject", | ||
"sender": "Sender", | ||
"recipient": "Recipient", | ||
"state": "State", | ||
"timestamp": "Timestamp", | ||
"row_actions": "Actions", | ||
}, | ||
serializers={ | ||
"id": [("copy_into", {"target": "bulk-action"})], | ||
"timestamp": [("date", {})], | ||
}, | ||
) | ||
|
||
DataFactory = ModelData.with_attributes( | ||
model=Email, | ||
use_naive_search=True, | ||
use_naive_filters=True, | ||
static_columns=[*sa.inspect(Email).columns, Email.id.label("bulk-action")], | ||
) | ||
|
||
FiltersFactory = Filters.with_attributes( | ||
static_actions=[ | ||
BulkAction( | ||
name="bulk-action", | ||
type="bulk_action", | ||
options={ | ||
"label": "Action", | ||
"options": [{"value": "1", "text": "Remove selected mails"}], | ||
}, | ||
), | ||
GlobalAction( | ||
name="clear_mails", | ||
type="global_action", | ||
options={ | ||
"label": "Clear mails", | ||
"attrs": { | ||
"type": "submit", | ||
"class": "btn btn-danger", | ||
"data-module": "ap-confirm-action", | ||
"data-module-content": ( | ||
"Are you sure you want to clear all mails?" | ||
), | ||
"data-module-with-data": "true", | ||
}, | ||
}, | ||
), | ||
RowAction( | ||
name="edit", | ||
type="row_action", | ||
options={ | ||
"endpoint": "mailcraft.mail_read", | ||
"label": "View", | ||
"params": {"mail_id": "$id"}, | ||
}, | ||
), | ||
], | ||
static_filters=[ | ||
InputFilter( | ||
name="q", | ||
type="input", | ||
options={ | ||
"label": "Search", | ||
"placeholder": "Search", | ||
}, | ||
), | ||
SelectFilter( | ||
name="state", | ||
type="select", | ||
options={ | ||
"label": "Level", | ||
"options": [ | ||
{"value": "", "text": "All"}, | ||
{"value": Email.State.failed, "text": "failed"}, | ||
{"value": Email.State.stopped, "text": "stopped"}, | ||
{"value": Email.State.success, "text": "success"}, | ||
], | ||
}, | ||
), | ||
LinkFilter( | ||
name="type", | ||
type="link", | ||
options={ | ||
"label": "Clear", | ||
"endpoint": "ap_report.logs", | ||
"kwargs": {}, | ||
}, | ||
), | ||
], | ||
) |
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
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 |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
from ckanext.ap_main.utils import ap_before_request | ||
|
||
import ckanext.mailcraft.config as mc_config | ||
from ckanext.collection.shared import get_collection | ||
|
||
mailcraft = Blueprint("mailcraft", __name__, url_prefix="/admin-panel/mailcraft") | ||
mailcraft.before_request(ap_before_request) | ||
|
@@ -23,57 +24,12 @@ def get(self) -> str: | |
return tk.render( | ||
"mailcraft/dashboard.html", | ||
extra_vars={ | ||
"page": self._get_pager( | ||
tk.get_action("mc_mail_list")(_build_context(), {}) | ||
"collection": get_collection( | ||
"mailcraft-dashboard", parse_params(tk.request.args) | ||
), | ||
"columns": self._get_table_columns(), | ||
"bulk_options": self._get_bulk_options(), | ||
}, | ||
) | ||
|
||
def _get_pager(self, mailcraft_list: list[dict[str, Any]]) -> Page: | ||
return Page( | ||
collection=mailcraft_list, | ||
page=tk.h.get_page_number(tk.request.args), | ||
url=tk.h.pager_url, | ||
item_count=len(mailcraft_list), | ||
items_per_page=mc_config.get_mail_per_page(), | ||
) | ||
|
||
def _get_table_columns(self) -> list[dict[str, Any]]: | ||
return [ | ||
tk.h.ap_table_column("id", sortable=False, width="5%"), | ||
tk.h.ap_table_column("subject", sortable=False, width="10%"), | ||
tk.h.ap_table_column("sender", sortable=False, width="10%"), | ||
tk.h.ap_table_column("recipient", sortable=False, width="20%"), | ||
tk.h.ap_table_column("state", sortable=False, width="5%"), | ||
tk.h.ap_table_column( | ||
"timestamp", column_renderer="ap_date", sortable=False, width="10%" | ||
), | ||
tk.h.ap_table_column( | ||
"actions", | ||
sortable=False, | ||
width="10%", | ||
column_renderer="ap_action_render", | ||
actions=[ | ||
tk.h.ap_table_action( | ||
"mailcraft.mail_read", | ||
label=tk._("View"), | ||
params={"mail_id": "$id"}, | ||
attributes={"class": "btn btn-primary"}, | ||
) | ||
], | ||
), | ||
] | ||
|
||
def _get_bulk_options(self): | ||
return [ | ||
{ | ||
"value": "1", | ||
"text": tk._("Remove selected mails"), | ||
}, | ||
] | ||
|
||
def _get_bulk_actions(self, value: str) -> Callable[[list[str]], bool] | None: | ||
return {"1": self._remove_emails}.get(value) | ||
|
||
|
@@ -173,10 +129,10 @@ def send_test_email() -> Response: | |
subject="Hello world", | ||
recipients=["[email protected]"], | ||
body="Hello world", | ||
body_html=tk.render("mailcraft/emails/test.html", extra_vars={ | ||
"site_url": mailer.site_url, | ||
"site_title": mailer.site_title | ||
}), | ||
body_html=tk.render( | ||
"mailcraft/emails/test.html", | ||
extra_vars={"site_url": mailer.site_url, "site_title": mailer.site_title}, | ||
), | ||
) | ||
tk.h.flash_success(tk._("Test email has been sent")) | ||
|
||
|