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

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

Set css class of <tr> tag.

  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1## $Id: utils.py 7477 2012-01-15 10:38:13Z 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 string
22from random import SystemRandom as r
23from waeup.sirp.interfaces import ISIRPUtils
24from waeup.sirp.smtp import send_mail as send_mail_internally
25
26def send_mail(from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config):
27    """Wrapper for the real SMTP functionality in :mod:`waeup.sirp.smtp`.
28
29    Merely here to stay compatible with lots of calls to this place.
30    """
31    mail_id = send_mail_internally(
32        from_name, from_addr, rcpt_name, rcpt_addr,
33        subject, body, config)
34    return True
35
36class SIRPUtils(grok.GlobalUtility):
37    """A collection of methods subject to customization.
38    """
39    grok.implements(ISIRPUtils)
40
41    def sendContactForm(self,from_name,from_addr,rcpt_name,rcpt_addr,
42                from_username,usertype,portal,body,subject):
43        """Send an email with data provided by forms.
44        """
45        config = grok.getSite()['configuration']
46        text = """Fullname: %s
47User Id: %s
48User Type: %s
49Portal: %s
50
51%s
52"""
53        body = text % (from_name,from_username,usertype,portal,body)
54        return send_mail(
55            from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config)
56
57    def fullname(self,firstname,lastname,middlename=None):
58        """Full name constructor.
59        """
60        # We do not necessarily have the middlename attribute
61        if middlename:
62            return string.capwords(
63                '%s %s %s' % (firstname, middlename, lastname))
64        else:
65            return string.capwords(
66                '%s %s' % (firstname, lastname))
67
68    def genPassword(self, length=8, chars=string.letters + string.digits):
69        """Generate a random password.
70        """
71        return ''.join([r().choice(chars) for i in range(length)])
72
73
74    def sendCredentials(self, user, password=None, login_url=None, msg=None):
75        """Send credentials as email.
76
77        Input is the applicant for which credentials are sent and the
78        password.
79
80        Returns True or False to indicate successful operation.
81        """
82        subject = 'Your SIRP credentials'
83        text = """Dear %s,
84
85%s
86Student Registration and Information Portal of
87%s.
88
89Your user name: %s
90Your password: %s
91Login page: %s
92
93Please remember your user name and keep
94your password secret!
95
96Please also note that passwords are case-sensitive.
97
98Regards
99"""
100        config = grok.getSite()['configuration']
101        from_name = config.name_admin
102        from_addr = config.email_admin
103        rcpt_name = user.title
104        rcpt_addr = user.email
105        body = text % (
106                rcpt_name, msg,config.name,user.name,password,login_url)
107        return send_mail(
108            from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config)
Note: See TracBrowser for help on using the repository browser.