# -*- coding: utf-8 -*- # vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4 ############################################################################### # ScribeEngine - Open Source Blog Software # # --------------------------------------------------------------------------- # # Copyright (c) 2010 Raoul Snyman # # --------------------------------------------------------------------------- # # 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; version 2 of the License. # # # # 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 # ############################################################################### import re import hashlib import hmac import string from random import choice from datetime import datetime from pylons import config from turbomail import Message from scribeengine.lib.base import render, c def send_mail(template, mail_to, mail_from, subject, variables={}, attachments=[]): """ Sends an e-mail using the template ``template``. ``template`` The template to use. ``mail_to`` One or more addresses to send the e-mail to. ``mail_from`` The address to send e-mail from. ``subject`` The subject of the e-mail. ``variables`` Variables to be used in the template. ``attachments`` If you want to attach files to the e-mail, use this list. """ for name, value in variables.iteritems(): setattr(c, name, value) message = Message(mail_from, mail_to, subject) message.plain = render(template) message.send() def generate_url(title): """ Generate a friendly URL from a blog post title. ``title`` The title of the blog post. """ return re.sub(r'[^a-zA-Z0-9]+', u'-', title.lower()) def hash_password(password): """ Return an HMAC SHA256 hash of a password. ``password`` The password to hash. """ return unicode(hmac.new(config[u'security.salt'], password, hashlib.sha256).hexdigest(), 'utf-8') def generate_key(length): """ Generate a random set of letters and numbers of length ``length``. Usually used to generate activation keys. ``length`` The length of the key. """ return ''.join([choice(string.letters + string.digits) for i in range(length)]) def month_first_day(datetime): """ Returns a modified datetime with the day being midnight of the first day of the month, given a datetime object. """ return datetime.replace(day=1, hour=0, minute=0, second=0, microsecond=0) def month_last_day(datetime): """ Returns a modified datetime with the day being the last day of the month, given a datetime object. """ if datetime.month in [1, 3, 5, 7, 8, 10, 12]: day = 31 elif datetime.month in [4, 6, 9, 11]: day = 30 else: if datetime.year % 4 == 0 and datetime.year % 100 != 0 or datetime.year % 400 == 0: day = 29 else: day = 28 return datetime.replace(day=day, hour=23, minute=59, second=59, microsecond=99999)