Skip to content

Commit

Permalink
Merge pull request #1 from smotornyuk/master
Browse files Browse the repository at this point in the history
feat: switch dashboard to collections
  • Loading branch information
mutantsan authored Jan 29, 2024
2 parents 73e230a + 037ad36 commit 988a28d
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 88 deletions.
142 changes: 142 additions & 0 deletions ckanext/mailcraft/collection.py
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": {},
},
),
],
)
13 changes: 11 additions & 2 deletions ckanext/mailcraft/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

import ckanext.ap_main.types as ap_types
from ckanext.ap_main.interfaces import IAdminPanel

from ckanext.collection.interfaces import ICollection, CollectionFactory
import ckanext.mailcraft.config as mc_config
from ckanext.mailcraft.mailer import DefaultMailer
from ckanext.mailcraft.collection import MailCollection


@toolkit.blanket.blueprints
Expand All @@ -20,6 +21,7 @@ class MailcraftPlugin(plugins.SingletonPlugin):
plugins.implements(plugins.IConfigurer)
plugins.implements(plugins.IConfigurable)
plugins.implements(IAdminPanel, inherit=True)
plugins.implements(ICollection, inherit=True)

# IConfigurer

Expand All @@ -28,7 +30,7 @@ def update_config(self, config_):
toolkit.add_public_directory(config_, "public")
toolkit.add_resource("assets", "mailcraft")

def update_config_schema(self, schema):
def _update_config_schema(self, schema):
for _, config in mc_config.get_config_options().items():
schema.update({config["key"]: config["validators"]})

Expand All @@ -41,6 +43,13 @@ def configure(self, config: CKANConfig) -> None:
mailer = DefaultMailer()
mailer.test_conn()

# ICollection

def get_collection_factories(self) -> dict[str, CollectionFactory]:
return {
"mailcraft-dashboard": MailCollection,
}

# IAdminPanel

def register_config_sections(
Expand Down
40 changes: 5 additions & 35 deletions ckanext/mailcraft/templates/mailcraft/dashboard.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
{% extends 'admin_panel/base.html' %}

{% import 'macros/autoform.html' as autoform %}
{% import 'macros/form.html' as form %}

{% block breadcrumb_content %}
<li class="active">{% link_for _("Dashboard"), named_route='mailcraft.dashboard' %}</li>
{% endblock breadcrumb_content %}
Expand All @@ -13,37 +10,10 @@ <h1>{{ _("Dashboard") }}</h1>
<a href="{{ h.url_for('mailcraft.test') }}" class="btn btn-primary mb-3">{{ _("Send test email") }}</a>

<div class="row g-3">
<form action="{{ h.url_for('mailcraft.dashboard') }}" method="POST">
{{ h.csrf_input() }}

{% if page.items %}
<div class="bulk-actions mb-3">
{{ form.select('bulk-action', id='bulk-action', label=_('Action'), options=bulk_options, selected="", error=error) }}

<button type="submit" id="bulk-submit" class="btn btn-primary me-2">
{{ _("Apply to selected items") }}
</button>

<button
id="clear-mails-btn"
name="clear_mails"
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>
{{ _("Clear mails") }}
</button>
</div>

{% snippet "admin_panel/config/snippets/sortable_table.html", items=page.items, columns=columns, order_by=order_by, sort=sort, bulk_check=1 %}
{% else %}
<p>{{ _("No mails found.") }}</p>
{% endif %}
</form>
{% if collection.data.total %}
{{ collection.serializer.render() | safe }}
{% else %}
<p>{{ _("No mails found.") }}</p>
{% endif %}
</div> <!-- row -->
{% endblock ap_content %}

{% block page_pagination %}
{{ page.pager() }}
{% endblock %}
58 changes: 7 additions & 51 deletions ckanext/mailcraft/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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"))

Expand Down

0 comments on commit 988a28d

Please sign in to comment.