forked from fedora-infra/fedocal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fedocal_cron.py
164 lines (129 loc) · 4.69 KB
/
fedocal_cron.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/python
#-*- coding: utf-8 -*-
"""
(c) 2012 - Copyright Pierre-Yves Chibon <pingou@pingoured.fr>
Distributed under License GPLv3 or later
You can find a copy of this license on the website
http://www.gnu.org/licenses/gpl.html
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 3 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.
This script is meant to be run as a cron job to send the reminders for
each meeting that asked for it.
"""
## These two lines are needed to run on EL6
__requires__ = ['SQLAlchemy >= 0.7', 'jinja2 >= 2.4']
import pkg_resources
import smtplib
import warnings
from email.mime.text import MIMEText
import fedocal
import fedocal.fedocallib as fedocallib
def fedmsg_init():
""" Instanciate fedmsg """
try:
import fedmsg
import fedmsg.config
except ImportError:
warnings.warn("fedmsg ImportError")
return
config = fedmsg.config.load_config()
config['active'] = True
config['name'] = 'relay_inbound'
config['cert_prefix'] = 'fedocal'
fedmsg.init(**config)
def fedmsg_publish(meeting, meeting_id):
""" Publish the meeting.reminder messages on fedmsg.
:arg meeting: a Meeting object from fedocallib.model
:arg meeting_id: an int representing the meeting identifier in the
database
"""
try:
import fedmsg
except ImportError:
return
meeting_dict = meeting.to_json()
meeting_dict['meeting_id'] = meeting_id
fedmsg.publish(
modname="fedocal",
topic="meeting.reminder",
msg=dict(
meeting=meeting_dict,
calendar=meeting.calendar.to_json()
),
)
def send_reminder_meeting(meeting, meeting_id):
""" This function sends the actual reminder of a given meeting.
:arg meeting: a Meeting object from fedocallib.model
:arg meeting_id: an int representing the meeting identifier in the
database
"""
if not meeting.reminder_id:
return
location = ''
if meeting.meeting_location:
location = 'At %s' % meeting.meeting_location
string = u"""Dear all,
You are kindly invited to the meeting:
%(name)s on %(date)s from %(time_start)s to %(time_stop)s %(timezone)s
%(location)s
The meeting will be about:
%(description)s
Source: %(host)s/meeting/%(id)s/
""" % ({
'name': u'%s' % meeting.meeting_name,
'date': meeting.meeting_date,
'time_start': meeting.meeting_time_start,
'time_stop': meeting.meeting_time_stop,
'timezone': meeting.meeting_timezone,
'location': u'%s' % location,
'description': u'%s' % meeting.meeting_information,
'id': meeting_id,
'host': fedocal.APP.config['APP_URL'],
})
if meeting.reminder.reminder_text:
string = string + u"""
Please note:
%s""" % meeting.reminder.reminder_text
msg = MIMEText(string.encode('utf-8'), 'plain', 'utf-8')
msg['Subject'] = '[Fedocal] Reminder meeting : %s' % meeting.meeting_name
from_email = meeting.meeting_manager[0]
from_email = '%s@fedoraproject.org' % from_email
msg['From'] = meeting.reminder.reminder_from or from_email
msg['To'] = meeting.reminder.reminder_to.replace(',', ', ')
# Send the message via our own SMTP server, but don't include the
# envelope header.
smtp = smtplib.SMTP(fedocal.APP.config['SMTP_SERVER'])
smtp.sendmail(
from_email,
meeting.reminder.reminder_to.split(','),
msg.as_string())
smtp.quit()
return msg
def send_reminder():
""" Retrieve all the meeting for which we should send a reminder and
do it.
"""
db_url = fedocal.APP.config['DB_URL']
session = fedocallib.create_session(db_url)
meetings = fedocallib.retrieve_meeting_to_remind(
session, offset=int(fedocal.APP.config['CRON_FREQUENCY']))
msgs = []
for meeting in meetings:
meeting_id = meeting.meeting_id
meeting = fedocallib.update_date_rec_meeting(meeting, action='next')
msgs.append(send_reminder_meeting(meeting, meeting_id))
fedmsg_publish(meeting, meeting_id)
return msgs
if __name__ == '__main__':
fedmsg_init()
send_reminder()