blechreiz-website/simpleforum/models.py

54 lines
1.6 KiB
Python
Raw Normal View History

from django.db import models
from django.utils.translation import ugettext as _
from django.contrib.auth.models import User
class Message ( models.Model ):
titel = models.CharField( max_length = 100, verbose_name = _("titel") )
text = models.TextField( blank=False, verbose_name = _("text") )
creation_time = models.DateTimeField( verbose_name=_("creation_time") )
author = models.ForeignKey( User, verbose_name=_("Author") )
2013-10-15 21:51:05 +02:00
def __unicode__( self ):
return self.author.username + " : " + self.titel
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.core.mail import EmailMultiAlternatives
2014-06-22 14:59:40 +02:00
from musicians.models import Musician
from django.template.loader import get_template
from django.template import Context
@receiver(post_save, sender=Message)
def my_handler(sender, instance, created, **kwargs):
if not created:
return
2014-06-22 14:59:40 +02:00
receivers = [ m.user.email for m in Musician.objects.all() ]
subject = "Blechreiz Forum: " + instance.titel
from_email = 'forum@blechreiz.com'
c = { 'messages': Message.objects.all().order_by('-creation_time')[:10] }
text_template = get_template( "simpleforum/mail.txt" )
#html_template = get_template( "simpleforum/mail.html" )
text_content = text_template.render( Context(c) )
#html_content = html_template.render( Context(c) )
msg = EmailMultiAlternatives( subject, text_content, from_email, receivers )
#msg.attach_alternative( html_content, "text/html" )
msg.send()