views:

30

answers:

1

I am looking at accessing the user who owns the content_type of a posted comment

Currently I can access the user who posts, the comment, however I would like to notify the person who owns the item...

I tried doing user = comment.content_type.user but I get an error.

In my main __init__.py file

As soon as I change that to user = request.user it works fine, but then the notification gets sent to the person who made the comment.

from django.contrib.comments.signals import comment_was_posted

if "notification" in settings.INSTALLED_APPS:
    from notification import models as notification

    def comment_notification(sender, comment, request, **kwargs):
        subject = comment.content_object
        for role in ['user']:
            if hasattr(subject, role) and isinstance(getattr(subject, role), User):
                user = getattr(subject, role)
                message = comment
                notification.send([user], "new_comment", {'message': message,})

    comment_was_posted.connect(comment_notification)
+2  A: 

comment.content_object.user is correct one. But this problem is tricki. Since comment can be attached to any model, you don't know if this model has user field. In many cases there can be different name of this field ie. If you have comment to article, article can have article.author and If you have car model, and you are commenting it, there will be probably car.owner. So using .user for this purpose will not work in this cases.

My proposition to solve this problem is making list of possible roles interested in comment, and try to send message to all of them:

from django.contrib.comments.signals import comment_was_posted

if "notification" in settings.INSTALLED_APPS:
    from notification import models as notification

    def comment_notification(sender, comment, request, **kwargs):
        subject = comment.content_object
        for role in ['user', 'author', 'owner', 'creator', 'leader', 'maker', 'type any more']:
        if hasattr(subject, role) and isinstance(getattr(subject, role), User):
            user = getattr(subject, role)
            message = comment
            notification.send([user], "new_comment", {'message': message,}) 

    comment_was_posted.connect(comment_notification)

You should also, move this list to some king of configuration:

from django.contrib.comments.signals import comment_was_posted
default_roles = ['user', 'author', 'owner']
_roles = settings.get('MYAPP_ROLES', default_roles)
if "notification" in settings.INSTALLED_APPS:
    from notification import models as notification

    def comment_notification(sender, comment, request, **kwargs):
        subject = comment.content_object
        for role in _roles:
        if hasattr(subject, role) and isinstance(getattr(subject, role), User):
            user = getattr(subject, role)
            message = comment
            notification.send([user], "new_comment", {'message': message,}) 

    comment_was_posted.connect(comment_notification)

Another approach to this problem would be to create mechanism which translates class to role. But it's much harder to get it right, so you probably don't want to do that.

Tomasz Wysocki
First of all thanks for the comment, just want to double check with you, you refer to content_object though there is only a content_type in the admin, so I presume you are referring to that instead?And I have tried the first code above, with no luck. it seems to go through, but no notifications actually get sent. I will updated my question with the lastest code.
ApPeL
You should read description of comment model: http://docs.djangoproject.com/en/1.2/ref/contrib/comments/models/ . Have you imported User object from Django auth?
Tomasz Wysocki