Skip to content

Commit

Permalink
Allow to send notification (DB and/or email) when an import in synthe…
Browse files Browse the repository at this point in the history
…sis is done.

Ref #414
  • Loading branch information
VincentCauchois authored and bouttier committed Jan 16, 2023
1 parent 32823d8 commit 0f042ae
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""add 'IMPORT-DONE' notification
Revision ID: 485a659efdcd
Revises: a11c9a2db7bb
Create Date: 2023-01-12 12:01:34.177079
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "485a659efdcd"
down_revision = "a11c9a2db7bb"
branch_labels = None
depends_on = ("36d0bd313a47",)

SCHEMA_NAME = "gn_notifications"


def upgrade():

# Insert the notification category 'IMPORT-DONE'
op.execute(
f"""
INSERT INTO {SCHEMA_NAME}.bib_notifications_categories
VALUES ('IMPORT-DONE', 'Import en synthèse terminé', 'Se déclenche lorsqu’un de vos imports est terminé et correctement intégré à la synthèse')
"""
)

# Insert templates 'EMAIL' and 'DB' for the category 'IMPORT-DONE'
op.execute(
"""
INSERT INTO gn_notifications.bib_notifications_templates
VALUES ('IMPORT-DONE', 'DB', '<b>Import n° {{ import.id_import }}</b> correctement terminé et intégré dans la synthèse')
"""
)
op.execute(
"""
INSERT INTO gn_notifications.bib_notifications_templates
VALUES ('IMPORT-DONE', 'EMAIL', '<p>Bonjour <i>{{ role.nom_complet }}</i> !</p> <p>Votre <a href="{{ url }}">import <b>n°{{ import.id_import }}</b></a> s’est terminé correctement {% if import.import_count == import.source_count %} 👌 et a été bien {% else %} 👍 mais a été partiellement {% endif %} intégré dans la synthèse.</p><p> {{ import.import_count }} / {{ import.source_count }} données ont pu être effectivement intégrées dans la synthèse.</p><hr><p><i>Vous recevez cet email automatiquement via le service de notification de GeoNature. <a href="{{url_notification_rules}}">Gestion de vos règles de notification</a>.</i></p>')
"""
)


def downgrade():

# First, remove the notifications rules corresponding to 'IMPORT-DONE'
op.execute(
f"""
DELETE FROM {SCHEMA_NAME}.t_notifications_rules WHERE code_category = 'IMPORT-DONE'
"""
)

# Then, Remove the notifications templates corresponding to 'IMPORT-DONE'
op.execute(
f"""
DELETE FROM {SCHEMA_NAME}.bib_notifications_templates WHERE code_category = 'IMPORT-DONE'
"""
)

# Lastly, Remove the notifications category 'IMPORT-DONE'
op.execute(
f"""
DELETE FROM {SCHEMA_NAME}.bib_notifications_categories WHERE code = 'IMPORT-DONE'
"""
)
22 changes: 22 additions & 0 deletions backend/gn_module_import/tasks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime

from flask import current_app
from sqlalchemy import func, distinct
from sqlalchemy.dialects.postgresql import array_agg, aggregate_order_by
from celery.utils.log import get_task_logger
Expand Down Expand Up @@ -33,6 +34,7 @@
check_digital_proof_urls,
)

from geonature.core.notifications.utils import dispatch_notifications

logger = get_task_logger(__name__)

Expand Down Expand Up @@ -160,4 +162,24 @@ def do_import_in_synthese(self, import_id):
.filter_by(source=imprt.source)
.scalar()
)

# Send element to notification system
notify_import_in_synthese_done(imprt)

db.session.commit()


# Send notification
def notify_import_in_synthese_done(imprt):
id_authors = [author.id_role for author in imprt.authors]
dispatch_notifications(
code_categories=["IMPORT-DONE%"],
id_roles=id_authors,
title="Import dans la synthèse",
url=(current_app.config["URL_APPLICATION"] + f"/#/import/{imprt.id_import}/report"),
context={
"import": imprt,
"url_notification_rules": current_app.config["URL_APPLICATION"]
+ "/#/notification/rules",
},
)

0 comments on commit 0f042ae

Please # to comment.