Skip to content

Commit

Permalink
update: use config_schema signal
Browse files Browse the repository at this point in the history
  • Loading branch information
mutantsan committed Apr 24, 2024
1 parent 795c459 commit c44e55a
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 95 deletions.
2 changes: 1 addition & 1 deletion ckanext/mailcraft/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import ckan.plugins.toolkit as tk

from ckanext.collection.utils import Filters, ModelData
from ckanext.collection.types import InputFilter, LinkFilter, SelectFilter
from ckanext.collection.types import InputFilter, SelectFilter

from ckanext.ap_main.collection.base import (
ApCollection,
Expand Down
54 changes: 1 addition & 53 deletions ckanext/mailcraft/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from __future__ import annotations

from typing import Any

import ckan.plugins.toolkit as tk
from ckan import types


CONF_TEST_CONN = "ckanext.mailcraft.test_conn_on_startup"
DEF_TEST_CONN = False
Expand Down Expand Up @@ -45,53 +43,3 @@ def get_mail_per_page() -> int:
def get_redirect_email() -> str | None:
"""Redirect outgoing emails to a specified email"""
return tk.config.get(CONF_REDIRECT_EMAILS_TO)


def get_config_options() -> dict[str, dict[str, Any]]:
"""Defines how we are going to render the global configuration
options for an extension."""
unicode_safe = tk.get_validator("unicode_safe")
boolean_validator = tk.get_validator("boolean_validator")
default = tk.get_validator("default")
int_validator = tk.get_validator("is_positive_integer")
email_validator = tk.get_validator("email_validator")

return {
"smtp_test": {
"key": CONF_TEST_CONN,
"label": "Test SMTP connection on CKAN startup",
"value": is_startup_conn_test_enabled(),
"validators": [default(DEF_TEST_CONN), boolean_validator], # type: ignore
"type": "select",
"options": [{"value": 1, "text": "Yes"}, {"value": 0, "text": "No"}],
},
"timeout": {
"key": CONF_CONN_TIMEOUT,
"label": "SMTP connection timeout",
"value": get_conn_timeout(),
"validators": [default(DEF_CONN_TIMEOUT), int_validator], # type: ignore
"type": "number",
},
"stop_outgoing": {
"key": CONF_STOP_OUTGOING,
"label": "Stop outgoing emails",
"value": stop_outgoing_emails(),
"validators": [default(DEF_STOP_OUTGOING), boolean_validator], # type: ignore
"type": "select",
"options": [{"value": 1, "text": "Yes"}, {"value": 0, "text": "No"}],
},
"mail_per_page": {
"key": CONF_MAIL_PER_PAGE,
"label": "Number of emails per page",
"value": get_mail_per_page(),
"validators": [default(DEF_MAIL_PER_PAGE), int_validator], # type: ignore
"type": "number",
},
"redirect_to": {
"key": CONF_REDIRECT_EMAILS_TO,
"label": "Redirect outgoing emails to",
"value": get_redirect_email(),
"validators": [unicode_safe, email_validator],
"type": "text",
},
}
6 changes: 0 additions & 6 deletions ckanext/mailcraft/config_declaration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ groups:
default: false
editable: true

- key: ckanext.mailcraft.mail_per_page
type: int
description: Specify a number of mails to show per page on a dashboard
default: 20
editable: true

- key: ckanext.mailcraft.redirect_emails_to
description: Redirect outgoing emails to a specified email
editable: true
13 changes: 4 additions & 9 deletions ckanext/mailcraft/config_schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ about: Configuration options for the DOI extension

fields:
- field_name: ckanext.mailcraft.test_conn_on_startup
label: Test connection on startup
validators: default(true), one_of([true, false])
preset: select
required: true
Expand All @@ -21,6 +22,7 @@ fields:
validators: default(10), int_validator

- field_name: ckanext.mailcraft.stop_outgoing_emails
label: Stop outgoing emails
validators: default(true), one_of([true, false])
preset: select
required: true
Expand All @@ -30,14 +32,7 @@ fields:
- value: false
label: Disabled

- field_name: ckanext.mailcraft.mail_per_page
label: Mails per page
description: Specify a number of mails to show per page on a dashboard
input_type: number
required: true
validators: default(20), int_validator

- field_name: ckanext.mailcraft.redirect_emails_to
label: Redirect emails to
description: Redirect outgoing emails to a specified email
validators: default(''), unicode_safe
description: Redirect outgoing emails to a specified email1
validators: ignore_empty, unicode_safe, email_validator
2 changes: 0 additions & 2 deletions ckanext/mailcraft/mailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ def mail_recipients(

msg["From"] = email_utils.formataddr((self.site_title, self.mail_from))
msg["To"] = msg["Bcc"] = ", ".join(recipients)
# msg['To'] = email_utils.formataddr((recipient_name, recipient_email))
msg["Subject"] = subject
msg["Date"] = email_utils.formatdate(time())

Expand Down Expand Up @@ -232,7 +231,6 @@ def mail_user(
raise MailerException(tk._("User doesn't have an email address"))

self.mail_recipients(
# user_obj.display_name,
subject,
[user_obj.email],
body,
Expand Down
47 changes: 24 additions & 23 deletions ckanext/mailcraft/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ def update_config(self, config_):
toolkit.add_public_directory(config_, "public")
toolkit.add_resource("assets", "mailcraft")

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

return schema

# IConfigurable

def configure(self, config: CKANConfig) -> None:
Expand All @@ -59,24 +53,31 @@ def configure(self, config: CKANConfig) -> None:
def get_signal_subscriptions(self) -> types.SignalMapping:
return {
toolkit.signals.ckanext.signal("ap_main:collect_config_sections"): [
collect_config_sections_subs
self.collect_config_sections_subs
],
toolkit.signals.ckanext.signal("ap_main:collect_config_schemas"): [
self.collect_config_schemas_subs
],
}

@staticmethod
def collect_config_sections_subs(sender: None):
return {
"name": "Mailcraft",
"configs": [
{
"name": "Global settings",
"blueprint": "mailcraft.config",
"info": "Global mailcraft configurations",
},
{
"name": "Dashboard",
"blueprint": "mailcraft.dashboard",
"info": "Mailcraft dashboard",
},
],
}

def collect_config_sections_subs(sender: None):
return {
"name": "Mailcraft",
"configs": [
{
"name": "Global settings",
"blueprint": "mailcraft.config",
"info": "Global mailcraft configurations",
},
{
"name": "Dashboard",
"blueprint": "mailcraft.dashboard",
"info": "Mailcraft dashboard",
},
],
}
@staticmethod
def collect_config_schemas_subs(sender: None):
return ["ckanext.mailcraft:config_schema.yaml"]
1 change: 0 additions & 1 deletion ckanext/mailcraft/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

from ckanext.collection.shared import get_collection

import ckanext.mailcraft.config as mc_config
from ckanext.mailcraft.utils import get_mailer

mailcraft = Blueprint("mailcraft", __name__, url_prefix="/admin-panel/mailcraft")
Expand Down

0 comments on commit c44e55a

Please sign in to comment.