Re: Signals getting sent twice

2009-03-02 Thread iris

Dear Steve,

Thank you for saving hours and hours of my team's time.

On Feb 17, 5:10 am, stevedegrace  wrote:
> Figured it out myself... you have to set dispatch_uid for the connect
> methods. See modification below to the last three lines of my example
> above. It's still annoying that the overhead of module import is
> apparently being incurred twice, but at least things work the way you
> expect. Aside from this bit of weirdness, the signalling framework is
> awesome, but it does seem to reveal an unfortunate characteristic of
> Django itself.
>
> comment_will_be_posted.connect(pre_comment, Comment,
> dispatch_uid='comments.pre_comment')
> comment_was_posted.connect(post_comment, Comment,
> dispatch_uid='comments.post_comment')
> comment_was_flagged.connect(flag_comment, Comment,
> dispatch_uid='comments.flag_comment')
>
> On Feb 15, 8:46 pm, stevedegrace  wrote:
>
> > I realise going through Google that this has been hashed over before,
> > but nevertheless, I want to know if there is any new thought on this.
> > Ever since I removed project references from imports in my code, I
> > have noticed a new problem whereby many functions connected to signals
> > are being called twice. I don't know if that caused the problem, but
> > it seems likely because everything I've been reading tracks the
> > problem down to imports.
>
> > A good example is in my __init__.py for my comments application, which
> > is basically just a bunch of customizations to the Django comments
> > application. That one has no imports of any of my models in it. It is
> > posted below.
>
> > This is a real pain. Is there any way to make it not do this, short of
> > going through my code and making all my imports through the project
> > again? Maybe that wouldn't even help. I tried making selected imports,
> > e.g., through the list of installed apps in settings.py import through
> > the project name again but that didn't fix it, so I don't see an easy
> > hack.
>
> > The same thing is happening whether I'm using manage.py runserver at
> > home or Apache2/mod_wsgi2 on Webfaction. On Webfaction I have no
> > choice but to have both the project directory and the directory
> > containing it on my path or I get Apache 500 errors, the only way I
> > can make my application load is to have both on the path.
>
> > This is really awful behaviour - it's probably the ugliest wart (maybe
> > the only significant one, but still) I have ever found in Django, and
> > it sucks because my applications are using signals a lot. I hope
> > there's a way around it.
>
> > Stephen
>
> > from django.contrib.comments.signals import comment_will_be_posted,
> > comment_was_posted, comment_was_flagged
> > from django.contrib.comments.models import Comment
> > from django.contrib.sites.models import Site
> > from django.template import Context, loader
> > from django.core.mail import send_mail
> > from django.conf import settings
>
> > def pre_comment(sender, comment, request, **kwargs):
> >     if not request.user.is_authenticated:
> >         comment.is_removed = True
>
> > def post_comment(sender, comment, request, **kwargs):
> >     if comment.is_removed:
> >         comment.delete()
> >     else:
> >         request.session['flash'].append('Thank you for your
> > comments.')
> >         request.session.modified = True
> >         site = Site.objects.get_current()
> >         template = loader.get_template('comments/comment_email.html')
> >         context = Context({
> >                 'comment': comment,
> >                 'protocol': 'https' if request.is_secure() else
> > 'http',
> >                 'site': site,
> >             })
> >         send_mail("A comment was posted at %s" % site.name,
> >             template.render(context), getattr(settings,
> > 'DEFAULT_FROM_EMAIL', None),
> >             getattr(settings, 'MODERATOR_EMAILS', None))
>
> > def flag_comment(sender, comment, flag, created, request, **kwargs):
> >     site = Site.objects.get_current()
> >     template = loader.get_template('comments/comment_flag_email.html')
> >     context = Context({
> >             'comment': comment,
> >             'flagged_by': request.user,
> >             'protocol': 'https' if request.is_secure() else 'http',
> >             'site': site,
> >         })
> >     send_mail("A comment was flagged by %s" % request.user.username,
> >         template.render(context), getattr(settings,
> > 'DEFAULT_FROM_EMAIL', None),
> >         getattr(settings, 'MODERATOR_EMAILS', None))
>
> > comment_will_be_posted.connect(pre_comment, Comment)
> > comment_was_posted.connect(post_comment, Comment)
> > comment_was_flagged.connect(flag_comment, Comment)

--~--~-~--~~~---~--~~
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 
django-users+unsubscr...@googlegroups.com
For more optio

Re: Signals getting sent twice

2009-02-16 Thread stevedegrace

Figured it out myself... you have to set dispatch_uid for the connect
methods. See modification below to the last three lines of my example
above. It's still annoying that the overhead of module import is
apparently being incurred twice, but at least things work the way you
expect. Aside from this bit of weirdness, the signalling framework is
awesome, but it does seem to reveal an unfortunate characteristic of
Django itself.

comment_will_be_posted.connect(pre_comment, Comment,
dispatch_uid='comments.pre_comment')
comment_was_posted.connect(post_comment, Comment,
dispatch_uid='comments.post_comment')
comment_was_flagged.connect(flag_comment, Comment,
dispatch_uid='comments.flag_comment')

On Feb 15, 8:46 pm, stevedegrace  wrote:
> I realise going through Google that this has been hashed over before,
> but nevertheless, I want to know if there is any new thought on this.
> Ever since I removed project references from imports in my code, I
> have noticed a new problem whereby many functions connected to signals
> are being called twice. I don't know if that caused the problem, but
> it seems likely because everything I've been reading tracks the
> problem down to imports.
>
> A good example is in my __init__.py for my comments application, which
> is basically just a bunch of customizations to the Django comments
> application. That one has no imports of any of my models in it. It is
> posted below.
>
> This is a real pain. Is there any way to make it not do this, short of
> going through my code and making all my imports through the project
> again? Maybe that wouldn't even help. I tried making selected imports,
> e.g., through the list of installed apps in settings.py import through
> the project name again but that didn't fix it, so I don't see an easy
> hack.
>
> The same thing is happening whether I'm using manage.py runserver at
> home or Apache2/mod_wsgi2 on Webfaction. On Webfaction I have no
> choice but to have both the project directory and the directory
> containing it on my path or I get Apache 500 errors, the only way I
> can make my application load is to have both on the path.
>
> This is really awful behaviour - it's probably the ugliest wart (maybe
> the only significant one, but still) I have ever found in Django, and
> it sucks because my applications are using signals a lot. I hope
> there's a way around it.
>
> Stephen
>
> from django.contrib.comments.signals import comment_will_be_posted,
> comment_was_posted, comment_was_flagged
> from django.contrib.comments.models import Comment
> from django.contrib.sites.models import Site
> from django.template import Context, loader
> from django.core.mail import send_mail
> from django.conf import settings
>
> def pre_comment(sender, comment, request, **kwargs):
>     if not request.user.is_authenticated:
>         comment.is_removed = True
>
> def post_comment(sender, comment, request, **kwargs):
>     if comment.is_removed:
>         comment.delete()
>     else:
>         request.session['flash'].append('Thank you for your
> comments.')
>         request.session.modified = True
>         site = Site.objects.get_current()
>         template = loader.get_template('comments/comment_email.html')
>         context = Context({
>                 'comment': comment,
>                 'protocol': 'https' if request.is_secure() else
> 'http',
>                 'site': site,
>             })
>         send_mail("A comment was posted at %s" % site.name,
>             template.render(context), getattr(settings,
> 'DEFAULT_FROM_EMAIL', None),
>             getattr(settings, 'MODERATOR_EMAILS', None))
>
> def flag_comment(sender, comment, flag, created, request, **kwargs):
>     site = Site.objects.get_current()
>     template = loader.get_template('comments/comment_flag_email.html')
>     context = Context({
>             'comment': comment,
>             'flagged_by': request.user,
>             'protocol': 'https' if request.is_secure() else 'http',
>             'site': site,
>         })
>     send_mail("A comment was flagged by %s" % request.user.username,
>         template.render(context), getattr(settings,
> 'DEFAULT_FROM_EMAIL', None),
>         getattr(settings, 'MODERATOR_EMAILS', None))
>
> comment_will_be_posted.connect(pre_comment, Comment)
> comment_was_posted.connect(post_comment, Comment)
> comment_was_flagged.connect(flag_comment, Comment)
--~--~-~--~~~---~--~~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Signals getting sent twice

2009-02-15 Thread stevedegrace

I realise going through Google that this has been hashed over before,
but nevertheless, I want to know if there is any new thought on this.
Ever since I removed project references from imports in my code, I
have noticed a new problem whereby many functions connected to signals
are being called twice. I don't know if that caused the problem, but
it seems likely because everything I've been reading tracks the
problem down to imports.

A good example is in my __init__.py for my comments application, which
is basically just a bunch of customizations to the Django comments
application. That one has no imports of any of my models in it. It is
posted below.

This is a real pain. Is there any way to make it not do this, short of
going through my code and making all my imports through the project
again? Maybe that wouldn't even help. I tried making selected imports,
e.g., through the list of installed apps in settings.py import through
the project name again but that didn't fix it, so I don't see an easy
hack.

The same thing is happening whether I'm using manage.py runserver at
home or Apache2/mod_wsgi2 on Webfaction. On Webfaction I have no
choice but to have both the project directory and the directory
containing it on my path or I get Apache 500 errors, the only way I
can make my application load is to have both on the path.

This is really awful behaviour - it's probably the ugliest wart (maybe
the only significant one, but still) I have ever found in Django, and
it sucks because my applications are using signals a lot. I hope
there's a way around it.

Stephen

from django.contrib.comments.signals import comment_will_be_posted,
comment_was_posted, comment_was_flagged
from django.contrib.comments.models import Comment
from django.contrib.sites.models import Site
from django.template import Context, loader
from django.core.mail import send_mail
from django.conf import settings

def pre_comment(sender, comment, request, **kwargs):
if not request.user.is_authenticated:
comment.is_removed = True

def post_comment(sender, comment, request, **kwargs):
if comment.is_removed:
comment.delete()
else:
request.session['flash'].append('Thank you for your
comments.')
request.session.modified = True
site = Site.objects.get_current()
template = loader.get_template('comments/comment_email.html')
context = Context({
'comment': comment,
'protocol': 'https' if request.is_secure() else
'http',
'site': site,
})
send_mail("A comment was posted at %s" % site.name,
template.render(context), getattr(settings,
'DEFAULT_FROM_EMAIL', None),
getattr(settings, 'MODERATOR_EMAILS', None))

def flag_comment(sender, comment, flag, created, request, **kwargs):
site = Site.objects.get_current()
template = loader.get_template('comments/comment_flag_email.html')
context = Context({
'comment': comment,
'flagged_by': request.user,
'protocol': 'https' if request.is_secure() else 'http',
'site': site,
})
send_mail("A comment was flagged by %s" % request.user.username,
template.render(context), getattr(settings,
'DEFAULT_FROM_EMAIL', None),
getattr(settings, 'MODERATOR_EMAILS', None))

comment_will_be_posted.connect(pre_comment, Comment)
comment_was_posted.connect(post_comment, Comment)
comment_was_flagged.connect(flag_comment, Comment)
--~--~-~--~~~---~--~~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---