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

Last change on this file since 7475 was 7471, checked in by uli, 13 years ago

Replace old send_mail() with wrapper to new location.

  • Property svn:keywords set to Id
File size: 3.4 KB
RevLine 
[7358]1## $Id: utils.py 7471 2012-01-14 12:45:17Z uli $
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):
58        # We do not necessarily have the middlename attribute
59        if middlename:
[7365]60            return string.capwords(
61                '%s %s %s' % (firstname, middlename, lastname))
[7359]62        else:
[7365]63            return string.capwords(
64                '%s %s' % (firstname, lastname))
65
66    def genPassword(self, length=8, chars=string.letters + string.digits):
67        return ''.join([r().choice(chars) for i in range(length)])
68
[7382]69
[7407]70    def sendCredentials(self, user, password=None, login_url=None, msg=None):
[7399]71        """Send credentials as email.
72
73        Input is the applicant for which credentials are sent and the
74        password.
75
76        Returns True or False to indicate successful operation.
[7365]77        """
[7399]78        subject = 'Your SIRP credentials'
[7365]79        text = """Dear %s,
80
81%s
82Student Registration and Information Portal of
[7368]83%s.
[7365]84
[7368]85Your user name: %s
86Your password: %s
[7365]87Login page: %s
88
89Please remember your user name and keep
90your password secret!
91
[7382]92Please also note that passwords are case-sensitive.
93
[7365]94Regards
95"""
[7399]96        config = grok.getSite()['configuration']
97        from_name = config.name_admin
[7402]98        from_addr = config.email_admin
[7407]99        rcpt_name = user.title
100        rcpt_addr = user.email
[7382]101        body = text % (
[7407]102                rcpt_name, msg,config.name,user.name,password,login_url)
[7399]103        return send_mail(
[7402]104            from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config)
Note: See TracBrowser for help on using the repository browser.