1
0
mirror of https://github.com/django/django.git synced 2025-10-27 23:56:08 +00:00

newforms-admin: Merged to [4579]

git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@4580 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty
2007-02-25 20:01:08 +00:00
parent 43829a95d3
commit 8a70334640
12 changed files with 116 additions and 20 deletions

View File

@@ -3,7 +3,8 @@
from django.conf import settings
from email.MIMEText import MIMEText
from email.Header import Header
import smtplib, rfc822
from email.Utils import formatdate
import smtplib
import socket
import time
import random
@@ -33,21 +34,34 @@ class SafeMIMEText(MIMEText):
val = Header(val, settings.DEFAULT_CHARSET)
MIMEText.__setitem__(self, name, val)
def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=settings.EMAIL_HOST_USER, auth_password=settings.EMAIL_HOST_PASSWORD):
def send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None):
"""
Easy wrapper for sending a single message to a recipient list. All members
of the recipient list will see the other recipients in the 'To' field.
If auth_user is None, the EMAIL_HOST_USER setting is used.
If auth_password is None, the EMAIL_HOST_PASSWORD setting is used.
"""
if auth_user is None:
auth_user = settings.EMAIL_HOST_USER
if auth_password is None:
auth_password = settings.EMAIL_HOST_PASSWORD
return send_mass_mail([[subject, message, from_email, recipient_list]], fail_silently, auth_user, auth_password)
def send_mass_mail(datatuple, fail_silently=False, auth_user=settings.EMAIL_HOST_USER, auth_password=settings.EMAIL_HOST_PASSWORD):
def send_mass_mail(datatuple, fail_silently=False, auth_user=None, auth_password=None):
"""
Given a datatuple of (subject, message, from_email, recipient_list), sends
each message to each recipient list. Returns the number of e-mails sent.
If from_email is None, the DEFAULT_FROM_EMAIL setting is used.
If auth_user and auth_password are set, they're used to log in.
If auth_user is None, the EMAIL_HOST_USER setting is used.
If auth_password is None, the EMAIL_HOST_PASSWORD setting is used.
"""
if auth_user is None:
auth_user = settings.EMAIL_HOST_USER
if auth_password is None:
auth_password = settings.EMAIL_HOST_PASSWORD
try:
server = smtplib.SMTP(settings.EMAIL_HOST, settings.EMAIL_PORT)
if auth_user and auth_password:
@@ -65,7 +79,7 @@ def send_mass_mail(datatuple, fail_silently=False, auth_user=settings.EMAIL_HOST
msg['Subject'] = subject
msg['From'] = from_email
msg['To'] = ', '.join(recipient_list)
msg['Date'] = rfc822.formatdate()
msg['Date'] = formatdate()
try:
random_bits = str(random.getrandbits(64))
except AttributeError: # Python 2.3 doesn't have random.getrandbits().

View File

@@ -312,11 +312,12 @@ class RequiredIfOtherFieldGiven(RequiredIfOtherFieldsGiven):
RequiredIfOtherFieldsGiven.__init__(self, [other_field_name], error_message)
class RequiredIfOtherFieldEquals(object):
def __init__(self, other_field, other_value, error_message=None):
def __init__(self, other_field, other_value, error_message=None, other_label=None):
self.other_field = other_field
self.other_value = other_value
other_label = other_label or other_value
self.error_message = error_message or lazy_inter(gettext_lazy("This field must be given if %(field)s is %(value)s"), {
'field': other_field, 'value': other_value})
'field': other_field, 'value': other_label})
self.always_test = True
def __call__(self, field_data, all_data):
@@ -324,11 +325,12 @@ class RequiredIfOtherFieldEquals(object):
raise ValidationError(self.error_message)
class RequiredIfOtherFieldDoesNotEqual(object):
def __init__(self, other_field, other_value, error_message=None):
def __init__(self, other_field, other_value, other_label=None, error_message=None):
self.other_field = other_field
self.other_value = other_value
other_label = other_label or other_value
self.error_message = error_message or lazy_inter(gettext_lazy("This field must be given if %(field)s is not %(value)s"), {
'field': other_field, 'value': other_value})
'field': other_field, 'value': other_label})
self.always_test = True
def __call__(self, field_data, all_data):

View File

@@ -60,7 +60,9 @@ class DatabaseWrapper(local):
def cursor(self):
from django.conf import settings
set_tz = False
if self.connection is None:
set_tz = True
if settings.DATABASE_NAME == '':
from django.core.exceptions import ImproperlyConfigured
raise ImproperlyConfigured, "You need to specify DATABASE_NAME in your Django settings file."
@@ -76,7 +78,8 @@ class DatabaseWrapper(local):
self.connection = Database.connect(conn_string, **self.options)
self.connection.set_isolation_level(1) # make transactions transparent to all cursors
cursor = self.connection.cursor()
cursor.execute("SET TIME ZONE %s", [settings.TIME_ZONE])
if set_tz:
cursor.execute("SET TIME ZONE %s", [settings.TIME_ZONE])
cursor = UnicodeCursorWrapper(cursor, settings.DEFAULT_CHARSET)
if settings.DEBUG:
return util.CursorDebugWrapper(cursor, self)

View File

@@ -28,7 +28,9 @@ class DatabaseWrapper(local):
def cursor(self):
from django.conf import settings
set_tz = False
if self.connection is None:
set_tz = True
if settings.DATABASE_NAME == '':
from django.core.exceptions import ImproperlyConfigured
raise ImproperlyConfigured, "You need to specify DATABASE_NAME in your Django settings file."
@@ -45,7 +47,8 @@ class DatabaseWrapper(local):
self.connection.set_isolation_level(1) # make transactions transparent to all cursors
cursor = self.connection.cursor()
cursor.tzinfo_factory = None
cursor.execute("SET TIME ZONE %s", [settings.TIME_ZONE])
if set_tz:
cursor.execute("SET TIME ZONE %s", [settings.TIME_ZONE])
if settings.DEBUG:
return util.CursorDebugWrapper(cursor, self)
return cursor

View File

@@ -17,7 +17,7 @@ class MergeDict(object):
def __contains__(self, key):
return self.has_key(key)
def get(self, key, default):
def get(self, key, default=None):
try:
return self[key]
except KeyError: