## $Id: utils.py 7619 2012-02-09 16:22:05Z 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 random import SystemRandom as r from waeup.sirp.interfaces import ISIRPUtils 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 methods subject to customization. """ grok.implements(ISIRPUtils) 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 = """Fullname: %s User Id: %s User Type: %s Portal: %s %s """ body = text % (from_name,from_username,usertype,portal,body) 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 = """Dear %s, %s Student Registration and Information Portal of %s. Your user name: %s Your password: %s Login page: %s 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 body = text % ( rcpt_name, msg,config.name,user.name,password,login_url) return send_mail( from_name,from_addr,rcpt_name,rcpt_addr,subject,body,config) def getVerdictsDict(self): """Provide a dictionary of verdicts. """ return { 'A1': 'First Class', '0': 'not yet', 'A3': 'Second Class Lower', 'A2': 'Second Class Upper', 'A5': 'Pass', 'A4': 'Third Class', 'A7': 'Credit', 'A6': 'Distinction', 'A8': 'Merit', 'A': 'Successful student', 'C': 'Student on probation', 'B': 'Student with carryover courses', 'E': 'Student who were previously on probation', 'D': 'Withdrawn from the faculty', 'G': 'Absent from examination', 'F': 'Medical case', 'I': 'Expelled/rusticated/suspended student', 'H': 'Withheld results', 'K': 'Unregistered student', 'J': 'Temporary withdrawn from the university', 'M': 'Reinstatement', 'L': 'Referred student', 'O': 'NCE-III repeater', 'N': 'Student on transfer', 'Y': 'No previous verdict', 'X': 'New 300 level student', 'Z': 'Successful student (provisional)'}