def new_monitoring(**kwargs): """ generates a record in newscache, when someone starts monitoring something """ # generates news only if not in raw mode (fixtures) # and for objects creation if not kwargs.get('raw', False) and kwargs.get('created', False): generating_item = kwargs['instance'] monitored_object = generating_item.content_object monitoring_user = generating_item.user.get_profile() # define context for textual representation of the news ctx = Context({ 'monitored_object': monitored_object, 'monitoring_user': monitoring_user }) # two news are generated # first news related to the monitored object, with priority 2 (home) # User X has started to monitor item Y News.objects.create(generating_object=generating_item, related_object=monitored_object, priority=2, news_type=News.NEWS_TYPE.community, text=News.get_text_for_news( ctx, 'newscache/object_monitored.html')) # second news related to the monitoring user, with priority 3 (user's page) # This user has started to monitor item Y News.objects.create(generating_object=generating_item, related_object=monitoring_user, priority=3, news_type=News.NEWS_TYPE.community, text=News.get_text_for_news( ctx, 'newscache/user_monitoring.html'))
def new_monitoring(**kwargs): """ generates a record in newscache, when someone starts monitoring something """ # generates news only if not in raw mode (fixtures) # and for objects creation if not kwargs.get("raw", False) and kwargs.get("created", False): generating_item = kwargs["instance"] monitored_object = generating_item.content_object monitoring_user = generating_item.user.get_profile() # define context for textual representation of the news ctx = Context({"monitored_object": monitored_object, "monitoring_user": monitoring_user}) # two news are generated # first news related to the monitored object, with priority 1 (home) # User X has started to monitor item Y News.objects.create( generating_object=generating_item, related_object=monitored_object, priority=1, news_type=News.NEWS_TYPE.community, text=News.get_text_for_news(ctx, "newscache/object_monitored.html"), ) # second news related to the monitoring user, with priority 3 (user's page) # This user has started to monitor item Y News.objects.create( generating_object=generating_item, related_object=monitoring_user, priority=3, news_type=News.NEWS_TYPE.community, text=News.get_text_for_news(ctx, "newscache/user_monitoring.html"), )
def new_user_activity(sender, **kwargs): """ Generates a record in the newscache, when a user comments an item """ raw_mode = kwargs['raw'] created = kwargs['created'] # the object (vote or comment) which generates the news item(s) generating_object = kwargs['instance'] # generate news only if not in raw mode (fixtures) # and when a new object (comment or vote) has been saved to the DB if not raw_mode and created: comment = generating_object content_object = comment.content_object user = comment.user.get_profile() # build a context for generating a textual representation of the news ctx = Context({ 'object': content_object, 'commenting_user': user }) # two news are generated # first news related to the commented object, with priority 1 (home) # User X has started to monitor item Y News.objects.create( generating_object=generating_object, related_object=content_object, priority=1, news_type=News.NEWS_TYPE.community, text=News.get_text_for_news(ctx, 'newscache/object_commented.html') ) ## the second news item is related to the commenting user, with priority 3 (user) News.objects.create( generating_object=generating_object, related_object=user, priority=2, news_type=News.NEWS_TYPE.community, text=News.get_text_for_news(ctx, 'newscache/user_commenting.html') )
def new_monitoring(**kwargs): """ generates a record in newscache, when someone starts monitoring something """ # generates news only if not in raw mode (fixtures) # and for objects creation if not kwargs.get('raw', False) and kwargs.get('created', False): generating_item = kwargs['instance'] monitored_object = generating_item.content_object monitoring_user = generating_item.user.get_profile() # define context for textual representation of the news ctx = Context({ 'monitored_object': monitored_object, 'monitoring_user': monitoring_user }) # two news are generated # first news related to the monitored object, with priority 3 News.objects.create( generating_object=generating_item, related_object=monitored_object, priority=3, news_type=News.NEWS_TYPE.community, text=News.get_text_for_news(ctx, 'newscache/object_monitored.html') ) # second news related to the monitoring user, with priority 2 News.objects.create( generating_object=generating_item, related_object=monitoring_user, priority=2, news_type=News.NEWS_TYPE.community, text=News.get_text_for_news(ctx, 'newscache/user_monitoring.html') )
def new_signature(**kwargs): """ generates a record in newscache, when an act is signed """ # generates news only if not in raw mode (fixtures) # and for objects creation if not kwargs.get('raw', False) and kwargs.get('created', False): generating_item = kwargs['instance'] act = generating_item.act signer = generating_item.charge # define context for textual representation of the news ctx = Context({ 'current_site': Site.objects.get(id=settings.SITE_ID), 'signature': generating_item, 'act': act, 'signer': signer }) News.objects.create(generating_object=generating_item, related_object=act, priority=3, text=News.get_text_for_news( ctx, 'newscache/act_signed.html')) News.objects.create(generating_object=generating_item, related_object=signer, priority=1, text=News.get_text_for_news( ctx, 'newscache/user_signed.html'))
def new_user_activity(sender, **kwargs): """ Generates a record in the newscache, when a user comments an item """ raw_mode = kwargs['raw'] created = kwargs['created'] # the object (vote or comment) which generates the news item(s) generating_object = kwargs['instance'] # generate news only if not in raw mode (fixtures) # and when a new object (comment or vote) has been saved to the DB if not raw_mode and created: comment = generating_object content_object = comment.content_object user = comment.user.get_profile() # build a context for generating a textual representation of the news ctx = Context({'object': content_object, 'commenting_user': user}) # two news are generated # first news related to the commented object, with priority 1 (home) # User X has started to monitor item Y News.objects.create(generating_object=generating_object, related_object=content_object, priority=1, news_type=News.NEWS_TYPE.community, text=News.get_text_for_news( ctx, 'newscache/object_commented.html')) ## the second news item is related to the commenting user, with priority 3 (user) News.objects.create(generating_object=generating_object, related_object=user, priority=2, news_type=News.NEWS_TYPE.community, text=News.get_text_for_news( ctx, 'newscache/user_commenting.html'))
def new_transition(**kwargs): if not kwargs.get('raw', False): transition = kwargs['instance'] act = transition.act.downcast() # modify act's status only when transition is created # avoid infinite loop if kwargs.get('created', False): # set act's status according to transition status act.status = transition.final_status act.save() # if it's a final status, set the according flag in the act's parent if act.is_final_status(act.status): act.act_ptr.status_is_final = True act.act_ptr.save() # generate news ctx = Context({ 'current_site': Site.objects.get(id=settings.SITE_ID), 'transition': transition, 'act': act }) # handle presentation and other transitions differently, # to shorten acts' presentations, with signatures if transition.final_status == 'PRESENTED': created = False news, created = News.objects.get_or_create( generating_object_pk=transition.pk, generating_content_type=ContentType.objects.get_for_model( transition), related_object_pk=act.pk, related_content_type=ContentType.objects.get_for_model(act), priority=1, text=News.get_text_for_news(ctx, 'newscache/act_presented.html')) if created: logger.debug(" act presentation news created") else: logger.debug(" act presentation news found") else: created = False news, created = News.objects.get_or_create( generating_object_pk=transition.pk, generating_content_type=ContentType.objects.get_for_model( transition), related_object_pk=act.pk, related_content_type=ContentType.objects.get_for_model(act), priority=1, text=News.get_text_for_news( ctx, 'newscache/act_changed_status.html')) if created: logger.debug(" act changed status news created") else: logger.debug(" act changed status news found")
def new_signature(**kwargs): """ generates a record in newscache, when an act is signed """ # generates news only if not in raw mode (fixtures) # for signatures after presentation_date if not kwargs.get('raw', False): signature = kwargs['instance'] act = signature.act.downcast() signer = signature.charge # define context for textual representation of the news ctx = Context({ 'current_site': Site.objects.get(id=settings.SITE_ID), 'signature': signature, 'act': act, 'signer': signer }) # force convertion into strings of two date try: signature_support_date = signature.support_date.strftime("%Y-%m-%d") except: signature_support_date = signature.support_date try: act.presentation_date = act.presentation_date.strftime("%Y-%m-%d") except: act.presentation_date = act.presentation_date # generate new signature after presentation, for the act if signature.support_date > act.presentation_date: created = False news, created = News.objects.get_or_create( generating_object_pk=signature.pk, generating_content_type=ContentType.objects.get_for_model(signature), related_object_pk=act.pk, related_content_type=ContentType.objects.get_for_model(act), priority=3, text=News.get_text_for_news(ctx, 'newscache/act_signed_after_presentation.html') ) if created: logger.debug(" act was signed after presentation news created") else: logger.debug(" act was signed after presentation news found") # generate signature news, for the politician created = False news, created = News.objects.get_or_create( generating_object_pk=signature.pk, generating_content_type=ContentType.objects.get_for_model(signature), related_object_pk=signer.pk, related_content_type=ContentType.objects.get_for_model(signer), priority=2, text=News.get_text_for_news(ctx, 'newscache/person_signed.html') ) if created: logger.debug(" user signed act news created") else: logger.debug(" user signed act news found")
def new_comment(**kwargs): """ generates a record in newscache, when someone votes on something """ # generates news only if not in raw mode (fixtures) # and for objects creation if not kwargs.get('raw', False) and kwargs.get('created', False): generating_item = kwargs['instance'] object = generating_item.content_object user = generating_item.user.get_profile() # define context for textual representation of the news ctx = Context({ 'object': object, 'user': user }) # two news are generated # the first news is related to the act, and generated only once per day, # when the first user votes the object # get today's datetime, at midnight t = datetime.datetime.today() d = datetime.datetime(year=t.year, month=t.month, day=t.day) # get list of all dates with news generated by comments, # related to the commented object's instance news_grouped_by_date = News.objects.filter( generating_content_type=ContentType.objects.get_for_model(generating_item), related_content_type=ContentType.objects.get_for_model(object), related_object_pk=object.pk ).dates('created', 'day') sender = ContentType.objects.get_for_model(generating_item) if sender.model == 'commentwithmood': template_particle = 'comment' elif sender.model == 'vote': template_particle = 'vot' else: raise ObjectDoesNotExist # generate news only if no news were already generated today if d not in news_grouped_by_date: News.objects.create( generating_object=generating_item, related_object=object, priority=2, news_type=News.NEWS_TYPE.community, text=News.get_text_for_news(ctx, 'newscache/object_%sed.html' % template_particle) ) # the second news is related to the commenting user, with priority 2 News.objects.create( generating_object=generating_item, related_object=user, priority=2, news_type=News.NEWS_TYPE.community, text=News.get_text_for_news(ctx, 'newscache/user_%sing.html' % template_particle) )
def new_user_activity(sender, **kwargs): """ Generates a record in the newscache, when a user votes an item. """ raw_mode = kwargs['raw'] created = kwargs['created'] # the object (vote or comment) which generates the news item(s) generating_object = kwargs['instance'] # generate news only if not in raw mode (fixtures) # and when a new object (comment or vote) has been saved to the DB if not raw_mode and created: vote = generating_object content_object = vote.object user = vote.user.get_profile() ctx = Context({'object': content_object, 'user': user}) ## Two news items are generated ## the first news item is related to the object being voted/commented, ## and generated only once a day, when that object has been voted/commented for the first time # get today's datetime, at midnight t = datetime.datetime.today() d = datetime.datetime(year=t.year, month=t.month, day=t.day) # retrieve the ``DateQuerySet`` of all days during which at least one news item # related to the content object at hand has been generated day_qs = News.objects.filter( generating_content_type=ContentType.objects.get_for_model( generating_object), related_content_type=ContentType.objects.get_for_model( content_object), related_object_pk=content_object.pk).dates('created', 'day') # generate a news item only if no other news items were already generated today # removed 07/12/2012, always generates news, uncomment to change behavior # if d not in day_qs News.objects.create(generating_object=generating_object, related_object=content_object, priority=1, news_type=News.NEWS_TYPE.community, text=News.get_text_for_news( ctx, 'newscache/object_voted.html')) ## the second news item is related to the commenting/voting user, with priority 3 News.objects.create(generating_object=generating_object, related_object=user, priority=3, news_type=News.NEWS_TYPE.community, text=News.get_text_for_news( ctx, 'newscache/user_voting.html'))
def new_transition(**kwargs): if not kwargs.get('raw', False): transition = kwargs['instance'] act = transition.act.downcast() # modify act's status only when transition is created # avoid infinite loop if kwargs.get('created', False): # set act's status according to transition status act.status = transition.final_status act.save() # if it's a final status, set the according flag in the act's parent if act.is_final_status(act.status): act.act_ptr.status_is_final = True act.act_ptr.save() # generate news ctx = Context({ 'current_site': Site.objects.get(id=settings.SITE_ID), 'transition': transition, 'act': act }) # handle presentation and other transitions differently, # to shorten acts' presentations, with signatures if transition.final_status == 'PRESENTED': created = False news, created = News.objects.get_or_create( generating_object_pk=transition.pk, generating_content_type=ContentType.objects.get_for_model(transition), related_object_pk=act.pk, related_content_type=ContentType.objects.get_for_model(act), priority=1, text=News.get_text_for_news(ctx, 'newscache/act_presented.html') ) if created: logger.debug(" act presentation news created") else: logger.debug(" act presentation news found") else: created = False news, created = News.objects.get_or_create( generating_object_pk=transition.pk, generating_content_type=ContentType.objects.get_for_model(transition), related_object_pk=act.pk, related_content_type=ContentType.objects.get_for_model(act), priority=1, text=News.get_text_for_news(ctx, 'newscache/act_changed_status.html') ) if created: logger.debug(" act changed status news created") else: logger.debug(" act changed status news found")
def new_user_activity(sender, **kwargs): """ Generates a record in the newscache, when a user votes an item. """ raw_mode = kwargs['raw'] created = kwargs['created'] # the object (vote or comment) which generates the news item(s) generating_object = kwargs['instance'] # generate news only if not in raw mode (fixtures) # and when a new object (comment or vote) has been saved to the DB if not raw_mode and created: vote = generating_object content_object = vote.object user = vote.user.get_profile() ctx = Context({ 'object': content_object, 'user': user }) ## Two news items are generated ## the first news item is related to the object being voted/commented, ## and generated only once a day, when that object has been voted/commented for the first time # get today's datetime, at midnight t = datetime.datetime.today() d = datetime.datetime(year=t.year, month=t.month, day=t.day) # retrieve the ``DateQuerySet`` of all days during which at least one news item # related to the content object at hand has been generated day_qs = News.objects.filter( generating_content_type=ContentType.objects.get_for_model(generating_object), related_content_type=ContentType.objects.get_for_model(content_object), related_object_pk=content_object.pk ).dates('created', 'day') # generate a news item only if no other news items were already generated today # removed 07/12/2012, always generates news, uncomment to change behavior # if d not in day_qs News.objects.create( generating_object=generating_object, related_object=content_object, priority=1, news_type=News.NEWS_TYPE.community, text=News.get_text_for_news(ctx, 'newscache/object_voted.html') ) ## the second news item is related to the commenting/voting user, with priority 3 News.objects.create( generating_object=generating_object, related_object=user, priority=3, news_type=News.NEWS_TYPE.community, text=News.get_text_for_news(ctx, 'newscache/user_voting.html') )
def new_transition(**kwargs): if not kwargs.get('raw', False) and kwargs.get('created', False): generating_item = kwargs['instance'] act = generating_item.act.downcast() # Presentation is already handled by new_act_published handler if generating_item.final_status != 'PRESENTED': # set act's status according to transition status act.status = generating_item.final_status if act.is_final_status(generating_item.final_status): act.status_is_final = True act.save() # generate news ctx = Context({ 'current_site': Site.objects.get(id=settings.SITE_ID), 'transition': generating_item, 'act': act }) News.objects.create(generating_object=generating_item, related_object=act, priority=2, text=News.get_text_for_news( ctx, 'newscache/act_changed_status.html'))
def new_signature(**kwargs): """ generates a record in newscache, when an act is signed """ # generates news only if not in raw mode (fixtures) # and for objects creation if not kwargs.get('raw', False) and kwargs.get('created', False): generating_item = kwargs['instance'] act = generating_item.act signer = generating_item.charge # define context for textual representation of the news ctx = Context({ 'current_site': Site.objects.get(id=settings.SITE_ID), 'signature': generating_item, 'act': act, 'signer': signer }) News.objects.create( generating_object=generating_item, related_object=act, priority=3, text=News.get_text_for_news(ctx, 'newscache/act_signed.html') ) News.objects.create( generating_object=generating_item, related_object=signer, priority=1, text=News.get_text_for_news(ctx, 'newscache/user_signed.html') )
def new_transition(**kwargs): if not kwargs.get('raw', False) and kwargs.get('created', False): generating_item = kwargs['instance'] act = generating_item.act.downcast() # Presentation is already handled by new_act_published handler if generating_item.final_status != 'PRESENTED': # set act's status according to transition status act.status = generating_item.final_status if act.is_final_status(generating_item.final_status): act.status_is_final = True act.save() # generate news ctx = Context({ 'current_site': Site.objects.get(id=settings.SITE_ID), 'transition': generating_item, 'act': act }) News.objects.create( generating_object=generating_item, related_object=act, priority=2, text=News.get_text_for_news(ctx, 'newscache/act_changed_status.html') )
def new_act_published(sender, **kwargs): """ generates a record in newscache when an act is presented (inserted in our DB) the news is generate only when an act is created, not updated and when this happens outside the fixture loading phase below, a trick used to handle signals when loading fixtures, it is not used now, but it may be useful, for testing purposes # instance for subclass fix, while loading fixtures # see http://bit.ly/yimn9S and # https://code.djangoproject.com/ticket/13299 if kwargs.get('raw', False): instance = kwargs['instance'] generating_item = instance.__class__._default_manager.get(pk=instance.pk) else: generating_item = kwargs['instance'] """ # generates news only if not in raw mode (fixtures) # and for objects creation if not kwargs.get('raw', False) and kwargs.get('created', False): generating_item = kwargs['instance'] # create transition: act is presented generating_item.transition_set.create( act=generating_item.act_ptr, final_status=generating_item.STATUS.presented, transition_date=generating_item.presentation_date, ) # define context for textual representation of the news ctx = Context({}) # generate news in newscache News.objects.create(generating_object=generating_item, related_object=generating_item, priority=1, text=News.get_text_for_news( ctx, 'newscache/act_published.html'))
def new_act_published(sender, **kwargs): """ generates a record in newscache when an act is presented (inserted in our DB) the news is generate only when an act is created, not updated and when this happens outside the fixture loading phase below, a trick used to handle signals when loading fixtures, it is not used now, but it may be useful, for testing purposes # instance for subclass fix, while loading fixtures # see http://bit.ly/yimn9S and # https://code.djangoproject.com/ticket/13299 if kwargs.get('raw', False): instance = kwargs['instance'] generating_item = instance.__class__._default_manager.get(pk=instance.pk) else: generating_item = kwargs['instance'] """ # generates news only if not in raw mode (fixtures) # and for objects creation if not kwargs.get('raw', False) and kwargs.get('created', False): generating_item = kwargs['instance'] # create transition: act is presented generating_item.transition_set.create( act=generating_item.act_ptr, final_status=generating_item.STATUS.presented, transition_date=generating_item.presentation_date, ) # define context for textual representation of the news ctx = Context({ }) # generate news in newscache News.objects.create( generating_object=generating_item, related_object=generating_item, priority=1, text=News.get_text_for_news(ctx, 'newscache/act_published.html') )
def new_signature(**kwargs): """ generates a record in newscache, when an act is signed """ # generates news only if not in raw mode (fixtures) # for signatures after presentation_date if not kwargs.get('raw', False): signature = kwargs['instance'] act = signature.act.downcast() signer = signature.charge # define context for textual representation of the news ctx = Context({ 'current_site': Site.objects.get(id=settings.SITE_ID), 'signature': signature, 'act': act, 'signer': signer }) # force convertion into strings of two date try: signature_support_date = signature.support_date.strftime( "%Y-%m-%d") except: signature_support_date = signature.support_date try: act.presentation_date = act.presentation_date.strftime("%Y-%m-%d") except: act.presentation_date = act.presentation_date # generate new signature after presentation, for the act if signature.support_date > act.presentation_date: created = False news, created = News.objects.get_or_create( generating_object_pk=signature.pk, generating_content_type=ContentType.objects.get_for_model( signature), related_object_pk=act.pk, related_content_type=ContentType.objects.get_for_model(act), priority=3, text=News.get_text_for_news( ctx, 'newscache/act_signed_after_presentation.html')) if created: logger.debug( " act was signed after presentation news created") else: logger.debug(" act was signed after presentation news found") # generate signature news, for the politician created = False news, created = News.objects.get_or_create( generating_object_pk=signature.pk, generating_content_type=ContentType.objects.get_for_model( signature), related_object_pk=signer.pk, related_content_type=ContentType.objects.get_for_model(signer), priority=2, text=News.get_text_for_news(ctx, 'newscache/person_signed.html')) if created: logger.debug(" user signed act news created") else: logger.debug(" user signed act news found")