Factor out Message() creation for internal use into separate function.

(imported from commit 0352010da4255fc74cd67835e04dbdb1f8bbf4e9)
This commit is contained in:
Luke Faraone 2012-12-06 17:43:15 -05:00
parent 5b70b4a04f
commit 7fbee21584
3 changed files with 26 additions and 25 deletions

View File

@ -21,14 +21,8 @@ class AdminHumbugHandler(logging.Handler):
def emit(self, record):
# We have to defer imports to avoid circular imports in settings.py.
from zephyr.models import Message, UserProfile, Recipient, \
create_stream_if_needed, get_client, do_send_message
create_stream_if_needed, get_client, internal_send_message
from django.conf import settings
message = Message()
message.sender = UserProfile.objects.get(user__email="humbug+errors@humbughq.com")
message.recipient = Recipient.objects.get(type_id=create_stream_if_needed(
message.sender.realm, "devel").id, type=Recipient.STREAM)
message.pub_date = now()
message.sending_client = get_client("Internal")
try:
request = record.request
@ -47,15 +41,16 @@ class AdminHumbugHandler(logging.Handler):
)
request = None
request_repr = "Request repr() unavailable."
message.subject = self.format_subject(subject)
subject = self.format_subject(subject)
if record.exc_info:
stack_trace = '\n'.join(traceback.format_exception(*record.exc_info))
else:
stack_trace = 'No stack trace available'
message.content = "~~~~ pytb\n%s\n\n%s\n~~~~" % (stack_trace, request_repr)
do_send_message(message)
internal_send_message("humbug+errors@humbughq.com",
Recipient.STREAM, "devel", subject,
"~~~~ pytb\n%s\n\n%s\n~~~~" % (stack_trace, request_repr))
def format_subject(self, subject):
"""

View File

@ -548,6 +548,19 @@ def do_send_message(message, no_log=False):
rendered = simplejson.dumps(rendered),
users = simplejson.dumps([str(user.id) for user in recipients])))
def internal_send_message(sender_email, recipient_type, recipient_name,
subject, content):
message = Message()
message.sender = UserProfile.objects.get(user__email=sender_email)
message.recipient = Recipient.objects.get(type_id=create_stream_if_needed(
message.sender.realm, recipient_name).id, type=recipient_type)
message.subject = subject
message.content = content
message.pub_date = timezone.now()
message.sending_client = get_client("Internal")
do_send_message(message)
class Subscription(models.Model):
user_profile = models.ForeignKey(UserProfile)
recipient = models.ForeignKey(Recipient)

View File

@ -16,7 +16,7 @@ from zephyr.models import Message, UserProfile, Stream, Subscription, \
do_activate_user, add_default_subs, do_create_user, do_send_message, \
create_mit_user_if_needed, create_stream_if_needed, StreamColor, \
PreregistrationUser, get_client, MitUser, User, UserActivity, \
log_subscription_property_change
log_subscription_property_change, internal_send_message
from zephyr.forms import RegistrationForm, HomepageForm, is_unique, \
is_active
from django.views.decorators.csrf import csrf_exempt
@ -96,21 +96,14 @@ def accounts_register(request):
user = do_create_user(email, password, realm, full_name, short_name)
add_default_subs(user)
message = Message()
message.sender = UserProfile.objects.get(user__email="humbug+signups@humbughq.com")
message.recipient = Recipient.objects.get(type_id=create_stream_if_needed(
message.sender.realm, "signups").id, type=Recipient.STREAM)
message.subject = realm.domain
message.content = "%s <`%s`> just signed up for Humbug! (total: **%i**)" % (
full_name,
email,
UserProfile.objects.filter(realm=realm, user__is_active=True).count(),
internal_send_message("humbug+signups@humbughq.com",
Recipient.STREAM, "signups", realm.domain,
"%s <`%s`> just signed up for Humbug! (total: **%i**)" % (
full_name,
email,
UserProfile.objects.filter(realm=realm, user__is_active=True).count(),
)
)
message.pub_date = now()
message.sending_client = get_client("Internal")
do_send_message(message)
login(request, authenticate(username=email, password=password))
return HttpResponseRedirect(reverse('zephyr.views.home'))