Skip to content

Commit 12fc231

Browse files
committed
Signall for send email
1 parent b07f3d5 commit 12fc231

File tree

4 files changed

+92
-15
lines changed

4 files changed

+92
-15
lines changed

dbaas/maintenance/models.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22
from __future__ import absolute_import, unicode_literals
33
import logging
44
import simple_audit
5-
from dateutil import rrule
5+
from datetime import datetime
6+
67
from copy import copy
8+
79
from django.db import models
8-
from django.core.exceptions import ValidationError
910
from django.utils.translation import ugettext_lazy as _
10-
from dateutil import tz
11-
from datetime import datetime
11+
from dateutil import rrule, tz
12+
from django.db.models.signals import post_save
13+
from django.db.models.signals import pre_delete
14+
from django.dispatch import receiver
15+
from celery.task import control
16+
17+
1218
from account.models import Team
1319
from backup.models import BackupGroup, Snapshot
1420
from logical.models import Database, Project
@@ -17,13 +23,10 @@
1723
Offering, EnginePatch)
1824
from notification.models import TaskHistory
1925
from util.models import BaseModel
20-
from django.db.models.signals import post_save
21-
from django.db.models.signals import pre_delete
22-
from django.dispatch import receiver
23-
from celery.task import control
2426
from maintenance.tasks import execute_scheduled_maintenance
2527
from .registered_functions.functools import _get_registered_functions
2628
from .managers import DatabaseMaintenanceTaskManager
29+
from util.email_notifications import schedule_task_notification
2730

2831

2932
LOG = logging.getLogger(__name__)
@@ -945,11 +948,13 @@ def __unicode__(self):
945948
simple_audit.register(HostMigrate)
946949
simple_audit.register(DatabaseMigrate)
947950
simple_audit.register(DatabaseUpgradePatch)
951+
simple_audit.register(TaskSchedule)
948952

949953

950-
#########
951-
# SIGNALS#
952-
#########
954+
#########################################################
955+
# SIGNALS #
956+
#########################################################
957+
953958

954959
@receiver(pre_delete, sender=Maintenance)
955960
def maintenance_pre_delete(sender, **kwargs):
@@ -991,3 +996,10 @@ def maintenance_post_save(sender, **kwargs):
991996

992997
maintenance.celery_task_id = task.task_id
993998
maintenance.save()
999+
1000+
1001+
@receiver(post_save, sender=TaskSchedule)
1002+
def task_schedule_post_save(sender, **kwargs):
1003+
task = kwargs.get("instance")
1004+
is_new = kwargs.get("created")
1005+
schedule_task_notification(task.database, task, is_new)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{% if is_new %}
2+
<h2>The SSL of database {{ database }} is about to expire.</h2><br/>
3+
That will expire at {{ ssl_expire_at }}.<br/>
4+
We schedule the update in your maintenance window at {{ scheduled_for }}.<br/>
5+
If you want change that date go to {{ database_url }}.<br/>
6+
<br/>
7+
{% else %}
8+
The schedule task of database {{ database }} was changed for {{ scheduled_for }}.<br/>
9+
If you want change that date go to {{ database_url }}<br/>
10+
<br/>
11+
{% endif %}
12+
You are receiving this email because in our records you are in team {{ database.team.name }}.<br/>
13+
If this is not right, contact the Dbaas system administrators.<br/>
14+
<br><br><br>
15+
Regards,<br>
16+
Dbaas notification robot<br>
17+
{{domain}}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{% if is_new %}
2+
The SSL of database {{ database }} is about to expire.
3+
That will expire at {{ ssl_expire_at }}.
4+
We schedule the update in your maintenance window at {{ scheduled_for }}.
5+
If you want change that date go to {{ database_url }}.
6+
7+
{% else %}
8+
The schedule task of database {{ database }} was changed for {{ scheduled_for }}.
9+
If you want change that date go to {{ database_url }}
10+
11+
{% endif %}
12+
You are receiving this email because in our records you are in team {{ database.team.name }}.
13+
If this is not right, contact the Dbaas system administrators.
14+
15+
Regards,
16+
Dbaas notification robot
17+
{{domain}}

dbaas/util/email_notifications.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ def notify_new_user_creation(user=None):
4545
LOG.debug("user: %s | addr_from: %s | addr_to: %s" %
4646
(user, addr_from, addr_to))
4747
if user and addr_from and addr_to:
48-
send_mail_template(subject, template, addr_from, addr_to,
49-
fail_silently=False, attachments=None, context=context)
48+
send_mail_template(
49+
subject, template, addr_from, addr_to,
50+
fail_silently=False, attachments=None, context=context
51+
)
5052
else:
5153
LOG.warning("could not send email for new user creation")
5254

@@ -64,8 +66,10 @@ def notify_team_change_for(user=None):
6466
context['url'] = domain
6567
context['teams'] = [team.name for team in user.team_set.all()]
6668
if user and addr_from and addr_to:
67-
send_mail_template(subject, template, addr_from, addr_to,
68-
fail_silently=False, attachments=None, context=context)
69+
send_mail_template(
70+
subject, template, addr_from, addr_to,
71+
fail_silently=False, attachments=None, context=context
72+
)
6973
else:
7074
LOG.warning("could not send email for team change")
7175
else:
@@ -149,3 +153,30 @@ def disk_resize_notification(database, new_disk, usage_percentage):
149153
subject, template, email_from(), email_to(database.team),
150154
fail_silently=False, attachments=None, context=context
151155
)
156+
157+
158+
def schedule_task_notification(database, scheduled_task, is_new):
159+
160+
subject = _('[DBaaS] Automatic Task {} for Database {}'.format(
161+
'created' if is_new else 'updated',
162+
database.name,
163+
))
164+
165+
template = "schedule_task_notification"
166+
domain = get_domain()
167+
168+
context = {
169+
'database': database,
170+
'ssl_expire_at': database.infra.earliest_ssl_expire_at,
171+
'scheduled_for': scheduled_task.scheduled_for,
172+
'database_url': "{}{}".format(domain, reverse(
173+
'admin:logical_database_maintenance', kwargs={'id': database.id}
174+
)),
175+
'is_new': is_new,
176+
'domain': domain
177+
}
178+
179+
send_mail_template(
180+
subject, template, email_from(), email_to(database.team),
181+
fail_silently=False, attachments=None, context=context
182+
)

0 commit comments

Comments
 (0)