This repository has been archived on 2024-11-20. You can view files and clone it, but cannot push or open issues or pull requests.
old-scribeengine/scribeengine/lib/utils.py
Raoul Snyman c0e74e19bb Added "logged in as" text to identify the current user.
Added pagination to the default post view.
Fixed a bug where the comments were being ordered incorrectly.
2010-01-31 15:45:20 +02:00

148 lines
5.1 KiB
Python

# -*- 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, c
from turbomail import Message
from scribeengine.lib.base import render, h
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()).strip('-')
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)
def paginate(query, page_size, current_page, base_url=None):
query_count = query.count()
page_count = query_count / page_size
if query_count % page_size > 0:
page_count += 1
offset = (current_page - 1) * page_size
records = query.offset(offset).limit(page_size).all()
prev_page = current_page - 1 if current_page - 1 >= 1 else 1
next_page = current_page + 1 if current_page + 1 <= page_count else page_count
if base_url:
return {
u'page': h.url_for(base_url, page=current_page),
u'first': h.url_for(base_url, page=1),
u'prev': h.url_for(base_url, page=prev_page),
u'next': h.url_for(base_url, page=next_page),
u'last': h.url_for(base_url, page=page_count),
u'start': offset + 1,
u'end': offset + len(records),
u'total': query_count,
u'records': records
}
else:
return {
u'page': current_page,
u'first': 1,
u'prev': prev_page,
u'next': next_page,
u'last': page_count,
u'start': offset + 1,
u'end': offset + len(records),
u'total': query_count,
u'records': records
}