## $Id: utils.py 7744 2012-03-01 22:00:34Z henrik $ ## ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## """General helper utilities for SIRP. """ import os import grok import string from zope.i18n import translate from random import SystemRandom as r from waeup.sirp.interfaces import ISIRPUtils from waeup.sirp.interfaces import MessageFactory as _ from waeup.sirp.smtp import send_mail as send_mail_internally def send_mail(from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config): """Wrapper for the real SMTP functionality in :mod:`waeup.sirp.smtp`. Merely here to stay compatible with lots of calls to this place. """ mail_id = send_mail_internally( from_name, from_addr, rcpt_name, rcpt_addr, subject, body, config) return True class SIRPUtils(grok.GlobalUtility): """A collection of parameters and methods subject to customization. """ grok.implements(ISIRPUtils) # This the only place where we define the portal language # which is used for the translation of system messages # (e.g. object histories). PORTAL_LANGUAGE = 'en' PREFERRED_LANGUAGES_DICT = { 'en':(1, u'English'), 'fr':(2, u'Français'), 'de':(3, u'Deutsch'), 'ha':(4, u'Hausa'), 'yo':(5, u'Yoruba'), 'ig':(6, u'Igbo'), } def getInstTypeDict(self): """Provide a dictionary of study modes. """ return { 'faculty': 'Faculty of', 'department': 'Department of', 'school': 'School of', 'office': 'Office for', 'centre': 'Centre for', 'institute': 'Institute of', 'school_for': 'School for', } def getStudyModesDict(self): """Provide a dictionary of study modes. """ return { 'rmd_ft': 'Remedial with deficiencies', 'dp_pt': 'Diploma Part Time', 'ct_ft': 'Certificate Full Time', 'dp_ft': 'Diploma Full Time', 'de_pt': 'Direct Entry Part Time', 'pg_ft': 'Postgraduate Full Time', 'pg_pt': 'Postgraduate Part Time', 'jm_ft': 'Joint Matriculation Full Time', 'ume_ft': 'UME Full Time', 'de_ft': 'Direct Entry Full Time', 'ph_ft': 'Post Higher Education Full Time', 'transfer_pt': 'Transfer Part Time', 'ug_pt': 'Undergraduate Part Time', 'transfer_ft': 'Transfer Full Time', 'ct_pt': 'Certificate Part Time', 'ug_ft': 'Undergraduate Full Time', 'rm_ft': 'Remedial' } def getAppCatDict(self): """Provide a dictionary of study modes. """ return { 'basic': 'PUME, PDE, PCE, PRENCE', 'no': 'no application', 'pg': 'Postgraduate', 'sandwich': 'Sandwich', 'cest': 'Part-Time, Diploma, Certificate' } def getSemesterDict(self): """Provide a dictionary of semester or trimester types. """ return { 1: 'First Semester', 2: 'Second Semester', 3: 'Combined', 9: 'N/A' } def sendContactForm(self,from_name,from_addr,rcpt_name,rcpt_addr, from_username,usertype,portal,body,subject): """Send an email with data provided by forms. """ config = grok.getSite()['configuration'] text = _(u"""Fullname: ${a} User Id: ${b} User Type: ${c} Portal: ${d} ${e} """) text = _(text, mapping = { 'a':from_name, 'b':from_username, 'c':usertype, 'd':portal, 'e':body}) body = translate(text, 'waeup.sirp', target_language=self.PORTAL_LANGUAGE) return send_mail( from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config) def fullname(self,firstname,lastname,middlename=None): """Full name constructor. """ # We do not necessarily have the middlename attribute if middlename: return string.capwords( '%s %s %s' % (firstname, middlename, lastname)) else: return string.capwords( '%s %s' % (firstname, lastname)) def genPassword(self, length=8, chars=string.letters + string.digits): """Generate a random password. """ return ''.join([r().choice(chars) for i in range(length)]) def sendCredentials(self, user, password=None, login_url=None, msg=None): """Send credentials as email. Input is the applicant for which credentials are sent and the password. Returns True or False to indicate successful operation. """ subject = 'Your SIRP credentials' text = _(u"""Dear ${a}, ${b} Student Registration and Information Portal of ${c}. Your user name: ${d} Your password: ${e} Login page: ${f} Please remember your user name and keep your password secret! Please also note that passwords are case-sensitive. Regards """) config = grok.getSite()['configuration'] from_name = config.name_admin from_addr = config.email_admin rcpt_name = user.title rcpt_addr = user.email text = _(text, mapping = { 'a':rcpt_name, 'b':msg, 'c':config.name, 'd':user.name, 'e':password, 'f':login_url}) body = translate(text, 'waeup.sirp', target_language=self.PORTAL_LANGUAGE) return send_mail( from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config)