source: main/waeup.sirp/trunk/src/waeup/sirp/utils/utils.py @ 7373

Last change on this file since 7373 was 7368, checked in by Henrik Bettermann, 13 years ago

Change label and title of registration page.

Check if application has started or ended.

Rename login icon.

  • Property svn:keywords set to Id
File size: 3.3 KB
Line 
1## $Id: utils.py 7368 2011-12-18 08:16:16Z henrik $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18"""General helper utilities for SIRP.
19"""
20import grok
21import smtplib
22import string
23from random import SystemRandom as r
24from email.mime.text import MIMEText
25from waeup.sirp.interfaces import ISIRPUtils
26
27class SIRPUtils(grok.GlobalUtility):
28    """A collection of methods subject to customization.
29    """
30    grok.implements(ISIRPUtils)
31
32    def sendMail(self,fullname,username,usertype,portal,body,
33                 email_from,email_to,subject):
34        """Send an email with data provided by forms.
35        """
36        config = grok.getSite()['configuration']
37        text = """Fullname: %s
38User Id: %s
39User Type: %s
40Portal: %s
41
42%s
43"""
44        msg = MIMEText(text % (fullname,username,usertype,portal,body))
45        msg['From'] = '%s <%s>' % (fullname,email_from)
46        msg['To'] = email_to
47        msg['Subject'] = subject
48        server = smtplib.SMTP(config.smtp_server)
49        if config.smtp_requires_login:
50            server.login(config.smtp_username,config.smtp_password)
51        try:
52            server.sendmail(email_from,email_to,msg.as_string())
53        except:
54            return False
55        server.quit()
56        return True
57
58    def fullname(self,firstname,lastname,middlename=None):
59        # We do not necessarily have the middlename attribute
60        if middlename:
61            return string.capwords(
62                '%s %s %s' % (firstname, middlename, lastname))
63        else:
64            return string.capwords(
65                '%s %s' % (firstname, lastname))
66
67    def genPassword(self, length=8, chars=string.letters + string.digits):
68        return ''.join([r().choice(chars) for i in range(length)])
69
70    def sendPassword(self,fullname,msg,username,password,login_url,
71                 email_to,subject):
72        """Send an email with user credentials.
73        """
74        config = grok.getSite()['configuration']
75        text = """Dear %s,
76
77%s
78Student Registration and Information Portal of
79%s.
80
81Your user name: %s
82Your password: %s
83Login page: %s
84
85Please remember your user name and keep
86your password secret!
87
88Regards
89"""
90        msg = MIMEText(text % (fullname,msg,config.name,username,password,login_url))
91        msg['From'] = '%s <%s>' % (config.name_admin,config.email_admin)
92        msg['To'] = email_to
93        msg['Subject'] = subject
94        server = smtplib.SMTP(config.smtp_server)
95        if config.smtp_requires_login:
96            server.login(config.smtp_username,config.smtp_password)
97        try:
98            server.sendmail(config.email_admin,email_to,msg.as_string())
99        except:
100            return False
101        server.quit()
102        return True
103
Note: See TracBrowser for help on using the repository browser.