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

Last change on this file since 7493 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
RevLine 
[7358]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
[7365]21import string
22from random import SystemRandom as r
[7358]23from waeup.sirp.interfaces import ISIRPUtils
[7471]24from waeup.sirp.smtp import send_mail as send_mail_internally
[7358]25
[7471]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`.
[7382]28
[7471]29    Merely here to stay compatible with lots of calls to this place.
[7400]30    """
[7471]31    mail_id = send_mail_internally(
32        from_name, from_addr, rcpt_name, rcpt_addr,
33        subject, body, config)
[7399]34    return True
35
[7358]36class SIRPUtils(grok.GlobalUtility):
37    """A collection of methods subject to customization.
38    """
39    grok.implements(ISIRPUtils)
40
[7404]41    def sendContactForm(self,from_name,from_addr,rcpt_name,rcpt_addr,
[7402]42                from_username,usertype,portal,body,subject):
[7358]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"""
[7402]53        body = text % (from_name,from_username,usertype,portal,body)
[7400]54        return send_mail(
[7402]55            from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config)
[7359]56
57    def fullname(self,firstname,lastname,middlename=None):
[7477]58        """Full name constructor.
59        """
[7359]60        # We do not necessarily have the middlename attribute
61        if middlename:
[7365]62            return string.capwords(
63                '%s %s %s' % (firstname, middlename, lastname))
[7359]64        else:
[7365]65            return string.capwords(
66                '%s %s' % (firstname, lastname))
67
68    def genPassword(self, length=8, chars=string.letters + string.digits):
[7477]69        """Generate a random password.
70        """
[7365]71        return ''.join([r().choice(chars) for i in range(length)])
72
[7382]73
[7407]74    def sendCredentials(self, user, password=None, login_url=None, msg=None):
[7399]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.
[7365]81        """
[7399]82        subject = 'Your SIRP credentials'
[7365]83        text = """Dear %s,
84
85%s
86Student Registration and Information Portal of
[7368]87%s.
[7365]88
[7368]89Your user name: %s
90Your password: %s
[7365]91Login page: %s
92
93Please remember your user name and keep
94your password secret!
95
[7382]96Please also note that passwords are case-sensitive.
97
[7365]98Regards
99"""
[7399]100        config = grok.getSite()['configuration']
101        from_name = config.name_admin
[7402]102        from_addr = config.email_admin
[7407]103        rcpt_name = user.title
104        rcpt_addr = user.email
[7382]105        body = text % (
[7407]106                rcpt_name, msg,config.name,user.name,password,login_url)
[7399]107        return send_mail(
[7402]108            from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config)
Note: See TracBrowser for help on using the repository browser.