2011-09-12 05:53:54 +00:00
from django . db . models import get_model
from django . db . utils import DatabaseError
from django . db . models . signals import post_save
from django . contrib . auth . models import User
2011-09-06 03:50:38 +00:00
from social_auth . signals import pre_update
from social_auth . backends . facebook import FacebookBackend
2011-09-16 02:53:44 +00:00
from tastypie . models import create_api_key
2011-09-06 03:50:38 +00:00
2012-03-29 20:13:36 +00:00
2011-12-29 04:11:13 +00:00
import registration . signals
2011-12-01 18:59:34 +00:00
import logging
logger = logging . getLogger ( __name__ )
2011-09-12 05:53:54 +00:00
2011-12-01 18:59:34 +00:00
# get email from Facebook registration
2011-09-06 03:50:38 +00:00
def facebook_extra_values ( sender , user , response , details , * * kwargs ) :
if response . get ( ' email ' ) is not None :
user . email = response . get ( ' email ' )
return True
pre_update . connect ( facebook_extra_values , sender = FacebookBackend )
2011-09-12 05:53:54 +00:00
2011-12-01 18:59:34 +00:00
# create Wishlist and UserProfile to associate with User
2011-10-24 17:36:26 +00:00
def create_user_objects ( sender , created , instance , * * kwargs ) :
2011-09-12 05:53:54 +00:00
# use get_model to avoid circular import problem with models
try :
Wishlist = get_model ( ' core ' , ' Wishlist ' )
2011-10-24 17:36:26 +00:00
UserProfile = get_model ( ' core ' , ' UserProfile ' )
2011-09-12 05:53:54 +00:00
if created :
Wishlist . objects . create ( user = instance )
2011-10-24 17:36:26 +00:00
UserProfile . objects . create ( user = instance )
2011-09-12 05:53:54 +00:00
except DatabaseError :
2011-09-16 02:53:44 +00:00
# this can happen when creating superuser during syncdb since the
# core_wishlist table doesn't exist yet
2011-09-12 05:53:54 +00:00
return
2011-10-24 17:36:26 +00:00
post_save . connect ( create_user_objects , sender = User )
2011-12-01 18:59:34 +00:00
# create API key for new User
2011-09-16 02:53:44 +00:00
post_save . connect ( create_api_key , sender = User )
2011-12-01 18:59:34 +00:00
2011-12-29 04:11:13 +00:00
def merge_emails ( sender , user , * * kwargs ) :
logger . info ( ' checking %s ' % user . username )
try :
old_user = User . objects . exclude ( id = user . id ) . get ( email = user . email )
old_user . username = user . username
old_user . password = user . password
user . delete ( )
old_user . save ( )
except User . DoesNotExist :
return
2012-03-27 15:52:57 +00:00
registration . signals . user_activated . connect ( merge_emails )
from django . conf import settings
from django . utils . translation import ugettext_noop as _
2012-03-27 16:01:09 +00:00
from django . db . models import signals
2012-03-27 15:52:57 +00:00
from notification import models as notification
2012-03-29 20:13:36 +00:00
2012-03-27 15:52:57 +00:00
2012-03-27 21:53:07 +00:00
# create notification types (using django-notification) -- tie to syncdb
2012-03-27 15:52:57 +00:00
def create_notice_types ( app , created_models , verbosity , * * kwargs ) :
notification . create_notice_type ( " wishlist_comment " , _ ( " Wishlist Comment " ) , _ ( " a comment has been received on one of your wishlist books " ) )
2012-03-29 05:21:37 +00:00
notification . create_notice_type ( " coment_on_commented " , _ ( " Comment on Commented Work " ) , _ ( " a comment has been received on a book that you ' ve commented on " ) )
2012-03-27 15:52:57 +00:00
signals . post_syncdb . connect ( create_notice_types , sender = notification )
2012-03-27 17:24:16 +00:00
2012-03-27 21:53:07 +00:00
# define the notifications and tie them to corresponding signals
2012-03-30 07:06:57 +00:00
from django . contrib . comments . signals import comment_was_posted
2012-03-27 17:24:16 +00:00
def notify_comment ( comment , request , * * kwargs ) :
2012-03-30 07:06:57 +00:00
logger . info ( ' comment %s notifying ' % comment . pk )
2012-03-29 05:21:37 +00:00
other_commenters = User . objects . filter ( comment_comments__content_type = comment . content_type , comment_comments__object_pk = comment . object_pk ) . distinct ( ) . exclude ( id = comment . user . id )
other_wishers = comment . content_object . wished_by ( ) . exclude ( id = comment . user . id ) . exclude ( id__in = other_commenters )
2012-03-29 20:13:36 +00:00
notification . queue ( other_commenters , " coment_on_commented " , { ' comment ' : comment } , True )
notification . queue ( other_wishers , " wishlist_comment " , { ' comment ' : comment } , True )
2012-03-30 07:06:57 +00:00
import regluit . core . tasks as tasks
tasks . emit_notifications . delay ( )
2012-03-27 17:24:16 +00:00
comment_was_posted . connect ( notify_comment )