-
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.
refactor: move dashboard to a separate ext
- Loading branch information
Showing
27 changed files
with
179 additions
and
91 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,94 @@ | ||
from __future__ import annotations | ||
|
||
import logging | ||
from datetime import datetime | ||
from typing import Any | ||
|
||
from sqlalchemy import Column, DateTime, Integer, Text | ||
from sqlalchemy.orm import Query | ||
from sqlalchemy.dialects.postgresql import JSONB | ||
from sqlalchemy.ext.mutable import MutableDict | ||
from typing_extensions import Self | ||
|
||
import ckan.model as model | ||
from ckan.plugins import toolkit as tk | ||
|
||
from ckanext.mailcraft.types import EmailData | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class Email(tk.BaseModel): | ||
__tablename__ = "mailcraft_mail" | ||
|
||
class State: | ||
failed = "failed" | ||
success = "success" | ||
stopped = "stopped" | ||
|
||
id = Column(Integer, primary_key=True) | ||
|
||
subject = Column(Text) | ||
timestamp = Column(DateTime, nullable=False, default=datetime.utcnow) | ||
sender = Column(Text) | ||
recipient = Column(Text) | ||
message = Column(Text) | ||
state = Column(Text, nullable=False, default=State.success) | ||
extras = Column("extras", MutableDict.as_mutable(JSONB)) | ||
|
||
@classmethod | ||
def all(cls) -> list[dict[str, Any]]: | ||
query: Query = model.Session.query.query(cls).order_by(cls.timestamp.desc()) | ||
|
||
return [mail.dictize({}) for mail in query.all()] | ||
|
||
@classmethod | ||
def save_mail( | ||
cls, | ||
email_data: EmailData, | ||
body_html: str, | ||
state: str, | ||
) -> Email: | ||
mail = cls( | ||
subject=email_data["Subject"], | ||
timestamp=email_data["Date"], | ||
sender=email_data["From"], | ||
recipient=email_data["To"], | ||
message=body_html, | ||
state=state, | ||
extras=email_data, | ||
) | ||
|
||
model.Session.add(mail) | ||
model.Session.commit() | ||
|
||
return mail | ||
|
||
def dictize(self, context): | ||
return { | ||
"id": self.id, | ||
"subject": self.subject, | ||
"timestamp": self.timestamp, | ||
"sender": self.sender, | ||
"recipient": self.recipient, | ||
"message": self.message, | ||
"state": self.state, | ||
"extras": self.extras or {}, | ||
} | ||
|
||
@classmethod | ||
def clear_emails(cls) -> int: | ||
rows_deleted = model.Session.query(cls).delete() | ||
model.Session.commit() | ||
|
||
return rows_deleted | ||
|
||
@classmethod | ||
def get(cls, mail_id: str) -> Self | None: | ||
query: Query = model.Session.query(cls).filter(cls.id == mail_id) | ||
|
||
return query.one_or_none() | ||
|
||
def delete(self) -> None: | ||
model.Session().autoflush = False | ||
model.Session.delete(self) |
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,66 @@ | ||
from __future__ import annotations | ||
|
||
import ckan.types as types | ||
import ckan.plugins as plugins | ||
import ckan.plugins.toolkit as toolkit | ||
|
||
from ckanext.collection.interfaces import ICollection, CollectionFactory | ||
|
||
from ckanext.mailcraft_dashboard.collection import MailCollection | ||
|
||
|
||
@toolkit.blanket.blueprints | ||
@toolkit.blanket.actions | ||
@toolkit.blanket.auth_functions | ||
@toolkit.blanket.validators | ||
@toolkit.blanket.config_declarations | ||
class MailcraftDashboardPlugin(plugins.SingletonPlugin): | ||
plugins.implements(plugins.IConfigurer) | ||
plugins.implements(plugins.ISignal) | ||
plugins.implements(ICollection, inherit=True) | ||
|
||
# ICollection | ||
|
||
def get_collection_factories(self) -> dict[str, CollectionFactory]: | ||
return { | ||
"mailcraft-dashboard": MailCollection, | ||
} | ||
|
||
# IConfigurer | ||
|
||
def update_config(self, config_): | ||
toolkit.add_template_directory(config_, "templates") | ||
|
||
# ISignal | ||
|
||
def get_signal_subscriptions(self) -> types.SignalMapping: | ||
return { | ||
toolkit.signals.ckanext.signal("ap_main:collect_config_sections"): [ | ||
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", | ||
}, | ||
], | ||
} | ||
|
||
@staticmethod | ||
def collect_config_schemas_subs(sender: None): | ||
return ["ckanext.mailcraft:config_schema.yaml"] |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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