Menu

[r710]: / trunk / wnslib / notificationScheduler.py  Maximize  Restore  History

Download this file

88 lines (73 with data), 3.3 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# -*- coding: utf-8 -*-
# ===============================================================================
#
# Authors: Massimiliano Cannata, Milan Antonovic
#
# Copyright (c) 2015 IST-SUPSI (www.supsi.ch/ist)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# ===============================================================================
from os import path
from walib import configManager
defaultCFGpath = path.join(path.dirname(path.split(path.abspath(__file__))[0]),
"services/default.cfg")
serviceconf = configManager.waServiceConfig(defaultCFGpath)
def notify(name, message, status=True):
"""Notify manager
check users subscription and raise notification
Args:
name: name of the notification
message: message to notify
status: if True update status on twitter
"""
from walib import databaseManager as dbm
from wnslib import notify
dbConnection = dbm.PgDB(
serviceconf.connectionWns['user'],
serviceconf.connectionWns['password'],
serviceconf.connectionWns['dbname'],
serviceconf.connectionWns['host'],
serviceconf.connectionWns['port'])
sql = """SELECT r.user_id_fk, r.not_list
FROM wns.registration r, wns.notification n
WHERE r.not_id_fk = n.id AND n.name=%s"""
params = (name,)
usersList = dbConnection.select(sql, params)
notifier = notify.Notify(serviceconf)
if status:
try:
notifier.post_twitter_status(message['twitter'], name)
except AttributeError as e:
# missing or wrong autentiation data
print e
for user in usersList:
sql = "SELECT * FROM wns.user WHERE id = %s"
par = [user['user_id_fk']]
contact = dict(dbConnection.select(sql, par)[0])
for con in user['not_list']:
if con == 'alert':s
notifier.alert(message['alert'])
if con == 'mail' or con == 'email':
if 'mail' in message.keys():
notifier.email(message['mail'], contact['email'])
elif con == 'twitter':
if 'twitter' in message.keys() and 'twitter' in contact.keys():
notifier.twitter(message['twitter'],
contact['twitter'], name)
elif con == 'fax':
notifier.fax(message, contact['fax'], name)
elif con == 'sms':
notifier.sms(message, contact['tel'], name)