Hi,

short summary for all who didn't follow or don't remember (it's been a  
while since the last reply...):

I have signals wired up for comments (email-notification on commenting).
Users are allowed to comment on three different models.
Where should the code live?

Am 29.09.2008 um 14:36 schrieb Benjamin Buch:

> I should write the code first, and then say something about it I  
> guess... ;-)
> I'll get back when I did so.
> benjamin

I wrote the code now.
Here it is:

from django.contrib.comments.models import Comment
from django.core.mail import mail_managers
from django.contrib.comments.signals import comment_was_posted

def mail_on_posted_comment(sender, comment, request, **kwargs):
     name = comment.user_name
     email = comment_user_email
     comment_body = comment.comment
     ct = comment.content_type
     ct_name = ct.name
     ct_object = ct.get_object_for_this_type(pk=comment.object_pk)
     domain = comment.site.domain
     url_read = domain + ct_object.get_absolute_url()
     url_edit = domain + '/admin/comments/comment/' + str(comment.id)

     email_body = '''Ein neuer Kommentar wurde erstellt:
von: %s
E-mail: %s
zu: %s %s
Kommentar: %s

Kommentar lesen: %s
Kommentar bearbeiten: %s''' % (name, email, ct_name, ct_object,  
comment_body, url_read, url_edit)
     email_subject = 'Neuer Kommentar'
     mail_managers(email_subject, email_body)

comment_was_posted.connect(mail_on_posted_comment, sender=Comment)

So the question is: Where should this code live?

As Erik said, it's more of a project related thing (because you can  
comment on three different models),
so I think it would be a bad idea to tie the code to one particular  
model and put it in some models.py.

Other places I could think of but considered them as 'not so good':
__init__.py:    Who would think that there are signals here? I guess  
nobody.
views.py: signals in views? I don't think so. Let's keep things  
separated.
urls.py: urls.py is for wiring up urls, not signals.

@Erik:
What exactly do you mean by 'put them somewhere in the project'?

benjamin

>>>
>>> Am 28.09.2008 um 17:14 schrieb Erik Allik:
>>>
>>>> The way I see it is that your comment notification is not tied to  
>>>> any particular application that has commentable models but  
>>>> instead is more like a project related thing. So depending on  
>>>> your source layout, I'd put them somewhere in the project.  
>>>> Basically this relates to the application reuse topic -- when you  
>>>> connect the handler to the comment signal, is it something you  
>>>> want to reuse in the future or it's just a one time thing for the  
>>>> current project?
>>>>
>>>> Erik
>>>>
>>>> On 28.09.2008, at 14:58, Benjamin Buch wrote:
>>>>
>>>>> Hi,
>>>>>
>>>>> I'm using the new comments framework, and I'd like to get  
>>>>> notified by mail when someone posts a comment.
>>>>> How to di it I think I know, but I'm not quite sure where the  
>>>>> code should live.
>>>>> The website has several kinds of entries where users can comment  
>>>>> on, so it would feel a little odd to put the comments' signal- 
>>>>> code in just one models.py.
>>>>> As I have even more signals, I thought it would be great to have  
>>>>> a file signals.py, where all signal handling is done.
>>>>>
>>>>> But where should signals.py live?
>>>>> Documentation says to signals:
>>>>> "Where should this code live?
>>>>> You can put signal handling and registration code anywhere you  
>>>>> like. However, you'll need to make sure that the module it's in  
>>>>> gets imported early on so that the signal handling gets  
>>>>> registered before any signals need to be sent. This makes your  
>>>>> app's models.py a good place to put registration of signal  
>>>>> handlers."
>>>>> What means "the module it's in gets imported early"?
>>>>> I suppose it's not enough to put my signals.py right there in my  
>>>>> projects' root folder?
>>>>> -benjamin


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to