Each election should only have one set of results; use UNIQUE to enforce.
[conservancy:voting.git] / bin / mail_renewals_to_foundation_list.py
1 #!/usr/bin/env python
2
3 import datetime
4 import logging
5 from optparse import OptionParser
6 from textwrap import dedent
7 import sys
8
9 from get_renewees import get_members_which_need_renewal, send_email
10
11 #    This program is free software: you can redistribute it and/or modify
12 #    it under the terms of the GNU General Public License as published by
13 #    the Free Software Foundation, either version 3 of the License, or
14 #    (at your option) any later version.
15 #
16 #    This program is distributed in the hope that it will be useful,
17 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
18 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 #    GNU General Public License for more details.
20 #
21 #    You should have received a copy of the GNU General Public License
22 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
24 __author__ = "Tobias Mueller"
25 __license__ = "GPLv3+"
26 __email__ = "tobiasmue@gnome.org"
27
28 TEMPLATE = dedent('''
29     Hi,
30      
31     as per point 1.3 of [1], here it comes a list of members in need of a
32     renew in case they didn't receive their individual e-mail:
33      
34     First name, Last name (Last renewed on)
35     %(members)s
36      
37     The Renewal form can be found at [2].
38      
39     Cheers,
40       GNOME Membership and Elections Committee
41      
42     [1] https://mail.gnome.org/archives/foundation-list/2011-November/msg00000.html
43     [2] http://www.gnome.org/foundation/membership/apply/
44
45 ''')
46
47 def format_members_for_mail(members, template=TEMPLATE):
48     fmt = " * %(firstname)s, %(lastname)s (%(token_or_last_renewed_on)s)"
49     member_lines = [fmt % member.__dict__ for member in members]
50
51     members_formatted = '\n'.join(member_lines)
52
53     mail = template % {'members': members_formatted}
54     
55     return mail
56     
57 def main(options=None):
58     log = logging.getLogger()
59
60     options = options or {}
61     if options:
62         if options.template:
63             template = open(options.template, 'r').read()
64         else:
65             template = TEMPLATE
66         
67         if options.recipient:
68             to = options.recipient
69         else:
70             to = 'foundation-list@gnome.org'
71
72         if options.sendmail:
73             sendmail = options.sendmail
74         else:
75             sendmail = False
76
77
78
79     members = get_members_which_need_renewal('month')
80     if not members:
81         log.warn('No one needs renewals! :-)')
82     
83     else: # We do have members
84         emailtext = format_members_for_mail(members)
85         
86         today = datetime.date.today()
87         subject = "Memberships needing renewal (%s)" % today.strftime("%02Y-%02m")
88         
89         if sendmail:
90             log.warn('Sending mail to %s: %s', to, subject)
91             send_email(to, subject, emailtext)
92         else:
93             log.info('Not sending mail to %s', to)
94             log.info('%s', subject)
95             log.info('%s', emailtext)
96     
97     return 0
98     
99 if __name__ == "__main__":
100     parser = OptionParser()
101     parser.add_option("-f", "--from-address", dest="fromaddress",
102                       help="Use that as sending address [default: %default]",
103                       default="Tobias Mueller <tobiasmue@gnome.org>") 
104     parser.add_option("-s", "--send-mail", dest="sendmail",
105                       help="Do indeed send mail [default: %default]",
106                       action="store_true",
107                       default=False)
108     parser.add_option("-l", "--loglevel", dest="loglevel",
109                       help="Sets the loglevel to one of debug, info, warn, "
110                             "error, critical", default="info") 
111     parser.add_option("-r", "--recipient", dest="recipient",
112                       help="Address to send an email to",
113                             default=None) 
114     parser.add_option("-t", "--template", dest="template",
115                       help="Use this file as a template, instead of the "
116                             "hardcoded default one. "
117                             "Please look at the source to see the available "
118                             "variables.",
119                             default=None) 
120     (options, args) = parser.parse_args()
121     loglevel = {'debug': logging.DEBUG, 'info': logging.INFO,
122                 'warn': logging.WARN, 'error': logging.ERROR,
123                 'critical': logging.CRITICAL}.get(options.loglevel, "warn")
124     LOGFORMAT = "%(asctime)s %(levelname)-8s %(name)s %(message)s"
125     DATEFORMAT = '%Y-%m-%d %H:%M:%S'
126     logging.basicConfig(level=loglevel, format=LOGFORMAT, datefmt=DATEFORMAT)
127     log = logging.getLogger('main')
128
129     sys.exit (main (options))
130