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